diff --git a/ecom-dmb-frontend/apps/web-antd/src/api/system/permission/apis.ts b/ecom-dmb-frontend/apps/web-antd/src/api/system/permission/apis.ts index a4785231..5a902afd 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/api/system/permission/apis.ts +++ b/ecom-dmb-frontend/apps/web-antd/src/api/system/permission/apis.ts @@ -76,3 +76,10 @@ export async function getPositionVisiblePermissionTreeApi(positionId: number) { export async function getSystemPermissionTreeApi() { return requestClient.get('/permission/system/tree'); } + +/** + * 批量更新权限排序 + */ +export async function batchSortPermissionApi(sortItems: { permissionId: number; sortOrder: number }[]) { + return requestClient.put('/permission/sort', { sortItems }); +} diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/system/permission/components/PermissionFormModal.vue b/ecom-dmb-frontend/apps/web-antd/src/views/system/permission/components/PermissionFormModal.vue index 39945fbe..30f480eb 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/system/permission/components/PermissionFormModal.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/system/permission/components/PermissionFormModal.vue @@ -5,10 +5,11 @@ import { ref } from 'vue'; import { useVbenModal } from '@vben/common-ui'; -import { Form, Input, message, Select, Switch } from 'ant-design-vue'; +import { Form, Input, message, Select, Switch, TreeSelect } from 'ant-design-vue'; import { createPermissionApi, + getSystemPermissionTreeApi, updatePermissionApi, } from '#/api/system/permission'; @@ -20,11 +21,11 @@ defineOptions({ name: 'PermissionFormModal' }); const emit = defineEmits(); -// 创建Vben Modal实例 +// 创建 Vben Modal 实例 const [Modal, modalApi] = useVbenModal({ title: '权限管理', - draggable: true, // 开启拖拽 - contentClass: 'px-6 py-4', // 使用Tailwind CSS设置内边距 + draggable: true, + contentClass: 'px-6 py-4', onConfirm: handleSubmit, onCancel: close, onOpenChange: (isOpen: boolean) => { @@ -44,21 +45,26 @@ interface FormData { code: string; name: string; type: string; + parentId: number | null; + sortOrder: number; status: boolean; remark: string; } +// 父级权限选项(树形) +const parentPermissionOptions = ref([]); + // 表单数据 const formData = ref({ code: '', name: '', - type: '', + type: 'api', + parentId: null, + sortOrder: 0, status: true, remark: '', }); -// 状态选项(开关组件不需要选项数组) - // 权限类型选项 const typeOptions = [ { label: '接口权限', value: 'api' }, @@ -66,16 +72,46 @@ const typeOptions = [ { label: '数据权限', value: 'data' }, ]; +// 加载父级权限树(用于选择父级) +async function loadParentPermissions() { + try { + const tree = await getSystemPermissionTreeApi(); + parentPermissionOptions.value = buildTreeOptions(tree || []); + } catch { + // ignore + } +} + +// 将权限树转为 TreeSelect 选项格式(排除自己) +function buildTreeOptions( + nodes: PermissionVO.PermissionInfoVO[], + disabledIds: Set = new Set(), +): any[] { + return nodes.map((node) => { + const disabled = disabledIds.has(node.id!); + return { + title: `${node.name} (${node.code})`, + value: node.id, + key: node.id, + disabled, + children: node.children ? buildTreeOptions(node.children, disabledIds) : undefined, + }; + }); +} + // 重置表单 function resetForm() { formData.value = { code: '', name: '', - type: '', + type: 'api', + parentId: null, + sortOrder: 0, status: true, remark: '', }; formRef.value?.resetFields(); + parentPermissionOptions.value = []; } // 关闭模态框 @@ -89,7 +125,6 @@ function open(permissionData?: null | PermissionVO.PermissionInfoVO) { isEdit.value = !!permissionData; currentPermissionData.value = permissionData || null; - // 更新模态框标题 modalApi.setState({ title: isEdit.value ? '编辑权限' : '添加权限', }); @@ -97,27 +132,55 @@ function open(permissionData?: null | PermissionVO.PermissionInfoVO) { resetForm(); if (permissionData) { + // 编辑时排除自己和子孙节点(不能选自己或子孙做父级) + const disabledIds = collectDescendantIds(permissionData); formData.value = { code: permissionData.code, name: permissionData.name, type: permissionData.type, - status: permissionData.status === 1, // 转换为boolean值 + parentId: permissionData.parentId ?? null, + sortOrder: permissionData.sortOrder ?? 0, + status: permissionData.status === 1, remark: permissionData.remark || '', }; + // 加载父级选项(编辑时传入禁用自己及子孙的集合) + loadParentPermissionsWithDisabled(disabledIds); + } else { + loadParentPermissions(); } modalApi.open(); } +// 递归收集子孙节点ID +function collectDescendantIds(node: PermissionVO.PermissionInfoVO): Set { + const ids = new Set([node.id!]); + if (node.children) { + for (const child of node.children) { + const childIds = collectDescendantIds(child); + childIds.forEach((id) => ids.add(id)); + } + } + return ids; +} + +// 加载父级选项(带禁用信息) +async function loadParentPermissionsWithDisabled(disabledIds: Set) { + try { + const tree = await getSystemPermissionTreeApi(); + parentPermissionOptions.value = buildTreeOptions(tree || [], disabledIds); + } catch { + // ignore + } +} + // 处理表单提交 async function handleSubmit() { try { await formRef.value?.validate(); - // 设置模态框为loading状态 modalApi.setState({ confirmLoading: true }); - // 转换状态值为数字 const submitData = { ...formData.value, status: formData.value.status ? 1 : 0, @@ -170,10 +233,7 @@ defineExpose({ }, ]" > - + + + + + + + + + = { showOverflow: true, size: 'small', expandAll: false, + rowConfig: { + drag: true, + }, + rowDragConfig: { + isPeerDrag: false, + }, toolbarConfig: { custom: true, refresh: true, @@ -134,7 +141,38 @@ const gridOptions: VxeGridProps = { }; // 表格实例 -const [Grid, gridApi] = useVbenVxeGrid({ gridOptions }); +const [Grid, gridApi] = useVbenVxeGrid({ + gridOptions, + gridEvents: { + rowDragend: handleDragSort, + } as any, +}); + +// 防重调用标志 +let isDragging = false; + +// 处理拖拽排序 +async function handleDragSort() { + if (isDragging) return; + isDragging = true; + try { + const vxeGrid = (gridApi as any).grid; + if (!vxeGrid) return; + const tableData = vxeGrid.getTableData?.()?.fullData || []; + if (!tableData.length) return; + // 按 tree 的顺序构建排序数据(VxeTable tree 内部已更新顺序) + const sortItems = tableData.map((item: any, index: number) => ({ + permissionId: item.id, + sortOrder: index + 1, + })); + await batchSortPermissionApi(sortItems); + } catch (error: any) { + message.error(error?.message || '排序更新失败'); + gridApi.reload(); + } finally { + isDragging = false; + } +} // 刷新表格 function refreshTable() { @@ -195,7 +233,7 @@ const getTypeColor = (type: string) => {