refactor: 移除前端接口缓存 — 每次 onShow 都实时请求最新数据

This commit is contained in:
2026-06-09 09:21:16 +08:00
parent 4e4b7dac69
commit ac20a70a20
2 changed files with 8 additions and 32 deletions

View File

@@ -110,21 +110,11 @@ import type { Dashboard } from '@/api/admin'
const loading = ref(true) const loading = ref(true)
const dashboard = ref<Dashboard | null>(null) const dashboard = ref<Dashboard | null>(null)
// 仪表盘缓存5分钟内不重复请求
let dashboardCache: { data: Dashboard; time: number } | null = null
const CACHE_TTL = 5 * 60 * 1000
onMounted(async () => { onMounted(async () => {
await waitForReady() await waitForReady()
try { try {
// 检查缓存是否有效 dashboard.value = await getDashboard()
if (dashboardCache && Date.now() - dashboardCache.time < CACHE_TTL) {
dashboard.value = dashboardCache.data
} else {
dashboard.value = await getDashboard()
dashboardCache = { data: dashboard.value, time: Date.now() }
}
} catch (e: any) { } catch (e: any) {
uni.showToast({ title: e.message || '加载失败', icon: 'none' }) uni.showToast({ title: e.message || '加载失败', icon: 'none' })
} finally { } finally {
@@ -132,13 +122,10 @@ onMounted(async () => {
} }
}) })
// 静默刷新(强制刷新缓存) // 静默刷新
async function refreshDashboard(force = false) { async function refreshDashboard() {
try { try {
if (force || !dashboardCache || Date.now() - dashboardCache.time >= CACHE_TTL) { dashboard.value = await getDashboard()
dashboard.value = await getDashboard()
dashboardCache = { data: dashboard.value, time: Date.now() }
}
} catch {} } catch {}
} }
@@ -148,7 +135,7 @@ onShow(() => {
}) })
onPullDownRefresh(async () => { onPullDownRefresh(async () => {
await refreshDashboard(true) await refreshDashboard()
uni.stopPullDownRefresh() uni.stopPullDownRefresh()
}) })

View File

@@ -204,18 +204,9 @@ function openLink(url: string) {
// #endif // #endif
} }
// 数据新鲜度控制30秒内不重复请求 async function loadData(silent = false) {
let lastLoadTime = 0
const CACHE_TTL = 30000
async function loadData(silent = false, force = false) {
await waitForReady() await waitForReady()
// 数据新鲜度判断30秒内不重复请求
if (!force && silent && Date.now() - lastLoadTime < CACHE_TTL) {
return
}
try { try {
if (!silent) loading.value = true if (!silent) loading.value = true
loadError.value = false loadError.value = false
@@ -226,7 +217,6 @@ async function loadData(silent = false, force = false) {
txStore.fetchTransactions({ page: 1 }) txStore.fetchTransactions({ page: 1 })
]) ])
recentTx.value = txStore.transactions.slice(0, 5) recentTx.value = txStore.transactions.slice(0, 5)
lastLoadTime = Date.now()
// 第二级:次要数据(异步,不阻塞首屏) // 第二级:次要数据(异步,不阻塞首屏)
Promise.all([ Promise.all([
@@ -270,13 +260,12 @@ onMounted(() => {
}) })
// Refresh data when returning from other pages (e.g., after adding a transaction) // Refresh data when returning from other pages (e.g., after adding a transaction)
// force=true 跳过缓存 TTL确保新记录立即可见
onShow(() => { onShow(() => {
if (initialLoaded.value && !loading.value) loadData(true, true) if (initialLoaded.value && !loading.value) loadData(true)
}) })
onPullDownRefresh(() => { onPullDownRefresh(() => {
loadData(false, true).finally(() => uni.stopPullDownRefresh()) loadData().finally(() => uni.stopPullDownRefresh())
}) })
function goAdd(type: string) { function goAdd(type: string) {