reminder.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <CustomNav title="健康提醒" leftType="back" />
  3. <view class="content">
  4. <view class="notification-toggle">
  5. <text class="notification-label">消息通知</text>
  6. <switch :checked="notificationsEnabled" @change="onNotificationChange" />
  7. </view>
  8. <view class="template-info">
  9. <text class="tmpl-title">每日健康上报提醒</text>
  10. <text class="tmpl-meta">模板编号:7536 · 模板ID:ACS7cwcbx0F0Y_YaB4GZr7rWP7BO2-7wQOtYsnUjmFI</text>
  11. </view>
  12. <view class="reminder-list">
  13. <view class="reminder-item" v-for="(reminder, index) in reminders" :key="index">
  14. <view class="reminder-info">
  15. <text class="reminder-title">{{ reminder.title }}</text>
  16. <text class="reminder-time">{{ reminder.time }}</text>
  17. </view>
  18. <switch :checked="reminder.enabled" @change="toggleReminder(index)" />
  19. </view>
  20. </view>
  21. </view>
  22. <TabBar />
  23. </template>
  24. <script setup lang="ts">
  25. import { ref, onMounted, computed } from 'vue'
  26. import CustomNav from '@/components/custom-nav.vue'
  27. import TabBar from '@/components/tab-bar.vue'
  28. interface Reminder {
  29. title: string
  30. time: string
  31. enabled: boolean
  32. }
  33. const reminders = ref<Reminder[]>([
  34. { title: '喝水提醒', time: '08:00', enabled: true },
  35. { title: '运动提醒', time: '18:00', enabled: false },
  36. { title: '测量血压', time: '07:00', enabled: true },
  37. { title: '测量血糖', time: '12:00', enabled: false },
  38. { title: '服药提醒', time: '09:00', enabled: true }
  39. ])
  40. // 模板 ID & 其他展示信息
  41. const TEMPLATE_ID = 'ACS7cwcbx0F0Y_YaB4GZr7rWP7BO2-7wQOtYsnUjmFI'
  42. const TEMPLATE_NO = '7536'
  43. // 全局消息开关
  44. const notificationsEnabled = ref<boolean>(false)
  45. // 记录页面进入来源:'healthIndex' | 'subscribe' | 'unknown'
  46. const entrySource = ref<'healthIndex' | 'subscribe' | 'unknown'>('unknown')
  47. const entrySourceText = computed(() => {
  48. switch (entrySource.value) {
  49. case 'healthIndex':
  50. return '来自 健康首页'
  51. case 'subscribe':
  52. return '来自 订阅消息'
  53. default:
  54. return '来源 未知'
  55. }
  56. })
  57. onMounted(() => {
  58. try {
  59. const val = (uni as any).getStorageSync('notificationsEnabled')
  60. if (typeof val === 'boolean') notificationsEnabled.value = val
  61. } catch (e) {
  62. // 忽略错误
  63. }
  64. // 检查用户订阅状态
  65. checkSubscriptionStatus()
  66. // 尝试判断来源:优先检查页面栈的上一个页面;若无(直接打开),则检查 launch options 的 query
  67. try {
  68. const pages = (getCurrentPages as any)()
  69. console.log('当前页面栈:', pages)
  70. if (pages && pages.length >= 1) {
  71. const currentPage = pages[pages.length - 1]
  72. const currentRoute = currentPage?.route || currentPage?.__route || currentPage?.$page?.route
  73. const currentOptions = currentPage?.options || {}
  74. console.log('当前页面路由:', currentRoute)
  75. console.log('当前页面参数:', currentOptions)
  76. // 检查当前页面是否包含 from=subscribe 参数
  77. if (currentOptions.from === 'subscribe') {
  78. entrySource.value = 'subscribe'
  79. console.log('通过当前页面参数识别为订阅消息来源')
  80. }
  81. // 检查当前页面是否包含 templateId 相关参数
  82. if (currentOptions.templateId === TEMPLATE_ID || currentOptions.template_id === TEMPLATE_ID) {
  83. entrySource.value = 'subscribe'
  84. console.log('通过当前页面的模板ID参数识别为订阅消息来源')
  85. }
  86. }
  87. if (pages && pages.length >= 2) {
  88. const prev = pages[pages.length - 2]
  89. const prevRoute = prev?.route || prev?.__route || prev?.$page?.route
  90. console.log('上一页路由:', prevRoute)
  91. if (prevRoute && String(prevRoute).includes('pages/health/index')) {
  92. entrySource.value = 'healthIndex'
  93. console.log('识别来源为健康首页')
  94. }
  95. } else {
  96. // 如果没有上一页信息,检查小程序启动参数(例如用户通过订阅消息打开)
  97. if ((uni as any).getLaunchOptionsSync) {
  98. const launch = (uni as any).getLaunchOptionsSync()
  99. console.log('启动参数:', launch)
  100. if (launch && launch.query) {
  101. console.log('查询参数:', launch.query)
  102. // 如果你在推送消息里把 from=subscribe 作为参数传入,这里会命中
  103. if (launch.query.from === 'subscribe') {
  104. entrySource.value = 'subscribe'
  105. console.log('通过 from 参数识别为订阅消息来源')
  106. }
  107. // 有些平台会把 templateId 或其它字段放在 query 中,可据此判断
  108. if (launch.query.templateId === TEMPLATE_ID || launch.query.template_id === TEMPLATE_ID) {
  109. entrySource.value = 'subscribe'
  110. console.log('通过模板ID参数识别为订阅消息来源')
  111. }
  112. }
  113. }
  114. }
  115. } catch (e) {
  116. console.error('识别入口来源时出错:', e)
  117. }
  118. // 显示来源提示(用于测试),延迟一点以保证 UI 已就绪
  119. try {
  120. setTimeout(() => {
  121. console.log('入口来源:', entrySource.value, entrySourceText.value)
  122. }, 250)
  123. } catch (e) {
  124. // 忽略错误
  125. }
  126. if (entrySource.value === 'unknown') {
  127. console.log('入口来源默认为未知')
  128. }
  129. })
  130. /**
  131. * 检查用户订阅状态
  132. */
  133. const checkSubscriptionStatus = () => {
  134. // 使用 uni.getSetting 检查用户授权状态
  135. if (typeof (uni as any).getSetting === 'function') {
  136. (uni as any).getSetting({
  137. withSubscriptions: true, // 启用订阅设置查询
  138. success: (res: any) => {
  139. console.log('用户设置:', res)
  140. // 检查订阅消息设置
  141. if (res.subscriptionsSetting) {
  142. console.log('订阅设置详情:', JSON.stringify(res.subscriptionsSetting, null, 2))
  143. // 检查主开关
  144. const mainSwitch = res.subscriptionsSetting.mainSwitch
  145. console.log('订阅主开关:', mainSwitch)
  146. // 检查针对特定模板的设置
  147. const itemSettings = res.subscriptionsSetting.itemSettings || {}
  148. const templateStatus = itemSettings[TEMPLATE_ID]
  149. console.log(`模板 ${TEMPLATE_ID} 状态:`, templateStatus)
  150. // 如果主开关关闭或特定模板被拒绝,则更新本地状态
  151. if (mainSwitch === false || templateStatus === 'reject' || templateStatus === 'ban') {
  152. console.log('用户关闭了订阅设置,正在更新本地状态')
  153. notificationsEnabled.value = false
  154. try {
  155. (uni as any).setStorageSync('notificationsEnabled', false)
  156. } catch (e) {
  157. console.error('更新存储失败:', e)
  158. }
  159. }
  160. // 如果模板被接受且主开关开启,更新本地状态为true
  161. else if (mainSwitch === true && templateStatus === 'accept') {
  162. console.log('用户开启了订阅设置,正在更新本地状态')
  163. notificationsEnabled.value = true
  164. try {
  165. (uni as any).setStorageSync('notificationsEnabled', true)
  166. } catch (e) {
  167. console.error('更新存储失败:', e)
  168. }
  169. }
  170. } else {
  171. console.log('响应中未找到订阅设置')
  172. }
  173. },
  174. fail: (err: any) => {
  175. console.error('获取用户设置失败:', err)
  176. }
  177. })
  178. } else {
  179. console.log('当前环境不支持 uni.getSetting')
  180. }
  181. }
  182. /**
  183. * 顶部总开关变更
  184. * - 打开时:调用 uni.requestSubscribeMessage 请求用户同意订阅 TEMPLATE_ID
  185. * - 如果用户同意(返回 accept),将状态置为 true 并持久化
  186. * - 否则回滚并提示
  187. */
  188. const onNotificationChange = (e: any) => {
  189. const newVal = e?.detail?.value
  190. console.log('通知开关更改为:', newVal)
  191. if (newVal) {
  192. // 先将开关设置为开启状态
  193. notificationsEnabled.value = true
  194. // 请求订阅(仅在微信/小程序有效)
  195. ;(uni as any).requestSubscribeMessage({
  196. tmplIds: [TEMPLATE_ID],
  197. success(res: any) {
  198. console.log('订阅消息请求成功结果:', res)
  199. // res 可能形如 { "ACS7...": 'accept' }
  200. const result = res && res[TEMPLATE_ID]
  201. if (result === 'accept') {
  202. console.log('用户接受了订阅')
  203. try {
  204. ;(uni as any).setStorageSync('notificationsEnabled', true)
  205. } catch (err) {
  206. // 忽略存储错误
  207. }
  208. uni.showToast({ title: '订阅成功', icon: 'success' })
  209. } else {
  210. console.log('用户未接受订阅,结果:', result)
  211. // 用户拒绝或关闭了弹窗
  212. notificationsEnabled.value = false
  213. try {
  214. ;(uni as any).setStorageSync('notificationsEnabled', false)
  215. } catch (err) {}
  216. uni.showToast({ title: '订阅被拒绝', icon: 'none' })
  217. }
  218. },
  219. fail(err: any) {
  220. console.log('订阅消息请求失败:', err)
  221. notificationsEnabled.value = false
  222. try {
  223. ;(uni as any).setStorageSync('notificationsEnabled', false)
  224. } catch (e) {}
  225. // 根据错误类型显示不同的提示信息
  226. if (err.errCode === 20004) {
  227. uni.showToast({ title: '推送权限已关闭', icon: 'none' })
  228. } else {
  229. uni.showToast({ title: '订阅请求失败', icon: 'none' })
  230. }
  231. }
  232. })
  233. } else {
  234. // 关闭订阅:不需要额外调用接口,只改变本地记录
  235. console.log('通知开关已关闭')
  236. notificationsEnabled.value = false
  237. try {
  238. ;(uni as any).setStorageSync('notificationsEnabled', false)
  239. } catch (err) {}
  240. uni.showToast({ title: '已关闭通知', icon: 'none' })
  241. }
  242. }
  243. const toggleReminder = (index: number) => {
  244. reminders.value[index].enabled = !reminders.value[index].enabled
  245. }
  246. </script>
  247. <style scoped>
  248. .content {
  249. padding: calc(var(--status-bar-height) + 44px) 50rpx 50rpx 50rpx;
  250. min-height: 100vh;
  251. background-color: #f5f5f5;
  252. box-sizing: border-box;
  253. }
  254. .reminder-list {
  255. background-color: #fff;
  256. border-radius: 12rpx;
  257. overflow: hidden;
  258. margin-top: 50rpx;
  259. }
  260. .reminder-item {
  261. display: flex;
  262. justify-content: space-between;
  263. align-items: center;
  264. padding: 30rpx 40rpx;
  265. border-bottom: 1rpx solid #eee;
  266. }
  267. .reminder-item:last-child {
  268. border-bottom: none;
  269. }
  270. .reminder-info {
  271. flex: 1;
  272. }
  273. .reminder-title {
  274. font-size: 32rpx;
  275. color: #000000;
  276. display: block;
  277. margin-bottom: 10rpx;
  278. }
  279. .reminder-time {
  280. font-size: 28rpx;
  281. color: #5a5a5a;
  282. }
  283. .notification-toggle {
  284. display: flex;
  285. justify-content: space-between;
  286. align-items: center;
  287. padding: 30rpx 40rpx;
  288. background-color: #fff;
  289. border-radius: 12rpx;
  290. margin-top: 20rpx;
  291. }
  292. .notification-label {
  293. font-size: 32rpx;
  294. color: #000;
  295. }
  296. .template-info {
  297. margin-top: 12rpx;
  298. padding: 0 10rpx;
  299. color: #666;
  300. font-size: 24rpx;
  301. }
  302. .tmpl-title {
  303. display: block;
  304. font-size: 26rpx;
  305. color: #333;
  306. }
  307. .tmpl-meta {
  308. display: block;
  309. font-size: 22rpx;
  310. color: #8a8a8a;
  311. margin-top: 6rpx;
  312. }
  313. </style>