Files
lilishop-uniapp/js_sdk/lili-pay/wx-pay.js
pikachu1995@126.com e1a48d6d11 refactor(after-sales): 删除独立售后页面并整合至订单列表
- 删除 afterSales.vue 独立页面,将售后入口统一至 myOrder 页面
- 修改售后相关页面跳转路径为 /pages/order/myOrder?status=5
- 重构 applySuccess 页面,增加微信订阅消息授权与视觉优化
- 重构预存款明细页面,增加收支筛选与日志解析功能
- 新增 store 端 API 基础地址配置
2026-11-02 16:57:29 +08:00

163 lines
4.8 KiB
JavaScript

/**
* 微信小程序支付
* 此处针对于微信小程序开发的支付插件
* 第一次支付成功后会跳出订阅的消息 如果用户拒绝或同意都会跳转到支付成功页面
*
* @param {sn,price,orderPromotionType}
*/
import { getWeChatMpMessage } from "@/api/message.js";
import { initiatePay } from "@/api/trade";
const SCENE_NAME_MAP = {
ORDER_DELIVER: ["订单发货提醒", "订单发货通知"],
ORDER_COMPLETE: ["订单已完成提醒", "订单完成通知"],
PINTUAN_SUCCESS: ["拼团成功通知"],
AFTER_SALE_AUDIT: ["售后申请审核通知", "售后审核通知"],
POINT_CHANGE: ["积分变更提醒", "积分变更通知"],
WALLET_CHANGE: ["账户余额变动通知", "余额账户变更通知"],
WALLET_WITHDRAWAL_AUDIT_ERROR: ["提现结果通知", "提现申请审核通知"],
};
let cachedMpMessageList = null;
export function prefetchMpMessageTemplates() {
return getWeChatMpMessage()
.then((res) => {
if (res && res.data && Array.isArray(res.data.result)) {
cachedMpMessageList = res.data.result;
}
return cachedMpMessageList || [];
})
.catch(() => cachedMpMessageList || []);
}
function extractTmplIdsByScenes(list, targetScenes) {
const tmplIds = [];
(targetScenes || []).forEach((scene) => {
const aliasNames = SCENE_NAME_MAP[scene] || [];
const matched = (list || []).find(
(item) =>
item &&
item.enable !== false &&
item.code &&
(item.sceneCode === scene || aliasNames.includes(item.name))
);
if (matched && matched.code && !tmplIds.includes(matched.code)) {
tmplIds.push(matched.code);
}
});
return tmplIds.slice(0, 3);
}
export function subscribeMpMessageByScenes(sceneCodes, onComplete) {
const done = typeof onComplete === "function" ? onComplete : () => {};
const targetScenes = Array.isArray(sceneCodes) ? sceneCodes : [];
if (!targetScenes.length) {
done();
return;
}
if (Array.isArray(cachedMpMessageList) && cachedMpMessageList.length) {
const finalTmplIds = extractTmplIdsByScenes(cachedMpMessageList, targetScenes);
if (!finalTmplIds.length) {
done();
return;
}
console.log("[微信订阅消息] 请求订阅 tmplIds:", finalTmplIds);
uni.requestSubscribeMessage({
tmplIds: finalTmplIds,
complete: (res) => {
console.log("[微信订阅消息] 授权返回结果:", res);
done();
},
});
return;
}
prefetchMpMessageTemplates()
.then((list) => {
const finalTmplIds = extractTmplIdsByScenes(list, targetScenes);
if (!finalTmplIds.length) {
done();
return;
}
uni.requestSubscribeMessage({
tmplIds: finalTmplIds,
complete: () => done(),
});
})
.catch(() => done());
}
export function subscribePayMessageThen(onComplete, options = {}) {
const isPintuan =
options &&
String(options.orderPromotionType || "").toUpperCase() === "PINTUAN";
const scenes = isPintuan
? ["ORDER_DELIVER", "ORDER_COMPLETE", "PINTUAN_SUCCESS"]
: ["ORDER_DELIVER", "ORDER_COMPLETE"];
subscribeMpMessageByScenes(scenes, onComplete);
}
class LiLiWXPay {
constructor(...payList) {
this.data = payList[0];
this.pay = () => {
uni.showLoading({
title: "加载中",
});
let submitData = {
sn: this.data.sn,
orderType: this.data.orderType || "TRADE",
clientType: "WECHAT_MP",
};
const paymentMethod = "WECHAT";
const paymentClient = "MP";
initiatePay(paymentMethod, paymentClient, submitData).then((res) => {
let response = res.data.result;
uni.hideLoading();
uni.requestPayment({
provider: "wxpay",
appid: response.appid,
timeStamp: response.timeStamp,
nonceStr: response.nonceStr,
package: response.package,
signType: response.signType,
paySign: response.paySign,
success: () => {
uni.showToast({
icon: "none",
title: "支付成功!",
});
subscribePayMessageThen(
() => {
uni.redirectTo({
url:
"/pages/cart/payment/success?paymentMethod=WECHAT" +
"&payPrice=" +
payList[0].price,
});
},
{ orderPromotionType: this.data.orderPromotionType }
);
},
fail: (e) => {
this.exception = e;
uni.showModal({
content: "支付失败,如果您已支付,请勿反复支付",
showCancel: false,
success: () => {
uni.redirectTo({
url: "/pages/order/myOrder?status=0",
});
},
});
},
});
});
};
}
}
export default LiLiWXPay;