fix(api): 处理401错误时重定向到登录页

docs(guide): 更新免费计划适用场景的描述
This commit is contained in:
anghunk
2026-01-22 11:05:56 +08:00
parent 286bbaa827
commit ec7ec41c77
2 changed files with 15 additions and 1 deletions

View File

@@ -35,6 +35,20 @@ async function request<T>(method: HttpMethod, path: string, body?: unknown): Pro
}
if (!res.ok) {
const message = data && data.message ? data.message : `请求失败,状态码 ${res.status}`;
if (res.status === 401 && (message === 'Token expired or invalid' || message === 'Unauthorized')) {
localStorage.removeItem('cwd_admin_token');
if (typeof window !== 'undefined') {
try {
const url = new URL(window.location.href);
url.pathname = '/login';
url.search = '';
url.hash = '';
window.location.href = url.toString();
} catch {
window.location.href = '/login';
}
}
}
throw new Error(message);
}
return data as T;