小说小程序前端代码仓库(小程序)
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.

93 lines
2.3 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. // #ifdef H5
  2. import api from '@/api/api.js'
  3. import config from "../config.js"
  4. import jWeixin from './lib/jweixin-module.js'
  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. api('getSignPackage', data).then(res => {
  13. if (res.code == 200) {
  14. let {
  15. appId,
  16. nonceStr,
  17. signature,
  18. timestamp
  19. } = res.result
  20. jWeixin.config({
  21. debug: false,
  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: uni.getStorageSync('login_logo'),
  47. success: function () {
  48. //分享成功可以做相应的数据处理
  49. console.log('注册分享成功');
  50. // uni.showToast({
  51. // mask: true,
  52. // duration: 1000,
  53. // title: '注册分享成功',
  54. // });
  55. },
  56. fail : function (e){
  57. console.log(e);
  58. }
  59. };
  60. //分享微信朋友圈内容设置
  61. jWeixin.updateTimelineShareData(shareData);
  62. //分享给朋友内容设置
  63. jWeixin.updateAppMessageShareData(shareData);
  64. //分享到微博内容设置
  65. jWeixin.onMenuShareWeibo(shareData);
  66. });
  67. jWeixin.error(function (err) {
  68. console.error(err);
  69. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  70. })
  71. }
  72. })
  73. }
  74. function addQueryParams(url) {
  75. if (url) {
  76. //获取用户id
  77. let userInfo = localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null
  78. if (userInfo) {
  79. url += `?inviter=${userInfo.id}`
  80. }
  81. }
  82. return url
  83. }
  84. export default share
  85. // #endif
  86. // #ifndef H5
  87. export default function(){}
  88. // #endif