diff --git a/apps/miniprogram/src/services/alert.ts b/apps/miniprogram/src/services/alert.ts index 0b30bf2..6226b1d 100644 --- a/apps/miniprogram/src/services/alert.ts +++ b/apps/miniprogram/src/services/alert.ts @@ -22,5 +22,5 @@ export async function listPatientAlerts(patientId: string, params?: { status?: s page: params?.page ?? 1, page_size: params?.page_size ?? 20, status: params?.status, - }); + }, 10_000); } diff --git a/apps/miniprogram/src/services/notification.ts b/apps/miniprogram/src/services/notification.ts index 9a2bf72..2e63754 100644 --- a/apps/miniprogram/src/services/notification.ts +++ b/apps/miniprogram/src/services/notification.ts @@ -8,5 +8,5 @@ export const notificationService = { markAllRead: () => api.put('/messages/read-all'), getUnreadCount: () => - api.get('/messages/unread-count'), + api.get('/messages/unread-count', undefined, 10_000), }; diff --git a/apps/miniprogram/src/services/request/cache.ts b/apps/miniprogram/src/services/request/cache.ts index c97aa39..85aabe9 100644 --- a/apps/miniprogram/src/services/request/cache.ts +++ b/apps/miniprogram/src/services/request/cache.ts @@ -28,9 +28,17 @@ export class ResponseCache { } get(url: string): T | null { - const entry = this.cache.get(this.cacheKey(url)); - if (entry && Date.now() < entry.expiry) return entry.data as T; - return null; + const key = this.cacheKey(url); + const entry = this.cache.get(key); + if (!entry) return null; + if (Date.now() >= entry.expiry) { + this.cache.delete(key); + return null; + } + // LRU: 命中时更新插入顺序,使该条目移到末尾 + this.cache.delete(key); + this.cache.set(key, entry); + return entry.data as T; } getInflight(url: string): Promise | null { diff --git a/apps/miniprogram/src/stores/auth.ts b/apps/miniprogram/src/stores/auth.ts index 6127d52..5753c10 100644 --- a/apps/miniprogram/src/stores/auth.ts +++ b/apps/miniprogram/src/stores/auth.ts @@ -109,9 +109,6 @@ export const useAuthStore = create((set, get) => ({ setCachedPatientId(currentPatient.id); } - // 状态有变化时清理请求缓存,避免返回过期数据 - clearRequestCache(); - // 跳过未变更的 set() const cur = get(); const userChanged = cur.user?.id !== user?.id; @@ -119,6 +116,8 @@ export const useAuthStore = create((set, get) => ({ const patientChanged = cur.currentPatient?.id !== currentPatient?.id; if (!userChanged && !rolesChanged && !patientChanged) return; + // 状态有变化时清理请求缓存,避免返回过期数据 + clearRequestCache(); set({ user, roles, currentPatient }); },