27 lines
1012 B
TypeScript
27 lines
1012 B
TypeScript
|
|
/** 配送点后台 HTTP 客户端,使用独立 API 前缀和 JWT 存储键。 */
|
||
|
|
const apiBaseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:12426/heqi/delivery/v1';
|
||
|
|
|
||
|
|
export const tokenStorageKey = 'delivery_admin_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);
|
||
|
|
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;
|
||
|
|
}
|