jingshuiji/pages/SubscribeMessage/SubscribeMessage.js
2025-06-06 15:07:26 +08:00

176 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Page({
data: {
subscribeList: [
{ id: 1, name: '订单付款通知', subscribed: false, },
{ id: 2, name: '订单续费通知', subscribed: false },
{ id: 3, name: '欠费通知', subscribed: false },
{ id: 4, name: '新注册用户提醒', subscribed: false },
{ id: 5, name: '工单状态通知', subscribed: false },
{ id: 6, name: '工单完成通知', subscribed: false },
{ id: 7, name: '工单派工通知', subscribed: false },
{ id: 8, name: '设备告警通知', subscribed: false },
{ id: 9, name: '订单重新指派提醒', subscribed: false },
{ id: 10, name: '新订单提醒', subscribed: false },
{ id: 11, name: '设备故障通知', subscribed: false },
]
},
onLoad() {
// 从缓存加载用户订阅状态
const savedSubs = wx.getStorageSync('subscribeSettings') || {};
this.setData({
subscribeList: this.data.subscribeList.map(item => ({
...item,
subscribed: savedSubs[item.id] || false
}))
});
},
toggleSubscribe(e) {
const { id } = e.currentTarget.dataset;
const newValue = e.detail.value;
// 检查是否之前被拒绝过
const rejected = wx.getStorageSync('rejectedSubscriptions') || {};
if (newValue && rejected[id]) {
wx.showModal({
title: '提示',
content: '您之前拒绝了订阅,需要前往设置重新开启',
confirmText: '去设置',
success: (res) => {
if (res.confirm) {
wx.openSetting({
success: (res) => {
if (res.authSetting['scope.subscribeMessage']) {
// 用户开启了订阅权限立即更新UI状态为开启
this.setData({
subscribeList: this.data.subscribeList.map(item =>
item.id === id ? { ...item, subscribed: true } : item
)
});
// 清除拒绝记录
const rejected = wx.getStorageSync('rejectedSubscriptions') || {};
delete rejected[id];
wx.setStorageSync('rejectedSubscriptions', rejected);
// 保存订阅状态
this.saveSubscribeStatus(id, true);
// 主动触发订阅请求
this.requestSubscribeMessage(id, true);
} else {
// 恢复开关状态
this.setData({
subscribeList: this.data.subscribeList.map(item =>
item.id === id ? { ...item, subscribed: false } : item
)
});
}
}
});
} else {
// 恢复开关状态
this.setData({
subscribeList: this.data.subscribeList.map(item =>
item.id === id ? { ...item, subscribed: false } : item
)
});
}
}
});
return;
}
// 立即更新UI状态为点击后的值临时状态
this.setData({
subscribeList: this.data.subscribeList.map(item =>
item.id === id ? { ...item, subscribed: newValue } : item
)
});
// 发送订阅请求
this.requestSubscribeMessage(id, newValue);
},
// 替换 toggleSubscribe 方法
handleSubscribe(e) {
const { id } = e.currentTarget.dataset;
const rejected = wx.getStorageSync('rejectedSubscriptions') || {};
if (rejected[id]) {
wx.showModal({
title: '提示',
content: '您之前拒绝了订阅,请点击确定前往设置页面开启订阅权限',
success: (res) => {
if (res.confirm) {
wx.openSetting({
success: (res) => {
// 这里只需要检查用户是否打开了总开关
if (res.authSetting['scope.subscribeMessage']) {
// 重新尝试订阅
this.requestSubscribeMessage(id);
}
}
});
}
}
});
} else {
this.requestSubscribeMessage(id);
}
},
// 修改 requestSubscribeMessage 方法
requestSubscribeMessage(id) {
const tmplIds = {
1: '5yPg-WOoP9-9ZU1fHjC4zg1KNaPWb76K87JzzKb58f0', // 滤芯更换提醒模板ID
2: '2BX7Zh5ccLzmHvvbCHuPWiaoZQyDCGfziCruxUct_EU', // 设备异常提醒模板ID
3: 'eEetEKoNpqXk_EY0xqMt22_Xd7NFEyiODY6j0t3_A24', // 水质检测报告模板ID
4: 're34uubgvrwJLaiM3LhQEmvxrRxoNcJbo7b8gcbxx44', // 服务到期提醒模板ID
5: '_s7GcsGNqapbnlLAJ5lUFexCEAx-dl4RD-DwwL9QqC4', // 服务到期提醒模板ID
6: 'CWtF10H3syth9rdUaGr-4XojSa8TJMflb2z7zStw384', // 服务到期提醒模板ID
7: 'qLYGETdlX5pR8WvTs8v4g4zlBOhA04z46KS_Q_yesr4', // 服务到期提醒模板ID
8: 'T87KsBIrVrjgO4VETEOpIn4c4-bsxOTpzM6lR0ghduo', // 服务到期提醒模板ID
9: 'q3Aa07wgYPgB23mu4JOuSzgFoiYhouUcw-NhV5CNuSA', // 服务到期提醒模板ID
10: 'pUkdPwh7jWLjCWWv2zyz7I086xNO_GoaJQ6A-cYuGVg', // 服务到期提醒模板ID
11: 'VXRbeTG6gPPOrxTSwF_da8jzqH0UVHxQykYAH0XH51s' // 促销活动通知模板ID
};
wx.requestSubscribeMessage({
tmplIds: [tmplIds[id]],
success: (res) => {
if (res[tmplIds[id]] === 'accept') {
// 清除拒绝记录
const rejected = wx.getStorageSync('rejectedSubscriptions') || {};
delete rejected[id];
wx.setStorageSync('rejectedSubscriptions', rejected);
// 保存订阅状态
this.saveSubscribeStatus(id, true);
wx.showToast({ title: '订阅成功' });
} else {
// 记录拒绝状态
const rejected = wx.getStorageSync('rejectedSubscriptions') || {};
rejected[id] = true;
wx.setStorageSync('rejectedSubscriptions', rejected);
wx.showToast({
title: '订阅被拒绝',
icon: 'none'
});
}
},
fail: (err) => {
wx.showToast({
title: '订阅失败',
icon: 'none'
});
}
});
},
// saveSubscribeStatus 方法保持不变
saveSubscribeStatus(id, subscribed) {
// 只更新缓存不更新UI
const settings = wx.getStorageSync('subscribeSettings') || {};
settings[id] = subscribed;
wx.setStorageSync('subscribeSettings', settings);
}
});