acdr-ui/dist/dev/mp-weixin/sheep/platform/share.js

186 lines
5.3 KiB
JavaScript

"use strict";
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
const common_vendor = require("../../common/vendor.js");
const sheep_store_index = require("../store/index.js");
const sheep_platform_index = require("./index.js");
const sheep_router_index = require("../router/index.js");
const sheep_url_index = require("../url/index.js");
const sheep_api_trade_brokerage = require("../api/trade/brokerage.js");
const platformMap = ["H5", "WechatOfficialAccount", "WechatMiniProgram", "App"];
const fromMap = ["forward", "poster", "link"];
const getShareInfo = (scene = {
title: "",
// 自定义分享标题
desc: "",
// 自定义描述
image: "",
// 自定义分享图片
params: {}
// 自定义分享参数
}, poster = {
// 自定义海报数据
type: "user"
}) => {
const shareInfo = {
title: "",
// 分享标题
desc: "",
// 描述
image: "",
// 分享图片
path: "",
// 分享页面+参数
link: "",
// 分享Url+参数
query: "",
// 分享参数
poster
// 海报所需数据
};
const app = sheep_store_index.$store("app");
const shareConfig = app.platform.share;
const query = buildSpmQuery(scene.params);
shareInfo.query = query;
shareInfo.link = buildSpmLink(query, shareConfig.linkAddress);
shareInfo.path = buildSpmPath();
if (shareConfig.methods.includes("forward")) {
if (shareConfig.forwardInfo.title === "" || shareConfig.forwardInfo.image === "") {
console.log("请在平台设置中配置转发信息");
}
shareInfo.title = scene.title || shareConfig.forwardInfo.title;
shareInfo.image = sheep_url_index.$url.cdn(scene.image || shareConfig.forwardInfo.image);
shareInfo.desc = scene.desc || shareConfig.forwardInfo.subtitle;
shareInfo.path = buildSpmPath(scene.path);
}
return shareInfo;
};
const buildSpmQuery = (params) => {
const user = sheep_store_index.$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;
}
const platform = platformMap.indexOf(sheep_platform_index._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`;
};
const buildSpmLink = (query, linkAddress = "") => {
return `${linkAddress}?${query}`;
};
const decryptSpm = (spm) => {
const user = sheep_store_index.$store("user");
const shareParamsArray = spm.split(".");
const shareParams = {
spm,
shareId: 0,
page: "",
query: {},
platform: "",
from: ""
};
let query;
shareParams.shareId = shareParamsArray[0];
switch (shareParamsArray[1]) {
case "1":
shareParams.page = "/modules/mall/index/index";
break;
case "2":
shareParams.page = "/modules/mall/goods/index";
shareParams.query = {
id: shareParamsArray[2]
};
break;
case "3":
shareParams.page = "/modules/mall/goods/groupon";
query = shareParamsArray[2].split(",");
shareParams.query = {
id: query[0],
activity_id: query[1]
// TODO 芋艿:接入分享后,应该统一成 id 参数
};
break;
case "4":
shareParams.page = "/modules/mall/goods/seckill";
query = shareParamsArray[2].split(",");
shareParams.query = {
id: query[1]
};
break;
case "5":
shareParams.page = "/modules/mall/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) {
bindBrokerageUser(shareParams.shareId);
} else {
common_vendor.index.setStorageSync("shareId", shareParams.shareId);
}
}
if (shareParams.page !== "/modules/mall/index/index") {
sheep_router_index.$router.go(shareParams.page, shareParams.query);
}
return shareParams;
};
const bindBrokerageUser = (val = void 0) => __async(exports, null, function* () {
try {
const shareId = val || common_vendor.index.getStorageSync("shareId");
if (!shareId) {
return;
}
yield sheep_api_trade_brokerage.BrokerageApi.bindBrokerageUser({ bindUserId: shareId });
common_vendor.index.removeStorageSync("shareId");
} catch (e) {
}
});
const updateShareInfo = (shareInfo) => {
};
const $share = {
getShareInfo,
updateShareInfo,
decryptSpm,
bindBrokerageUser
};
exports.$share = $share;
//# sourceMappingURL=share.js.map