Suzhou-SafetyToolsWarning-WX/pages/warning/settings/settings.js

185 lines
3.3 KiB
JavaScript

// pages/warning/settings/settings.js
const app = getApp();
Page({
data: {
warningDays: 30,
subscriptionStatus: {
inspection: false,
warning: false
}
},
onLoad() {
this.loadSettings();
},
onShow() {
this.checkSubscriptionStatus();
},
/**
* 加载设置
*/
loadSettings() {
const settings = app.getNotificationSettings();
this.setData({
warningDays: settings.advanceDays || 30
});
},
/**
* 检查订阅状态
*/
checkSubscriptionStatus() {
const status = app.getSubscriptionStatus();
this.setData({
subscriptionStatus: {
inspection: status.inspectionReminder,
warning: status.warningNotice
}
});
},
/**
* 显示天数选择器
*/
showDaysPicker() {
wx.showActionSheet({
itemList: ['7天', '15天', '30天', '60天', '90天'],
success: (res) => {
const days = [7, 15, 30, 60, 90][res.tapIndex];
this.setData({ warningDays: days });
wx.showToast({
title: '设置成功',
icon: 'success'
});
}
});
},
/**
* 订阅检验提醒
*/
async subscribeInspection() {
if (!app.isLoggedIn()) {
this.showLoginTip();
return;
}
try {
await app.requestSubscribeMessage([
app.TEMPLATE_IDS.INSPECTION_REMINDER
]);
this.checkSubscriptionStatus();
wx.showToast({
title: '订阅成功',
icon: 'success'
});
} catch (error) {
console.error('订阅失败', error);
wx.showToast({
title: '订阅失败',
icon: 'none'
});
}
},
/**
* 订阅预警通知
*/
async subscribeWarning() {
if (!app.isLoggedIn()) {
this.showLoginTip();
return;
}
try {
await app.requestSubscribeMessage([
app.TEMPLATE_IDS.WARNING_NOTICE
]);
this.checkSubscriptionStatus();
wx.showToast({
title: '订阅成功',
icon: 'success'
});
} catch (error) {
console.error('订阅失败', error);
wx.showToast({
title: '订阅失败',
icon: 'none'
});
}
},
/**
* 订阅所有消息
*/
async subscribeAll() {
if (!app.isLoggedIn()) {
this.showLoginTip();
return;
}
try {
await app.subscribeAllMessages();
this.checkSubscriptionStatus();
wx.showToast({
title: '订阅成功',
icon: 'success'
});
} catch (error) {
console.error('订阅失败', error);
wx.showToast({
title: '订阅失败',
icon: 'none'
});
}
},
/**
* 保存设置
*/
saveSettings() {
const settings = {
enabled: true,
inspectionReminder: this.data.subscriptionStatus.inspection,
warningNotice: this.data.subscriptionStatus.warning,
advanceDays: this.data.warningDays
};
app.saveNotificationSettings(settings);
wx.showToast({
title: '保存成功',
icon: 'success'
});
},
/**
* 显示登录提示
*/
showLoginTip() {
wx.showModal({
title: '提示',
content: '请先登录',
confirmText: '去登录',
success: res => {
if (res.confirm) {
wx.navigateTo({
url: '/pages/login/login'
});
}
}
});
}
});