123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- import $store from '@/sheep/store';
- import $platform from '@/sheep/platform';
- import $router from '@/sheep/router';
- import $url from '@/sheep/url';
- import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
- const platformMap = ['H5', 'WechatOfficialAccount', 'WechatMiniProgram', 'App'];
- const fromMap = ['forward', 'poster', 'link'];
- const getShareInfo = (
- scene = {
- title: '',
- desc: '',
- image: '',
- params: {},
- },
- poster = {
-
- type: 'user',
- },
- ) => {
- let shareInfo = {
- title: '',
- desc: '',
- image: '',
- path: '',
- link: '',
- query: '',
- poster,
- };
- const app = $store('app');
- const shareConfig = app.platform.share;
-
- const query = buildSpmQuery(scene.params);
- shareInfo.query = query;
-
-
- shareInfo.link = buildSpmLink(query, shareConfig.linkAddress);
-
-
- if (shareConfig.methods.includes('forward')) {
- if (shareConfig.forwardInfo.title === '' || shareConfig.forwardInfo.image === '') {
- console.log('请在平台设置中配置转发信息');
- }
-
- shareInfo.title = scene.title || shareConfig.forwardInfo.title;
- shareInfo.image = $url.cdn(scene.image || shareConfig.forwardInfo.image);
- shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle;
- shareInfo.path = buildSpmPath(query);
- }
- return shareInfo;
- };
- const buildSpmQuery = (params) => {
- const user = $store('user');
- let shareId = '0';
- if (typeof params.shareId === 'undefined') {
- if (user.isLogin) {
-
- shareId = user.userInfo.id;
- }
- }
- let page = '1';
- if (typeof params.page !== 'undefined') {
- page = params.page;
- }
- let query = '0';
- if (typeof params.query !== 'undefined') {
- query = params.query;
- }
- let platform = platformMap.indexOf($platform.name) + 1;
- let from = '1';
- if (typeof params.from !== 'undefined') {
- from = platformMap.indexOf(params.from) + 1;
- }
-
- return `spm=${shareId}.${page}.${query}.${platform}.${from}`;
- };
- const buildSpmPath = (query) => {
- return `/pages/index/index?${query}`;
- };
- const buildSpmLink = (query, linkAddress = '') => {
- return `${linkAddress}?${query}`;
- };
- const decryptSpm = (spm) => {
- const user = $store('user');
- let shareParamsArray = spm.split('.');
- let shareParams = {
- spm,
- shareId: 0,
- page: '',
- query: {},
- platform: '',
- from: '',
- };
- let query;
- shareParams.shareId = shareParamsArray[0];
- switch (shareParamsArray[1]) {
- case '1':
-
- shareParams.page = '/pages/index/index';
- break;
- case '2':
-
- shareParams.page = '/pages/goods/index';
- shareParams.query = {
- linkId: shareParamsArray[2],
- };
- console.log(shareParams.query)
- break;
- case '3':
-
- shareParams.page = '/pages/goods/groupon';
- query = shareParamsArray[2].split(',');
- shareParams.query = {
- id: query[0],
- activity_id: query[1],
- };
- break;
- case '4':
-
- shareParams.page = '/pages/goods/seckill';
- query = shareParamsArray[2].split(',');
- shareParams.query = {
- id: query[1],
- };
- break;
- case '5':
-
- shareParams.page = '/pages/activity/groupon/detail';
- shareParams.query = {
- id: shareParamsArray[2],
- };
- break;
- }
- shareParams.platform = platformMap[shareParamsArray[3] - 1];
- shareParams.from = fromMap[shareParamsArray[4] - 1];
- if (shareParams.shareId != 0) {
-
- if (user.isLogin) {
- user.addShareLog(shareParams);
- } else {
-
- uni.setStorageSync('shareLog', shareParams);
- }
- }
- if (shareParams.page !== '/pages/index/index') {
- $router.go(shareParams.page, shareParams.query);
- }
- return shareParams;
- };
- const updateShareInfo = (shareInfo) => {
-
- if ($platform.name === 'WechatOfficialAccount') {
- $wxsdk.updateShareInfo(shareInfo);
- }
-
- };
- export default {
- getShareInfo,
- updateShareInfo,
- decryptSpm,
- };
|