{"version":3,"file":"share.js","sources":["../../../../../src/sheep/platform/share.js"],"sourcesContent":["import $store from '@/sheep/store'\r\nimport $platform from '@/sheep/platform'\r\nimport $router from '@/sheep/router'\r\nimport $url from '@/sheep/url'\r\nimport BrokerageApi from '@/sheep/api/trade/brokerage'\r\n// #ifdef H5\r\nimport $wxsdk from '@/sheep/libs/sdk-h5-weixin'\r\n// #endif\r\n\r\n// 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App,...按需扩展\r\nconst platformMap = ['H5', 'WechatOfficialAccount', 'WechatMiniProgram', 'App']\r\n\r\n// 设置分享方式: 1=直接转发,2=海报,3=复制链接,...按需扩展\r\nconst fromMap = ['forward', 'poster', 'link']\r\n\r\n// TODO 芋艿:分享的接入\r\n// 设置分享信息参数\r\nconst getShareInfo = (\r\n scene = {\r\n title: '', // 自定义分享标题\r\n desc: '', // 自定义描述\r\n image: '', // 自定义分享图片\r\n params: {}, // 自定义分享参数\r\n },\r\n poster = {\r\n // 自定义海报数据\r\n type: 'user',\r\n },\r\n) => {\r\n const shareInfo = {\r\n title: '', // 分享标题\r\n desc: '', // 描述\r\n image: '', // 分享图片\r\n path: '', // 分享页面+参数\r\n link: '', // 分享Url+参数\r\n query: '', // 分享参数\r\n poster, // 海报所需数据\r\n }\r\n\r\n const app = $store('app')\r\n const shareConfig = app.platform.share\r\n\r\n // 自动拼接分享用户参数\r\n const query = buildSpmQuery(scene.params)\r\n shareInfo.query = query\r\n\r\n // 配置分享链接地址\r\n shareInfo.link = buildSpmLink(query, shareConfig.linkAddress)\r\n // 配置页面地址带参数\r\n shareInfo.path = buildSpmPath(query)\r\n\r\n // 配置转发参数\r\n if (shareConfig.methods.includes('forward')) {\r\n // TODO puhui999: forward 这块有点问题\r\n if (shareConfig.forwardInfo.title === '' || shareConfig.forwardInfo.image === '') {\r\n console.log('请在平台设置中配置转发信息')\r\n }\r\n // 设置自定义分享信息\r\n shareInfo.title = scene.title || shareConfig.forwardInfo.title\r\n shareInfo.image = $url.cdn(scene.image || shareConfig.forwardInfo.image)\r\n shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle\r\n shareInfo.path = buildSpmPath(scene.path, query)\r\n }\r\n\r\n return shareInfo\r\n}\r\n\r\n/**\r\n * 构造 spm 分享参数\r\n *\r\n * @param params json 格式,其中包含:1)shareId 分享用户的编号;2)page 页面类型;3)query 页面 ID(参数);4)platform 平台类型;5)from 分享来源类型。\r\n * @return 分享串 `spm=${shareId}.${page}.${query}.${platform}.${from}`\r\n */\r\nconst buildSpmQuery = (params) => {\r\n const user = $store('user')\r\n let shareId = '0' // 设置分享者用户ID\r\n if (typeof params.shareId === 'undefined') {\r\n if (user.isLogin) {\r\n shareId = user.userInfo.id\r\n }\r\n }\r\n let page = '1' // 页面类型: 1=首页(默认),2=商品,3=拼团商品,4=秒杀商品,5=邀请参团,6=分销邀请...按需扩展\r\n if (typeof params.page !== 'undefined') {\r\n page = params.page\r\n }\r\n let query = '0' // 设置页面ID: 如商品ID、拼团ID等\r\n if (typeof params.query !== 'undefined') {\r\n query = params.query\r\n }\r\n const platform = platformMap.indexOf($platform.name) + 1\r\n let from = '1'\r\n if (typeof params.from !== 'undefined') {\r\n from = platformMap.indexOf(params.from) + 1\r\n }\r\n // spmParams = ... 可按需扩展\r\n return `spm=${shareId}.${page}.${query}.${platform}.${from}`\r\n}\r\n\r\n// 构造页面分享参数: 所有的分享都先到首页进行 spm 参数解析\r\nconst buildSpmPath = (query) => {\r\n // 默认是主页,页面 page,例如 pages/index/index,根路径前不要填加 /,\r\n // 不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面。scancode_time为系统保留参数,不允许配置\r\n return `pages/index/index`\r\n}\r\n\r\n// 构造分享链接\r\nconst buildSpmLink = (query, linkAddress = '') => {\r\n return `${linkAddress}?${query}`\r\n}\r\n\r\n// 解析Spm\r\nconst decryptSpm = (spm) => {\r\n const user = $store('user')\r\n const shareParamsArray = spm.split('.')\r\n const shareParams = {\r\n spm,\r\n shareId: 0,\r\n page: '',\r\n query: {},\r\n platform: '',\r\n from: '',\r\n }\r\n let query\r\n shareParams.shareId = shareParamsArray[0]\r\n switch (shareParamsArray[1]) {\r\n case '1':\r\n // 默认首页不跳转\r\n shareParams.page = '/modules/mall/index/index'\r\n break\r\n case '2':\r\n // 普通商品\r\n shareParams.page = '/modules/mall/goods/index'\r\n shareParams.query = {\r\n id: shareParamsArray[2],\r\n }\r\n break\r\n case '3':\r\n // 拼团商品\r\n shareParams.page = '/modules/mall/goods/groupon'\r\n query = shareParamsArray[2].split(',')\r\n shareParams.query = {\r\n id: query[0],\r\n activity_id: query[1], // TODO 芋艿:接入分享后,应该统一成 id 参数\r\n }\r\n break\r\n case '4':\r\n // 秒杀商品\r\n shareParams.page = '/modules/mall/goods/seckill'\r\n query = shareParamsArray[2].split(',')\r\n shareParams.query = {\r\n id: query[1],\r\n }\r\n break\r\n case '5':\r\n // 参与拼团\r\n shareParams.page = '/modules/mall/activity/groupon/detail'\r\n shareParams.query = {\r\n id: shareParamsArray[2],\r\n }\r\n break\r\n }\r\n shareParams.platform = platformMap[shareParamsArray[3] - 1]\r\n shareParams.from = fromMap[shareParamsArray[4] - 1]\r\n if (shareParams.shareId !== 0) {\r\n // 已登录 绑定推广员\r\n if (user.isLogin) {\r\n bindBrokerageUser(shareParams.shareId)\r\n } else {\r\n // 记录分享者编号\r\n uni.setStorageSync('shareId', shareParams.shareId)\r\n }\r\n }\r\n\r\n if (shareParams.page !== '/modules/mall/index/index') {\r\n $router.go(shareParams.page, shareParams.query)\r\n }\r\n return shareParams\r\n}\r\n\r\n// 绑定推广员\r\nconst bindBrokerageUser = async (val = undefined) => {\r\n try {\r\n const shareId = val || uni.getStorageSync('shareId')\r\n if (!shareId) {\r\n return\r\n }\r\n await BrokerageApi.bindBrokerageUser({ bindUserId: shareId })\r\n uni.removeStorageSync('shareId')\r\n } catch {}\r\n}\r\n\r\n// 更新公众号分享sdk\r\nconst updateShareInfo = (shareInfo) => {\r\n // #ifdef H5\r\n if ($platform.name === 'WechatOfficialAccount') {\r\n $wxsdk.updateShareInfo(shareInfo)\r\n }\r\n // #endif\r\n}\r\n\r\nexport default {\r\n getShareInfo,\r\n updateShareInfo,\r\n decryptSpm,\r\n bindBrokerageUser,\r\n}\r\n"],"names":["$store","$url","$platform","uni","$router","BrokerageApi"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,MAAM,cAAc,CAAC,MAAM,yBAAyB,qBAAqB,KAAK;AAG9E,MAAM,UAAU,CAAC,WAAW,UAAU,MAAM;AAI5C,MAAM,eAAe,CACnB,QAAQ;AAAA,EACN,OAAO;AAAA;AAAA,EACP,MAAM;AAAA;AAAA,EACN,OAAO;AAAA;AAAA,EACP,QAAQ,CAAE;AAAA;AACX,GACD,SAAS;AAAA;AAAA,EAEP,MAAM;AACP,MACE;AACH,QAAM,YAAY;AAAA,IAChB,OAAO;AAAA;AAAA,IACP,MAAM;AAAA;AAAA,IACN,OAAO;AAAA;AAAA,IACP,MAAM;AAAA;AAAA,IACN,MAAM;AAAA;AAAA,IACN,OAAO;AAAA;AAAA,IACP;AAAA;AAAA,EACD;AAED,QAAM,MAAMA,kBAAM,OAAC,KAAK;AACxB,QAAM,cAAc,IAAI,SAAS;AAGjC,QAAM,QAAQ,cAAc,MAAM,MAAM;AACxC,YAAU,QAAQ;AAGlB,YAAU,OAAO,aAAa,OAAO,YAAY,WAAW;AAE5D,YAAU,OAAO,aAAkB;AAGnC,MAAI,YAAY,QAAQ,SAAS,SAAS,GAAG;AAE3C,QAAI,YAAY,YAAY,UAAU,MAAM,YAAY,YAAY,UAAU,IAAI;AAChF,cAAQ,IAAI,eAAe;AAAA,IAC5B;AAED,cAAU,QAAQ,MAAM,SAAS,YAAY,YAAY;AACzD,cAAU,QAAQC,gBAAAA,KAAK,IAAI,MAAM,SAAS,YAAY,YAAY,KAAK;AACvE,cAAU,OAAO,MAAM,QAAQ,YAAY,YAAY;AACvD,cAAU,OAAO,aAAa,MA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