refactor(ai-pick): 卡片缓存改为 AppStorage
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
import router from '@ohos.router';
|
import router from '@ohos.router';
|
||||||
import { CardData, PickTabData } from './AiPickTypes';
|
import { CardData, CardsDataCache, PickTabData } from './AiPickTypes';
|
||||||
import { fetchAiPickTabs, parseRawTabs } from './AiPickApi';
|
import { fetchAiPickTabs, parseRawTabs } from './AiPickApi';
|
||||||
import { AiPickStorage } from './AiPickStorage';
|
|
||||||
|
// 顶层缓存 key:对应 Vue 端 __GLOBAL__.cardsDataCache(跨组件/跨页面共享)
|
||||||
|
const APP_STORAGE_CACHE_KEY = 'cardsDataCache';
|
||||||
|
|
||||||
@Observed
|
@Observed
|
||||||
export class AiPickModel {
|
export class AiPickModel {
|
||||||
@@ -16,7 +18,7 @@ export class AiPickModel {
|
|||||||
|
|
||||||
// 构造时即同步读出 showData,对应 Vue 端 useCardHandler.showData 三态:
|
// 构造时即同步读出 showData,对应 Vue 端 useCardHandler.showData 三态:
|
||||||
// 1. data_completed=true 且 mod_data[0].data 内嵌 → 直接解析内嵌数据(无需请求)
|
// 1. data_completed=true 且 mod_data[0].data 内嵌 → 直接解析内嵌数据(无需请求)
|
||||||
// 2. 否则从卡片级缓存 AiPickStorage 读出(对应 __GLOBAL__.cardsDataCache)
|
// 2. 否则从 AppStorage 卡片级缓存读出(对应 __GLOBAL__.cardsDataCache)
|
||||||
// 3. 都没有 → 空数组,View 层展示 loading;后续 loadData() 异步拉取
|
// 3. 都没有 → 空数组,View 层展示 loading;后续 loadData() 异步拉取
|
||||||
constructor(cardData: CardData) {
|
constructor(cardData: CardData) {
|
||||||
this.cardData = cardData;
|
this.cardData = cardData;
|
||||||
@@ -33,12 +35,13 @@ export class AiPickModel {
|
|||||||
return parseRawTabs(embedded);
|
return parseRawTabs(embedded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return AiPickStorage.get(this.cardKey) ?? [];
|
const cache = AppStorage.get<CardsDataCache>(APP_STORAGE_CACHE_KEY);
|
||||||
|
return cache?.[this.cardKey] ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 拉取数据:
|
// 拉取数据:
|
||||||
// - data_completed=true:直接完成,不再发请求
|
// - data_completed=true:直接完成,不再发请求
|
||||||
// - 其他:调用 fetchAiPickTabs,请求成功后写入 AiPickStorage
|
// - 其他:调用 fetchAiPickTabs,请求成功后写入 AppStorage
|
||||||
async loadData(): Promise<void> {
|
async loadData(): Promise<void> {
|
||||||
if (this.cardData.data_completed === true) {
|
if (this.cardData.data_completed === true) {
|
||||||
this.dataReady = true;
|
this.dataReady = true;
|
||||||
@@ -48,7 +51,11 @@ export class AiPickModel {
|
|||||||
try {
|
try {
|
||||||
const data = await fetchAiPickTabs(this.cardData);
|
const data = await fetchAiPickTabs(this.cardData);
|
||||||
this.tabs = data;
|
this.tabs = data;
|
||||||
AiPickStorage.set(this.cardKey, data);
|
// 写入 AppStorage:合并式更新(AppStorage 不支持原地改 Map)
|
||||||
|
const current: CardsDataCache = AppStorage.get<CardsDataCache>(APP_STORAGE_CACHE_KEY) ?? {};
|
||||||
|
const next: CardsDataCache = { ...current };
|
||||||
|
next[this.cardKey] = data;
|
||||||
|
AppStorage.setOrCreate<CardsDataCache>(APP_STORAGE_CACHE_KEY, next);
|
||||||
this.dataError = false;
|
this.dataError = false;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// 失败时保留原本的 showData(如有缓存命中)
|
// 失败时保留原本的 showData(如有缓存命中)
|
||||||
|
|||||||
@@ -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<string, PickTabData[]> = 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user