fix(platform-admin): 中文化账户参数校验提示
This commit is contained in:
@@ -5,6 +5,17 @@ export const tokenStorageKey = 'token';
|
||||
|
||||
export type PageResult<T> = { total: number; list: T[] };
|
||||
|
||||
// 将后端 SDK 的通用英文错误转换为面向用户的中文提示。
|
||||
const API_ERROR_MESSAGES: Record<string, string> = {
|
||||
'Invalid Argument': '请求参数不正确,请检查填写内容',
|
||||
};
|
||||
|
||||
// 优先保留后端提供的具体中文信息,仅翻译已知的通用英文错误。
|
||||
function localizeApiErrorMessage(message?: string) {
|
||||
if (!message) return '请求失败';
|
||||
return API_ERROR_MESSAGES[message.trim()] ?? message;
|
||||
}
|
||||
|
||||
export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const token = localStorage.getItem(tokenStorageKey);
|
||||
let response: Response;
|
||||
@@ -21,6 +32,8 @@ export async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
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 || '请求失败');
|
||||
if (!response.ok || payload.code !== 0) {
|
||||
throw new Error(localizeApiErrorMessage(payload.message));
|
||||
}
|
||||
return payload.details as T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user