diff --git a/stores/latest.ts b/stores/latest.ts index e158229..2ebe044 100644 --- a/stores/latest.ts +++ b/stores/latest.ts @@ -5,6 +5,13 @@ export const useLatestLogs = defineStore('latest_logs', () => { const transactionMap = ref>(new Map()); const startGetData = async () => { + await fetch(); + + // 每隔 10 秒重新获取数据 + setTimeout(startGetData, 10 * 1000); + }; + + const fetch = async () => { const resp = await $fetch(`/api/product`, { retry: 3 }); logs.value = resp.latest.map((log) => ({ ...log, uploadedAt: new Date(log.uploadedAt) })); // 将数据转换为 Map 方便查询 @@ -13,16 +20,14 @@ export const useLatestLogs = defineStore('latest_logs', () => { const key = `${log.name}-from${log.sourceCity}to${log.targetCity}`; transactionMap.value.set(key, log); }); - - // 每隔 10 秒重新获取数据 - setTimeout(startGetData, 10 * 1000); - }; + } return { logs, getLatestLog(sourceCity: string, productName: string, targetCity: string) { return transactionMap.value.get(`${productName}-from${sourceCity}to${targetCity}`); }, + fetch, startGetData }; });