fix: 优化认证异常提示

This commit is contained in:
hh
2026-07-13 16:34:27 +08:00
parent 826fc124e6
commit c5983abe35
+10
View File
@@ -48,6 +48,11 @@ const API_BASE = import.meta.env.VITE_API_BASE_URL || "";
const TOKEN_KEY = "airshelf_token";
const REMEMBER_KEY = "airshelf_remember"; // { username, expireAt } · 勾选「记住我 7 天」时写
const REMEMBER_DAYS = 7;
const AUTH_MISSING_MESSAGES = new Set([
"身份认证信息未提供。",
"Authentication credentials were not provided."
]);
const FRIENDLY_AUTH_MISSING_MESSAGE = "登录状态异常,请刷新页面后重新登录;若仍无法操作,请联系管理员。";
export type RememberInfo = { username: string; expireAt: number };
@@ -122,6 +127,11 @@ async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
// 非 JSON(如 DEBUG 的整页 HTML 报错栈):绝不把整页怼进 toast(会撑爆 / 白屏),只给状态行
message = `服务器错误 ${response.status} ${response.statusText}`.trim();
}
// 后端默认的“身份认证信息未提供”只描述技术原因,用户无法判断该怎么处理。
// 仅翻译这一种“根本没有携带凭证”的响应;无效 token 等其他认证错误仍保留原始提示,方便区分。
if (response.status === 401 && AUTH_MISSING_MESSAGES.has(message)) {
message = FRIENDLY_AUTH_MISSING_MESSAGE;
}
throw new ApiError(response.status, message);
}
if (response.status === 204) return undefined as T;