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

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