diff --git a/src/router/guard/permission-guard.js b/src/router/guard/permission-guard.js index 76919ec..fefab3c 100644 --- a/src/router/guard/permission-guard.js +++ b/src/router/guard/permission-guard.js @@ -3,42 +3,40 @@ import { usePermissionStore } from '@/store/modules/permission' import { NOT_FOUND_ROUTE } from '@/router/routes' import { getToken, refreshAccessToken, removeToken } from '@/utils/token' import { toLogin } from '@/utils/auth' +import { isNullOrWhitespace } from '@/utils/is' const WHITE_LIST = ['/login', '/redirect'] export function createPermissionGuard(router) { const userStore = useUserStore() const permissionStore = usePermissionStore() - router.beforeEach(async (to, from, next) => { + router.beforeEach(async (to) => { const token = getToken() - if (token) { - if (to.path === '/login') { - next({ path: '/' }) - } else { - if (userStore.userId) { - // 已经拿到用户信息 - refreshAccessToken() - next() - } else { - await userStore.getUserInfo().catch((error) => { - removeToken() - toLogin() - $message.error(error.message || '获取用户信息失败!') - return - }) - const accessRoutes = permissionStore.generateRoutes(userStore.role) - accessRoutes.forEach((route) => { - !router.hasRoute(route.name) && router.addRoute(route) - }) - router.addRoute(NOT_FOUND_ROUTE) - next({ ...to, replace: true }) - } - } - } else { - if (WHITE_LIST.includes(to.path)) { - next() - } else { - next({ path: '/login', query: { ...to.query, redirect: to.path } }) - } + + /** 没有token的情况 */ + if (isNullOrWhitespace(token)) { + if (WHITE_LIST.includes(to.path)) return true + return { path: 'login', query: { ...to.query, redirect: to.path } } } + + /** 有token的情况 */ + + if (to.path === '/login') return { path: '/', replace: true } + + // 已经拿到用户信息 + if (userStore.userId) { + refreshAccessToken() + return true + } + await userStore.getUserInfo().catch((error) => { + removeToken() + toLogin() + $message.error(error.message || '获取用户信息失败!') + }) + const accessRoutes = permissionStore.generateRoutes(userStore.role) + accessRoutes.forEach((route) => { + !router.hasRoute(route.name) && router.addRoute(route) + }) + router.addRoute(NOT_FOUND_ROUTE) + return { ...to, replace: true } }) }