From ac20a70a20c10797dc15317e0c64ca462297f3ba Mon Sep 17 00:00:00 2001 From: wangxiaogang <1433729587@qq.com> Date: Tue, 9 Jun 2026 09:21:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E6=8E=A5=E5=8F=A3=E7=BC=93=E5=AD=98=20=E2=80=94=20?= =?UTF-8?q?=E6=AF=8F=E6=AC=A1=20onShow=20=E9=83=BD=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=9C=80=E6=96=B0=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/pages/admin/index.vue | 23 +++++------------------ client/src/pages/index/index.vue | 17 +++-------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/client/src/pages/admin/index.vue b/client/src/pages/admin/index.vue index 2323fa9..0e948d7 100644 --- a/client/src/pages/admin/index.vue +++ b/client/src/pages/admin/index.vue @@ -110,21 +110,11 @@ import type { Dashboard } from '@/api/admin' const loading = ref(true) const dashboard = ref(null) -// 仪表盘缓存(5分钟内不重复请求) -let dashboardCache: { data: Dashboard; time: number } | null = null -const CACHE_TTL = 5 * 60 * 1000 - onMounted(async () => { await waitForReady() try { - // 检查缓存是否有效 - if (dashboardCache && Date.now() - dashboardCache.time < CACHE_TTL) { - dashboard.value = dashboardCache.data - } else { - dashboard.value = await getDashboard() - dashboardCache = { data: dashboard.value, time: Date.now() } - } + dashboard.value = await getDashboard() } catch (e: any) { uni.showToast({ title: e.message || '加载失败', icon: 'none' }) } finally { @@ -132,13 +122,10 @@ onMounted(async () => { } }) -// 静默刷新(强制刷新缓存) -async function refreshDashboard(force = false) { +// 静默刷新 +async function refreshDashboard() { try { - if (force || !dashboardCache || Date.now() - dashboardCache.time >= CACHE_TTL) { - dashboard.value = await getDashboard() - dashboardCache = { data: dashboard.value, time: Date.now() } - } + dashboard.value = await getDashboard() } catch {} } @@ -148,7 +135,7 @@ onShow(() => { }) onPullDownRefresh(async () => { - await refreshDashboard(true) + await refreshDashboard() uni.stopPullDownRefresh() }) diff --git a/client/src/pages/index/index.vue b/client/src/pages/index/index.vue index febf033..da5d97a 100644 --- a/client/src/pages/index/index.vue +++ b/client/src/pages/index/index.vue @@ -204,18 +204,9 @@ function openLink(url: string) { // #endif } -// 数据新鲜度控制(30秒内不重复请求) -let lastLoadTime = 0 -const CACHE_TTL = 30000 - -async function loadData(silent = false, force = false) { +async function loadData(silent = false) { await waitForReady() - // 数据新鲜度判断:30秒内不重复请求 - if (!force && silent && Date.now() - lastLoadTime < CACHE_TTL) { - return - } - try { if (!silent) loading.value = true loadError.value = false @@ -226,7 +217,6 @@ async function loadData(silent = false, force = false) { txStore.fetchTransactions({ page: 1 }) ]) recentTx.value = txStore.transactions.slice(0, 5) - lastLoadTime = Date.now() // 第二级:次要数据(异步,不阻塞首屏) Promise.all([ @@ -270,13 +260,12 @@ onMounted(() => { }) // Refresh data when returning from other pages (e.g., after adding a transaction) -// force=true 跳过缓存 TTL,确保新记录立即可见 onShow(() => { - if (initialLoaded.value && !loading.value) loadData(true, true) + if (initialLoaded.value && !loading.value) loadData(true) }) onPullDownRefresh(() => { - loadData(false, true).finally(() => uni.stopPullDownRefresh()) + loadData().finally(() => uni.stopPullDownRefresh()) }) function goAdd(type: string) {