fix: 修改路由功能
This commit is contained in:
@@ -22,18 +22,34 @@ function setupCommonGuard(router: Router) {
|
||||
router.beforeEach(async (to) => {
|
||||
const accountStore = useAccountStore()
|
||||
|
||||
// 首次导航时验证 token 有效性(检查持久化的 token 是否过期)
|
||||
// await accountStore.initAuth()
|
||||
|
||||
// 已登录用户访问公开路由(如登录页),直接跳转首页
|
||||
if (accountStore.isLoggedIn() && isPublicPath(to.path)) {
|
||||
if (!!accountStore.accessToken && isPublicPath(to.path)) {
|
||||
return '/'
|
||||
}
|
||||
|
||||
// 未登录用户访问非公开路由,跳转登录页
|
||||
if (!accountStore.isLoggedIn() && !isPublicPath(to.path)) {
|
||||
// 将原本要访问的路径作为 redirect 参数传递,登录后可跳回
|
||||
return { path: '/auth/login', query: { redirect: to.fullPath } }
|
||||
// accessToken 检查
|
||||
if (!accountStore.accessToken) {
|
||||
// 明确声明忽略权限访问权限,则可以访问
|
||||
if (to.meta.ignoreAccess) {
|
||||
return true
|
||||
}
|
||||
|
||||
// 未登录用户访问非公开路由,跳转登录页
|
||||
if (!isPublicPath(to.path)) {
|
||||
// 将原本要访问的路径作为 redirect 参数传递,登录后可跳回
|
||||
return { path: '/auth/login', query: { redirect: to.fullPath }, replace: true }
|
||||
}
|
||||
|
||||
// 没有访问权限,跳转登录页面
|
||||
if (to.fullPath !== '/auth/login') {
|
||||
return {
|
||||
path: '/auth/login',
|
||||
query: { redirect: to.fullPath },
|
||||
// 携带当前跳转的页面,登录后重新跳转该页面
|
||||
replace: true
|
||||
}
|
||||
}
|
||||
return to
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user