修改菜单管理页面,角色管理页面,修改后台返回的路由数据适配项目

This commit is contained in:
2025-02-17 18:31:33 +08:00
parent 96290b97da
commit 964aab217d
9 changed files with 382 additions and 209 deletions

View File

@@ -4,6 +4,7 @@ import { store } from "@/store";
import router from "@/router";
import MenuAPI, { type RouteVO } from "@/api/account/menu";
const isTest = true//是否是本地调试
const modules = import.meta.glob("../../views/**/**.vue");
const Layout = () => import("@/layout/index.vue");
@@ -16,6 +17,8 @@ export const usePermissionStore = defineStore("permission", () => {
// 路由是否加载完成
const isRoutesLoaded = ref(false);
/**
* 获取后台动态路由数据,解析并注册到全局路由
*
@@ -25,12 +28,18 @@ export const usePermissionStore = defineStore("permission", () => {
return new Promise<RouteRecordRaw[]>((resolve, reject) => {
MenuAPI.getRoutes()
.then((data) => {
// const dynamicRoutes = parseDynamicRoutes(data);
// routes.value = [...constantRoutes, ...dynamicRoutes];
// isRoutesLoaded.value = true;
// resolve(dynamicRoutes);
isRoutesLoaded.value = true;
resolve(constantRoutes);
if (!isTest) {
const dynamicRoutes = parseDynamicRoutes(data.filter(v => v.type == 0));
routes.value = [...constantRoutes, ...dynamicRoutes];
console.log(routes.value);
isRoutesLoaded.value = true;
resolve(dynamicRoutes);
} else {
isRoutesLoaded.value = true;
resolve(constantRoutes);
}
})
.catch((error) => {
reject(error);
@@ -55,7 +64,9 @@ export const usePermissionStore = defineStore("permission", () => {
*/
const resetRouter = () => {
// 清空本地存储的路由和菜单数据
// routes.value = [];
if (!isTest) {
routes.value = [];
}
routes.value = constantRoutes;
mixedLayoutLeftRoutes.value = [];
// 从 Vue Router 中移除所有动态注册的路由
@@ -90,21 +101,33 @@ export const usePermissionStore = defineStore("permission", () => {
*/
const parseDynamicRoutes = (rawRoutes: RouteVO[]): RouteRecordRaw[] => {
const parsedRoutes: RouteRecordRaw[] = [];
rawRoutes.forEach((route) => {
const normalizedRoute = { ...route } as RouteRecordRaw;
console.log(route.path)
const normalizedRoute = {
path: route.path,
meta: {
title: route.title,
icon: route.icon,
keepAlive: route.cache,
alwaysShow: route.path && route.path.startsWith('/') ? true : false,
hidden: route.hidden,
},
children: route.children,
component: route.component,
} as RouteRecordRaw;
// 处理组件路径
normalizedRoute.component =
normalizedRoute.component?.toString() === "Layout"
!normalizedRoute.component
? Layout
: modules[`../../views/${normalizedRoute.component}.vue`] ||
modules["../../views/error-page/404.vue"];
modules["../../views/error-page/404.vue"];
// 递归解析子路由
if (normalizedRoute.children) {
// normalizedRoute.redirect = (!normalizedRoute.redirect && route.children.length <= 1) ? normalizedRoute.children[0].path : normalizedRoute.path
normalizedRoute.children = parseDynamicRoutes(route.children);
}
console.log(normalizedRoute)
parsedRoutes.push(normalizedRoute);
});