share.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template></template>
  2. <script>
  3. import req from '../../utils/request.js';
  4. export default {
  5. // props: ['title', 'desc', 'url', 'image', 'hideItem'],
  6. data() {
  7. return {
  8. shareContent: ''
  9. };
  10. },
  11. methods: {
  12. getData() {
  13. var configRes = JSON.parse(req.getStorage('configRes'));
  14. return {
  15. title: this.shareContent && this.shareContent.title ? this.shareContent.title : '绿萝云', // 分享标题
  16. desc: this.shareContent && this.shareContent.desc ? this.shareContent.desc : '绿萝云', // 分享描述
  17. link: this.shareContent && this.shareContent.path ? this.shareContent.path : window.location
  18. .href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  19. imgUrl: this.shareContent && this.shareContent.imageUrl ? this.shareContent.imageUrl : configRes.CONFIG_PROJECT_LOGO, // 分享图标
  20. success: () => {
  21. }
  22. };
  23. },
  24. wxShareTimeline() {
  25. let data = this.getData();
  26. console.log('wxShareTimeline>>>>>>>', data);
  27. jWeixin.updateAppMessageShareData(data);
  28. jWeixin.onMenuShareTimeline(data);
  29. jWeixin.onMenuShareAppMessage(data);
  30. jWeixin.onMenuShareQQ(data);
  31. jWeixin.onMenuShareQZone(data);
  32. },
  33. wxShareAppMessage() {
  34. let data = this.getData();
  35. console.log('wxShareAppMessage>>>>>>>', data);
  36. jWeixin.updateTimelineShareData(data);
  37. },
  38. hideShareItem() {
  39. var url = window.location.href;
  40. req.getRequestKDX(
  41. '/api/getWeiXinJsApiInfo', {
  42. url: url
  43. },
  44. data => {
  45. jWeixin.config({
  46. debug: false, // 开启调试模式
  47. appId: data.appId, // 必填,公众号的唯一标识
  48. timestamp: data.timestamp, // 必填,生成签名的时间戳
  49. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  50. signature: data.signature, // 必填,签名
  51. jsApiList: [
  52. 'hideMenuItems' //自定义“分享给朋友”及“分享到QQ”按钮的分享内容
  53. ]
  54. });
  55. jWeixin.ready(function() {
  56. jWeixin.hideMenuItems({
  57. menuList: [
  58. 'menuItem:share:appMessage',
  59. 'menuItem:share:timeline',
  60. 'menuItem:share:qq',
  61. 'menuItem:share:facebook',
  62. 'menuItem:share:QZone',
  63. 'menuItem:copyUrl'
  64. ]
  65. });
  66. });
  67. },
  68. err => {}
  69. );
  70. },
  71. initShare() {
  72. let that = this;
  73. var url = window.location.href;
  74. req.getRequestKDX(
  75. '/api/getWeiXinJsApiInfo', {
  76. url: url
  77. },
  78. res => {
  79. jWeixin.config({
  80. debug: false, // 开启调试模式
  81. appId: res.appId, // 必填,公众号的唯一标识
  82. timestamp: res.timestamp, // 必填,生成签名的时间戳
  83. nonceStr: res.nonceStr, // 必填,生成签名的随机串
  84. signature: res.signature, // 必填,签名
  85. jsApiList: [
  86. 'updateAppMessageShareData',
  87. 'updateTimelineShareData',
  88. 'onMenuShareTimeline',
  89. 'onMenuShareAppMessage',
  90. 'onMenuShareQQ',
  91. 'onMenuShareQZone',
  92. 'chooseWXPay', //
  93. 'scanQRCode', //扫码
  94. 'getLocation', //定位
  95. ]
  96. });
  97. jWeixin.ready(function(res) {
  98. that.wxShareAppMessage();
  99. that.wxShareTimeline();
  100. });
  101. jWeixin.error(function(res) {
  102. console.log(res);
  103. });
  104. },
  105. err => {}
  106. );
  107. },
  108. init(shareContent) {
  109. this.shareContent = shareContent;
  110. var ua = navigator.userAgent.toLowerCase();
  111. if (ua.indexOf('micromessenger') != -1) {
  112. console.log(this.hideItem);
  113. if (this.hideItem) {
  114. this.hideShareItem();
  115. } else {
  116. this.initShare();
  117. }
  118. }
  119. },
  120. },
  121. created() {
  122. }
  123. };
  124. </script>