mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 18:29:20 +08:00
refactor: 调整页面组件位置
This commit is contained in:
@@ -0,0 +1,473 @@
|
||||
<script lang="ts" setup>
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { MenuVO } from '#/api/system/menu/types';
|
||||
|
||||
import { onMounted, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
|
||||
|
||||
import { Button, message, Popconfirm, Space, Tag } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import {
|
||||
batchSortMenuApi,
|
||||
deleteMenuApi,
|
||||
getAllMenusApi,
|
||||
} from '#/api/system/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: {
|
||||
isHover: true,
|
||||
drag: true, // 启用行拖拽
|
||||
height: 52,
|
||||
},
|
||||
showOverflow: true,
|
||||
size: 'small',
|
||||
rowDragConfig: {
|
||||
isPeerDrag: true, // 启用同层级拖拽限制
|
||||
},
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
refresh: true,
|
||||
zoom: true,
|
||||
export: true, // 启用导出功能
|
||||
},
|
||||
data: [],
|
||||
treeConfig: {
|
||||
transform: true,
|
||||
rowField: 'id',
|
||||
parentField: 'parentId',
|
||||
},
|
||||
// 配置远程加载
|
||||
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 handleModalSuccess(
|
||||
payload?: { mode: 'update'; menu: MenuVO.MenuInfo } | { mode: 'create' },
|
||||
) {
|
||||
if (payload?.mode === 'update' && payload.menu) {
|
||||
patchMenuRowFromServer(payload.menu);
|
||||
return;
|
||||
}
|
||||
refreshTable();
|
||||
}
|
||||
|
||||
// 防重复调用标志
|
||||
let isUpdating = false;
|
||||
|
||||
// 处理拖拽排序
|
||||
const handleDragSort = async (_params: any) => {
|
||||
// 防止重复调用
|
||||
if (isUpdating) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
isUpdating = true;
|
||||
|
||||
// 获取当前表格数据
|
||||
const tableDataResult = gridApi.grid.getTableData();
|
||||
const tableData = tableDataResult?.tableData || [];
|
||||
|
||||
// 构建排序数据
|
||||
const sortItems = tableData.map((item: MenuVO.MenuInfo, index: number) => ({
|
||||
menuId: item.id,
|
||||
order: index + 1, // 排序值从1开始
|
||||
}));
|
||||
|
||||
// 调用排序接口
|
||||
await batchSortMenuApi({ sortItems });
|
||||
|
||||
// 排序成功后不需要重新加载数据,因为表格数据已经更新
|
||||
} catch (error: any) {
|
||||
console.error('拖拽排序失败:', error);
|
||||
if (error?.message) {
|
||||
message.error(error.message);
|
||||
} else {
|
||||
message.error('排序更新失败');
|
||||
}
|
||||
// 排序失败时重新加载数据,恢复原始状态
|
||||
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';
|
||||
}
|
||||
|
||||
// 创建 VbenVxeGrid 实例
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions,
|
||||
showSearchForm: false,
|
||||
tableTitle: '菜单管理',
|
||||
gridEvents: {
|
||||
// 拖拽排序事件
|
||||
rowDragend: handleDragSort,
|
||||
} 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">
|
||||
<!-- 工具栏工具 -->
|
||||
<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">
|
||||
💡 提示:可直接拖拽菜单名称列进行排序
|
||||
</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" />
|
||||
</Page>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
:deep(.menu-grid .vxe-body--column) {
|
||||
vertical-align: middle;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user