|
@@ -71,6 +71,9 @@ onMounted(() => {
|
|
|
// ignore
|
|
// ignore
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 检查用户订阅状态
|
|
|
|
|
+ checkSubscriptionStatus()
|
|
|
|
|
+
|
|
|
// 尝试判断来源:优先检查页面栈的上一个页面;若无(直接打开),则检查 launch options 的 query
|
|
// 尝试判断来源:优先检查页面栈的上一个页面;若无(直接打开),则检查 launch options 的 query
|
|
|
try {
|
|
try {
|
|
|
const pages = (getCurrentPages as any)()
|
|
const pages = (getCurrentPages as any)()
|
|
@@ -143,6 +146,63 @@ onMounted(() => {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 检查用户订阅状态
|
|
|
|
|
+ */
|
|
|
|
|
+const checkSubscriptionStatus = () => {
|
|
|
|
|
+ // 使用 uni.getSetting 检查用户授权状态
|
|
|
|
|
+ if (typeof (uni as any).getSetting === 'function') {
|
|
|
|
|
+ (uni as any).getSetting({
|
|
|
|
|
+ withSubscriptions: true, // 启用订阅设置查询
|
|
|
|
|
+ success: (res: any) => {
|
|
|
|
|
+ console.log('User settings:', res)
|
|
|
|
|
+
|
|
|
|
|
+ // 检查订阅消息设置
|
|
|
|
|
+ if (res.subscriptionsSetting) {
|
|
|
|
|
+ console.log('Subscriptions setting:', JSON.stringify(res.subscriptionsSetting, null, 2))
|
|
|
|
|
+
|
|
|
|
|
+ // 检查主开关
|
|
|
|
|
+ const mainSwitch = res.subscriptionsSetting.mainSwitch
|
|
|
|
|
+ console.log('Subscription main switch:', mainSwitch)
|
|
|
|
|
+
|
|
|
|
|
+ // 检查针对特定模板的设置
|
|
|
|
|
+ const itemSettings = res.subscriptionsSetting.itemSettings || {}
|
|
|
|
|
+ const templateStatus = itemSettings[TEMPLATE_ID]
|
|
|
|
|
+ console.log(`Template ${TEMPLATE_ID} status:`, templateStatus)
|
|
|
|
|
+
|
|
|
|
|
+ // 如果主开关关闭或特定模板被拒绝,则更新本地状态
|
|
|
|
|
+ if (mainSwitch === false || templateStatus === 'reject' || templateStatus === 'ban') {
|
|
|
|
|
+ console.log('Subscription disabled by user settings, updating local state')
|
|
|
|
|
+ notificationsEnabled.value = false
|
|
|
|
|
+ try {
|
|
|
|
|
+ (uni as any).setStorageSync('notificationsEnabled', false)
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error('Failed to update storage:', e)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 如果模板被接受且主开关开启,更新本地状态为true
|
|
|
|
|
+ else if (mainSwitch === true && templateStatus === 'accept') {
|
|
|
|
|
+ console.log('Subscription enabled by user settings, updating local state')
|
|
|
|
|
+ notificationsEnabled.value = true
|
|
|
|
|
+ try {
|
|
|
|
|
+ (uni as any).setStorageSync('notificationsEnabled', true)
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error('Failed to update storage:', e)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.log('No subscriptionsSetting found in response')
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: (err: any) => {
|
|
|
|
|
+ console.error('Failed to get user settings:', err)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.log('uni.getSetting is not available in this environment')
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 顶部总开关变更
|
|
* 顶部总开关变更
|
|
|
* - 打开时:调用 uni.requestSubscribeMessage 请求用户同意订阅 TEMPLATE_ID
|
|
* - 打开时:调用 uni.requestSubscribeMessage 请求用户同意订阅 TEMPLATE_ID
|
|
@@ -151,14 +211,18 @@ onMounted(() => {
|
|
|
*/
|
|
*/
|
|
|
const onNotificationChange = (e: any) => {
|
|
const onNotificationChange = (e: any) => {
|
|
|
const newVal = e?.detail?.value
|
|
const newVal = e?.detail?.value
|
|
|
|
|
+ console.log('Notification switch changed to:', newVal)
|
|
|
|
|
+
|
|
|
if (newVal) {
|
|
if (newVal) {
|
|
|
// 请求订阅(仅在微信/小程序有效)
|
|
// 请求订阅(仅在微信/小程序有效)
|
|
|
;(uni as any).requestSubscribeMessage({
|
|
;(uni as any).requestSubscribeMessage({
|
|
|
tmplIds: [TEMPLATE_ID],
|
|
tmplIds: [TEMPLATE_ID],
|
|
|
success(res: any) {
|
|
success(res: any) {
|
|
|
|
|
+ console.log('requestSubscribeMessage success result:', res)
|
|
|
// res 可能形如 { "ACS7...": 'accept' }
|
|
// res 可能形如 { "ACS7...": 'accept' }
|
|
|
const result = res && res[TEMPLATE_ID]
|
|
const result = res && res[TEMPLATE_ID]
|
|
|
if (result === 'accept') {
|
|
if (result === 'accept') {
|
|
|
|
|
+ console.log('User accepted the subscription')
|
|
|
notificationsEnabled.value = true
|
|
notificationsEnabled.value = true
|
|
|
try {
|
|
try {
|
|
|
;(uni as any).setStorageSync('notificationsEnabled', true)
|
|
;(uni as any).setStorageSync('notificationsEnabled', true)
|
|
@@ -167,6 +231,7 @@ const onNotificationChange = (e: any) => {
|
|
|
}
|
|
}
|
|
|
uni.showToast({ title: '订阅成功', icon: 'success' })
|
|
uni.showToast({ title: '订阅成功', icon: 'success' })
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ console.log('User did not accept the subscription, result:', result)
|
|
|
// 用户拒绝或关闭了弹窗
|
|
// 用户拒绝或关闭了弹窗
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
@@ -176,6 +241,7 @@ const onNotificationChange = (e: any) => {
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
fail(err: any) {
|
|
fail(err: any) {
|
|
|
|
|
+ console.log('requestSubscribeMessage failed:', err)
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
@@ -185,6 +251,7 @@ const onNotificationChange = (e: any) => {
|
|
|
})
|
|
})
|
|
|
} else {
|
|
} else {
|
|
|
// 关闭订阅:不需要额外调用接口,只改变本地记录
|
|
// 关闭订阅:不需要额外调用接口,只改变本地记录
|
|
|
|
|
+ console.log('Notification switch turned off')
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
@@ -271,4 +338,4 @@ const toggleReminder = (index: number) => {
|
|
|
color: #8a8a8a;
|
|
color: #8a8a8a;
|
|
|
margin-top: 6rpx;
|
|
margin-top: 6rpx;
|
|
|
}
|
|
}
|
|
|
-</style>
|
|
|
|
|
|
|
+</style>
|