四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

  1. import login from '@/api/modules/login'
  2. // import config from "../config.js"
  3. import jWeixin from './lib/jweixin-module.js'
  4. import Vue from 'vue'
  5. function share() { //微信分享
  6. //获取签名
  7. let data = {
  8. // location.href.split('#')[0])
  9. // url: Vue.prototype.$config.redirect + '/#/'
  10. url: location.href.split('#')[0]
  11. }
  12. login.getSignPackage(data).then(res => {
  13. if (res.code == 200) {
  14. let {
  15. appId,
  16. nonceStr,
  17. signature,
  18. timestamp
  19. } = res.result
  20. console.log(appId);
  21. jWeixin.config({
  22. // debug: true,
  23. debug: false,
  24. appId: appId,
  25. nonceStr: nonceStr,
  26. signature: signature,
  27. timestamp: timestamp,
  28. jsApiList: [
  29. 'updateTimelineShareData',
  30. 'updateAppMessageShareData',
  31. 'onMenuShareWeibo',
  32. 'getLocation',
  33. 'chooseWXPay',
  34. 'requestMerchantTransfer',
  35. 'checkJsApi',
  36. 'scanQRCode',
  37. ],
  38. success: function () {
  39. console.log('配置成功');
  40. }
  41. });
  42. jWeixin.ready(function () {
  43. // 微信分享的数据
  44. var shareData = {
  45. "link": addQueryParams(data.url),
  46. "desc": "四零语境",
  47. "title": "四零语境,温柔呵护每一刻!",
  48. imgUrl: location.href.split('#')[0] + '/static/share/logo.png',
  49. success: function () {
  50. //分享成功可以做相应的数据处理
  51. console.log('注册分享成功');
  52. // uni.showToast({
  53. // mask: true,
  54. // duration: 1000,
  55. // title: '注册分享成功',
  56. // });
  57. }
  58. };
  59. //分享微信朋友圈内容设置
  60. jWeixin.updateTimelineShareData(shareData);
  61. //分享给朋友内容设置
  62. jWeixin.updateAppMessageShareData(shareData);
  63. //分享到微博内容设置
  64. jWeixin.onMenuShareWeibo(shareData);
  65. });
  66. jWeixin.error(function (err) {
  67. console.error(err);
  68. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  69. })
  70. }
  71. })
  72. }
  73. function addQueryParams(url) {
  74. if (url) {
  75. //获取用户id
  76. let userInfo = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
  77. if (userInfo) {
  78. url += `?inviter=${userInfo.id}`
  79. }
  80. }
  81. return url
  82. }
  83. export default share