perf: 优化路由 rank,当rank 不存在时,根据顺序自动创建,首页路由永远在第一位

This commit is contained in:
xiaoxian521 2022-12-06 21:18:12 +08:00
parent 27056e7560
commit 4ccf200059
4 changed files with 18 additions and 14 deletions

View File

@ -32,7 +32,7 @@
"@ctrl/tinycolor": "^3.4.1", "@ctrl/tinycolor": "^3.4.1",
"@pureadmin/descriptions": "^1.1.0", "@pureadmin/descriptions": "^1.1.0",
"@pureadmin/table": "^1.8.3", "@pureadmin/table": "^1.8.3",
"@pureadmin/utils": "^1.7.4", "@pureadmin/utils": "^1.7.5",
"@vueuse/core": "^9.6.0", "@vueuse/core": "^9.6.0",
"@vueuse/motion": "2.0.0-beta.12", "@vueuse/motion": "2.0.0-beta.12",
"animate.css": "^4.1.1", "animate.css": "^4.1.1",

8
pnpm-lock.yaml generated
View File

@ -10,7 +10,7 @@ specifiers:
"@pureadmin/descriptions": ^1.1.0 "@pureadmin/descriptions": ^1.1.0
"@pureadmin/table": ^1.8.3 "@pureadmin/table": ^1.8.3
"@pureadmin/theme": ^2.4.0 "@pureadmin/theme": ^2.4.0
"@pureadmin/utils": ^1.7.4 "@pureadmin/utils": ^1.7.5
"@types/element-resize-detector": 1.1.3 "@types/element-resize-detector": 1.1.3
"@types/js-cookie": ^3.0.1 "@types/js-cookie": ^3.0.1
"@types/lodash": ^4.14.180 "@types/lodash": ^4.14.180
@ -91,7 +91,7 @@ dependencies:
"@ctrl/tinycolor": 3.4.1 "@ctrl/tinycolor": 3.4.1
"@pureadmin/descriptions": 1.1.1_element-plus@2.2.26 "@pureadmin/descriptions": 1.1.1_element-plus@2.2.26
"@pureadmin/table": 1.8.3_element-plus@2.2.26 "@pureadmin/table": 1.8.3_element-plus@2.2.26
"@pureadmin/utils": 1.7.4_aotapuqn7htzdjltsyimavekky "@pureadmin/utils": 1.7.5_aotapuqn7htzdjltsyimavekky
"@vueuse/core": 9.6.0_vue@3.2.45 "@vueuse/core": 9.6.0_vue@3.2.45
"@vueuse/motion": 2.0.0-beta.12_vue@3.2.45 "@vueuse/motion": 2.0.0-beta.12_vue@3.2.45
animate.css: 4.1.1 animate.css: 4.1.1
@ -1064,10 +1064,10 @@ packages:
string-hash: 1.1.3 string-hash: 1.1.3
dev: true dev: true
/@pureadmin/utils/1.7.4_aotapuqn7htzdjltsyimavekky: /@pureadmin/utils/1.7.5_aotapuqn7htzdjltsyimavekky:
resolution: resolution:
{ {
integrity: sha512-uJNHcb2sO7R2avALf+v4TGyuZtJix0Wpw/kMb6eO4C003ZQImuGGi9WlxHaOlESrMyFHZ1AjWm5AqLwJLnpVlw== integrity: sha512-WqjtvMD67egsIJuPYx9V9aaXG3iYOTMj48XPfYxBzWTuUSvGkEj9M+3P1IrgG6GZQlK8dCPqaog8B0vujLjYJA==
} }
peerDependencies: peerDependencies:
dayjs: "*" dayjs: "*"

View File

@ -4,6 +4,7 @@ export default {
meta: { meta: {
icon: "informationLine", icon: "informationLine",
title: "异常页面", title: "异常页面",
// showLink: false,
rank: 9 rank: 9
}, },
children: [ children: [

View File

@ -13,6 +13,7 @@ import { useTimeoutFn } from "@vueuse/core";
import { RouteConfigs } from "@/layout/types"; import { RouteConfigs } from "@/layout/types";
import { import {
isString, isString,
isAllEmpty,
storageSession, storageSession,
isIncludeAllChildren isIncludeAllChildren
} from "@pureadmin/utils"; } from "@pureadmin/utils";
@ -28,19 +29,21 @@ const modulesRoutes = import.meta.glob("/src/views/**/*.{vue,tsx}");
// 动态路由 // 动态路由
import { getAsyncRoutes } from "@/api/routes"; import { getAsyncRoutes } from "@/api/routes";
function handRank(ramk: number, name: string, path: string) {
return isAllEmpty(ramk) || (ramk === 0 && name !== "Home" && path !== "/")
? true
: false;
}
/** 按照路由中meta下的rank等级升序来排序路由 */ /** 按照路由中meta下的rank等级升序来排序路由 */
function ascending(arr: any[]) { function ascending(arr: any[]) {
arr.forEach(v => { arr.forEach((v, index) => {
if (v?.meta?.rank === null) v.meta.rank = undefined; // 当rank不存在时根据顺序自动创建首页路由永远在第一位
if (v?.meta?.rank === 0) { if (handRank(v?.meta?.rank, v.name, v.path)) v.meta.rank = index + 2;
if (v.name !== "Home" && v.path !== "/") {
console.warn("rank only the home page can be 0");
}
}
}); });
return arr.sort( return arr.sort(
(a: { meta: { rank: number } }, b: { meta: { rank: number } }) => { (a: { meta: { rank: number } }, b: { meta: { rank: number } }) => {
return a?.meta?.rank - b?.meta?.rank; return a?.meta.rank - b?.meta.rank;
} }
); );
} }
@ -252,7 +255,7 @@ function formatTwoStageRoutes(routesList: RouteRecordRaw[]) {
children: [] children: []
}); });
} else { } else {
newRoutesList[0].children.push({ ...v }); newRoutesList[0]?.children.push({ ...v });
} }
}); });
return newRoutesList; return newRoutesList;