feat: 完善平台总后台模块
This commit is contained in:
21
frontend/platform_admin/src/api/http.ts
Normal file
21
frontend/platform_admin/src/api/http.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/** 平台总后台的共享 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;
|
||||
}
|
||||
Reference in New Issue
Block a user