share.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // #ifdef H5
  2. import jweixin from 'jweixin-module'
  3. import { wxShare } from '@/api/common'
  4. // #endif
  5. export default {
  6. data() {
  7. return {
  8. shareData: null
  9. }
  10. },
  11. // #ifdef MP-WEIXIN
  12. onShareAppMessage(res) {
  13. return this.shareData
  14. },
  15. // #endif
  16. methods: {
  17. async jweixinFn(data) {
  18. let that = this
  19. function success(e) {
  20. console.log(e)
  21. // that.shareStatiscFN(data.linkurl, data.owner_user_id, data.puser_id)
  22. }
  23. // #ifdef H5
  24. await that.getWeiXinOptin(data) // 需在用户可能点击分享按钮前就先调用
  25. jweixin.ready(function() {
  26. let option = {
  27. title: data.title, // 分享标题
  28. desc: data.desc, // 分享描述
  29. link: data.pagePath, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  30. imgUrl: data.imgUrl, // 分享图标
  31. success
  32. }
  33. // 分享给朋友”及“分享到QQ
  34. // jweixin.updateAppMessageShareData(option)
  35. // 分享到朋友圈”及“分享到QQ空间
  36. // jweixin.updateTimelineShareData(option)
  37. // “分享到腾讯微博”
  38. // jweixin.onMenuShareWeibo(option)
  39. //分享给朋友接口
  40. jweixin.onMenuShareAppMessage(option)
  41. //分享到朋友圈接口
  42. jweixin.onMenuShareTimeline(option);
  43. })
  44. // #endif
  45. // #ifdef MP-WEIXIN
  46. that.shareData = {
  47. title: data.title, // 分享标题
  48. path: data.pagePath,
  49. imageUrl: data.imgUrl,
  50. success
  51. }
  52. // #endif
  53. },
  54. // ==分享统计回调==
  55. // shareStatiscFN(res, owner_user_id, puser_id) {
  56. // const userid = uni.getStorageSync('user_id') || null
  57. // const params = {}
  58. // if (userid == owner_user_id) {
  59. // params.share_user_id = userid
  60. // params.owner_user_id = owner_user_id
  61. // } else {
  62. // params.owner_user_id = owner_user_id
  63. // params.share_user_id = userid
  64. // params.p_share_user_id = puser_id
  65. // }
  66. // // shareStatisc(params)
  67. // },
  68. /**
  69. * jsSdk入权限验证配置
  70. * @parmas apiList 为需要获取的微信jsdk的接口列表
  71. */
  72. getWeiXinOptin(data, apiList = ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage']) {
  73. return new Promise((resolve, reject) => {
  74. const params = {
  75. // url: location.origin
  76. url: data.pagePath
  77. }
  78. const jsApiList = apiList
  79. wxShare(params).then(res => {
  80. let resdata = res.data
  81. jweixin.config({
  82. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  83. appId: resdata.appId, // 必填,公众号的唯一标识
  84. timestamp: resdata.timestamp, // 必填,生成签名的时间戳
  85. nonceStr: resdata.nonceStr, // 必填,生成签名的随机串
  86. signature: resdata.signature, // 必填,签名,见附录1
  87. jsApiList// 必填,需要使用的JS接口列表
  88. })
  89. resolve()
  90. })
  91. })
  92. }
  93. }
  94. }