fix(api): 处理401错误时重定向到登录页
docs(guide): 更新免费计划适用场景的描述
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user