feat: 优化系统菜单和权限管理页

This commit is contained in:
2026-08-19 17:26:18 +08:00
parent 5de538c8f4
commit 22a5300e55
13 changed files with 314 additions and 128 deletions
@@ -1,8 +1,8 @@
<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { MenuVO } from '#/api/sys/menu/types';
import type { MenuDTO, MenuVO } from '#/api/sys/menu/types';
import { onMounted, ref } from 'vue';
import { ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
@@ -124,14 +124,17 @@ const gridOptions: VxeGridProps<any> = {
border: true,
stripe: true,
rowConfig: {
useKey: true,
isHover: true,
drag: true, // 启用行拖拽
drag: true,
height: 52,
},
showOverflow: true,
size: 'small',
rowDragConfig: {
isPeerDrag: true, // 启用同层级拖拽限制
isCrossDrag: true,
isToChildDrag: true,
isSelfToChildDrag: true,
},
toolbarConfig: {
custom: true,
@@ -317,38 +320,50 @@ function handleModalSuccess(
// 防重复调用标志
let isUpdating = false;
// 处理拖拽排序
const handleDragSort = async (_params: any) => {
// 防止重复调用
/** 从树节点递归收集 parentId + 同级 order(VXE tree 的 fullData 是根节点树) */
function collectMenuSortItems(
nodes: any[],
parentId: null | number = null,
): MenuDTO.MenuSortItem[] {
const items: MenuDTO.MenuSortItem[] = [];
(nodes || []).forEach((node, index) => {
if (node?.id == null) {
return;
}
items.push({
menuId: node.id,
parentId,
order: index + 1,
});
const children = node.children;
if (Array.isArray(children) && children.length > 0) {
items.push(...collectMenuSortItems(children, node.id));
}
});
return items;
}
// 处理拖拽排序/跨父移动
const handleDragSort = async () => {
if (isUpdating) {
return;
}
try {
isUpdating = true;
const vxeGrid = (gridApi as any).grid;
if (!vxeGrid) return;
const treeRoots = vxeGrid.getTableData?.()?.fullData || [];
if (!treeRoots.length) return;
// 获取当前表格数据
const tableDataResult = gridApi.grid.getTableData();
const tableData = tableDataResult?.tableData || [];
const sortItems = collectMenuSortItems(treeRoots);
if (!sortItems.length) return;
// 构建排序数据
const sortItems = tableData.map((item: MenuVO.MenuInfo, index: number) => ({
menuId: item.id,
order: index + 1, // 排序值从1开始
}));
// 调用排序接口
await batchSortMenuApi({ sortItems });
// 排序成功后不需要重新加载数据,因为表格数据已经更新
message.success('已保存排序');
} catch (error: any) {
console.error('拖拽排序失败:', error);
if (error?.message) {
message.error(error.message);
} else {
message.error('排序更新失败');
}
// 排序失败时重新加载数据,恢复原始状态
message.error(error?.message || '排序更新失败');
gridApi.reload();
} finally {
isUpdating = false;
@@ -399,44 +414,21 @@ function getMenuTypeColor(type: string) {
return colorMap[type] || 'default';
}
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
showSearchForm: false,
tableTitle: '菜单管理',
gridEvents: {
// 拖拽排序事件
rowDragend: handleDragSort,
cellMenu: onCellMenu,
} as any,
});
// 添加Grid组件的ref
const gridRef = ref();
// 在组件挂载后绑定拖拽事件
onMounted(() => {
// 延迟绑定,确保Grid组件完全初始化
setTimeout(() => {
if (gridRef.value) {
// 尝试直接绑定VxeTable的拖拽事件
const vxeGrid = gridRef.value;
if (vxeGrid && vxeGrid.$refs && vxeGrid.$refs.grid) {
const grid = vxeGrid.$refs.grid;
// 绑定拖拽事件
grid.on('row-drag-end', (params: any) => {
handleDragSort(params);
});
}
}
}, 1000);
});
</script>
<template>
<Page title="菜单管理" auto-content-height>
<!-- VbenVxeGrid 表格 -->
<Grid ref="gridRef" :loading="loading" class="menu-grid">
<Grid :loading="loading" class="menu-grid">
<!-- 工具栏工具 -->
<template #toolbar-tools>
<Space>
@@ -453,7 +445,8 @@ onMounted(() => {
全部收起
</Button>
<div class="text-sm text-gray-500">
💡 可拖拽菜单名称列排序;右键行可编辑 / 增下级 / 删除
支持跨父级拖拽(按住 Ctrl
可拖成目标子节点);右键行可编辑 / 增下级 / 删除
</div>
</Space>
</template>