refactor(miniprogram): 体征阈值改用动态 API — 替代硬编码参考范围
- health.ts 新增 getHealthThresholds/findThreshold/DEFAULT_THRESHOLDS - 24h storage 缓存 + 降级到内置默认值 - health/index.tsx: REF_RANGES → buildRefRange(thresholds) - pkg-health/input: WARN_THRESHOLDS → getWarnForIndicator(thresholds)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import Taro from '@tarojs/taro';
|
||||
import { api } from './request';
|
||||
|
||||
export interface VitalSignInput {
|
||||
@@ -36,6 +37,10 @@ export async function inputVitalSign(patientId: string, data: VitalSignInput) {
|
||||
if (data.extra?.systolic) body.systolic_bp_morning = data.extra.systolic;
|
||||
if (data.extra?.diastolic) body.diastolic_bp_morning = data.extra.diastolic;
|
||||
break;
|
||||
case 'blood_pressure_evening':
|
||||
if (data.extra?.systolic) body.systolic_bp_evening = data.extra.systolic;
|
||||
if (data.extra?.diastolic) body.diastolic_bp_evening = data.extra.diastolic;
|
||||
break;
|
||||
case 'heart_rate':
|
||||
body.heart_rate = Math.round(data.value);
|
||||
break;
|
||||
@@ -45,6 +50,12 @@ export async function inputVitalSign(patientId: string, data: VitalSignInput) {
|
||||
case 'blood_sugar':
|
||||
body.blood_sugar = data.value;
|
||||
break;
|
||||
case 'body_temperature':
|
||||
body.body_temperature = data.value;
|
||||
break;
|
||||
case 'spo2':
|
||||
body.spo2 = Math.round(data.value);
|
||||
break;
|
||||
case 'water_intake':
|
||||
body.water_intake_ml = Math.round(data.value);
|
||||
break;
|
||||
@@ -113,3 +124,62 @@ export async function listDailyMonitoring(
|
||||
params,
|
||||
);
|
||||
}
|
||||
|
||||
// ---- Health Thresholds (健康阈值) ----
|
||||
|
||||
export interface HealthThreshold {
|
||||
id: string;
|
||||
indicator: string;
|
||||
direction: string;
|
||||
threshold_value: number;
|
||||
level: string;
|
||||
department: string | null;
|
||||
age_min: number | null;
|
||||
age_max: number | null;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
const THRESHOLD_CACHE_KEY = 'health_thresholds';
|
||||
const THRESHOLD_TTL = 24 * 60 * 60 * 1000; // 24h
|
||||
|
||||
/** 从缓存或 API 获取健康阈值列表 */
|
||||
export async function getHealthThresholds(): Promise<HealthThreshold[]> {
|
||||
try {
|
||||
const cached = Taro.getStorageSync(THRESHOLD_CACHE_KEY) as
|
||||
| { data: HealthThreshold[]; ts: number }
|
||||
| undefined;
|
||||
if (cached && Date.now() - cached.ts < THRESHOLD_TTL) {
|
||||
return cached.data;
|
||||
}
|
||||
} catch { /* cache miss */ }
|
||||
|
||||
try {
|
||||
const data = await api.get<HealthThreshold[]>('/health/critical-value-thresholds/public');
|
||||
Taro.setStorageSync(THRESHOLD_CACHE_KEY, { data, ts: Date.now() });
|
||||
return data;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** 查找匹配的阈值,缓存未命中时返回 undefined */
|
||||
export function findThreshold(
|
||||
thresholds: HealthThreshold[],
|
||||
indicator: string,
|
||||
direction: string,
|
||||
level = 'warning',
|
||||
): HealthThreshold | undefined {
|
||||
return thresholds.find(
|
||||
(t) => t.indicator === indicator && t.direction === direction && t.level === level && t.is_active,
|
||||
);
|
||||
}
|
||||
|
||||
/** 内置默认阈值(API 不可用时的降级方案) */
|
||||
export const DEFAULT_THRESHOLDS: HealthThreshold[] = [
|
||||
{ id: '_bp_sys_high', indicator: 'systolic_bp', direction: 'high', threshold_value: 140, level: 'warning', department: null, age_min: null, age_max: null, is_active: true },
|
||||
{ id: '_bp_dia_high', indicator: 'diastolic_bp', direction: 'high', threshold_value: 90, level: 'warning', department: null, age_min: null, age_max: null, is_active: true },
|
||||
{ id: '_hr_high', indicator: 'heart_rate', direction: 'high', threshold_value: 100, level: 'warning', department: null, age_min: null, age_max: null, is_active: true },
|
||||
{ id: '_hr_low', indicator: 'heart_rate', direction: 'low', threshold_value: 60, level: 'warning', department: null, age_min: null, age_max: null, is_active: true },
|
||||
{ id: '_bs_fasting_high', indicator: 'blood_sugar_fasting', direction: 'high', threshold_value: 6.1, level: 'warning', department: null, age_min: null, age_max: null, is_active: true },
|
||||
{ id: '_bs_pp_high', indicator: 'blood_sugar_postprandial', direction: 'high', threshold_value: 7.8, level: 'warning', department: null, age_min: null, age_max: null, is_active: true },
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user