diff --git a/src/layout/components/TagsView/index.vue b/src/layout/components/TagsView/index.vue index 50a695d..bf014d2 100644 --- a/src/layout/components/TagsView/index.vue +++ b/src/layout/components/TagsView/index.vue @@ -385,7 +385,8 @@ // 关闭全部 const closeAll = () => { tabsViewStore.closeAllTabs(); - router.replace(PageEnum.BASE_HOME_REDIRECT); + router.replace(storage.get('FIRST-PATH')); + // router.replace(PageEnum.BASE_HOME_REDIRECT); updateNavScroll(); }; diff --git a/src/router/generator-routers.ts b/src/router/generator-routers.ts index a9dcbe0..3407eaf 100644 --- a/src/router/generator-routers.ts +++ b/src/router/generator-routers.ts @@ -164,3 +164,10 @@ export const dynamicImport = ( return; } }; + +/** + * 查找第一个路由 + * */ +export const findFirstRoutePath = (routes)=>{ + return routes[0].redirect?routes[0].redirect:routes[0].path +} diff --git a/src/router/router-guards.ts b/src/router/router-guards.ts index 59698b7..d91e878 100644 --- a/src/router/router-guards.ts +++ b/src/router/router-guards.ts @@ -6,6 +6,8 @@ import { ACCESS_TOKEN } from '@/store/mutation-types'; import { storage } from '@/utils/Storage'; import { PageEnum } from '@/enums/pageEnum'; import { ErrorPageRoute,AboutPageRoute } from '@/router/base'; +import { findFirstRoutePath } from '@/router/generator-routers'; +import {findTreeByPath} from "@/utils/auth"; import NProgress from 'nprogress'; const LOGIN_PATH = PageEnum.BASE_LOGIN; @@ -17,10 +19,6 @@ export function createRouterGuards(router: Router) { const asyncRouteStore = useAsyncRouteStoreWidthOut(); router.beforeEach(async (to, from, next) => { NProgress.start(); - if (from.path === LOGIN_PATH && to.name === 'errorPage') { - next(PageEnum.BASE_HOME); - return; - } // Whitelist can be directly entered if (whitePathList.includes(to.path as PageEnum)) { @@ -64,15 +62,20 @@ export function createRouterGuards(router: Router) { routes.forEach((item) => { router.addRoute(item as unknown as RouteRecordRaw); }); - //添加404 const isErrorPage = router.getRoutes().findIndex((item) => item.name === ErrorPageRoute.name); if (isErrorPage === -1) { router.addRoute(ErrorPageRoute as unknown as RouteRecordRaw); } router.addRoute(AboutPageRoute as unknown as RouteRecordRaw); - - const redirectPath = (from.query.redirect || to.path) as string; + const firstRoutePath = findFirstRoutePath(routes) + storage.set('FIRST-PATH',firstRoutePath) + if (from.path === LOGIN_PATH && (to.name === 'errorPage' || !to.name)) { + next(firstRoutePath); + return; + } + let formPath = findTreeByPath(routes,from.query.redirect).length > 0 ? from.query.redirect:to.path + const redirectPath = (formPath) as string; const redirect = decodeURIComponent(redirectPath); const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }; asyncRouteStore.setDynamicAddedRoute(true); diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 2366f58..3fa86d2 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -175,6 +175,20 @@ export const buildTree =(array)=> { } }); + return result; + } +/*** + * 递归查找 + */ +export const findTreeByPath =(tree, path, result = []) =>{ + for (const node of tree) { + if (node.path === path) { + result.push(node); + } + if (node.children && node.children.length > 0) { + findTreeByPath(node.children, path, result); + } + } return result; } export const getComponentName = (route: RouteLocationNormalized) => {