From d6a329a63ceafb06997e1cede81815bc638e1962 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Thu, 18 Aug 2022 18:34:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E9=87=8D?= =?UTF-8?q?=E7=BD=AE=E8=B7=AF=E7=94=B1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock/asyncRoutes.ts | 4 ++++ src/api/routes.ts | 9 +++++++-- src/layout/hooks/useNav.ts | 6 +++++- src/router/index.ts | 13 +++++++++++++ src/router/utils.ts | 24 +++++++++++------------- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/mock/asyncRoutes.ts b/mock/asyncRoutes.ts index 763f381a2..7edd55230 100644 --- a/mock/asyncRoutes.ts +++ b/mock/asyncRoutes.ts @@ -4,6 +4,7 @@ import { MockMethod } from "vite-plugin-mock"; // http://mockjs.com/examples.html#Object const systemRouter = { path: "/system", + name: "User", redirect: "/system/user/index", meta: { icon: "setting", @@ -51,6 +52,7 @@ const systemRouter = { const permissionRouter = { path: "/permission", redirect: "/permission/page/index", + name: "PermissionPage", meta: { title: "menus.permission", icon: "lollipop", @@ -77,6 +79,7 @@ const permissionRouter = { const frameRouter = { path: "/iframe", + name: "FramePure", redirect: "/iframe/pure", meta: { icon: "monitor", @@ -113,6 +116,7 @@ const frameRouter = { const tabsRouter = { path: "/tabs", redirect: "/tabs/index", + name: "Tabs", meta: { icon: "IF-team-icontabs", title: "menus.hstabs", diff --git a/src/api/routes.ts b/src/api/routes.ts index 2c6dc6ae4..7da04fbee 100644 --- a/src/api/routes.ts +++ b/src/api/routes.ts @@ -1,5 +1,10 @@ import { http } from "../utils/http"; -export const getAsyncRoutes = (params?: object) => { - return http.request("get", "/getAsyncRoutes", { params }); +type Result = { + code: number; + info: Array; +}; + +export const getAsyncRoutes = (params?: object) => { + return http.request("get", "/getAsyncRoutes", { params }); }; diff --git a/src/layout/hooks/useNav.ts b/src/layout/hooks/useNav.ts index 3cfbcbbf6..920110836 100644 --- a/src/layout/hooks/useNav.ts +++ b/src/layout/hooks/useNav.ts @@ -4,11 +4,11 @@ import { getConfig } from "/@/config"; import { useRouter } from "vue-router"; import { emitter } from "/@/utils/mitt"; import { routeMetaType } from "../types"; -import { remainingPaths } from "/@/router"; import type { StorageConfigs } from "/#/index"; import { routerArrays } from "/@/layout/types"; import { transformI18n } from "/@/plugins/i18n"; import { useAppStoreHook } from "/@/store/modules/app"; +import { remainingPaths, resetRouter } from "/@/router"; import { i18nChangeLanguage } from "@wangeditor/editor"; import { storageSession, useGlobal } from "@pureadmin/utils"; import { useEpThemeStoreHook } from "/@/store/modules/epTheme"; @@ -72,6 +72,10 @@ export function useNav() { useMultiTagsStoreHook().handleTags("equal", [...routerArrays]); storageSession.removeItem("info"); router.push("/login"); + resetRouter(); + setTimeout(() => { + console.log("----", router.options.routes, router.getRoutes()); + }, 3000); } function backHome() { diff --git a/src/router/index.ts b/src/router/index.ts index 8b012330e..75e56900e 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -98,6 +98,19 @@ export const router: Router = createRouter({ } }); +// 重置路由 +export function resetRouter() { + router.getRoutes().forEach(route => { + const { name, meta } = route; + if (name && router.hasRoute(name) && meta?.backstage) { + router.removeRoute(name); + router.options.routes = formatTwoStageRoutes( + formatFlatteningRoutes(buildHierarchyTree(ascending(routes))) + ); + } + }); +} + // 路由白名单 const whiteList = ["/login"]; diff --git a/src/router/utils.ts b/src/router/utils.ts index a81cb3c82..926292605 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -104,14 +104,14 @@ function findRouteByPath(path: string, routes: RouteRecordRaw[]) { } } -// 重置路由 -function resetRouter(): void { - router.getRoutes().forEach(route => { - const { name } = route; - if (name) { - router.hasRoute(name) && router.removeRoute(name); - } - }); +function addPathMatch() { + if (!router.hasRoute("pathMatch")) { + router.addRoute({ + path: "/:pathMatch(.*)", + name: "pathMatch", + redirect: "/error/404" + }); + } } // 初始化路由 @@ -146,10 +146,7 @@ function initRouter(name: string) { ); usePermissionStoreHook().changeSetting(info); } - router.addRoute({ - path: "/:pathMatch(.*)", - redirect: "/error/404" - }); + addPathMatch(); }); }); } @@ -230,6 +227,8 @@ function addAsyncRoutes(arrRoutes: Array) { if (!arrRoutes || !arrRoutes.length) return; const modulesRoutesKeys = Object.keys(modulesRoutes); arrRoutes.forEach((v: RouteRecordRaw) => { + // 将backstage属性加入meta,标识此路由为后端返回路由 + v.meta.backstage = true; if (v.redirect) { v.component = Layout; } else if (v.meta?.frameSrc) { @@ -295,7 +294,6 @@ export { ascending, filterTree, initRouter, - resetRouter, hasPermissions, getHistoryMode, addAsyncRoutes,