登录失效跳转修复

This commit is contained in:
2026-08-12 10:47:18 +08:00
parent 10097f3003
commit d29075e9b4
11 changed files with 2151 additions and 759 deletions

View File

@@ -75,21 +75,6 @@ const router = createRouter({
routes,
});
const whiteList = ["/login"];
router.beforeEach((to) => {
const hasToken = Boolean(getToken());
if (!hasToken && !whiteList.includes(to.path)) {
return {
path: "/login",
query: { redirect: to.fullPath },
};
}
return true;
});
export function resetRouter() {
// 当前项目暂无动态 addRoute 路由,保留该方法兼容登出流程。
}

60
src/router/routerGuard.js Normal file
View File

@@ -0,0 +1,60 @@
import router from './index'
import { getToken } from '@/utils/auth'
import { useUserStore } from '@/store/modules/user.js'
const whiteList = ['/login'] // no redirect whitelist
// const setPageTitle = function (meta) {
// let title = '海灵标'
// if (meta && meta.title) {
// title = `${meta.title}`
// }
// document.title = title
// }
// 路由守卫
router.beforeEach(async (to, from, next) => {
$loadingBar.start()
// setPageTitle(to.meta)
const hasToken = getToken()
const userStore = useUserStore()
if (hasToken) {
if (to.path === '/login') {
next({ path: '/' })
$loadingBar.finish()
} else {
const hasGetUserInfo = userStore.name
if (hasGetUserInfo !== '') {
next()
} else {
try {
// get user info
const infoVO = await userStore.getInfo()
next({ ...to, replace: true })
} catch (error) {
await userStore.resetToken()
next(`/login?redirect=${to.path}`)
// remove token and go to login page to re-login
console.error(error)
$loadingBar.error()
}
}
}
} else {
/* has no token */
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
next()
} else {
next(`/login?redirect=${to.path}`)
$loadingBar.finish()
}
}
})
router.afterEach((to) => {
if (to.matched.length === 0) {
router.push('/404')
// fixme 不能自动跳转404, 需要引导
}
$loadingBar.finish()
})