From b81600b90e1e51e24b40943532bb85d4c13a3e86 Mon Sep 17 00:00:00 2001 From: clz Date: Tue, 21 Jul 2026 21:30:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ai-pick):=20=E5=8D=A1=E7=89=87?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=94=B9=E4=B8=BA=20AppStorage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/ai-pick/AiPickModel.ets | 19 +++++++++++++------ entry/src/main/ets/ai-pick/AiPickStorage.ets | 20 -------------------- 2 files changed, 13 insertions(+), 26 deletions(-) delete mode 100644 entry/src/main/ets/ai-pick/AiPickStorage.ets diff --git a/entry/src/main/ets/ai-pick/AiPickModel.ets b/entry/src/main/ets/ai-pick/AiPickModel.ets index b14cf5b..fa2ca33 100644 --- a/entry/src/main/ets/ai-pick/AiPickModel.ets +++ b/entry/src/main/ets/ai-pick/AiPickModel.ets @@ -1,7 +1,9 @@ import router from '@ohos.router'; -import { CardData, PickTabData } from './AiPickTypes'; +import { CardData, CardsDataCache, PickTabData } from './AiPickTypes'; import { fetchAiPickTabs, parseRawTabs } from './AiPickApi'; -import { AiPickStorage } from './AiPickStorage'; + +// 顶层缓存 key:对应 Vue 端 __GLOBAL__.cardsDataCache(跨组件/跨页面共享) +const APP_STORAGE_CACHE_KEY = 'cardsDataCache'; @Observed export class AiPickModel { @@ -16,7 +18,7 @@ export class AiPickModel { // 构造时即同步读出 showData,对应 Vue 端 useCardHandler.showData 三态: // 1. data_completed=true 且 mod_data[0].data 内嵌 → 直接解析内嵌数据(无需请求) - // 2. 否则从卡片级缓存 AiPickStorage 读出(对应 __GLOBAL__.cardsDataCache) + // 2. 否则从 AppStorage 卡片级缓存读出(对应 __GLOBAL__.cardsDataCache) // 3. 都没有 → 空数组,View 层展示 loading;后续 loadData() 异步拉取 constructor(cardData: CardData) { this.cardData = cardData; @@ -33,12 +35,13 @@ export class AiPickModel { return parseRawTabs(embedded); } } - return AiPickStorage.get(this.cardKey) ?? []; + const cache = AppStorage.get(APP_STORAGE_CACHE_KEY); + return cache?.[this.cardKey] ?? []; } // 拉取数据: // - data_completed=true:直接完成,不再发请求 - // - 其他:调用 fetchAiPickTabs,请求成功后写入 AiPickStorage + // - 其他:调用 fetchAiPickTabs,请求成功后写入 AppStorage async loadData(): Promise { if (this.cardData.data_completed === true) { this.dataReady = true; @@ -48,7 +51,11 @@ export class AiPickModel { try { const data = await fetchAiPickTabs(this.cardData); this.tabs = data; - AiPickStorage.set(this.cardKey, data); + // 写入 AppStorage:合并式更新(AppStorage 不支持原地改 Map) + const current: CardsDataCache = AppStorage.get(APP_STORAGE_CACHE_KEY) ?? {}; + const next: CardsDataCache = { ...current }; + next[this.cardKey] = data; + AppStorage.setOrCreate(APP_STORAGE_CACHE_KEY, next); this.dataError = false; } catch (e) { // 失败时保留原本的 showData(如有缓存命中) diff --git a/entry/src/main/ets/ai-pick/AiPickStorage.ets b/entry/src/main/ets/ai-pick/AiPickStorage.ets deleted file mode 100644 index 7f65bb8..0000000 --- a/entry/src/main/ets/ai-pick/AiPickStorage.ets +++ /dev/null @@ -1,20 +0,0 @@ -// AI选期第二层卡片级 session 缓存 -// 对应 Vue 端 __GLOBAL__.cardsDataCache[cardKey] -// 鸿蒙侧不需要 Memory/AppStorage 桥接,直接用 Map;session 级,不持久化 -import { PickTabData } from './AiPickTypes'; - -export class AiPickStorage { - private static cache: Map = new Map(); - - static get(cardKey: string): PickTabData[] | undefined { - return this.cache.get(cardKey); - } - - static set(cardKey: string, data: PickTabData[]): void { - this.cache.set(cardKey, data); - } - - static has(cardKey: string): boolean { - return this.cache.has(cardKey); - } -}