Skip to content

Commit 40701a9

Browse files
author
CodeJudge
committed
feat: 401自动跳转登录页
1 parent ac63569 commit 40701a9

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

frontend/src/services/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ async function request<T>(url: string, options: RequestInit = {}): Promise<T> {
1515
const res = await fetch(`${BASE}${url}`, { ...options, headers });
1616

1717
if (!res.ok) {
18+
if (res.status === 401 && !url.includes('/auth/login') && !url.includes('/auth/register')) {
19+
localStorage.removeItem('oj_token');
20+
window.location.href = '/login';
21+
throw new Error('登录已过期,请重新登录');
22+
}
1823
const err = await res.json().catch(() => ({ error: '网络错误' }));
1924
throw new Error(err.error || `请求失败 (${res.status})`);
2025
}

0 commit comments

Comments
 (0)