fix localized login network error

This commit is contained in:
david
2026-07-29 17:32:24 +08:00
parent 82742bde58
commit ab0915edaf
2 changed files with 18 additions and 10 deletions

View File

@@ -7,14 +7,19 @@ export type PageResult<T> = { total: number; list: T[] };
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
const token = localStorage.getItem(tokenStorageKey);
const response = await fetch(`${apiBaseURL}${path}`, {
...init,
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: token } : {}),
...(init?.headers ?? {}),
},
});
let response: Response;
try {
response = await fetch(`${apiBaseURL}${path}`, {
...init,
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: token } : {}),
...(init?.headers ?? {}),
},
});
} catch {
throw new Error('无法连接服务器,请确认服务已启动');
}
const payload = (await response.json()) as { code?: number; message?: string; details?: T };
if (!response.ok || payload.code !== 0) throw new Error(payload.message || '请求失败');
return payload.details as T;