@@ -156,10 +156,19 @@ async function rawRequest<T>(
|
||||
if (authToken) headers.Authorization = `Bearer ${authToken}`;
|
||||
|
||||
const res = await fetch(`${apiBase}${path}`, { ...options, headers });
|
||||
const json = await res.json().catch(() => ({ code: res.status, message: '网络异常' }));
|
||||
const json = await res.json().catch(() => ({ code: res.status, message: '网络异常' })) as {
|
||||
code: number;
|
||||
message?: string;
|
||||
data?: T;
|
||||
reason?: string;
|
||||
};
|
||||
if (json.code !== 0) {
|
||||
const err = new Error(json.message || '请求失败') as Error & { status?: number };
|
||||
const err = new Error(json.message || '请求失败') as Error & {
|
||||
status?: number;
|
||||
reason?: string;
|
||||
};
|
||||
err.status = json.code === 401 ? 401 : json.code;
|
||||
err.reason = json.reason;
|
||||
if (json.code === 400) {
|
||||
reportApiError(
|
||||
{ apiBase, clientApp: CLIENT_APP, getToken: () => localStorage.getItem(ACCESS_TOKEN) },
|
||||
@@ -224,8 +233,19 @@ export async function request<T>(
|
||||
try {
|
||||
return await requestWithAuthRetry<T>(path, fetchOptions);
|
||||
} catch (e) {
|
||||
const err = e as Error & { status?: number };
|
||||
const err = e as Error & { status?: number; reason?: string };
|
||||
const message = err.message || '请求失败';
|
||||
// 账号停用 / 合伙人绑定失效:强制退出登录
|
||||
if (err.reason === 'ACCOUNT_DISABLED') {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
clearAuth({ keepProfile: true });
|
||||
if (!silent) showPartnerToast(message, 'error');
|
||||
if (typeof window !== 'undefined' && !isOnAppPath('/login')) {
|
||||
window.location.href = toAppPath('/login?disabled=1');
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (err.status === 401) {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
clearAuth({ keepProfile: true });
|
||||
@@ -260,7 +280,14 @@ export async function ensureSession(): Promise<{ authenticated: boolean; partner
|
||||
touchPartnerSession();
|
||||
return { authenticated: true, partner };
|
||||
} catch (e) {
|
||||
const err = e as Error & { status?: number };
|
||||
const err = e as Error & { status?: number; reason?: string };
|
||||
if (err.reason === 'ACCOUNT_DISABLED') {
|
||||
clearAuth({ keepProfile: true });
|
||||
if (typeof window !== 'undefined' && !isOnAppPath('/login')) {
|
||||
window.location.href = toAppPath('/login?disabled=1');
|
||||
}
|
||||
return { authenticated: false, partner: getPartnerProfile() };
|
||||
}
|
||||
if (err.status === 401) {
|
||||
const refreshed = await refreshSession();
|
||||
if (refreshed?.partner) {
|
||||
@@ -277,3 +304,17 @@ export async function ensureSession(): Promise<{ authenticated: boolean; partner
|
||||
|
||||
/** @deprecated 使用 PartnerSessionPayload */
|
||||
export type PartnerAuthPayload = PartnerSessionPayload;
|
||||
|
||||
export function submitStoreInfoChangeRequest(storeId: string, fields: Record<string, unknown>) {
|
||||
return request('PARTNER_H5', `/partner/stores/${storeId}/info-change-request`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(fields),
|
||||
});
|
||||
}
|
||||
|
||||
export function listStoreInfoChangeRequests(storeId: string) {
|
||||
return request<Array<{ status?: string }>>(
|
||||
'PARTNER_H5',
|
||||
`/partner/stores/${storeId}/info-change-requests`,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user