From 434ab1c560b260f8a19895405eb1d3c3313052d7 Mon Sep 17 00:00:00 2001 From: Soybean Date: Mon, 6 Jun 2022 22:44:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(projects):=20=E5=8A=A8=E6=80=81=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E6=A0=B9=E8=B7=AF=E7=94=B1=E9=87=8D=E5=AE=9A=E5=90=91?= =?UTF-8?q?=E5=8F=AA=E9=9C=80=E5=8F=96=E5=86=B3=E4=BA=8E=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9A=84=E8=B7=AF=E7=94=B1=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ISSUES CLOSED: \ --- .env | 1 + src/router/guard/dynamic.ts | 8 ++++++++ src/router/routes/index.ts | 19 +++++++++++-------- src/store/modules/route/index.ts | 16 +++++++++++++++- src/utils/router/helpers.ts | 2 +- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.env b/.env index 3d62562f4..9daec0b65 100644 --- a/.env +++ b/.env @@ -9,4 +9,5 @@ VITE_APP_DESC=SoybeanAdmin是一个中后台管理系统模版 # 权限路由模式: static | dynamic VITE_AUTH_ROUTE_MODE=dynamic +# 路由首页(根路由重定向), 用于static模式的权限路由,dynamic模式取决于后端返回的路由首页 VITE_ROUTE_HOME_PATH=/dashboard/analysis diff --git a/src/router/guard/dynamic.ts b/src/router/guard/dynamic.ts index 08d0cabea..cecdbd391 100644 --- a/src/router/guard/dynamic.ts +++ b/src/router/guard/dynamic.ts @@ -31,6 +31,14 @@ export async function createDynamicRouteGuard( if (to.name === routeName('not-found-page')) { // 动态路由没有加载导致被not-found-page路由捕获,等待权限路由加载好了,回到之前的路由 + + // 若路由是从根路由重定向过来的,重新回到根路由 + const ROOT_ROUTE_NAME: AuthRoute.RouteKey = 'root'; + if (to.redirectedFrom?.name === ROOT_ROUTE_NAME) { + next({ path: '/', replace: true, query: to.query }); + return false; + } + next({ path: to.fullPath, replace: true, query: to.query }); return false; } diff --git a/src/router/routes/index.ts b/src/router/routes/index.ts index 00a0adc9c..2c86722eb 100644 --- a/src/router/routes/index.ts +++ b/src/router/routes/index.ts @@ -1,15 +1,18 @@ import { getLoginModuleRegExp } from '@/utils'; +/** 根路由: / */ +export const ROOT_ROUTE: AuthRoute.Route = { + name: 'root', + path: '/', + redirect: import.meta.env.VITE_ROUTE_HOME_PATH, + meta: { + title: 'Root' + } +}; + /** 固定的路由 */ export const constantRoutes: AuthRoute.Route[] = [ - { - name: 'root', - path: '/', - redirect: import.meta.env.VITE_ROUTE_HOME_PATH, - meta: { - title: 'Root' - } - }, + ROOT_ROUTE, { name: 'login', path: '/login', diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 326fe1452..5462f8d57 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -1,14 +1,16 @@ import { defineStore } from 'pinia'; -import { router, constantRoutes, routes as staticRoutes } from '@/router'; +import { router, ROOT_ROUTE, constantRoutes, routes as staticRoutes } from '@/router'; import { fetchUserRoutes } from '@/service'; import { getUserInfo, transformAuthRouteToMenu, transformAuthRoutesToVueRoutes, + transformAuthRouteToVueRoute, transformAuthRoutesToSearchMenus, getCacheRoutes, filterAuthRoutesByUserPermission, transformRoutePathToRouteName, + transformRouteNameToRoutePath, getConstantRouteNames } from '@/utils'; import { useAuthStore } from '../auth'; @@ -75,12 +77,24 @@ export const useRouteStore = defineStore('route-store', { this.cacheRoutes = getCacheRoutes(vueRoutes); }, + /** 动态路由模式下:更新根路由的重定向 */ + handleUpdateRootRedirect(routeKey: AuthRoute.RouteKey) { + if (routeKey === 'root' || routeKey === 'not-found-page') { + throw Error('routeKey的值不能为root或者not-found-page'); + } + const rootRoute: AuthRoute.Route = { ...ROOT_ROUTE, redirect: transformRouteNameToRoutePath(routeKey) }; + const rootRouteName: AuthRoute.RouteKey = 'root'; + router.removeRoute(rootRouteName); + const rootVueRoute = transformAuthRouteToVueRoute(rootRoute)[0]; + router.addRoute(rootVueRoute); + }, /** 初始化动态路由 */ async initDynamicRoute() { const { userId } = getUserInfo(); const { data } = await fetchUserRoutes(userId); if (data) { this.routeHomeName = data.home; + this.handleUpdateRootRedirect(data.home); this.handleAuthRoutes(data.routes); } }, diff --git a/src/utils/router/helpers.ts b/src/utils/router/helpers.ts index c9d608e9e..3f536d624 100644 --- a/src/utils/router/helpers.ts +++ b/src/utils/router/helpers.ts @@ -82,7 +82,7 @@ type ComponentAction = Record void>; * 将单个权限路由转换成vue路由 * @param item - 单个权限路由 */ -function transformAuthRouteToVueRoute(item: AuthRoute.Route) { +export function transformAuthRouteToVueRoute(item: AuthRoute.Route) { const resultRoute: RouteRecordRaw[] = []; const itemRoute = { ...item } as RouteRecordRaw;