refactor(auth): 页面跳转逻辑优化
This commit is contained in:
parent
f97f226656
commit
c84c37d1a6
@ -24,33 +24,37 @@ export default function createRouterGuide(router: Router) {
|
|||||||
|
|
||||||
function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
|
function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
|
||||||
const token = getToken();
|
const token = getToken();
|
||||||
|
const isLogin = Boolean(token);
|
||||||
|
const needLogin = Boolean(to.meta?.requiresAuth);
|
||||||
|
|
||||||
const routerAction: [boolean, () => void][] = [
|
const routerAction: [boolean, () => void][] = [
|
||||||
// 已登录状态跳转登录页,跳转至首页
|
// 已登录状态跳转登录页,跳转至首页
|
||||||
[
|
[
|
||||||
to.name === RouteNameMap.get('login') && Boolean(token),
|
isLogin && to.name === RouteNameMap.get('login'),
|
||||||
() => {
|
() => {
|
||||||
next({ name: RouteNameMap.get('root') });
|
next({ name: RouteNameMap.get('root') });
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
// 不需要权限的页面直接通行
|
// 不需要登录权限的页面直接通行
|
||||||
[
|
[
|
||||||
!to.meta?.requiresAuth,
|
!needLogin,
|
||||||
() => {
|
() => {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
// 需要权限的页面
|
// 未登录状态进入需要登录权限的页面
|
||||||
[
|
[
|
||||||
Boolean(to.meta?.requiresAuth),
|
!isLogin && needLogin,
|
||||||
() => {
|
() => {
|
||||||
if (token) {
|
|
||||||
// 有权限直接通行
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
// 没有权限,跳转至登录页
|
|
||||||
const redirectUrl = window.location.href;
|
const redirectUrl = window.location.href;
|
||||||
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
|
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
// 登录状态进入需要登录权限的页面,直接通行
|
||||||
|
[
|
||||||
|
needLogin && isLogin,
|
||||||
|
() => {
|
||||||
|
next();
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
Loading…
Reference in New Issue
Block a user