22 lines
871 B
TypeScript
22 lines
871 B
TypeScript
/** 平台总后台的共享 HTTP 客户端,统一处理响应体和 JWT 请求头。 */
|
|
const apiBaseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:12426/heqi/platform/v1';
|
|
|
|
export const tokenStorageKey = 'token';
|
|
|
|
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 ?? {}),
|
|
},
|
|
});
|
|
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;
|
|
}
|