四零语境前端代码仓库
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.

91 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: uni.getStorageSync('login_logo'),
  49. success: function () {
  50. //分享成功可以做相应的数据处理
  51. console.log('注册分享成功');
  52. // uni.showToast({
  53. // mask: true,
  54. // duration: 1000,
  55. // title: '注册分享成功',
  56. // });
  57. },
  58. fail : function (e){
  59. console.log(e);
  60. }
  61. };
  62. //分享微信朋友圈内容设置
  63. jWeixin.updateTimelineShareData(shareData);
  64. //分享给朋友内容设置
  65. jWeixin.updateAppMessageShareData(shareData);
  66. //分享到微博内容设置
  67. jWeixin.onMenuShareWeibo(shareData);
  68. });
  69. jWeixin.error(function (err) {
  70. console.error(err);
  71. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  72. })
  73. }
  74. })
  75. }
  76. function addQueryParams(url) {
  77. if (url) {
  78. //获取用户id
  79. let userInfo = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
  80. if (userInfo) {
  81. url += `?inviter=${userInfo.id}`
  82. }
  83. }
  84. return url
  85. }
  86. export default share