From 22e33114b1d7e625d443bca55cbc39d0fc47e5b9 Mon Sep 17 00:00:00 2001 From: iven Date: Fri, 22 May 2026 12:08:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(mp):=20=E5=BE=AE=E4=BF=A1=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E6=B6=88=E6=81=AF=E8=AE=A2=E9=98=85=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 requestSubscribe() 统一订阅函数,消除页面内类型断言重复 - 用药页面新增 requestSubscribeMessage 订阅(MEDICATION_REMINDER 模板) - 告警页面改用 requestSubscribe('CRITICAL_HEALTH_ALERT') - wechat-templates 新增 MEDICATION_REMINDER 模板 ID 环境变量 --- .../src/pages/pkg-health/alerts/index.tsx | 10 ++-------- .../src/pages/pkg-profile/medication/index.tsx | 10 +++++++++- apps/miniprogram/src/services/wechat-templates.ts | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/miniprogram/src/pages/pkg-health/alerts/index.tsx b/apps/miniprogram/src/pages/pkg-health/alerts/index.tsx index a180bba..19ea053 100644 --- a/apps/miniprogram/src/pages/pkg-health/alerts/index.tsx +++ b/apps/miniprogram/src/pages/pkg-health/alerts/index.tsx @@ -4,6 +4,7 @@ import Taro from '@tarojs/taro'; import { safeNavigateTo } from '@/utils/navigate'; import { usePageData } from '@/hooks/usePageData'; import { listPatientAlerts, type Alert } from '@/services/alert'; +import { requestSubscribe } from '@/services/wechat-templates'; import { useAuthStore } from '@/stores/auth'; import Loading from '@/components/Loading'; import ErrorState from '@/components/ErrorState'; @@ -73,14 +74,7 @@ export default function PatientAlerts() { Taro.setNavigationBarTitle({ title: '健康告警' }); await fetchAlerts(1, status, true); // 请求 critical 告警推送订阅 - try { - const tmplId = process.env.TARO_APP_WX_TEMPLATE_CRITICAL_ALERT || ''; - if (tmplId) { - await (Taro.requestSubscribeMessage as (option: { tmplIds: string[] }) => Promise)({ tmplIds: [tmplId] }); - } - } catch { - // 用户拒绝或已订阅,不阻塞页面 - } + requestSubscribe('CRITICAL_HEALTH_ALERT'); }, { throttleMs: 10000, enablePullDown: true }, ); diff --git a/apps/miniprogram/src/pages/pkg-profile/medication/index.tsx b/apps/miniprogram/src/pages/pkg-profile/medication/index.tsx index fdd240f..c1afd06 100644 --- a/apps/miniprogram/src/pages/pkg-profile/medication/index.tsx +++ b/apps/miniprogram/src/pages/pkg-profile/medication/index.tsx @@ -3,6 +3,7 @@ import { View, Text, Input, Picker } from '@tarojs/components'; import Taro from '@tarojs/taro'; import { usePageData } from '@/hooks/usePageData'; import { getCachedPatientId } from '@/services/request'; +import { requestSubscribe } from '@/services/wechat-templates'; import EmptyState from '../../../components/EmptyState'; import { listReminders, @@ -36,7 +37,14 @@ export default function MedicationReminder() { } }, []); - usePageData(fetchReminders, { throttleMs: 5000, enablePullDown: true }); + usePageData( + async () => { + await fetchReminders(); + // 请求用药提醒推送订阅 + requestSubscribe('MEDICATION_REMINDER'); + }, + { throttleMs: 5000, enablePullDown: true }, + ); const handleToggle = async (r: MedicationReminder) => { try { diff --git a/apps/miniprogram/src/services/wechat-templates.ts b/apps/miniprogram/src/services/wechat-templates.ts index bd444bc..100cb9d 100644 --- a/apps/miniprogram/src/services/wechat-templates.ts +++ b/apps/miniprogram/src/services/wechat-templates.ts @@ -1,3 +1,5 @@ +import Taro from '@tarojs/taro'; + // 微信订阅消息模板 ID — 通过环境变量注入 // 注册路径:公众平台 → 功能 → 订阅消息 → 添加模板 // 环境变量:TARO_APP_WX_TEMPLATE_APPOINTMENT / FOLLOWUP / REPORT / CRITICAL_ALERT / HEALTH_ABNORMAL @@ -7,6 +9,7 @@ export const TEMPLATE_IDS = { REPORT_NOTIFICATION: process.env.TARO_APP_WX_TEMPLATE_REPORT || '', CRITICAL_HEALTH_ALERT: process.env.TARO_APP_WX_TEMPLATE_CRITICAL_ALERT || '', HEALTH_DATA_ABNORMAL: process.env.TARO_APP_WX_TEMPLATE_HEALTH_ABNORMAL || '', + MEDICATION_REMINDER: process.env.TARO_APP_WX_TEMPLATE_MEDICATION || '', } as const; /** 检查模板 ID 是否已配置,未配置时返回 false 并打印警告 */ @@ -17,3 +20,15 @@ export function isTemplateConfigured(key: keyof typeof TEMPLATE_IDS): boolean { } return true; } + +/** 请求订阅消息授权,模板未配置时静默跳过 */ +export async function requestSubscribe(...keys: (keyof typeof TEMPLATE_IDS)[]): Promise { + const tmplIds = keys.map((k) => TEMPLATE_IDS[k]).filter(Boolean); + if (tmplIds.length === 0) return; + try { + await (Taro as unknown as { requestSubscribeMessage: (o: { tmplIds: string[] }) => Promise }) + .requestSubscribeMessage({ tmplIds }); + } catch { + // 用户拒绝或已订阅,不阻塞页面 + } +}