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,6 +1,7 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useCallback } from 'react';
import { View, Text, RichText } from '@tarojs/components';
import Taro, { useRouter, useShareAppMessage } from '@tarojs/taro';
import { usePageData } from '@/hooks/usePageData';
import { getArticleDetail, getPublicArticleDetail, Article } from '../../../services/article';
import { trackEvent } from '@/services/analytics';
import { sanitizeHtml } from '@/utils/sanitize-html';
@@ -24,17 +25,23 @@ export default function ArticleDetail() {
};
});
useEffect(() => {
const fetchArticle = useCallback(async () => {
if (!id) return;
setLoading(true);
const user = useAuthStore.getState().user;
const fetcher = user ? getArticleDetail(id) : getPublicArticleDetail(id);
fetcher
.then((data) => setArticle(data))
.catch(() => Taro.showToast({ title: '加载失败', icon: 'none' }))
.finally(() => setLoading(false));
try {
const user = useAuthStore.getState().user;
const fetcher = user ? getArticleDetail(id) : getPublicArticleDetail(id);
const data = await fetcher;
setArticle(data);
} catch {
Taro.showToast({ title: '加载失败', icon: 'none' });
} finally {
setLoading(false);
}
}, [id]);
usePageData(fetchArticle, { throttleMs: 60000 });
if (loading) {
return (
<View className={`article-detail-page ${modeClass}`}>

View File

@@ -1,7 +1,7 @@
import React, { useState, useCallback, useEffect } from 'react';
import { View, Text, Image, ScrollView } from '@tarojs/components';
import Taro, { usePullDownRefresh, useReachBottom } from '@tarojs/taro';
import { useThrottledDidShow } from '@/hooks/useThrottledDidShow';
import Taro, { useReachBottom } from '@tarojs/taro';
import { usePageData } from '@/hooks/usePageData';
import { listArticles, listCategories, Article, ArticleCategory } from '../../services/article';
import EmptyState from '../../components/EmptyState';
import Loading from '../../components/Loading';
@@ -49,15 +49,10 @@ export default function ArticleList() {
fetchCategories();
}, [fetchCategories]);
useThrottledDidShow(() => {
fetchData(1, false, null);
}, 10000);
usePullDownRefresh(() => {
fetchData(1, false, null).finally(() => {
Taro.stopPullDownRefresh();
});
});
usePageData(
useCallback(() => fetchData(1, false, null), [fetchData]),
{ throttleMs: 10000, enablePullDown: true },
);
useReachBottom(() => {
if (!loading && articles.length < total) {