import { useState, useEffect, useCallback } from 'react'; import { View, Text } from '@tarojs/components'; import { useRouter } from '@tarojs/taro'; import { usePageData } from '@/hooks/usePageData'; import { useHealthStore } from '@/stores/health'; import TrendChart from '@/components/TrendChart'; import Loading from '@/components/Loading'; import ErrorState from '@/components/ErrorState'; import EmptyState from '@/components/EmptyState'; import SegmentTabs from '@/components/SegmentTabs'; import PageShell from '@/components/ui/PageShell'; import ContentCard from '@/components/ui/ContentCard'; import { useElderClass } from '../../../hooks/useElderClass'; import './index.scss'; const RANGE_OPTIONS = [ { value: '7d', label: '7天' }, { value: '30d', label: '30天' }, { value: '90d', label: '90天' }, ]; const INDICATOR_META: Record = { systolic_bp_morning: { label: '收缩压(晨)', unit: 'mmHg', refMin: 90, refMax: 140 }, diastolic_bp_morning: { label: '舒张压(晨)', unit: 'mmHg', refMin: 60, refMax: 90 }, systolic_bp_evening: { label: '收缩压(晚)', unit: 'mmHg', refMin: 90, refMax: 140 }, diastolic_bp_evening: { label: '舒张压(晚)', unit: 'mmHg', refMin: 60, refMax: 90 }, heart_rate: { label: '心率', unit: 'bpm', refMin: 60, refMax: 100 }, blood_sugar: { label: '血糖', unit: 'mmol/L', refMin: 3.9, refMax: 6.1 }, weight: { label: '体重', unit: 'kg' }, }; export default function Trend() { const modeClass = useElderClass(); const router = useRouter(); const indicator = router.params.indicator || 'heart_rate'; const [range, setRange] = useState('7d'); const [points, setPoints] = useState<{ date: string; value: number }[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(false); const getTrend = useHealthStore((s) => s.getTrend); const fetchTrend = useCallback(async () => { setLoading(true); setError(false); try { const data = await getTrend(indicator, range); setPoints(data); } catch { setError(true); setPoints([]); } finally { setLoading(false); } }, [getTrend, indicator, range]); usePageData(fetchTrend, { throttleMs: 60000, enablePullDown: true }); // range 切换时手动触发 useEffect(() => { fetchTrend(); }, [fetchTrend]); const meta = INDICATOR_META[indicator] || { label: indicator, unit: '' }; const isOutOfRange = (val: number) => { if (meta.refMin !== undefined && val < meta.refMin) return true; if (meta.refMax !== undefined && val > meta.refMax) return true; return false; }; return ( {/* 页面标题 */} T {meta.label}趋势 {/* 时间范围切换 */} ({ key: o.value, label: o.label }))} activeKey={range} onChange={setRange} variant="pill" /> {/* ECharts 折线图 */} {loading ? ( ) : error ? ( ) : points.length === 0 ? ( ) : ( )} {/* 参考区间 */} {!loading && points.length > 0 && meta.refMin !== undefined && meta.refMax !== undefined && ( 参考区间 {meta.refMin} ~ {meta.refMax} {meta.unit} )} {/* 数据列表 */} {points.length > 0 && ( 历史记录 {points.slice().reverse().map((p, i) => { const abnormal = isOutOfRange(p.value); return ( {p.date} {abnormal && ( 偏高 )} {p.value}{meta.unit ? ` ${meta.unit}` : ''} ); })} )} ); }