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

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