Files
dukang/apps/mini-user/src/lib/home-catalog-session.ts
T
jacy 06cbcdeb9c 首页商品:对齐门店「当次会话」——首次进入 / 城市变化 / 登录态变化 / 下拉刷新才拉列表,切 Tab 不再重复请求;退出登录会清缓存。
核销成功时间:按 Asia/Shanghai 格式化为「2026年8月3日 13点45分」。
核销码编号:单行省略显示,双击复制(有「双击复制」提示)。
2026-08-03 13:51:29 +08:00

76 lines
1.7 KiB
TypeScript

/**
* 首页商品列表「当次登录」会话 —— 切 tab 不重复拉商品;
* 城市 / 登录态变化或下拉刷新时再请求。logout 时 clear。
*/
import Taro from '@tarojs/taro';
export type HomeCatalogCache = {
cityCode: string;
authKey: string;
products: unknown[];
};
type HomeSession = {
bootstrapped: boolean;
cache: HomeCatalogCache | null;
};
const STORAGE_KEY = 'dukang_home_catalog_session_v1';
let memory: HomeSession | null = null;
function emptySession(): HomeSession {
return { bootstrapped: false, cache: null };
}
function readSession(): HomeSession {
if (memory) return memory;
try {
const raw = Taro.getStorageSync(STORAGE_KEY);
if (!raw) {
memory = emptySession();
return memory;
}
const parsed = (typeof raw === 'string' ? JSON.parse(raw) : raw) as Partial<HomeSession>;
memory = {
bootstrapped: !!parsed.bootstrapped,
cache: (parsed.cache as HomeCatalogCache | null) ?? null,
};
return memory;
} catch {
memory = emptySession();
return memory;
}
}
function writeSession(next: HomeSession) {
memory = next;
try {
Taro.setStorageSync(STORAGE_KEY, JSON.stringify(next));
} catch {
/* ignore quota */
}
}
export function isHomeCatalogBootstrapped(): boolean {
return readSession().bootstrapped;
}
export function getHomeCatalogCache(): HomeCatalogCache | null {
return readSession().cache;
}
export function setHomeCatalogCache(cache: HomeCatalogCache): void {
writeSession({ bootstrapped: true, cache });
}
export function resetHomeCatalogBootstrap(): void {
memory = emptySession();
try {
Taro.removeStorageSync(STORAGE_KEY);
} catch {
/* ignore */
}
}