mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-04-06 03:57:54 +08:00
feat(router): 去除router next参数
This commit is contained in:
parent
299d805aa6
commit
32be17e913
@ -5,7 +5,7 @@ import { usePermission } from '@/hooks'
|
|||||||
|
|
||||||
// 引入所有页面
|
// 引入所有页面
|
||||||
const modules = import.meta.glob('../../views/**/*.vue');
|
const modules = import.meta.glob('../../views/**/*.vue');
|
||||||
/* 将路由树转换成一维数组 */
|
/* 路由树转换成一维数组 */
|
||||||
function FlatAuthRoutes(routes: AppRoute.Route[]) {
|
function FlatAuthRoutes(routes: AppRoute.Route[]) {
|
||||||
let result: AppRoute.Route[] = [];
|
let result: AppRoute.Route[] = [];
|
||||||
// 浅拷贝一层去降维,否则影响原数组
|
// 浅拷贝一层去降维,否则影响原数组
|
||||||
@ -22,7 +22,7 @@ function FlatAuthRoutes(routes: AppRoute.Route[]) {
|
|||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
/* 路由无权限过滤 */
|
||||||
function filterPermissionRoutes(routes: AppRoute.Route[]) {
|
function filterPermissionRoutes(routes: AppRoute.Route[]) {
|
||||||
const { hasPermission } = usePermission();
|
const { hasPermission } = usePermission();
|
||||||
return routes.filter((route) => {
|
return routes.filter((route) => {
|
||||||
|
@ -5,7 +5,7 @@ import { useAppInfo } from '@/hooks';
|
|||||||
const { title } = useAppInfo();
|
const { title } = useAppInfo();
|
||||||
|
|
||||||
export function setupRouterGuard(router: Router) {
|
export function setupRouterGuard(router: Router) {
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from) => {
|
||||||
// 判断是否是外链,如果是直接打开网页并拦截跳转
|
// 判断是否是外链,如果是直接打开网页并拦截跳转
|
||||||
if (to.meta.herf) {
|
if (to.meta.herf) {
|
||||||
window.open(to.meta.herf);
|
window.open(to.meta.herf);
|
||||||
@ -14,7 +14,8 @@ export function setupRouterGuard(router: Router) {
|
|||||||
// 开始 loadingBar
|
// 开始 loadingBar
|
||||||
window.$loadingBar?.start();
|
window.$loadingBar?.start();
|
||||||
// 权限操作
|
// 权限操作
|
||||||
await createPermissionGuard(to, from, next);
|
await createPermissionGuard(to, from);
|
||||||
|
|
||||||
});
|
});
|
||||||
router.afterEach((to) => {
|
router.afterEach((to) => {
|
||||||
// 修改网页标题
|
// 修改网页标题
|
||||||
|
@ -5,48 +5,39 @@ import { useRouteStore, useTabStore } from '@/store';
|
|||||||
export async function createPermissionGuard(
|
export async function createPermissionGuard(
|
||||||
to: RouteLocationNormalized,
|
to: RouteLocationNormalized,
|
||||||
from: RouteLocationNormalized,
|
from: RouteLocationNormalized,
|
||||||
next: NavigationGuardNext,
|
|
||||||
) {
|
) {
|
||||||
|
console.log("🚀 ~ file: permission.ts:9 ~ to:", to)
|
||||||
|
|
||||||
const routeStore = useRouteStore();
|
const routeStore = useRouteStore();
|
||||||
const tabStore = useTabStore();
|
const tabStore = useTabStore();
|
||||||
|
|
||||||
// 判断有无TOKEN,登录鉴权
|
// 判断有无TOKEN,登录鉴权
|
||||||
const isLogin = Boolean(getToken());
|
const isLogin = Boolean(getToken());
|
||||||
|
|
||||||
if (!isLogin) {
|
if (!isLogin) {
|
||||||
if (to.name === 'login') {
|
const redirect = to.name === 'not-found' ? undefined : to.fullPath;
|
||||||
next();
|
return { path: '/login', query: { redirect } };
|
||||||
} else {
|
|
||||||
// login除了404,其余在登录后重定向到之前的网页
|
|
||||||
const redirect = to.name === 'not-found' ? undefined : to.fullPath;
|
|
||||||
next({ path: '/login', query: { redirect } });
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断路由有无进行初始化
|
// 判断路由有无进行初始化
|
||||||
if (!routeStore.isInitAuthRoute) {
|
if (!routeStore.isInitAuthRoute) {
|
||||||
await routeStore.initAuthRoute();
|
await routeStore.initAuthRoute();
|
||||||
// 动态路由加载完回到根路由
|
|
||||||
if (to.name === 'not-found') {
|
|
||||||
// 等待权限路由加载好了,回到之前的路由,否则404
|
|
||||||
const path = to.redirectedFrom?.fullPath;
|
|
||||||
next({ path, replace: true, query: to.query, hash: to.hash });
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 权限路由已经加载,仍然未找到,重定向到404
|
// 动态路由加载完回到根路由
|
||||||
if (to.name === 'not-found') {
|
if (to.name === 'not-found' && to.redirectedFrom) {
|
||||||
next({ name: 'not-found', replace: true });
|
// 等待权限路由加载好了,回到之前的路由,否则404
|
||||||
return false;
|
const path = to.redirectedFrom.fullPath;
|
||||||
|
return { path, replace: true, query: to.query, hash: to.hash };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断当前页是否在login,则定位去首页
|
// 判断当前页是否在login,则定位去首页
|
||||||
if (to.name === 'login') {
|
if (to.name === 'login') {
|
||||||
next({ path: '/appRoot' })
|
return { path: '/appRoot' }
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
// 权限路由已经加载,仍然未找到,重定向到404
|
||||||
|
if (to.name === 'not-found') {
|
||||||
|
return { name: 'not-found', replace: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置菜单高亮
|
// 设置菜单高亮
|
||||||
@ -60,5 +51,4 @@ export async function createPermissionGuard(
|
|||||||
tabStore.addTab(to);
|
tabStore.addTab(to);
|
||||||
// 设置高亮标签;
|
// 设置高亮标签;
|
||||||
tabStore.setCurrentTab(to.name as string);
|
tabStore.setCurrentTab(to.name as string);
|
||||||
next()
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user