mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 09:49:20 +08:00
542 lines
13 KiB
Vue
542 lines
13 KiB
Vue
<script lang="ts" setup>
|
||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||
import type { MenuDTO, MenuVO } from '#/api/sys/menu/types';
|
||
|
||
import { ref } from 'vue';
|
||
|
||
import { Page } from '@vben/common-ui';
|
||
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
|
||
|
||
import { Button, message, Modal, Popconfirm, Space, Tag } from 'ant-design-vue';
|
||
|
||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||
import {
|
||
batchSortMenuApi,
|
||
deleteMenuApi,
|
||
getAllMenusApi,
|
||
} from '#/api/sys/menu';
|
||
import {
|
||
useVxeRowContextMenu,
|
||
VxeRowContextMenu,
|
||
} from '#/components/vxe-row-context-menu';
|
||
|
||
import MenuFormModal from './components/MenuFormModal.vue';
|
||
|
||
defineOptions({ name: 'MenuManagement' });
|
||
// 创建图标组件
|
||
const PlusIcon = createIconifyIcon('lucide:plus');
|
||
const EditIcon = createIconifyIcon('lucide:edit');
|
||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||
const PlusCircleIcon = createIconifyIcon('lucide:plus-circle');
|
||
const ExpandIcon = createIconifyIcon('lucide:chevrons-down');
|
||
const CollapseIcon = createIconifyIcon('lucide:chevrons-up');
|
||
|
||
// 响应式数据
|
||
const loading = ref(false);
|
||
const menuFormModalRef = ref();
|
||
|
||
/** 与 proxy query 返回的 `items` 同一数组引用,用于就地更新行,避免依赖 getTableData 结构差异导致误判走 reload */
|
||
const lastMenuFlatItems = ref<MenuVO.MenuInfo[]>([]);
|
||
|
||
function normalizeMenuParentId(v: unknown): number | null {
|
||
if (v === null || v === undefined) {
|
||
return null;
|
||
}
|
||
const n = Number(v);
|
||
return Number.isFinite(n) && n > 0 ? n : null;
|
||
}
|
||
|
||
function sameMenuParent(a: unknown, b: unknown) {
|
||
return normalizeMenuParentId(a) === normalizeMenuParentId(b);
|
||
}
|
||
|
||
// VbenVxeGrid 表格配置
|
||
const gridOptions: VxeGridProps<any> = {
|
||
columns: [
|
||
{
|
||
type: 'seq' as const,
|
||
minWidth: 60,
|
||
title: '序号',
|
||
},
|
||
{
|
||
field: 'name',
|
||
title: '菜单名称',
|
||
minWidth: 200,
|
||
showOverflow: 'tooltip' as const,
|
||
treeNode: true, // 启用树节点显示
|
||
dragSort: true, // 启用拖拽排序
|
||
// 移除自定义插槽,让拖拽控件正常显示
|
||
},
|
||
{
|
||
field: 'title',
|
||
title: '菜单标题',
|
||
minWidth: 150,
|
||
showOverflow: 'tooltip' as const,
|
||
},
|
||
{
|
||
field: 'icon',
|
||
title: '图标',
|
||
width: 72,
|
||
align: 'center' as const,
|
||
slots: { default: 'icon' },
|
||
},
|
||
{
|
||
field: 'path',
|
||
title: '路由路径',
|
||
minWidth: 200,
|
||
showOverflow: 'tooltip' as const,
|
||
},
|
||
{
|
||
field: 'component',
|
||
title: '页面组件',
|
||
minWidth: 180,
|
||
showOverflow: 'tooltip' as const,
|
||
},
|
||
{
|
||
title: '权限列表',
|
||
minWidth: 200,
|
||
slots: { default: 'permissions' },
|
||
},
|
||
{
|
||
title: '类型',
|
||
minWidth: 100,
|
||
slots: { default: 'menuType' },
|
||
},
|
||
{
|
||
title: '状态',
|
||
minWidth: 100,
|
||
slots: { default: 'menuStatus' },
|
||
},
|
||
{
|
||
field: 'createTime',
|
||
title: '创建时间',
|
||
minWidth: 160,
|
||
},
|
||
{
|
||
title: '操作',
|
||
minWidth: 280,
|
||
fixed: 'right' as const,
|
||
showOverflow: false,
|
||
slots: { default: 'action' },
|
||
},
|
||
],
|
||
height: 'auto',
|
||
border: true,
|
||
stripe: true,
|
||
rowConfig: {
|
||
useKey: true,
|
||
isHover: true,
|
||
drag: true,
|
||
height: 52,
|
||
},
|
||
showOverflow: true,
|
||
size: 'small',
|
||
rowDragConfig: {
|
||
isCrossDrag: true,
|
||
isToChildDrag: true,
|
||
isSelfToChildDrag: true,
|
||
},
|
||
toolbarConfig: {
|
||
custom: true,
|
||
refresh: true,
|
||
zoom: true,
|
||
export: true, // 启用导出功能
|
||
},
|
||
data: [],
|
||
treeConfig: {
|
||
transform: true,
|
||
rowField: 'id',
|
||
parentField: 'parentId',
|
||
},
|
||
// 启用 cell-menu 事件,但隐藏 Vxe 原生面板(由 Ant Design 二次封装接管)
|
||
menuConfig: {
|
||
enabled: true,
|
||
trigger: 'cell',
|
||
visibleMethod: () => false,
|
||
},
|
||
// 配置远程加载
|
||
proxyConfig: {
|
||
ajax: {
|
||
query: async () => {
|
||
try {
|
||
loading.value = true;
|
||
const data = await getAllMenusApi();
|
||
lastMenuFlatItems.value = data;
|
||
// 直接返回扁平化数据,让 vxe-table 自动构建树形结构
|
||
return {
|
||
items: data,
|
||
total: data.length,
|
||
};
|
||
} catch (error) {
|
||
console.error('获取菜单列表失败:', error);
|
||
message.error('获取菜单列表失败');
|
||
lastMenuFlatItems.value = [];
|
||
return {
|
||
items: [],
|
||
total: 0,
|
||
};
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
},
|
||
},
|
||
},
|
||
};
|
||
|
||
// 刷新表格数据
|
||
function refreshTable() {
|
||
if (gridApi) {
|
||
gridApi.reload();
|
||
}
|
||
}
|
||
|
||
function getGridMenuFlatFallback(): MenuVO.MenuInfo[] {
|
||
const grid = gridApi?.grid as any;
|
||
if (!grid?.getTableData) {
|
||
return [];
|
||
}
|
||
const td = grid.getTableData();
|
||
return (td?.fullData ?? td?.tableData ?? []) as MenuVO.MenuInfo[];
|
||
}
|
||
|
||
/** 就地合并更新后的菜单行,保持树展开状态;父级变更时需整表重建树结构 */
|
||
function patchMenuRowFromServer(menu: MenuVO.MenuInfo) {
|
||
if (!menu?.id) {
|
||
refreshTable();
|
||
return;
|
||
}
|
||
const idNum = Number(menu.id);
|
||
const list =
|
||
lastMenuFlatItems.value.length > 0
|
||
? lastMenuFlatItems.value
|
||
: getGridMenuFlatFallback();
|
||
const row = list.find((r) => Number(r.id) === idNum);
|
||
if (!row) {
|
||
refreshTable();
|
||
return;
|
||
}
|
||
if (!sameMenuParent(row.parentId, menu.parentId)) {
|
||
refreshTable();
|
||
return;
|
||
}
|
||
Object.assign(row, menu);
|
||
row.permissions = menu.permissions ? [...menu.permissions] : [];
|
||
}
|
||
|
||
// 处理添加菜单
|
||
function handleAdd() {
|
||
menuFormModalRef.value?.open(false);
|
||
}
|
||
|
||
// 处理添加下级菜单
|
||
function handleAddChild(record: MenuVO.MenuInfo) {
|
||
menuFormModalRef.value?.open(false, record);
|
||
}
|
||
|
||
// 处理编辑菜单
|
||
function handleEdit(record: MenuVO.MenuInfo) {
|
||
menuFormModalRef.value?.open(true, record);
|
||
}
|
||
|
||
// 处理删除菜单
|
||
async function handleDelete(record: MenuVO.MenuInfo) {
|
||
try {
|
||
await deleteMenuApi(record.id);
|
||
message.success('删除成功');
|
||
refreshTable();
|
||
} catch (error) {
|
||
console.error('删除菜单失败:', error);
|
||
message.error('删除菜单失败');
|
||
}
|
||
}
|
||
|
||
function confirmDeleteMenu(record: MenuVO.MenuInfo) {
|
||
Modal.confirm({
|
||
title: '确定要删除这个菜单吗?',
|
||
content: record.title || record.name || `ID ${record.id}`,
|
||
okText: '删除',
|
||
okType: 'danger',
|
||
cancelText: '取消',
|
||
onOk: () => handleDelete(record),
|
||
});
|
||
}
|
||
|
||
const {
|
||
open: ctxMenuOpen,
|
||
x: ctxMenuX,
|
||
y: ctxMenuY,
|
||
row: ctxMenuRow,
|
||
items: ctxMenuItems,
|
||
onCellMenu,
|
||
} = useVxeRowContextMenu<MenuVO.MenuInfo>((_row) => [
|
||
{
|
||
key: 'addChild',
|
||
label: '新增',
|
||
icon: PlusCircleIcon,
|
||
},
|
||
{
|
||
key: 'edit',
|
||
label: '编辑',
|
||
icon: EditIcon,
|
||
},
|
||
{
|
||
key: 'delete',
|
||
label: '删除',
|
||
icon: TrashIcon,
|
||
danger: true,
|
||
},
|
||
]);
|
||
|
||
function handleCtxMenuSelect(key: string) {
|
||
const row = ctxMenuRow.value;
|
||
if (!row) return;
|
||
switch (key) {
|
||
case 'addChild': {
|
||
handleAddChild(row);
|
||
break;
|
||
}
|
||
case 'edit': {
|
||
handleEdit(row);
|
||
break;
|
||
}
|
||
case 'delete': {
|
||
confirmDeleteMenu(row);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 处理模态框成功:更新菜单只合并当前行;新建仍整表刷新
|
||
function handleModalSuccess(
|
||
payload?: { mode: 'update'; menu: MenuVO.MenuInfo } | { mode: 'create' },
|
||
) {
|
||
if (payload?.mode === 'update' && payload.menu) {
|
||
patchMenuRowFromServer(payload.menu);
|
||
return;
|
||
}
|
||
refreshTable();
|
||
}
|
||
|
||
// 防重复调用标志
|
||
let isUpdating = false;
|
||
|
||
/** 从树节点递归收集 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 sortItems = collectMenuSortItems(treeRoots);
|
||
if (!sortItems.length) return;
|
||
|
||
await batchSortMenuApi({ sortItems });
|
||
message.success('已保存排序');
|
||
} catch (error: any) {
|
||
console.error('拖拽排序失败:', error);
|
||
message.error(error?.message || '排序更新失败');
|
||
gridApi.reload();
|
||
} finally {
|
||
isUpdating = false;
|
||
}
|
||
};
|
||
|
||
// 全部展开树形表格
|
||
function expandAllTree() {
|
||
if (gridApi) {
|
||
const vxeTable = (gridApi as any).grid;
|
||
if (vxeTable && typeof vxeTable.setAllTreeExpand === 'function') {
|
||
vxeTable.setAllTreeExpand(true);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 全部收起树形表格
|
||
function collapseAllTree() {
|
||
if (gridApi) {
|
||
const vxeTable = (gridApi as any).grid;
|
||
if (vxeTable && typeof vxeTable.setAllTreeExpand === 'function') {
|
||
vxeTable.setAllTreeExpand(false);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 获取菜单类型标签
|
||
function getMenuTypeLabel(type: string) {
|
||
const typeMap: Record<string, string> = {
|
||
catalog: '目录',
|
||
menu: '菜单',
|
||
button: '按钮',
|
||
embedded: '内联',
|
||
link: '外链',
|
||
};
|
||
return typeMap[type] || type;
|
||
}
|
||
|
||
// 获取菜单类型颜色
|
||
function getMenuTypeColor(type: string) {
|
||
const colorMap: Record<string, string> = {
|
||
catalog: 'blue',
|
||
menu: 'green',
|
||
button: 'orange',
|
||
embedded: 'purple',
|
||
link: 'cyan',
|
||
};
|
||
return colorMap[type] || 'default';
|
||
}
|
||
|
||
const [Grid, gridApi] = useVbenVxeGrid({
|
||
gridOptions,
|
||
showSearchForm: false,
|
||
tableTitle: '菜单管理',
|
||
gridEvents: {
|
||
rowDragend: handleDragSort,
|
||
cellMenu: onCellMenu,
|
||
} as any,
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<Page title="菜单管理" auto-content-height>
|
||
<!-- VbenVxeGrid 表格 -->
|
||
<Grid :loading="loading" class="menu-grid">
|
||
<!-- 工具栏工具 -->
|
||
<template #toolbar-tools>
|
||
<Space>
|
||
<Button type="primary" @click="handleAdd">
|
||
<PlusIcon />
|
||
添加菜单
|
||
</Button>
|
||
<Button @click="expandAllTree">
|
||
<ExpandIcon />
|
||
全部展开
|
||
</Button>
|
||
<Button @click="collapseAllTree">
|
||
<CollapseIcon />
|
||
全部收起
|
||
</Button>
|
||
<div class="text-sm text-gray-500">
|
||
支持跨父级拖拽(按住 Ctrl
|
||
可拖成目标子节点);右键行可编辑 / 增下级 / 删除
|
||
</div>
|
||
</Space>
|
||
</template>
|
||
|
||
<!-- 菜单名称列 - 移除自定义插槽以支持拖拽控件正常显示 -->
|
||
|
||
<!-- 图标列 -->
|
||
<template #icon="{ row }: { row: MenuVO.MenuInfo }">
|
||
<div
|
||
v-if="row.icon"
|
||
class="flex items-center justify-center"
|
||
:title="row.icon"
|
||
>
|
||
<IconifyIcon :icon="row.icon" class="size-4 text-blue-600" />
|
||
</div>
|
||
<span v-else class="text-gray-400">-</span>
|
||
</template>
|
||
|
||
<!-- 菜单类型列 -->
|
||
<template #menuType="{ row }: { row: any }">
|
||
<Tag :color="getMenuTypeColor(row.type)">
|
||
{{ getMenuTypeLabel(row.type) }}
|
||
</Tag>
|
||
</template>
|
||
|
||
<!-- 菜单状态列 -->
|
||
<template #menuStatus="{ row }: { row: any }">
|
||
<Tag :color="row.status === 1 ? 'green' : 'red'">
|
||
{{ row.status === 1 ? '启用' : '禁用' }}
|
||
</Tag>
|
||
</template>
|
||
|
||
<!-- 权限名称列 -->
|
||
<template #permissions="{ row }: { row: any }">
|
||
<div
|
||
v-if="row.permissions && row.permissions.length > 0"
|
||
class="flex flex-wrap gap-1"
|
||
>
|
||
<Tag
|
||
v-for="permission in row.permissions"
|
||
:key="permission.id"
|
||
:color="permission.status === 1 ? 'blue' : 'default'"
|
||
size="small"
|
||
>
|
||
{{ permission.name }}
|
||
</Tag>
|
||
</div>
|
||
<span v-else class="text-gray-400">-</span>
|
||
</template>
|
||
|
||
<!-- 操作列 -->
|
||
<template #action="{ row }: { row: any }">
|
||
<Space size="small" :wrap="false">
|
||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||
<PlusCircleIcon class="h-3 w-3" />
|
||
增加下级
|
||
</Button>
|
||
<Button type="link" size="small" @click="handleEdit(row)">
|
||
<EditIcon class="h-3 w-3" />
|
||
编辑
|
||
</Button>
|
||
<Popconfirm
|
||
title="确定要删除这个菜单吗?"
|
||
@confirm="handleDelete(row)"
|
||
>
|
||
<Button type="link" size="small" danger>
|
||
<TrashIcon class="h-3 w-3" />
|
||
删除
|
||
</Button>
|
||
</Popconfirm>
|
||
</Space>
|
||
</template>
|
||
</Grid>
|
||
|
||
<!-- 菜单表单模态框 -->
|
||
<MenuFormModal ref="menuFormModalRef" @success="handleModalSuccess" />
|
||
|
||
<VxeRowContextMenu
|
||
v-model:open="ctxMenuOpen"
|
||
:x="ctxMenuX"
|
||
:y="ctxMenuY"
|
||
:items="ctxMenuItems"
|
||
@select="handleCtxMenuSelect"
|
||
/>
|
||
</Page>
|
||
</template>
|
||
|
||
<style scoped>
|
||
:deep(.menu-grid .vxe-body--column) {
|
||
vertical-align: middle;
|
||
}
|
||
</style>
|