文件
shanghaipeiwan/pwuniapp/common/subscribe.js
xcbyy 029c6189a7 feat(mini): 下单/提现/报名前补订阅消息授权(提升微信服务通知到达率)
- 新增 common/subscribe.js:页面预加载模板ID + 点击动作中同步请求授权(避免 await 丢失手势上下文)

- 下单支付前请求订单状态/核销码模板;提现前请求到账模板;组局报名确认后请求报名模板

- 自动过滤 tmpl_ 占位模板;授权失败不阻塞业务

- 新增回归护栏 subscribe-authorization.spec.ts(80 套件/615 用例全绿)
2026-10-19 09:01:36 +08:00

52 行
1.5 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

// 已读取AGENTS.md
// 微信订阅消息授权工具:预加载模板ID,在关键动作点击时同步请求授权。
// 说明:订阅消息为一次性订阅,需在用户点击(或确认弹窗)动作中调用 requestSubscribeFor。
import { getSubscribeTemplates } from './api.js';
const SCENE_KEYS = {
order: ['orderStatusTemplateId', 'verifyCodeTemplateId'],
withdraw: ['withdrawSuccessTemplateId'],
activity: ['activityJoinTemplateId']
};
let templates = null;
/** 页面加载时预加载模板配置(避免在点击时 await 丢失手势上下文) */
export async function preloadSubscribeTemplates() {
try {
templates = await getSubscribeTemplates();
} catch (_) {
templates = null;
}
return templates;
}
/** 过滤掉占位/空模板ID,只保留真实模板 */
function resolveTmplIds(scene) {
if (!templates || templates.enablePush === false) return [];
const keys = SCENE_KEYS[scene] || [];
return keys
.map((k) => templates[k])
.filter((x) => x && !String(x).startsWith('tmpl_'));
}
/**
* 在关键动作前请求订阅授权(同步调用,不阻塞业务)。
* scene: 'order' | 'withdraw' | 'activity'
*/
export function requestSubscribeFor(scene) {
try {
// #ifdef MP-WEIXIN
if (typeof uni.requestSubscribeMessage !== 'function') return;
const tmplIds = resolveTmplIds(scene);
if (tmplIds.length === 0) return;
uni.requestSubscribeMessage({
tmplIds,
complete: () => {}
});
// #endif
} catch (_) {
// 授权失败不影响主流程
}
}