mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
fix(projects): 修复登录的重定向地址
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import type { Router } from 'vue-router';
|
||||
import type { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
|
||||
import { useTitle } from '@vueuse/core';
|
||||
import { getToken, getLoginRedirectUrl } from '@/utils';
|
||||
import { RouteNameMap, whitelistRoutes } from './routes';
|
||||
import { getToken } from '@/utils';
|
||||
import { RouteNameMap } from './routes';
|
||||
|
||||
/**
|
||||
* 路由守卫函数
|
||||
@ -9,22 +9,10 @@ import { RouteNameMap, whitelistRoutes } from './routes';
|
||||
*/
|
||||
export default function createRouterGuide(router: Router) {
|
||||
router.beforeEach((to, from, next) => {
|
||||
// 开始 loadingBar
|
||||
window.$loadingBar?.start();
|
||||
const token = getToken();
|
||||
if (whitelistRoutes.includes(to.name as string)) {
|
||||
if (to.name === RouteNameMap.get('login') && token) {
|
||||
next('/');
|
||||
return;
|
||||
}
|
||||
next();
|
||||
return;
|
||||
}
|
||||
if (token) {
|
||||
next();
|
||||
} else {
|
||||
const redirectUrl = getLoginRedirectUrl();
|
||||
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
|
||||
}
|
||||
// 页面跳转逻辑
|
||||
handleRouterAction(to, from, next);
|
||||
});
|
||||
router.afterEach(to => {
|
||||
// 设置document title
|
||||
@ -33,3 +21,45 @@ export default function createRouterGuide(router: Router) {
|
||||
window.$loadingBar?.finish();
|
||||
});
|
||||
}
|
||||
|
||||
function handleRouterAction(to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) {
|
||||
const token = getToken();
|
||||
const routerAction: [boolean, () => void][] = [
|
||||
// 已登录状态跳转登录页,跳转至首页
|
||||
[
|
||||
to.name === RouteNameMap.get('login') && Boolean(token),
|
||||
() => {
|
||||
next({ name: RouteNameMap.get('root') });
|
||||
}
|
||||
],
|
||||
// 不需要权限的页面直接通行
|
||||
[
|
||||
!to.meta?.requiresAuth,
|
||||
() => {
|
||||
next();
|
||||
}
|
||||
],
|
||||
// 需要权限的页面
|
||||
[
|
||||
Boolean(to.meta?.requiresAuth),
|
||||
() => {
|
||||
if (token) {
|
||||
// 有权限直接通行
|
||||
next();
|
||||
} else {
|
||||
// 没有权限,跳转至登录页
|
||||
const redirectUrl = window.location.href;
|
||||
next({ name: RouteNameMap.get('login'), query: { redirectUrl } });
|
||||
}
|
||||
}
|
||||
]
|
||||
];
|
||||
|
||||
routerAction.some(item => {
|
||||
const flag = item[0];
|
||||
if (flag) {
|
||||
item[1]();
|
||||
}
|
||||
return flag;
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user