完成代码,提交了再删除其他页面

This commit is contained in:
2026-04-22 14:27:44 +08:00
parent 1312131b1e
commit 188b396fea
12 changed files with 173 additions and 177 deletions

View File

@@ -34,6 +34,17 @@ const mockAdapter = createAlovaMockAdapter([...mocks], {
},
});
function redirectToLogin() {
const loginPath = PageEnum.BASE_LOGIN;
const { pathname, search, hash } = window.location;
const currentPath = `${pathname}${search}${hash}`;
const redirectPath =
pathname === loginPath ? loginPath : `${loginPath}?redirect=${encodeURIComponent(currentPath)}`;
storage.clear();
window.location.replace(redirectPath);
}
export const Alova = createAlova({
baseURL: apiUrl,
statesHook: VueHook,
@@ -59,7 +70,7 @@ export const Alova = createAlova({
const token = userStore.getToken;
// 添加 token 到请求头
if (!method.meta?.ignoreToken && token) {
method.config.headers['token'] = token;
method.config.headers['Authorization'] = `Bearer ${token}`;
}
// 处理 api 请求前缀
const isUrlStr = isUrl(method.url as string);
@@ -91,6 +102,12 @@ export const Alova = createAlova({
throw error;
}
const responseCode = Number(res?.code);
if (responseCode === ResultEnum.LOGIN_EXPIRED) {
redirectToLogin();
throw new Error(res?.message || '请先登录');
}
// 是否返回原生响应头 比如:需要获取响应头时使用该属性
if (method.meta?.isReturnNativeResponse) {
return res;