From c84c37d1a6a36f8500c3f17e1a7690cc29fa4555 Mon Sep 17 00:00:00 2001 From: Soybean <2570172956@qq.com> Date: Thu, 16 Sep 2021 14:17:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor(auth):=20=E9=A1=B5=E9=9D=A2=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/permission.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/router/permission.ts b/src/router/permission.ts index d1d23884..7849c140 100644 --- a/src/router/permission.ts +++ b/src/router/permission.ts @@ -24,33 +24,37 @@ export default function createRouterGuide(router: Router) { function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) { const token = getToken(); + const isLogin = Boolean(token); + const needLogin = Boolean(to.meta?.requiresAuth); + const routerAction: [boolean, () => void][] = [ // 已登录状态跳转登录页,跳转至首页 [ - to.name === RouteNameMap.get('login') && Boolean(token), + isLogin && to.name === RouteNameMap.get('login'), () => { next({ name: RouteNameMap.get('root') }); } ], - // 不需要权限的页面直接通行 + // 不需要登录权限的页面直接通行 [ - !to.meta?.requiresAuth, + !needLogin, () => { next(); } ], - // 需要权限的页面 + // 未登录状态进入需要登录权限的页面 [ - Boolean(to.meta?.requiresAuth), + !isLogin && needLogin, () => { - if (token) { - // 有权限直接通行 - next(); - } else { - // 没有权限,跳转至登录页 - const redirectUrl = window.location.href; - next({ name: RouteNameMap.get('login'), query: { redirectUrl } }); - } + const redirectUrl = window.location.href; + next({ name: RouteNameMap.get('login'), query: { redirectUrl } }); + } + ], + // 登录状态进入需要登录权限的页面,直接通行 + [ + needLogin && isLogin, + () => { + next(); } ] ];