门店账户多账号

This commit is contained in:
2026-07-12 12:24:34 +08:00
parent 06b1cb22e0
commit 54a15d6da7
39 changed files with 1962 additions and 311 deletions
+100 -17
View File
@@ -1,12 +1,22 @@
export const apiBase = '/api/v1';
const CLIENT_APP = 'SHOP_H5';
export type ShopStoreOption = {
storeId: string;
name: string;
status: string;
district?: string;
address?: string;
};
export type StoreSessionStore = {
id: string;
storeId: string;
name: string;
phone: string;
storeName: string;
isPrimary?: boolean;
stores?: ShopStoreOption[];
};
export type StoreProfile = {
@@ -15,13 +25,25 @@ export type StoreProfile = {
name: string;
phone: string;
status?: string;
store?: { id: string; name: string };
isPrimary?: boolean;
account?: {
id: string;
name: string;
phone: string;
isPrimary: boolean;
hasWechat?: boolean;
};
store?: ShopStoreOption | { id: string; name: string } | null;
stores?: ShopStoreOption[];
};
export type ShopSessionPayload = {
accessToken: string;
refreshToken: string;
store?: StoreSessionStore;
stores?: ShopStoreOption[];
account?: StoreProfile['account'];
selectedStoreId?: string;
};
const ACCESS_TOKEN = 'accessToken';
@@ -73,8 +95,27 @@ export function saveAuth(data: ShopSessionPayload) {
localStorage.setItem(ACCESS_TOKEN, data.accessToken);
if (data.refreshToken) localStorage.setItem(REFRESH_TOKEN, data.refreshToken);
if (data.store) {
localStorage.setItem(STORE_PROFILE, JSON.stringify(data.store));
localStorage.setItem(LAST_PHONE, data.store.phone);
const profile: StoreSessionStore = {
...data.store,
stores: data.stores ?? data.store.stores,
isPrimary: data.account?.isPrimary ?? data.store.isPrimary,
};
localStorage.setItem(STORE_PROFILE, JSON.stringify(profile));
if (data.store.phone) localStorage.setItem(LAST_PHONE, data.store.phone);
} else if (data.account) {
localStorage.setItem(
STORE_PROFILE,
JSON.stringify({
id: data.account.id,
storeId: '',
name: data.account.name,
phone: data.account.phone,
storeName: '',
isPrimary: data.account.isPrimary,
stores: data.stores ?? [],
} satisfies StoreSessionStore),
);
localStorage.setItem(LAST_PHONE, data.account.phone);
}
}
@@ -101,15 +142,45 @@ export function isLoggedIn() {
}
function profileFromMe(me: StoreProfile): StoreSessionStore {
const selected =
me.store && 'storeId' in me.store
? me.store
: me.store && 'id' in me.store
? { storeId: String((me.store as { id: string }).id), name: me.store.name }
: null;
return {
id: me.id,
storeId: me.storeId,
name: me.name,
phone: me.phone,
storeName: me.store?.name ?? me.name,
id: me.account?.id ?? me.id,
storeId: selected?.storeId ?? me.storeId ?? '',
name: me.account?.name ?? me.name,
phone: me.account?.phone ?? me.phone,
storeName: selected?.name ?? '',
isPrimary: me.account?.isPrimary ?? me.isPrimary,
stores: me.stores ?? [],
};
}
export function needsStoreSelection(session: {
store?: StoreSessionStore | null;
stores?: ShopStoreOption[];
selectedStoreId?: string;
}): boolean {
const storeId = session.store?.storeId || session.selectedStoreId || '';
const stores = session.stores ?? session.store?.stores ?? [];
if (stores.length > 1 && !storeId) return true;
if (!storeId && stores.length !== 1) return true;
return false;
}
export async function selectStore(storeId: string): Promise<ShopSessionPayload> {
const data = await requestWithAuthRetry<ShopSessionPayload>('/shop/auth/select-store', {
method: 'POST',
body: JSON.stringify({ storeId }),
});
saveAuth(data);
touchShopSession();
return data;
}
async function rawRequest<T>(
path: string,
options: RequestInit = {},
@@ -190,13 +261,17 @@ export async function request<T>(clientApp: string, path: string, options: Reque
return requestWithAuthRetry<T>(path, options);
}
export async function ensureSession(): Promise<{ authenticated: boolean; store: StoreSessionStore | null }> {
export async function ensureSession(): Promise<{
authenticated: boolean;
store: StoreSessionStore | null;
needsSelectStore: boolean;
}> {
if (!isLoggedIn()) {
return { authenticated: false, store: null };
return { authenticated: false, store: null, needsSelectStore: false };
}
if (isShopSessionExpired()) {
clearAuth({ keepProfile: true });
return { authenticated: false, store: getStoreProfile() };
return { authenticated: false, store: getStoreProfile(), needsSelectStore: false };
}
try {
const me = await rawRequest<StoreProfile>('/shop/auth/me');
@@ -205,21 +280,29 @@ export async function ensureSession(): Promise<{ authenticated: boolean; store:
accessToken: localStorage.getItem(ACCESS_TOKEN) ?? '',
refreshToken: localStorage.getItem(REFRESH_TOKEN) ?? '',
store,
stores: me.stores,
account: me.account,
});
touchShopSession();
return { authenticated: true, store };
return {
authenticated: true,
store,
needsSelectStore: needsStoreSelection({ store, stores: me.stores }),
};
} catch (e) {
const err = e as Error & { status?: number };
if (err.status === 401) {
const refreshed = await refreshSession();
if (refreshed?.store) {
return { authenticated: true, store: refreshed.store };
if (refreshed) {
return {
authenticated: true,
store: refreshed.store ?? getStoreProfile(),
needsSelectStore: needsStoreSelection(refreshed),
};
}
clearAuth({ keepProfile: true });
return { authenticated: false, store: getStoreProfile() };
return { authenticated: false, store: getStoreProfile(), needsSelectStore: false };
}
const cached = getStoreProfile();
if (cached) return { authenticated: true, store: cached };
throw e;
}
}