update 优化token失效后跳转登录页的逻辑

This commit is contained in:
zk
2024-05-20 11:14:09 +08:00
parent 6ff0b25f1b
commit 60be7c4fe2
3 changed files with 23 additions and 4 deletions
+5 -1
View File
@@ -6,7 +6,7 @@ import { TOKEN_KEY } from '/@/enum/cacheEnum.ts';
import { useMessage } from '/@/hooks/web/useMessage.ts';
import { checkStatus } from '/@/api/checkStatus.ts';
import router from '/@/router';
import { getBaseLogin } from '/@/enum/pageEnum.ts';
import { getBaseLogin, toLoginByNowPath } from '/@/enum/pageEnum.ts';
const { createMessage, createErrorModal } = useMessage();
const { VITE_GLOB_API_URL } = getAppEnvConfig();
@@ -34,6 +34,10 @@ service.interceptors.response.use(
switch (error.response.data.code) {
case 401:
let path = getBaseLogin();
if (!path) {
path = toLoginByNowPath(router.currentRoute.value.path);
}
createMessage.warn(error.response.data.message);
router.replace({
path: path,
});
+15 -2
View File
@@ -7,9 +7,22 @@ export enum PageEnum {
}
export function setBaseLogin(baseLogin) {
sessionStorage.setItem('baseLogin', baseLogin);
localStorage.setItem('baseLogin', baseLogin);
}
export function getBaseLogin() {
return sessionStorage.getItem('baseLogin');
return localStorage.getItem('baseLogin');
}
//根据当前路径跳转登录页
export function toLoginByNowPath(nowPath: string) {
//西安的页面路径
const xianPath = ['/home', '/secondScreen'];
//银川的页面路径
const yinchuanPath = ['/yinChuanHome'];
if (xianPath.includes(nowPath)) {
return PageEnum.BASE_LOGIN;
} else if (yinchuanPath.includes(nowPath)) {
return PageEnum.BASE_LOGIN_YINCHUAN;
}
}
@@ -60,7 +60,9 @@
function setList(data = {}) {
list.value.forEach((item) => {
const { key } = item;
item.value = data[key];
if (data && data.hasOwnProperty(key)) {
item.value = data[key];
}
});
}