新增配送点资料只读卡片
This commit is contained in:
@@ -5,6 +5,11 @@ const apiBaseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:12426/
|
||||
|
||||
export type PageResult<T> = { total: number; list: T[] };
|
||||
|
||||
/**
|
||||
* 发送配送点后台请求。
|
||||
* 参数:path 为 API 相对路径,init 为标准 Fetch 请求选项。
|
||||
* 返回值:统一响应中的 details;网络、协议或业务失败时抛出可读错误。
|
||||
*/
|
||||
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const token = getToken();
|
||||
let response: Response;
|
||||
@@ -20,7 +25,15 @@ export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
} catch {
|
||||
throw new Error('无法连接服务器,请确认服务已启动');
|
||||
}
|
||||
const payload = (await response.json()) as { code?: number; message?: string; details?: T };
|
||||
const responseText = await response.text();
|
||||
let payload: { code?: number; message?: string; details?: T };
|
||||
try {
|
||||
payload = responseText ? JSON.parse(responseText) : {};
|
||||
} catch {
|
||||
// 网关、代理和不存在的路由可能返回纯文本,不能把底层 JSON 异常暴露给用户。
|
||||
if (!response.ok) throw new Error(`请求失败(HTTP ${response.status})`);
|
||||
throw new Error('服务器返回了无法识别的数据格式');
|
||||
}
|
||||
if (!response.ok || payload.code !== 0) throw new Error(payload.message || '请求失败');
|
||||
return payload.details as T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user