修复菜单逻辑

This commit is contained in:
李志强 2025-11-04 10:57:32 +08:00
parent 34035fb007
commit 86c4060843
4 changed files with 95 additions and 49 deletions

View File

@ -24,34 +24,7 @@
</template>
</el-menu-item>
<template v-for="item in sortedMenuList" :key="item.path">
<el-menu-item
v-if="!item.children || item.children.length === 0"
:index="item.path"
>
<i :class="['icons', 'fa', item.icon]"></i>
<template #title>
<span>{{ item.label }}</span>
</template>
</el-menu-item>
<el-sub-menu
v-else
:index="item.path"
>
<template #title>
<i :class="['icons', 'fa', item.icon]"></i>
<span>{{ item.label }}</span>
</template>
<el-menu-item
v-for="subItem in item.children"
:key="subItem.path"
:index="subItem.path"
>
<i :class="['icons', 'fa', subItem.icon && typeof subItem.icon === 'string' ? subItem.icon.trim() : '']"></i>
<template #title>
<span>{{ subItem.label }}</span>
</template>
</el-menu-item>
</el-sub-menu>
<MenuTreeItem :menu="item" />
</template>
</el-menu>
</el-aside>
@ -62,6 +35,7 @@ import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useAllDataStore } from '@/stores';
import { getAllMenus } from '@/api/menu';
import MenuTreeItem from './MenuTreeItem.vue';
const emit = defineEmits(['menu-click']);
@ -130,42 +104,59 @@ const activeBgColor = computed(() => {
//
const transformMenuData = (menus) => {
//
const mappedMenus = menus.map(menu => ({
id: menu.id,
path: menu.path,
icon: menu.icon || 'fa-circle',
label: menu.name,
route: menu.path,
parentId: menu.parentId || 0,
order: menu.order || 0,
children: []
}));
//
const functionPageKeywords = ['/detail', '/add', '/edit', '/delete', '/create', '/update', '/category', '/tag'];
//
const isFunctionPage = (path) => {
if (!path) return false;
const lowerPath = path.toLowerCase();
return functionPageKeywords.some(keyword => lowerPath.includes(keyword));
};
//
// menu_type=1
const allMenus = menus
.filter(menu => {
// API
if (menu.menuType !== 1) return false;
//
if (isFunctionPage(menu.path)) return false;
return true;
})
.map(menu => ({
id: menu.id,
path: menu.path,
icon: menu.icon || 'fa-circle',
label: menu.name,
route: menu.path,
parentId: menu.parentId || 0,
order: menu.order || 0,
children: []
}));
//
const menuMap = new Map();
const rootMenus = [];
//
mappedMenus.forEach(menu => {
allMenus.forEach(menu => {
menuMap.set(menu.id, menu);
});
//
mappedMenus.forEach(menu => {
//
const rootMenus = [];
allMenus.forEach(menu => {
if (menu.parentId === 0) {
//
rootMenus.push(menu);
} else {
//
const parent = menuMap.get(menu.parentId);
if (parent) {
// children
if (!parent.children) {
parent.children = [];
}
parent.children.push(menu);
} else {
//
rootMenus.push(menu);
}
// menu_type=2
}
});

View File

@ -0,0 +1,35 @@
<template>
<el-menu-item
v-if="!menu.children || menu.children.length === 0"
:index="menu.path"
>
<i :class="['icons', 'fa', menu.icon || 'fa-circle']"></i>
<template #title>
<span>{{ menu.label }}</span>
</template>
</el-menu-item>
<el-sub-menu
v-else
:index="menu.path"
>
<template #title>
<i :class="['icons', 'fa', menu.icon || 'fa-circle']"></i>
<span>{{ menu.label }}</span>
</template>
<MenuTreeItem
v-for="child in menu.children"
:key="child.path"
:menu="child"
/>
</el-sub-menu>
</template>
<script setup>
defineProps({
menu: {
type: Object,
required: true
}
});
</script>

View File

@ -0,0 +1,7 @@
<template>
<div>员工管理</div>
</template>
<script setup></script>
<style lang="scss" scoped></style>

View File

@ -0,0 +1,13 @@
<template>
<div>
</div>
</template>
<script setup>
</script>
<style lang="scss" scoped>
</style>