feat: 标准资源使用独立页面并优化详情布局
This commit is contained in:
@@ -1,13 +1,28 @@
|
||||
/** 平台总后台的共享 HTTP 客户端,统一处理响应体和 JWT 请求头。 */
|
||||
const apiBaseURL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:12426/heqi/platform/v1';
|
||||
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[] };
|
||||
|
||||
/** 保留 HTTP 状态,供独立页面区分无权限、不存在和普通请求错误。 */
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly status: number,
|
||||
) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
// 将后端 SDK 的通用英文错误转换为面向用户的中文提示。
|
||||
const API_ERROR_MESSAGES: Record<string, string> = {
|
||||
'Invalid Argument': '请求参数不正确,请检查填写内容',
|
||||
'Record Not Found': '记录不存在',
|
||||
'Permission Denied': '无权访问该记录',
|
||||
};
|
||||
|
||||
// 优先保留后端提供的具体中文信息,仅翻译已知的通用英文错误。
|
||||
@@ -31,9 +46,16 @@ 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 payload = (await response.json()) as {
|
||||
code?: number;
|
||||
message?: string;
|
||||
details?: T;
|
||||
};
|
||||
if (!response.ok || payload.code !== 0) {
|
||||
throw new Error(localizeApiErrorMessage(payload.message));
|
||||
throw new ApiError(
|
||||
localizeApiErrorMessage(payload.message),
|
||||
response.status,
|
||||
);
|
||||
}
|
||||
return payload.details as T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user