fa3484041e
HQ 可配置 visibilityWhitelistEnabled + 手机号;C 端公开门店列表/详情按登录手机号过滤;登录态变化后小程序重新拉门店列表。 Co-authored-by: Cursor <cursoragent@cursor.com>
138 lines
3.3 KiB
TypeScript
138 lines
3.3 KiB
TypeScript
/**
|
|
* 门店列表「当次登录」会话 —— 用 Taro Storage 持久化,
|
|
* 避免模块多实例 / globalThis 不可靠导致切 tab 后当成首次进入。
|
|
* 仅 logout 时 clear。
|
|
*/
|
|
|
|
import Taro from '@tarojs/taro';
|
|
|
|
export type StoresSessionRegion = {
|
|
province: string;
|
|
city: string;
|
|
district: string;
|
|
};
|
|
|
|
export type StoresSessionCategory = {
|
|
parentId: string;
|
|
parentName: string;
|
|
childId: string;
|
|
childName: string;
|
|
};
|
|
|
|
export type StoresListCache = {
|
|
cityKey: string;
|
|
cityCode: string;
|
|
/** 登录态指纹:token 变化时需重新拉取(白名单) */
|
|
authKey: string;
|
|
listRegion: StoresSessionRegion;
|
|
items: unknown[];
|
|
filterRegion: StoresSessionRegion;
|
|
keyword: string;
|
|
keywordInput: string;
|
|
category: StoresSessionCategory;
|
|
};
|
|
|
|
type StoresSession = {
|
|
bootstrapped: boolean;
|
|
cache: StoresListCache | null;
|
|
};
|
|
|
|
const STORAGE_KEY = 'dukang_stores_session_v1';
|
|
|
|
let memory: StoresSession | null = null;
|
|
|
|
function emptySession(): StoresSession {
|
|
return { bootstrapped: false, cache: null };
|
|
}
|
|
|
|
function readSession(): StoresSession {
|
|
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<StoresSession>;
|
|
memory = {
|
|
bootstrapped: !!parsed.bootstrapped,
|
|
cache: (parsed.cache as StoresListCache | null) ?? null,
|
|
};
|
|
return memory;
|
|
} catch {
|
|
memory = emptySession();
|
|
return memory;
|
|
}
|
|
}
|
|
|
|
function writeSession(next: StoresSession) {
|
|
memory = next;
|
|
try {
|
|
Taro.setStorageSync(STORAGE_KEY, JSON.stringify(next));
|
|
} catch {
|
|
/* ignore quota */
|
|
}
|
|
}
|
|
|
|
export function isStoresSessionBootstrapped(): boolean {
|
|
return readSession().bootstrapped;
|
|
}
|
|
|
|
export function markStoresSessionBootstrapped(): void {
|
|
const cur = readSession();
|
|
writeSession({ ...cur, bootstrapped: true });
|
|
}
|
|
|
|
export function getStoresListCache(): StoresListCache | null {
|
|
return readSession().cache;
|
|
}
|
|
|
|
export function setStoresListCache(cache: StoresListCache | null): void {
|
|
const cur = readSession();
|
|
writeSession({ ...cur, bootstrapped: true, cache });
|
|
}
|
|
|
|
export function patchStoresFilterCache(
|
|
patch: Partial<
|
|
Pick<StoresListCache, 'filterRegion' | 'keyword' | 'keywordInput' | 'category'>
|
|
>,
|
|
): void {
|
|
const cur = readSession();
|
|
if (!cur.cache) {
|
|
// 列表尚未写入时也要记下用户筛选,避免切回丢失
|
|
writeSession({
|
|
bootstrapped: true,
|
|
cache: {
|
|
cityKey: '',
|
|
cityCode: '',
|
|
listRegion: patch.filterRegion ?? { province: '', city: '', district: '' },
|
|
items: [],
|
|
filterRegion: patch.filterRegion ?? { province: '', city: '', district: '' },
|
|
keyword: patch.keyword ?? '',
|
|
keywordInput: patch.keywordInput ?? '',
|
|
category: patch.category ?? {
|
|
parentId: '',
|
|
parentName: '',
|
|
childId: '',
|
|
childName: '',
|
|
},
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
writeSession({
|
|
...cur,
|
|
bootstrapped: true,
|
|
cache: { ...cur.cache, ...patch },
|
|
});
|
|
}
|
|
|
|
export function resetStoresSessionBootstrap(): void {
|
|
memory = emptySession();
|
|
try {
|
|
Taro.removeStorageSync(STORAGE_KEY);
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|