share.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import $store from '@/sheep/store';
  2. import $platform from '@/sheep/platform';
  3. import $router from '@/sheep/router';
  4. import $url from '@/sheep/url';
  5. // #ifdef H5
  6. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  7. // #endif
  8. // 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App,...按需扩展
  9. const platformMap = ['H5', 'WechatOfficialAccount', 'WechatMiniProgram', 'App'];
  10. // 设置分享方式: 1=直接转发,2=海报,3=复制链接,...按需扩展
  11. const fromMap = ['forward', 'poster', 'link'];
  12. // TODO 非繁人:分享的接入
  13. // 设置分享信息参数
  14. const getShareInfo = (
  15. scene = {
  16. title: '', // 自定义分享标题
  17. desc: '', // 自定义描述
  18. image: '', // 自定义分享图片
  19. params: {}, // 自定义分享参数
  20. },
  21. poster = {
  22. // 自定义海报数据
  23. type: 'user',
  24. },
  25. ) => {
  26. let shareInfo = {
  27. title: '', // 分享标题
  28. desc: '', // 描述
  29. image: '', // 分享图片
  30. path: '', // 分享页面+参数
  31. link: '', // 分享Url+参数
  32. query: '', // 分享参数
  33. poster, // 海报所需数据
  34. };
  35. const app = $store('app');
  36. const shareConfig = app.platform.share;
  37. // 自动拼接分享用户参数
  38. const query = buildSpmQuery(scene.params);
  39. shareInfo.query = query;
  40. // 配置分享链接地址
  41. shareInfo.link = buildSpmLink(query, shareConfig.linkAddress);
  42. // console.log("share.js link", shareInfo.link)
  43. // 配置转发参数
  44. if (shareConfig.methods.includes('forward')) {
  45. if (shareConfig.forwardInfo.title === '' || shareConfig.forwardInfo.image === '') {
  46. console.log('请在平台设置中配置转发信息');
  47. }
  48. // 设置自定义分享信息
  49. shareInfo.title = scene.title || shareConfig.forwardInfo.title;
  50. shareInfo.image = $url.cdn(scene.image || shareConfig.forwardInfo.image);
  51. shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle;
  52. shareInfo.path = buildSpmPath(query);
  53. }
  54. return shareInfo;
  55. };
  56. // 构造spm分享参数
  57. const buildSpmQuery = (params) => {
  58. const user = $store('user');
  59. let shareId = '0'; // 设置分享者用户ID
  60. if (typeof params.shareId === 'undefined') {
  61. if (user.isLogin) {
  62. // todo @非繁人 id为空
  63. shareId = user.userInfo.id;
  64. }
  65. }
  66. let page = '1'; // 页面类型: 1=首页(默认),2=商品,3=拼团商品,4=秒杀商品,5=邀请参团...按需扩展
  67. if (typeof params.page !== 'undefined') {
  68. page = params.page;
  69. }
  70. let query = '0'; // 设置页面ID: 如商品ID、拼团ID等
  71. if (typeof params.query !== 'undefined') {
  72. query = params.query;
  73. }
  74. let platform = platformMap.indexOf($platform.name) + 1;
  75. let from = '1';
  76. if (typeof params.from !== 'undefined') {
  77. from = platformMap.indexOf(params.from) + 1;
  78. }
  79. //spmParams = ... 可按需扩展
  80. return `spm=${shareId}.${page}.${query}.${platform}.${from}`;
  81. };
  82. // 构造页面分享参数
  83. const buildSpmPath = (query) => {
  84. return `/pages/index/index?${query}`;
  85. };
  86. // 构造分享链接
  87. const buildSpmLink = (query, linkAddress = '') => {
  88. return `${linkAddress}?${query}`;
  89. };
  90. // 解析Spm
  91. const decryptSpm = (spm) => {
  92. const user = $store('user');
  93. let shareParamsArray = spm.split('.');
  94. let shareParams = {
  95. spm,
  96. shareId: 0,
  97. page: '',
  98. query: {},
  99. platform: '',
  100. from: '',
  101. };
  102. let query;
  103. shareParams.shareId = shareParamsArray[0];
  104. switch (shareParamsArray[1]) {
  105. case '1':
  106. // 默认首页不跳转
  107. shareParams.page = '/pages/index/index';
  108. break;
  109. case '2':
  110. // 普通商品
  111. shareParams.page = '/pages/goods/index';
  112. shareParams.query = {
  113. linkId: shareParamsArray[2],
  114. };
  115. console.log(shareParams.query)
  116. break;
  117. case '3':
  118. // 拼团商品
  119. shareParams.page = '/pages/goods/groupon';
  120. query = shareParamsArray[2].split(',');
  121. shareParams.query = {
  122. id: query[0],
  123. activity_id: query[1],
  124. };
  125. break;
  126. case '4':
  127. // 秒杀商品
  128. shareParams.page = '/pages/goods/seckill';
  129. query = shareParamsArray[2].split(',');
  130. shareParams.query = {
  131. id: query[1],
  132. };
  133. break;
  134. case '5':
  135. // 参与拼团
  136. shareParams.page = '/pages/activity/groupon/detail';
  137. shareParams.query = {
  138. id: shareParamsArray[2],
  139. };
  140. break;
  141. }
  142. shareParams.platform = platformMap[shareParamsArray[3] - 1];
  143. shareParams.from = fromMap[shareParamsArray[4] - 1];
  144. if (shareParams.shareId != 0) {
  145. // 已登录 立即添加分享记录
  146. if (user.isLogin) {
  147. user.addShareLog(shareParams);
  148. } else {
  149. // 未登录 待用户登录后添加分享记录
  150. uni.setStorageSync('shareLog', shareParams);
  151. }
  152. }
  153. if (shareParams.page !== '/pages/index/index') {
  154. $router.go(shareParams.page, shareParams.query);
  155. }
  156. return shareParams;
  157. };
  158. // 更新公众号分享sdk
  159. const updateShareInfo = (shareInfo) => {
  160. // #ifdef H5
  161. if ($platform.name === 'WechatOfficialAccount') {
  162. $wxsdk.updateShareInfo(shareInfo);
  163. }
  164. // #endif
  165. };
  166. export default {
  167. getShareInfo,
  168. updateShareInfo,
  169. decryptSpm,
  170. };