index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Share v1.0.0
  3. * @Class Share
  4. * @description shopro-share 1.0.0 分享方法
  5. * @Author lidongtony
  6. * @Date 2021-04-19
  7. * @Email lidongtony@qq.com
  8. */
  9. import store from "@/shopro/store";
  10. import $platform from "@/shopro/platform";
  11. // #ifdef H5
  12. import wxsdk from "@/shopro/wechat/sdk";
  13. // #endif
  14. export default {
  15. // 设置分享信息
  16. setShareInfo(scene = {
  17. title: '', // 自定义分享标题
  18. desc: '', // 自定义描述
  19. image: '', // 自定义分享图片
  20. params: {} // 自定义分享参数
  21. }) {
  22. let that = this;
  23. let shareInfo = {
  24. title: '', // 分享标题
  25. desc: '', // 描述
  26. image: '', // 分享图片
  27. path: '', // 分享路径
  28. copyLink: '', // 复制链接
  29. query: '' // 分享参数
  30. }
  31. let shareConfig = store.getters.initShare;
  32. let shopConfig = store.getters.initShop;
  33. if (shopConfig?.domain === '' || shareConfig.title === '' || shareConfig.image === '') {
  34. throw '请在商城配置中设置商城域名和分享信息'
  35. }
  36. // 设置自定义分享信息
  37. shareInfo.title = scene.title !== '' ? scene.title : shareConfig.title;
  38. shareInfo.image = scene.image !== '' ? scene.image : shareConfig.image;
  39. shareInfo.desc = scene.desc !== '' ? scene.desc : '';
  40. // 自动拼接分享用户参数
  41. let query = that.setShareQuery(scene.params);
  42. // #ifdef MP
  43. shareInfo.path = `/pages/index/index?${query}`;
  44. // #endif
  45. // #ifndef MP
  46. shareInfo.path = `${store.getters.initShop?.domain}?${query}`;
  47. // #endif
  48. shareInfo.copyLink = `${store.getters.initShop?.domain}?${query}`;
  49. shareInfo.query = query;
  50. // #ifdef H5
  51. if($platform.get() === 'wxOfficialAccount') {
  52. wxsdk.share(shareInfo);
  53. }
  54. // #endif
  55. store.commit("shareInfo", shareInfo);
  56. return shareInfo;
  57. },
  58. // 自定义分享参数拼接: 由于小程序码长度限制(B码最大32位长度),为了确保分享参数最大可扩展性,使用spm方法拼接 shareUserId.page.pageId.platform.from ----例 spm=88888888.3.1666666.3.2 即为ID为88888888用户通过微信网页平台生成了拼团ID为1666666的拼团分享海报
  59. setShareQuery(params) {
  60. let shareUserId = '0'; // 设置分享者用户ID
  61. if (params.shareId === undefined) {
  62. if (store.getters.isLogin) {
  63. shareUserId = store.getters.userInfo.id;
  64. }
  65. }
  66. let page = '1'; // 页面类型: 1=首页(默认),2=商品,3=拼团...按需扩展
  67. if (params.page !== undefined) {
  68. page = params.page;
  69. }
  70. let pageId = '0'; // 设置页面ID: 如商品ID、拼团ID等
  71. if (params.pageId !== undefined) {
  72. pageId = params.pageId;
  73. }
  74. let platform = ['H5', 'wxOfficialAccount', 'wxMiniProgram', 'App'].indexOf($platform.get()) +
  75. 1; // 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App,...按需扩展
  76. let from = '1'; // 设置分享方式: 1=直接转发,2=海报,3=链接,...按需扩展
  77. if (params.from !== undefined) {
  78. from = params.from;
  79. }
  80. //spmParam = ... 可按需扩展
  81. return `spm=${shareUserId}.${page}.${pageId}.${platform}.${from}`;
  82. },
  83. getShareQuery(spm) {
  84. let shareParams = {};
  85. let shareParamsArray = spm.split('.');
  86. if(shareParamsArray[0] != '0') {
  87. shareParams.shareUserId = shareParamsArray[0];
  88. }
  89. switch(shareParamsArray[1]) {
  90. case '1':
  91. // 默认首页不跳转
  92. break;
  93. case '2':
  94. shareParams.page = '/pages/goods/detail';
  95. break;
  96. case '3':
  97. shareParams.page = '/pages/activity/groupon/detail';
  98. break;
  99. }
  100. if(shareParamsArray[2] !== '0') {
  101. shareParams.pageId = shareParamsArray[2];
  102. }
  103. shareParams.platform = shareParamsArray[3];
  104. shareParams.from = shareParamsArray[4];
  105. return shareParams;
  106. }
  107. }