|
@@ -68,7 +68,7 @@ onMounted(() => {
|
|
|
const val = (uni as any).getStorageSync('notificationsEnabled')
|
|
const val = (uni as any).getStorageSync('notificationsEnabled')
|
|
|
if (typeof val === 'boolean') notificationsEnabled.value = val
|
|
if (typeof val === 'boolean') notificationsEnabled.value = val
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- // ignore
|
|
|
|
|
|
|
+ // 忽略错误
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 检查用户订阅状态
|
|
// 检查用户订阅状态
|
|
@@ -77,72 +77,72 @@ onMounted(() => {
|
|
|
// 尝试判断来源:优先检查页面栈的上一个页面;若无(直接打开),则检查 launch options 的 query
|
|
// 尝试判断来源:优先检查页面栈的上一个页面;若无(直接打开),则检查 launch options 的 query
|
|
|
try {
|
|
try {
|
|
|
const pages = (getCurrentPages as any)()
|
|
const pages = (getCurrentPages as any)()
|
|
|
- console.log('Current pages stack:', pages)
|
|
|
|
|
|
|
+ console.log('当前页面栈:', pages)
|
|
|
|
|
|
|
|
if (pages && pages.length >= 1) {
|
|
if (pages && pages.length >= 1) {
|
|
|
const currentPage = pages[pages.length - 1]
|
|
const currentPage = pages[pages.length - 1]
|
|
|
const currentRoute = currentPage?.route || currentPage?.__route || currentPage?.$page?.route
|
|
const currentRoute = currentPage?.route || currentPage?.__route || currentPage?.$page?.route
|
|
|
const currentOptions = currentPage?.options || {}
|
|
const currentOptions = currentPage?.options || {}
|
|
|
|
|
|
|
|
- console.log('Current page route:', currentRoute)
|
|
|
|
|
- console.log('Current page options:', currentOptions)
|
|
|
|
|
|
|
+ console.log('当前页面路由:', currentRoute)
|
|
|
|
|
+ console.log('当前页面参数:', currentOptions)
|
|
|
|
|
|
|
|
// 检查当前页面是否包含 from=subscribe 参数
|
|
// 检查当前页面是否包含 from=subscribe 参数
|
|
|
if (currentOptions.from === 'subscribe') {
|
|
if (currentOptions.from === 'subscribe') {
|
|
|
entrySource.value = 'subscribe'
|
|
entrySource.value = 'subscribe'
|
|
|
- console.log('Entry source identified as subscribe via current page options')
|
|
|
|
|
|
|
+ console.log('通过当前页面参数识别为订阅消息来源')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 检查当前页面是否包含 templateId 相关参数
|
|
// 检查当前页面是否包含 templateId 相关参数
|
|
|
if (currentOptions.templateId === TEMPLATE_ID || currentOptions.template_id === TEMPLATE_ID) {
|
|
if (currentOptions.templateId === TEMPLATE_ID || currentOptions.template_id === TEMPLATE_ID) {
|
|
|
entrySource.value = 'subscribe'
|
|
entrySource.value = 'subscribe'
|
|
|
- console.log('Entry source identified as subscribe via templateId in current page options')
|
|
|
|
|
|
|
+ console.log('通过当前页面的模板ID参数识别为订阅消息来源')
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (pages && pages.length >= 2) {
|
|
if (pages && pages.length >= 2) {
|
|
|
const prev = pages[pages.length - 2]
|
|
const prev = pages[pages.length - 2]
|
|
|
const prevRoute = prev?.route || prev?.__route || prev?.$page?.route
|
|
const prevRoute = prev?.route || prev?.__route || prev?.$page?.route
|
|
|
- console.log('Previous page route:', prevRoute)
|
|
|
|
|
|
|
+ console.log('上一页路由:', prevRoute)
|
|
|
if (prevRoute && String(prevRoute).includes('pages/health/index')) {
|
|
if (prevRoute && String(prevRoute).includes('pages/health/index')) {
|
|
|
entrySource.value = 'healthIndex'
|
|
entrySource.value = 'healthIndex'
|
|
|
- console.log('Entry source identified as healthIndex')
|
|
|
|
|
|
|
+ console.log('识别来源为健康首页')
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
// 如果没有上一页信息,检查小程序启动参数(例如用户通过订阅消息打开)
|
|
// 如果没有上一页信息,检查小程序启动参数(例如用户通过订阅消息打开)
|
|
|
if ((uni as any).getLaunchOptionsSync) {
|
|
if ((uni as any).getLaunchOptionsSync) {
|
|
|
const launch = (uni as any).getLaunchOptionsSync()
|
|
const launch = (uni as any).getLaunchOptionsSync()
|
|
|
- console.log('Launch options:', launch)
|
|
|
|
|
|
|
+ console.log('启动参数:', launch)
|
|
|
if (launch && launch.query) {
|
|
if (launch && launch.query) {
|
|
|
- console.log('Query params:', launch.query)
|
|
|
|
|
|
|
+ console.log('查询参数:', launch.query)
|
|
|
// 如果你在推送消息里把 from=subscribe 作为参数传入,这里会命中
|
|
// 如果你在推送消息里把 from=subscribe 作为参数传入,这里会命中
|
|
|
if (launch.query.from === 'subscribe') {
|
|
if (launch.query.from === 'subscribe') {
|
|
|
entrySource.value = 'subscribe'
|
|
entrySource.value = 'subscribe'
|
|
|
- console.log('Entry source identified as subscribe via from param')
|
|
|
|
|
|
|
+ console.log('通过 from 参数识别为订阅消息来源')
|
|
|
}
|
|
}
|
|
|
// 有些平台会把 templateId 或其它字段放在 query 中,可据此判断
|
|
// 有些平台会把 templateId 或其它字段放在 query 中,可据此判断
|
|
|
if (launch.query.templateId === TEMPLATE_ID || launch.query.template_id === TEMPLATE_ID) {
|
|
if (launch.query.templateId === TEMPLATE_ID || launch.query.template_id === TEMPLATE_ID) {
|
|
|
entrySource.value = 'subscribe'
|
|
entrySource.value = 'subscribe'
|
|
|
- console.log('Entry source identified as subscribe via templateId param')
|
|
|
|
|
|
|
+ console.log('通过模板ID参数识别为订阅消息来源')
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- console.error('Error identifying entry source:', e)
|
|
|
|
|
|
|
+ console.error('识别入口来源时出错:', e)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 显示来源提示(用于测试),延迟一点以保证 UI 已就绪
|
|
// 显示来源提示(用于测试),延迟一点以保证 UI 已就绪
|
|
|
try {
|
|
try {
|
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
|
- console.log('Entry source:', entrySource.value, entrySourceText.value)
|
|
|
|
|
|
|
+ console.log('入口来源:', entrySource.value, entrySourceText.value)
|
|
|
}, 250)
|
|
}, 250)
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- // ignore
|
|
|
|
|
|
|
+ // 忽略错误
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (entrySource.value === 'unknown') {
|
|
if (entrySource.value === 'unknown') {
|
|
|
- console.log('Entry source defaulted to unknown')
|
|
|
|
|
|
|
+ console.log('入口来源默认为未知')
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -155,51 +155,51 @@ const checkSubscriptionStatus = () => {
|
|
|
(uni as any).getSetting({
|
|
(uni as any).getSetting({
|
|
|
withSubscriptions: true, // 启用订阅设置查询
|
|
withSubscriptions: true, // 启用订阅设置查询
|
|
|
success: (res: any) => {
|
|
success: (res: any) => {
|
|
|
- console.log('User settings:', res)
|
|
|
|
|
|
|
+ console.log('用户设置:', res)
|
|
|
|
|
|
|
|
// 检查订阅消息设置
|
|
// 检查订阅消息设置
|
|
|
if (res.subscriptionsSetting) {
|
|
if (res.subscriptionsSetting) {
|
|
|
- console.log('Subscriptions setting:', JSON.stringify(res.subscriptionsSetting, null, 2))
|
|
|
|
|
|
|
+ console.log('订阅设置详情:', JSON.stringify(res.subscriptionsSetting, null, 2))
|
|
|
|
|
|
|
|
// 检查主开关
|
|
// 检查主开关
|
|
|
const mainSwitch = res.subscriptionsSetting.mainSwitch
|
|
const mainSwitch = res.subscriptionsSetting.mainSwitch
|
|
|
- console.log('Subscription main switch:', mainSwitch)
|
|
|
|
|
|
|
+ console.log('订阅主开关:', mainSwitch)
|
|
|
|
|
|
|
|
// 检查针对特定模板的设置
|
|
// 检查针对特定模板的设置
|
|
|
const itemSettings = res.subscriptionsSetting.itemSettings || {}
|
|
const itemSettings = res.subscriptionsSetting.itemSettings || {}
|
|
|
const templateStatus = itemSettings[TEMPLATE_ID]
|
|
const templateStatus = itemSettings[TEMPLATE_ID]
|
|
|
- console.log(`Template ${TEMPLATE_ID} status:`, templateStatus)
|
|
|
|
|
|
|
+ console.log(`模板 ${TEMPLATE_ID} 状态:`, templateStatus)
|
|
|
|
|
|
|
|
// 如果主开关关闭或特定模板被拒绝,则更新本地状态
|
|
// 如果主开关关闭或特定模板被拒绝,则更新本地状态
|
|
|
if (mainSwitch === false || templateStatus === 'reject' || templateStatus === 'ban') {
|
|
if (mainSwitch === false || templateStatus === 'reject' || templateStatus === 'ban') {
|
|
|
- console.log('Subscription disabled by user settings, updating local state')
|
|
|
|
|
|
|
+ console.log('用户关闭了订阅设置,正在更新本地状态')
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
|
(uni as any).setStorageSync('notificationsEnabled', false)
|
|
(uni as any).setStorageSync('notificationsEnabled', false)
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- console.error('Failed to update storage:', e)
|
|
|
|
|
|
|
+ console.error('更新存储失败:', e)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
// 如果模板被接受且主开关开启,更新本地状态为true
|
|
// 如果模板被接受且主开关开启,更新本地状态为true
|
|
|
else if (mainSwitch === true && templateStatus === 'accept') {
|
|
else if (mainSwitch === true && templateStatus === 'accept') {
|
|
|
- console.log('Subscription enabled by user settings, updating local state')
|
|
|
|
|
|
|
+ console.log('用户开启了订阅设置,正在更新本地状态')
|
|
|
notificationsEnabled.value = true
|
|
notificationsEnabled.value = true
|
|
|
try {
|
|
try {
|
|
|
(uni as any).setStorageSync('notificationsEnabled', true)
|
|
(uni as any).setStorageSync('notificationsEnabled', true)
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- console.error('Failed to update storage:', e)
|
|
|
|
|
|
|
+ console.error('更新存储失败:', e)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- console.log('No subscriptionsSetting found in response')
|
|
|
|
|
|
|
+ console.log('响应中未找到订阅设置')
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
fail: (err: any) => {
|
|
fail: (err: any) => {
|
|
|
- console.error('Failed to get user settings:', err)
|
|
|
|
|
|
|
+ console.error('获取用户设置失败:', err)
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
} else {
|
|
} else {
|
|
|
- console.log('uni.getSetting is not available in this environment')
|
|
|
|
|
|
|
+ console.log('当前环境不支持 uni.getSetting')
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -211,27 +211,29 @@ const checkSubscriptionStatus = () => {
|
|
|
*/
|
|
*/
|
|
|
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)
|
|
|
|
|
|
|
+ console.log('通知开关更改为:', newVal)
|
|
|
|
|
|
|
|
if (newVal) {
|
|
if (newVal) {
|
|
|
|
|
+ // 先将开关设置为开启状态
|
|
|
|
|
+ notificationsEnabled.value = true
|
|
|
|
|
+
|
|
|
// 请求订阅(仅在微信/小程序有效)
|
|
// 请求订阅(仅在微信/小程序有效)
|
|
|
;(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)
|
|
|
|
|
|
|
+ console.log('订阅消息请求成功结果:', 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
|
|
|
|
|
|
|
+ console.log('用户接受了订阅')
|
|
|
try {
|
|
try {
|
|
|
;(uni as any).setStorageSync('notificationsEnabled', true)
|
|
;(uni as any).setStorageSync('notificationsEnabled', true)
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
- // ignore storage error
|
|
|
|
|
|
|
+ // 忽略存储错误
|
|
|
}
|
|
}
|
|
|
uni.showToast({ title: '订阅成功', icon: 'success' })
|
|
uni.showToast({ title: '订阅成功', icon: 'success' })
|
|
|
} else {
|
|
} else {
|
|
|
- console.log('User did not accept the subscription, result:', result)
|
|
|
|
|
|
|
+ console.log('用户未接受订阅,结果:', result)
|
|
|
// 用户拒绝或关闭了弹窗
|
|
// 用户拒绝或关闭了弹窗
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
@@ -241,17 +243,22 @@ const onNotificationChange = (e: any) => {
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
fail(err: any) {
|
|
fail(err: any) {
|
|
|
- console.log('requestSubscribeMessage failed:', err)
|
|
|
|
|
|
|
+ console.log('订阅消息请求失败:', err)
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
|
} catch (e) {}
|
|
} catch (e) {}
|
|
|
- uni.showToast({ title: '订阅请求失败', icon: 'none' })
|
|
|
|
|
|
|
+ // 根据错误类型显示不同的提示信息
|
|
|
|
|
+ if (err.errCode === 20004) {
|
|
|
|
|
+ uni.showToast({ title: '推送权限已关闭', icon: 'none' })
|
|
|
|
|
+ } else {
|
|
|
|
|
+ uni.showToast({ title: '订阅请求失败', icon: 'none' })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
} else {
|
|
} else {
|
|
|
// 关闭订阅:不需要额外调用接口,只改变本地记录
|
|
// 关闭订阅:不需要额外调用接口,只改变本地记录
|
|
|
- console.log('Notification switch turned off')
|
|
|
|
|
|
|
+ console.log('通知开关已关闭')
|
|
|
notificationsEnabled.value = false
|
|
notificationsEnabled.value = false
|
|
|
try {
|
|
try {
|
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|
|
;(uni as any).setStorageSync('notificationsEnabled', false)
|