feat(mp): App 级告警长轮询 + 健康总览 TS 修复
- 新增 useAlertPolling hook:10s 间隔轮询 critical 告警 - requestUnlimited 独立通道,不占并发槽位 - generation counter 防重叠 + 失败指数退避(max 30s/10次) - 新告警弹窗 Taro.showModal + TabBar 角标 - 修复 HealthThreshold 属性名(indicator/level 非 indicator_name/severity) - 修复 usePageData fetchData 返回类型
This commit is contained in:
123
apps/miniprogram/src/hooks/useAlertPolling.ts
Normal file
123
apps/miniprogram/src/hooks/useAlertPolling.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import { useRef, useCallback } from 'react';
|
||||
import Taro from '@tarojs/taro';
|
||||
import { useDidShow, useDidHide } from '@tarojs/taro';
|
||||
import { requestUnlimited } from '@/services/request';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
import type { Alert } from '@/services/alert';
|
||||
|
||||
interface PollState {
|
||||
generation: number;
|
||||
timer: ReturnType<typeof setTimeout> | null;
|
||||
failCount: number;
|
||||
lastAlertCount: number;
|
||||
}
|
||||
|
||||
const POLL_INTERVAL = 10_000;
|
||||
const MAX_FAILURES = 10;
|
||||
const FAIL_BACKOFF_BASE = 2000;
|
||||
const FAIL_BACKOFF_MAX = 30_000;
|
||||
|
||||
/**
|
||||
* App 级告警长轮询。
|
||||
*
|
||||
* - 登录态 + 有 patientId 时启动
|
||||
* - 使用 requestUnlimited 走独立通道,不占并发槽位
|
||||
* - generation counter 防止重叠
|
||||
* - useDidShow/Hide 控制前后台
|
||||
* - critical 告警弹窗提醒 + TabBar 角标
|
||||
*/
|
||||
export function useAlertPolling() {
|
||||
const stateRef = useRef<PollState>({
|
||||
generation: 0,
|
||||
timer: null,
|
||||
failCount: 0,
|
||||
lastAlertCount: 0,
|
||||
});
|
||||
|
||||
const patientId = useAuthStore((s) => s.currentPatient?.id);
|
||||
const isLoggedIn = useAuthStore((s) => !!s.user);
|
||||
const enabled = isLoggedIn && !!patientId;
|
||||
|
||||
const poll = useCallback(async (gen: number, failCount: number) => {
|
||||
const s = stateRef.current;
|
||||
if (gen !== s.generation) return;
|
||||
if (failCount >= MAX_FAILURES) return;
|
||||
|
||||
try {
|
||||
const pid = useAuthStore.getState().currentPatient?.id;
|
||||
if (!pid) return;
|
||||
|
||||
const res = await requestUnlimited<{ data: Alert[]; total: number }>(
|
||||
'GET',
|
||||
`/health/alerts?patient_id=${pid}&status=pending&severity=critical&page=1&page_size=5`,
|
||||
undefined,
|
||||
8000,
|
||||
);
|
||||
|
||||
if (gen !== s.generation) return;
|
||||
|
||||
const count = res.total ?? 0;
|
||||
|
||||
// TabBar 角标
|
||||
try {
|
||||
if (count > 0) {
|
||||
Taro.setTabBarBadge({ index: 2, text: String(count) });
|
||||
} else {
|
||||
Taro.removeTabBarBadge({ index: 2 });
|
||||
}
|
||||
} catch { /* TabBar 可能不存在 */ }
|
||||
|
||||
// 告警数量增加时弹窗提醒
|
||||
if (count > s.lastAlertCount) {
|
||||
Taro.showModal({
|
||||
title: '健康告警',
|
||||
content: `您有 ${count} 条待处理的危急值告警,请及时关注`,
|
||||
showCancel: false,
|
||||
confirmText: '知道了',
|
||||
});
|
||||
}
|
||||
s.lastAlertCount = count;
|
||||
|
||||
failCount = 0;
|
||||
} catch {
|
||||
failCount++;
|
||||
}
|
||||
|
||||
if (gen !== s.generation) return;
|
||||
const delay = failCount > 0 ? Math.min(failCount * FAIL_BACKOFF_BASE, FAIL_BACKOFF_MAX) : POLL_INTERVAL;
|
||||
s.timer = setTimeout(() => {
|
||||
if (gen === s.generation) poll(gen, failCount);
|
||||
}, delay);
|
||||
}, []);
|
||||
|
||||
const start = useCallback(() => {
|
||||
const s = stateRef.current;
|
||||
s.generation++;
|
||||
s.failCount = 0;
|
||||
if (s.timer) { clearTimeout(s.timer); s.timer = null; }
|
||||
poll(s.generation, 0);
|
||||
}, [poll]);
|
||||
|
||||
const stop = useCallback(() => {
|
||||
const s = stateRef.current;
|
||||
s.generation++;
|
||||
if (s.timer) { clearTimeout(s.timer); s.timer = null; }
|
||||
try { Taro.removeTabBarBadge({ index: 2 }); } catch { /* ignore */ }
|
||||
}, []);
|
||||
|
||||
useDidShow(() => {
|
||||
if (enabled) start();
|
||||
});
|
||||
|
||||
useDidHide(() => {
|
||||
stop();
|
||||
});
|
||||
|
||||
// enabled 变化时启停
|
||||
const prevEnabledRef = useRef(enabled);
|
||||
if (enabled !== prevEnabledRef.current) {
|
||||
prevEnabledRef.current = enabled;
|
||||
if (enabled) start();
|
||||
else stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user