feat(mp): 微信模板消息订阅统一封装
- 新增 requestSubscribe() 统一订阅函数,消除页面内类型断言重复
- 用药页面新增 requestSubscribeMessage 订阅(MEDICATION_REMINDER 模板)
- 告警页面改用 requestSubscribe('CRITICAL_HEALTH_ALERT')
- wechat-templates 新增 MEDICATION_REMINDER 模板 ID 环境变量
This commit is contained in:
@@ -4,6 +4,7 @@ import Taro from '@tarojs/taro';
|
|||||||
import { safeNavigateTo } from '@/utils/navigate';
|
import { safeNavigateTo } from '@/utils/navigate';
|
||||||
import { usePageData } from '@/hooks/usePageData';
|
import { usePageData } from '@/hooks/usePageData';
|
||||||
import { listPatientAlerts, type Alert } from '@/services/alert';
|
import { listPatientAlerts, type Alert } from '@/services/alert';
|
||||||
|
import { requestSubscribe } from '@/services/wechat-templates';
|
||||||
import { useAuthStore } from '@/stores/auth';
|
import { useAuthStore } from '@/stores/auth';
|
||||||
import Loading from '@/components/Loading';
|
import Loading from '@/components/Loading';
|
||||||
import ErrorState from '@/components/ErrorState';
|
import ErrorState from '@/components/ErrorState';
|
||||||
@@ -73,14 +74,7 @@ export default function PatientAlerts() {
|
|||||||
Taro.setNavigationBarTitle({ title: '健康告警' });
|
Taro.setNavigationBarTitle({ title: '健康告警' });
|
||||||
await fetchAlerts(1, status, true);
|
await fetchAlerts(1, status, true);
|
||||||
// 请求 critical 告警推送订阅
|
// 请求 critical 告警推送订阅
|
||||||
try {
|
requestSubscribe('CRITICAL_HEALTH_ALERT');
|
||||||
const tmplId = process.env.TARO_APP_WX_TEMPLATE_CRITICAL_ALERT || '';
|
|
||||||
if (tmplId) {
|
|
||||||
await (Taro.requestSubscribeMessage as (option: { tmplIds: string[] }) => Promise<unknown>)({ tmplIds: [tmplId] });
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// 用户拒绝或已订阅,不阻塞页面
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
{ throttleMs: 10000, enablePullDown: true },
|
{ throttleMs: 10000, enablePullDown: true },
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { View, Text, Input, Picker } from '@tarojs/components';
|
|||||||
import Taro from '@tarojs/taro';
|
import Taro from '@tarojs/taro';
|
||||||
import { usePageData } from '@/hooks/usePageData';
|
import { usePageData } from '@/hooks/usePageData';
|
||||||
import { getCachedPatientId } from '@/services/request';
|
import { getCachedPatientId } from '@/services/request';
|
||||||
|
import { requestSubscribe } from '@/services/wechat-templates';
|
||||||
import EmptyState from '../../../components/EmptyState';
|
import EmptyState from '../../../components/EmptyState';
|
||||||
import {
|
import {
|
||||||
listReminders,
|
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) => {
|
const handleToggle = async (r: MedicationReminder) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import Taro from '@tarojs/taro';
|
||||||
|
|
||||||
// 微信订阅消息模板 ID — 通过环境变量注入
|
// 微信订阅消息模板 ID — 通过环境变量注入
|
||||||
// 注册路径:公众平台 → 功能 → 订阅消息 → 添加模板
|
// 注册路径:公众平台 → 功能 → 订阅消息 → 添加模板
|
||||||
// 环境变量:TARO_APP_WX_TEMPLATE_APPOINTMENT / FOLLOWUP / REPORT / CRITICAL_ALERT / HEALTH_ABNORMAL
|
// 环境变量: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 || '',
|
REPORT_NOTIFICATION: process.env.TARO_APP_WX_TEMPLATE_REPORT || '',
|
||||||
CRITICAL_HEALTH_ALERT: process.env.TARO_APP_WX_TEMPLATE_CRITICAL_ALERT || '',
|
CRITICAL_HEALTH_ALERT: process.env.TARO_APP_WX_TEMPLATE_CRITICAL_ALERT || '',
|
||||||
HEALTH_DATA_ABNORMAL: process.env.TARO_APP_WX_TEMPLATE_HEALTH_ABNORMAL || '',
|
HEALTH_DATA_ABNORMAL: process.env.TARO_APP_WX_TEMPLATE_HEALTH_ABNORMAL || '',
|
||||||
|
MEDICATION_REMINDER: process.env.TARO_APP_WX_TEMPLATE_MEDICATION || '',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
/** 检查模板 ID 是否已配置,未配置时返回 false 并打印警告 */
|
/** 检查模板 ID 是否已配置,未配置时返回 false 并打印警告 */
|
||||||
@@ -17,3 +20,15 @@ export function isTemplateConfigured(key: keyof typeof TEMPLATE_IDS): boolean {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 请求订阅消息授权,模板未配置时静默跳过 */
|
||||||
|
export async function requestSubscribe(...keys: (keyof typeof TEMPLATE_IDS)[]): Promise<void> {
|
||||||
|
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<unknown> })
|
||||||
|
.requestSubscribeMessage({ tmplIds });
|
||||||
|
} catch {
|
||||||
|
// 用户拒绝或已订阅,不阻塞页面
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user