share.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. import BrokerageApi from '@/sheep/api/trade/brokerage';
  6. // #ifdef H5
  7. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  8. // #endif
  9. // 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App,...按需扩展
  10. const platformMap = ['H5', 'WechatOfficialAccount', 'WechatMiniProgram', 'App'];
  11. // 设置分享方式: 1=直接转发,2=海报,3=复制链接,...按需扩展
  12. const fromMap = ['forward', 'poster', 'link'];
  13. // TODO 芋艿:分享的接入
  14. // 设置分享信息参数
  15. const getShareInfo = (
  16. scene = {
  17. title: '', // 自定义分享标题
  18. desc: '', // 自定义描述
  19. image: '', // 自定义分享图片
  20. params: {}, // 自定义分享参数
  21. },
  22. poster = {
  23. // 自定义海报数据
  24. type: 'user',
  25. },
  26. ) => {
  27. const shareInfo = {
  28. title: '', // 分享标题
  29. desc: '', // 描述
  30. image: '', // 分享图片
  31. path: '', // 分享页面+参数
  32. link: '', // 分享Url+参数
  33. query: '', // 分享参数
  34. poster, // 海报所需数据
  35. };
  36. const app = $store('app');
  37. const shareConfig = app.platform.share;
  38. // 自动拼接分享用户参数
  39. const query = buildSpmQuery(scene.params);
  40. shareInfo.query = query;
  41. // 配置分享链接地址
  42. shareInfo.link = buildSpmLink(query, shareConfig.linkAddress);
  43. // 配置页面地址带参数
  44. shareInfo.path = buildSpmPath(query);
  45. // 配置转发参数
  46. if (shareConfig.methods.includes('forward')) {
  47. // TODO puhui999: forward 这块有点问题
  48. if (shareConfig.forwardInfo.title === '' || shareConfig.forwardInfo.image === '') {
  49. console.log('请在平台设置中配置转发信息');
  50. }
  51. // 设置自定义分享信息
  52. shareInfo.title = scene.title || shareConfig.forwardInfo.title;
  53. shareInfo.image = $url.cdn(scene.image || shareConfig.forwardInfo.image);
  54. shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle;
  55. shareInfo.path = buildSpmPath(scene.path, query);
  56. }
  57. return shareInfo;
  58. };
  59. // 构造spm分享参数
  60. const buildSpmQuery = (params) => {
  61. const user = $store('user');
  62. let shareId = '0'; // 设置分享者用户ID
  63. if (typeof params.shareId === 'undefined') {
  64. if (user.isLogin) {
  65. shareId = user.userInfo.id;
  66. }
  67. }
  68. let page = '1'; // 页面类型: 1=首页(默认),2=商品,3=拼团商品,4=秒杀商品,5=邀请参团,6=分销邀请...按需扩展
  69. if (typeof params.page !== 'undefined') {
  70. page = params.page;
  71. }
  72. let query = '0'; // 设置页面ID: 如商品ID、拼团ID等
  73. if (typeof params.query !== 'undefined') {
  74. query = params.query;
  75. }
  76. let platform = platformMap.indexOf($platform.name) + 1;
  77. let from = '1';
  78. if (typeof params.from !== 'undefined') {
  79. from = platformMap.indexOf(params.from) + 1;
  80. }
  81. //spmParams = ... 可按需扩展
  82. return `spm=${shareId}.${page}.${query}.${platform}.${from}`;
  83. };
  84. // 构造页面分享参数: 所有的分享都先到首页进行 spm 参数解析
  85. const buildSpmPath = (query) => {
  86. // 默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,
  87. // 不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面。scancode_time为系统保留参数,不允许配置
  88. return `pages/index/index`;
  89. };
  90. // 构造分享链接
  91. const buildSpmLink = (query, linkAddress = '') => {
  92. return `${linkAddress}?${query}`;
  93. };
  94. // 解析Spm
  95. const decryptSpm = (spm) => {
  96. const user = $store('user');
  97. let shareParamsArray = spm.split('.');
  98. let shareParams = {
  99. spm,
  100. shareId: 0,
  101. page: '',
  102. query: {},
  103. platform: '',
  104. from: '',
  105. };
  106. let query;
  107. shareParams.shareId = shareParamsArray[0];
  108. switch (shareParamsArray[1]) {
  109. case '1':
  110. // 默认首页不跳转
  111. shareParams.page = '/pages/index/index';
  112. break;
  113. case '2':
  114. // 普通商品
  115. shareParams.page = '/pages/goods/index';
  116. shareParams.query = {
  117. id: shareParamsArray[2],
  118. };
  119. break;
  120. case '3':
  121. // 拼团商品
  122. shareParams.page = '/pages/goods/groupon';
  123. query = shareParamsArray[2].split(',');
  124. shareParams.query = {
  125. id: query[0],
  126. activity_id: query[1], // TODO 芋艿:接入分享后,应该统一成 id 参数
  127. };
  128. break;
  129. case '4':
  130. // 秒杀商品
  131. shareParams.page = '/pages/goods/seckill';
  132. query = shareParamsArray[2].split(',');
  133. shareParams.query = {
  134. id: query[1],
  135. };
  136. break;
  137. case '5':
  138. // 参与拼团
  139. shareParams.page = '/pages/activity/groupon/detail';
  140. shareParams.query = {
  141. id: shareParamsArray[2],
  142. };
  143. break;
  144. }
  145. shareParams.platform = platformMap[shareParamsArray[3] - 1];
  146. shareParams.from = fromMap[shareParamsArray[4] - 1];
  147. if (shareParams.shareId !== 0) {
  148. // 已登录 绑定推广员
  149. if (user.isLogin) {
  150. bindBrokerageUser(shareParams.shareId);
  151. } else {
  152. // 记录分享者编号
  153. uni.setStorageSync('shareId', shareParams.shareId);
  154. }
  155. }
  156. if (shareParams.page !== '/pages/index/index') {
  157. $router.go(shareParams.page, shareParams.query);
  158. }
  159. return shareParams;
  160. };
  161. // 绑定推广员
  162. const bindBrokerageUser = async (val= undefined) => {
  163. try {
  164. const shareId = val || uni.getStorageSync('shareId');
  165. if (!shareId) {
  166. return;
  167. }
  168. await BrokerageApi.bindBrokerageUser({ bindUserId: shareId });
  169. uni.removeStorageSync('shareId');
  170. } catch {
  171. }
  172. };
  173. // 更新公众号分享sdk
  174. const updateShareInfo = (shareInfo) => {
  175. // #ifdef H5
  176. if ($platform.name === 'WechatOfficialAccount') {
  177. $wxsdk.updateShareInfo(shareInfo);
  178. }
  179. // #endif
  180. };
  181. export default {
  182. getShareInfo,
  183. updateShareInfo,
  184. decryptSpm,
  185. bindBrokerageUser,
  186. };