share.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // 配置转发参数
  43. if (shareConfig.methods.includes('forward')) {
  44. if (shareConfig.forwardInfo.title === '' || shareConfig.forwardInfo.image === '') {
  45. console.log('请在平台设置中配置转发信息');
  46. }
  47. // 设置自定义分享信息
  48. shareInfo.title = scene.title || shareConfig.forwardInfo.title;
  49. shareInfo.image = $url.cdn(scene.image || shareConfig.forwardInfo.image);
  50. shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle;
  51. shareInfo.path = buildSpmPath(query);
  52. }
  53. return shareInfo;
  54. };
  55. // 构造spm分享参数
  56. const buildSpmQuery = (params) => {
  57. const user = $store('user');
  58. let shareId = '0'; // 设置分享者用户ID
  59. if (typeof params.shareId === 'undefined') {
  60. if (user.isLogin) {
  61. // todo @非繁人 id为空
  62. shareId = user.userInfo.id;
  63. }
  64. }
  65. let page = '1'; // 页面类型: 1=首页(默认),2=商品,3=拼团商品,4=秒杀商品,5=邀请参团...按需扩展
  66. if (typeof params.page !== 'undefined') {
  67. page = params.page;
  68. }
  69. let query = '0'; // 设置页面ID: 如商品ID、拼团ID等
  70. if (typeof params.query !== 'undefined') {
  71. query = params.query;
  72. }
  73. let platform = platformMap.indexOf($platform.name) + 1;
  74. let from = '1';
  75. if (typeof params.from !== 'undefined') {
  76. from = platformMap.indexOf(params.from) + 1;
  77. }
  78. //spmParams = ... 可按需扩展
  79. return `spm=${shareId}.${page}.${query}.${platform}.${from}`;
  80. };
  81. // 构造页面分享参数
  82. const buildSpmPath = (query) => {
  83. return `/pages/index/index?${query}`;
  84. };
  85. // 构造分享链接
  86. const buildSpmLink = (query, linkAddress = '') => {
  87. return `${linkAddress}?${query}`;
  88. };
  89. // 解析Spm
  90. const decryptSpm = (spm) => {
  91. const user = $store('user');
  92. let shareParamsArray = spm.split('.');
  93. let shareParams = {
  94. spm,
  95. shareId: 0,
  96. page: '',
  97. query: {},
  98. platform: '',
  99. from: '',
  100. };
  101. let query;
  102. shareParams.shareId = shareParamsArray[0];
  103. switch (shareParamsArray[1]) {
  104. case '1':
  105. // 默认首页不跳转
  106. shareParams.page = '/pages/index/index';
  107. break;
  108. case '2':
  109. // 普通商品
  110. shareParams.page = '/pages/goods/index';
  111. shareParams.query = {
  112. id: shareParamsArray[2],
  113. };
  114. break;
  115. case '3':
  116. // 拼团商品
  117. shareParams.page = '/pages/goods/groupon';
  118. query = shareParamsArray[2].split(',');
  119. shareParams.query = {
  120. id: query[0],
  121. activity_id: query[1],
  122. };
  123. break;
  124. case '4':
  125. // 秒杀商品
  126. shareParams.page = '/pages/goods/seckill';
  127. query = shareParamsArray[2].split(',');
  128. shareParams.query = {
  129. id: query[1],
  130. };
  131. break;
  132. case '5':
  133. // 参与拼团
  134. shareParams.page = '/pages/activity/groupon/detail';
  135. shareParams.query = {
  136. id: shareParamsArray[2],
  137. };
  138. break;
  139. }
  140. shareParams.platform = platformMap[shareParamsArray[3] - 1];
  141. shareParams.from = fromMap[shareParamsArray[4] - 1];
  142. if (shareParams.shareId != 0) {
  143. // 已登录 立即添加分享记录
  144. if (user.isLogin) {
  145. user.addShareLog(shareParams);
  146. } else {
  147. // 未登录 待用户登录后添加分享记录
  148. uni.setStorageSync('shareLog', shareParams);
  149. }
  150. }
  151. if (shareParams.page !== '/pages/index/index') {
  152. $router.go(shareParams.page, shareParams.query);
  153. }
  154. return shareParams;
  155. };
  156. // 更新公众号分享sdk
  157. const updateShareInfo = (shareInfo) => {
  158. // #ifdef H5
  159. if ($platform.name === 'WechatOfficialAccount') {
  160. $wxsdk.updateShareInfo(shareInfo);
  161. }
  162. // #endif
  163. };
  164. export default {
  165. getShareInfo,
  166. updateShareInfo,
  167. decryptSpm,
  168. };