fix: 修复登录失败提示错误

This commit is contained in:
2026-05-20 11:30:37 +08:00
parent 90023663fd
commit 3995358095
3 changed files with 36 additions and 5 deletions
-1
View File
@@ -194,7 +194,6 @@ watch(
setUnauthorizedHandler(() => {
token.value = '';
message.warning('登录已过期');
});
async function handleLogin(payload: { username: string; password: string }) {
+29 -4
View File
@@ -50,16 +50,24 @@ export async function apiPost<T>(url: string, body?: unknown): Promise<T> {
export async function request<T>(url: string, init: RequestInit = {}): Promise<T> {
const headers = new Headers(init.headers);
const token = getToken();
const token = isLoginRequest(url) ? '' : getToken();
if (token) {
headers.set('Authorization', `Bearer ${token}`);
}
const response = await fetch(apiUrl(url), { ...init, headers });
if (response.status === 401) {
if (isLoginRequest(url)) {
const error = await readError(response);
throw new ApiRequestError(error.message, response.status, error.detail);
}
clearToken();
onUnauthorized?.();
throw new Error('登录已过期,请重新登录');
throw new ApiRequestError('登录已过期,请重新登录', response.status, {
code: 'UNAUTHORIZED',
message: '登录已过期,请重新登录'
});
}
if (!response.ok) {
@@ -94,7 +102,12 @@ export function upload<T>(
if (xhr.status === 401) {
clearToken();
onUnauthorized?.();
reject(new Error('登录已过期,请重新登录'));
reject(
new ApiRequestError('登录已过期,请重新登录', xhr.status, {
code: 'UNAUTHORIZED',
message: '登录已过期,请重新登录'
})
);
return;
}
@@ -143,7 +156,10 @@ export async function downloadGet(url: string, fallbackFilename: string): Promis
if (response.status === 401) {
clearToken();
onUnauthorized?.();
throw new Error('登录已过期,请重新登录');
throw new ApiRequestError('登录已过期,请重新登录', response.status, {
code: 'UNAUTHORIZED',
message: '登录已过期,请重新登录'
});
}
if (!response.ok) {
@@ -215,3 +231,12 @@ function apiUrl(url: string): string {
}
return `${API_BASE}${url.startsWith('/') ? url : `/${url}`}`;
}
function isLoginRequest(url: string): boolean {
try {
const parsed = new URL(url, window.location.origin);
return parsed.pathname === '/api/login';
} catch {
return url.replace(/^https?:\/\/[^/]+/i, '').split('?')[0] === '/api/login';
}
}