refactor(mp): 架构重构 — usePageData 统一数据加载 + Store 解耦 + 大页面拆分

新增 usePageData hook(useDidShow 节流 + usePullDownRefresh + loadingRef 防重入 + enabled 条件守卫),
44/58 页面迁移接入,消灭 4 种数据加载模式并存。

- 新增 hooks/usePageData.ts — 统一页面数据加载生命周期
- 新增 stores/index.ts — resetAllStores() 解耦 auth↔health store 依赖
- 新增 pages/index/useHomeData.ts — 首页数据 hook(424→282 行)
- 新增 pages/health/useHealthData.ts — 健康页数据 hook(422→254 行)
- 44 个页面迁移到 usePageData(9 患者端 + 15 医生端 + 20 子包)
- auth store logout 不再直接导入 health store

构建通过,测试 74/75(1 个预存失败)。
This commit is contained in:
iven
2026-05-15 01:13:01 +08:00
parent 0f58af245d
commit 1fd2c7a533
52 changed files with 791 additions and 664 deletions

View File

@@ -1,7 +1,7 @@
import React, { useState, useCallback, useRef } from 'react';
import React, { useState, useCallback } from 'react';
import { View, Text } from '@tarojs/components';
import Taro, { usePullDownRefresh } from '@tarojs/taro';
import { useThrottledDidShow } from '@/hooks/useThrottledDidShow';
import Taro from '@tarojs/taro';
import { usePageData } from '@/hooks/usePageData';
import { listPatientAlerts, type Alert } from '@/services/alert';
import { useAuthStore } from '@/stores/auth';
import Loading from '@/components/Loading';
@@ -30,12 +30,10 @@ export default function PatientAlerts() {
const [page, setPage] = useState(1);
const [status, setStatus] = useState('');
const [loading, setLoading] = useState(false);
const loadingRef = useRef(false);
const fetchAlerts = useCallback(
async (pageNum: number, s: string, isRefresh = false) => {
if (!currentPatient || loadingRef.current) return;
loadingRef.current = true;
if (!currentPatient) return;
setLoading(true);
try {
const res = await listPatientAlerts(currentPatient.id, {
@@ -54,21 +52,19 @@ export default function PatientAlerts() {
} catch {
Taro.showToast({ title: '加载失败', icon: 'none' });
} finally {
loadingRef.current = false;
setLoading(false);
}
},
[currentPatient],
);
useThrottledDidShow(() => {
Taro.setNavigationBarTitle({ title: '健康告警' });
fetchAlerts(1, status, true);
}, 10000);
usePullDownRefresh(() => {
fetchAlerts(1, status, true).finally(() => Taro.stopPullDownRefresh());
});
usePageData(
async () => {
Taro.setNavigationBarTitle({ title: '健康告警' });
await fetchAlerts(1, status, true);
},
{ throttleMs: 10000, enablePullDown: true },
);
const handleTabChange = (key: string) => {
setStatus(key);

View File

@@ -1,7 +1,7 @@
import { useState } from 'react';
import { useState, useCallback } from 'react';
import { View, Text, Input, Picker } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useThrottledDidShow } from '@/hooks/useThrottledDidShow';
import { usePageData } from '@/hooks/usePageData';
import { num, validateStr } from '@/utils/validate';
import { inputVitalSign, getHealthThresholds, findThreshold, DEFAULT_THRESHOLDS, type HealthThreshold } from '../../../services/health';
import { useAuthStore } from '../../../stores/auth';
@@ -68,9 +68,14 @@ export default function HealthInput() {
const clearCache = useHealthStore((s) => s.clearCache);
/** 从 storage 中读取设备同步回传的数据并自动填充表单 */
useThrottledDidShow(() => {
const loadThresholdsAndSync = useCallback(async () => {
setLoadingThresholds(true);
getHealthThresholds().then((t) => { if (t.length > 0) setThresholds(t); }).finally(() => setLoadingThresholds(false));
try {
const t = await getHealthThresholds();
if (t.length > 0) setThresholds(t);
} finally {
setLoadingThresholds(false);
}
try {
const raw = Taro.getStorageSync('device_sync_result');
if (!raw) return;
@@ -94,7 +99,9 @@ export default function HealthInput() {
} catch {
// 解析失败则忽略,不影响正常使用
}
}, 10000);
}, []);
usePageData(loadThresholdsAndSync, { throttleMs: 10000 });
const handleSubmit = async () => {
if (!currentPatient) {

View File

@@ -1,6 +1,7 @@
import { useState, useEffect } from 'react';
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';
@@ -33,13 +34,24 @@ export default function Trend() {
const [loading, setLoading] = useState(true);
const getTrend = useHealthStore((s) => s.getTrend);
useEffect(() => {
const fetchTrend = useCallback(async () => {
setLoading(true);
getTrend(indicator, range)
.then(setPoints)
.catch(() => setPoints([]))
.finally(() => setLoading(false));
}, [indicator, range]);
try {
const data = await getTrend(indicator, range);
setPoints(data);
} catch {
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: '' };