2026-06-15 14:24:25 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 微信订阅消息授权工具
|
|
|
|
|
|
* 负责在关键操作前请求用户授权订阅消息,实现提醒推送。
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// 微信公众平台配置的模板ID(与后端 .env 配置保持一致)
|
|
|
|
|
|
var TEMPLATE_IDS = {
|
|
|
|
|
|
// 订单状态变更通知
|
|
|
|
|
|
order_status_change: "TkCDtUd1d2MPzMMy_QwvaoHxvweBY-uA9p3jk3nsKi0",
|
|
|
|
|
|
// 订单待审批通知
|
|
|
|
|
|
order_approval_needed: "xNlOt-_6jMTh5VKf9F_pX4aMtwpjXZKUBBTp8QtzTag",
|
|
|
|
|
|
// 物流任务分配通知
|
|
|
|
|
|
task_assigned: "uzivHUxL1A3cBguMvC6HDO4aXqvmCV8X3o538SpDEX4",
|
|
|
|
|
|
// 物流超时提醒
|
|
|
|
|
|
logistics_timeout: "1NtPbIS_LtmsJhl4V4V3poWO_EZYYFNbKoLzO1TLTDc",
|
|
|
|
|
|
// 欠款提醒
|
|
|
|
|
|
arrears: "ulYRf4HgTXNWYSqT0KYVKJiHnG19_12tjcDXMYD2kHo",
|
|
|
|
|
|
// 客户沉默提醒
|
|
|
|
|
|
inactive_customer: "pWYFYbZNJvnOGD-4ycNf8zxmseIiuogFAXa9biVscBc",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-27 09:55:39 +08:00
|
|
|
|
var CONFIG_KEY_TO_TEMPLATE_TYPE = {
|
|
|
|
|
|
wechat_template_order_status: "order_status_change",
|
|
|
|
|
|
wechat_template_approval: "order_approval_needed",
|
|
|
|
|
|
wechat_template_task_assigned: "task_assigned",
|
|
|
|
|
|
wechat_template_logistics_timeout: "logistics_timeout",
|
|
|
|
|
|
wechat_template_arrears: "arrears",
|
|
|
|
|
|
wechat_template_inactive_customer: "inactive_customer",
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-06-15 14:24:25 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 设置模板ID(在 app.js 或配置文件中调用)
|
|
|
|
|
|
* @param {Object} ids - 模板ID映射
|
|
|
|
|
|
*/
|
|
|
|
|
|
function setTemplateIds(ids) {
|
|
|
|
|
|
Object.assign(TEMPLATE_IDS, ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-27 09:55:39 +08:00
|
|
|
|
function applyConfigItems(configItems) {
|
|
|
|
|
|
var ids = {};
|
|
|
|
|
|
(configItems || []).forEach(function (item) {
|
|
|
|
|
|
var templateType = CONFIG_KEY_TO_TEMPLATE_TYPE[item.config_key];
|
|
|
|
|
|
if (templateType && item.config_value) {
|
|
|
|
|
|
ids[templateType] = item.config_value;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
setTemplateIds(ids);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-15 14:24:25 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 请求订阅消息授权
|
|
|
|
|
|
* @param {string[]} tmplIds - 模板ID数组
|
|
|
|
|
|
* @returns {Promise<Object>} 授权结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
function requestSubscribe(tmplIds) {
|
|
|
|
|
|
// 过滤空的模板ID
|
|
|
|
|
|
var validIds = tmplIds.filter(function (id) {
|
|
|
|
|
|
return id && id.length > 0;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (validIds.length === 0) {
|
|
|
|
|
|
return Promise.resolve({});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Promise(function (resolve) {
|
|
|
|
|
|
wx.requestSubscribeMessage({
|
|
|
|
|
|
tmplIds: validIds,
|
|
|
|
|
|
success: function (res) {
|
|
|
|
|
|
console.log("[Subscribe] 授权结果:", res);
|
|
|
|
|
|
resolve(res);
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: function (err) {
|
|
|
|
|
|
console.warn("[Subscribe] 授权失败:", err);
|
|
|
|
|
|
resolve({});
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取操作对应的模板ID列表
|
|
|
|
|
|
* @param {string} action - 操作类型
|
|
|
|
|
|
* @returns {string[]} 模板ID数组
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getTemplateIdsForAction(action) {
|
|
|
|
|
|
var mapping = {
|
|
|
|
|
|
// 创建订单 -> 通知管理层审批
|
|
|
|
|
|
create_order: ["order_approval_needed"],
|
|
|
|
|
|
// 审批操作 -> 通知业务员
|
|
|
|
|
|
approve: ["order_status_change"],
|
|
|
|
|
|
// 接单 -> 订单状态变更通知
|
|
|
|
|
|
accept_task: ["order_status_change"],
|
|
|
|
|
|
// 揽货 -> 订单状态变更通知
|
|
|
|
|
|
pickup_task: ["order_status_change"],
|
|
|
|
|
|
// 送达 -> 订单状态变更通知
|
|
|
|
|
|
deliver_task: ["order_status_change"],
|
|
|
|
|
|
// 分配任务 -> 通知司机
|
|
|
|
|
|
assign_task: ["task_assigned"],
|
|
|
|
|
|
// 通用登录 -> 接收所有类型通知
|
|
|
|
|
|
login: [
|
|
|
|
|
|
"order_status_change",
|
|
|
|
|
|
"order_approval_needed",
|
|
|
|
|
|
"task_assigned",
|
|
|
|
|
|
"logistics_timeout",
|
|
|
|
|
|
"arrears",
|
|
|
|
|
|
],
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return mapping[action] || [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在关键操作前确保已授权订阅消息
|
|
|
|
|
|
* @param {string} action - 操作类型
|
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function ensureSubscribePermission(action) {
|
|
|
|
|
|
var tmplIds = getTemplateIdsForAction(action);
|
|
|
|
|
|
if (tmplIds.length === 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 检查订阅消息总开关
|
|
|
|
|
|
var setting = await new Promise(function (resolve) {
|
|
|
|
|
|
wx.getSetting({
|
|
|
|
|
|
success: function (res) {
|
|
|
|
|
|
resolve(res);
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: function () {
|
|
|
|
|
|
resolve({});
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var subscriptionsSetting = setting.subscriptionsSetting || {};
|
|
|
|
|
|
|
|
|
|
|
|
// 如果总开关已关闭,提示用户开启
|
|
|
|
|
|
if (subscriptionsSetting.mainSwitch === false) {
|
|
|
|
|
|
console.log("[Subscribe] 订阅消息总开关已关闭");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否已有授权
|
|
|
|
|
|
var itemSettings = subscriptionsSetting.itemSettings || {};
|
|
|
|
|
|
var needRequest = tmplIds.some(function (id) {
|
|
|
|
|
|
return !itemSettings[id] || itemSettings[id] === "reject";
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (needRequest) {
|
|
|
|
|
|
await requestSubscribe(tmplIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.warn("[Subscribe] 检查授权状态失败:", e);
|
|
|
|
|
|
// 降级:直接请求授权
|
|
|
|
|
|
await requestSubscribe(tmplIds);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
setTemplateIds: setTemplateIds,
|
2026-06-27 09:55:39 +08:00
|
|
|
|
applyConfigItems: applyConfigItems,
|
2026-06-15 14:24:25 +08:00
|
|
|
|
requestSubscribe: requestSubscribe,
|
|
|
|
|
|
getTemplateIdsForAction: getTemplateIdsForAction,
|
|
|
|
|
|
ensureSubscribePermission: ensureSubscribePermission,
|
|
|
|
|
|
};
|