Files
hms/apps/miniprogram/src/pages/mall/detail/index.tsx
iven fcfc0ba5d9
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled
perf(miniprogram): 全面性能优化 — 分包加载 + 请求缓存 + 渲染优化
分包加载(主包从 517KB 降至 275KB,-47%):
- 将 27 个页面拆入 6 个分包(health/doctor/mall/profile/content/device)
- vendors.js 从 192KB 降至 36KB(-81%)
- echarts 514KB 仅在访问健康趋势页时按需加载

请求层优化:
- GET 请求增加 in-flight 去重 + 60s TTL 响应缓存
- 新建 points store 集中管理积分/签到状态(消除 5 处重复调用)
- health store todaySummary 增加 60s TTL
- mutation 后自动失效缓存(health input/daily-monitoring)
- logout 时清空请求缓存

渲染优化:
- 7 个组件添加 React.memo(EcCanvas/TrendChart/Loading/EmptyState 等)
- 修复 TrendChart setChartReady 导致的双重渲染
- 静态数组(quickServices/quickActions/trendLinks)提取到模块级
- restoreAuth 从页面级提升到 App 级别
- 文章列表图片添加 lazyLoad

构建优化:
- prod 配置添加 terser(drop_console + drop_debugger)
- crypto-js 从全量引入改为按需引入(AES + Utf8)
2026-04-28 11:44:37 +08:00

186 lines
6.3 KiB
TypeScript

import React, { useState, useCallback, useRef } from 'react';
import { View, Text } from '@tarojs/components';
import Taro, { useDidShow, useReachBottom, usePullDownRefresh } from '@tarojs/taro';
import { listMyTransactions } from '../../../services/points';
import type { PointsTransaction } from '../../../services/points';
import { usePointsStore } from '../../../stores/points';
import EmptyState from '../../../components/EmptyState';
import Loading from '../../../components/Loading';
import './index.scss';
const TYPE_TABS = [
{ key: '', label: '全部' },
{ key: 'earn', label: '收入' },
{ key: 'spend', label: '支出' },
];
export default function PointsDetail() {
const { account, refresh: refreshPoints } = usePointsStore();
const [transactions, setTransactions] = useState<PointsTransaction[]>([]);
const [activeTab, setActiveTab] = useState('');
const [page, setPage] = useState(1);
const [total, setTotal] = useState(0);
const [loading, setLoading] = useState(false);
const loadingRef = useRef(false);
const fetchTransactions = useCallback(
async (pageNum: number, type: string, isRefresh = false) => {
if (loadingRef.current) return;
loadingRef.current = true;
setLoading(true);
try {
const res = await listMyTransactions({
page: pageNum,
page_size: 10,
});
let list = res.data || [];
if (type) {
list = list.filter((t) => t.type === type);
}
if (isRefresh) {
setTransactions(list);
} else {
setTransactions((prev) => [...prev, ...list]);
}
setTotal(res.total);
setPage(pageNum);
} catch {
Taro.showToast({ title: '加载失败', icon: 'none' });
} finally {
loadingRef.current = false;
setLoading(false);
}
},
[],
);
const loadAll = useCallback(
async (type?: string) => {
const t = type !== undefined ? type : activeTab;
await Promise.all([refreshPoints(), fetchTransactions(1, t, true)]);
},
[refreshPoints, fetchTransactions, activeTab],
);
useDidShow(() => {
Taro.setNavigationBarTitle({ title: '积分明细' });
loadAll();
});
usePullDownRefresh(() => {
loadAll().finally(() => {
Taro.stopPullDownRefresh();
});
});
useReachBottom(() => {
if (!loading && transactions.length < total) {
fetchTransactions(page + 1, activeTab);
}
});
const handleTabChange = (key: string) => {
setActiveTab(key);
fetchTransactions(1, key, true);
};
const getTypeLabel = (type: string) => {
if (type === 'earn') return '收';
if (type === 'spend') return '支';
return '过';
};
const getTypeClass = (type: string) => {
if (type === 'earn') return 'earn';
if (type === 'spend') return 'spend';
return 'expired';
};
const formatAmount = (tx: PointsTransaction) => {
const amt = tx.amount;
if (tx.type === 'earn') return `+${amt.toLocaleString()}`;
if (tx.type === 'spend') return `-${amt.toLocaleString()}`;
return `-${amt.toLocaleString()}`;
};
const formatDate = (dateStr: string) => {
if (!dateStr) return '';
const d = new Date(dateStr);
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
};
const balance = account?.balance ?? 0;
return (
<View className='detail-page'>
{/* 余额卡片 */}
<View className='balance-card'>
<Text className='balance-label'></Text>
<Text className='balance-value'>{balance.toLocaleString()}</Text>
<View className='balance-stats'>
<View className='stat-item'>
<Text className='stat-value stat-earn'>{(account?.total_earned ?? 0).toLocaleString()}</Text>
<Text className='stat-label'></Text>
</View>
<View className='stat-divider' />
<View className='stat-item'>
<Text className='stat-value stat-spend'>{(account?.total_spent ?? 0).toLocaleString()}</Text>
<Text className='stat-label'></Text>
</View>
<View className='stat-divider' />
<View className='stat-item'>
<Text className='stat-value stat-expired'>{(account?.total_expired ?? 0).toLocaleString()}</Text>
<Text className='stat-label'></Text>
</View>
</View>
</View>
{/* 类型筛选标签 */}
<View className='type-tabs'>
{TYPE_TABS.map((tab) => (
<View
key={tab.key}
className={`type-tab ${activeTab === tab.key ? 'active' : ''}`}
onClick={() => handleTabChange(tab.key)}
>
<Text className='type-tab-text'>{tab.label}</Text>
</View>
))}
</View>
{/* 交易列表 */}
{transactions.length === 0 && !loading ? (
<EmptyState icon='' text='暂无积分记录' hint='签到或兑换后将显示记录' />
) : (
<View className='transaction-list'>
{transactions.map((tx) => {
return (
<View className='transaction-item' key={tx.id}>
<View className={`tx-badge tx-badge-${getTypeClass(tx.type)}`}>
<Text className='tx-badge-text'>{getTypeLabel(tx.type)}</Text>
</View>
<View className='tx-info'>
<Text className='tx-desc'>
{tx.description || (tx.type === 'earn' ? '积分收入' : tx.type === 'spend' ? '积分消费' : '积分过期')}
</Text>
<Text className='tx-date'>{formatDate(tx.created_at)}</Text>
</View>
<View className='tx-amount-col'>
<Text className={`tx-amount tx-amount-${tx.type === 'earn' ? 'positive' : 'negative'}`}>
{formatAmount(tx)}
</Text>
<Text className='tx-remaining'> {tx.balance_after.toLocaleString()}</Text>
</View>
</View>
);
})}
{loading && <Loading />}
{!loading && transactions.length >= total && total > 0 && (
<Loading text='没有更多了' />
)}
</View>
)}
</View>
);
}