Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/views/system/permission/index.vue
T

282 lines
6.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { PermissionVO } from '#/api/system/permission';
import { computed, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { Button, message, Popconfirm, Space, Tag, Spin } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
batchSortPermissionApi,
deletePermissionApi,
getSystemPermissionTreeApi,
} from '#/api/system/permission';
import PermissionFormModal from './components/PermissionFormModal.vue';
defineOptions({ name: 'PermissionManagement' });
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
// 响应式数据
const loading = ref(false);
const permissionFormModalRef = ref();
// 扁平化树数据(VxeTable treeConfig.transform 需要扁平数据)
function flattenTree(
nodes: PermissionVO.PermissionInfoVO[],
result: PermissionVO.PermissionInfoVO[] = [],
): PermissionVO.PermissionInfoVO[] {
for (const node of nodes) {
result.push(node);
if (node.children && node.children.length > 0) {
flattenTree(node.children, result);
}
}
return result;
}
// VxeGrid 树形表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'seq' as const,
minWidth: 60,
title: '序号',
},
{
field: 'name',
title: '权限名称',
minWidth: 200,
showOverflow: 'tooltip' as const,
treeNode: true, // 启用树节点显示
},
{
field: 'code',
title: '权限码',
minWidth: 200,
showOverflow: 'tooltip' as const,
},
{
field: 'type',
title: '权限类型',
minWidth: 120,
slots: { default: 'type' },
},
{
field: 'sortOrder',
title: '排序',
minWidth: 80,
},
{
field: 'status',
title: '状态',
minWidth: 80,
slots: { default: 'status' },
},
{
field: 'remark',
title: '备注',
minWidth: 200,
showOverflow: 'tooltip' as const,
},
{
field: 'createTime',
title: '创建时间',
minWidth: 160,
},
{
title: '操作',
width: 120,
fixed: 'right' as const,
slots: { default: 'action' },
},
],
height: 'auto',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
expandAll: false,
rowConfig: {
drag: true,
},
rowDragConfig: {
isPeerDrag: false,
},
toolbarConfig: {
custom: true,
refresh: true,
zoom: true,
},
data: [],
treeConfig: {
transform: true,
rowField: 'id',
parentField: 'parentId',
},
proxyConfig: {
ajax: {
query: async () => {
try {
loading.value = true;
const treeData = await getSystemPermissionTreeApi();
const flatData = flattenTree(treeData || []);
return { items: flatData, total: flatData.length };
} catch (error: any) {
message.error(error?.message || '获取权限树失败');
return { items: [], total: 0 };
} finally {
loading.value = false;
}
},
},
},
};
// 表格实例
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() {
if (gridApi) gridApi.reload();
}
// 打开新增
function handleAdd() {
permissionFormModalRef.value?.open();
}
// 打开编辑
function handleEdit(row: PermissionVO.PermissionInfoVO) {
permissionFormModalRef.value?.open(row);
}
// 删除
async function handleDelete(row: PermissionVO.PermissionInfoVO) {
try {
await deletePermissionApi(row.id);
message.success('删除成功');
refreshTable();
} catch (error: any) {
message.error(
error?.response?.data?.message || error?.message || '删除失败',
);
}
}
// 表单弹窗成功回调
function handleModalSuccess() {
refreshTable();
}
// 类型文本
const getTypeText = (type: string) => {
const typeMap: Record<string, string> = {
api: '接口权限',
button: '按钮权限',
data: '数据权限',
module: '模块分组',
};
return typeMap[type] || type;
};
const getTypeColor = (type: string) => {
const colorMap: Record<string, string> = {
api: 'blue',
button: 'green',
data: 'orange',
module: 'purple',
};
return colorMap[type] || 'default';
};
</script>
<template>
<Page title="系统权限" auto-content-height>
<template #description>
<div class="text-foreground/80 mt-2">
平台完整权限树结构,支持拖拽排序
</div>
</template>
<Grid>
<template #toolbar-tools>
<Space>
<Button type="primary" @click="handleAdd">
<PlusIcon />
添加权限
</Button>
</Space>
</template>
<template #type="{ row }">
<Tag :color="getTypeColor(row.type)">
{{ getTypeText(row.type) }}
</Tag>
</template>
<template #status="{ row }">
<Tag :color="row.status === 1 ? 'green' : 'red'">
{{ row.status === 1 ? '启用' : '禁用' }}
</Tag>
</template>
<template #action="{ row }">
<Space>
<Button size="small" @click="handleEdit(row)">编辑</Button>
<Popconfirm
title="确定要删除这个权限吗?"
@confirm="handleDelete(row)"
>
<Button size="small" danger>删除</Button>
</Popconfirm>
</Space>
</template>
</Grid>
<!-- 权限表单弹窗 -->
<PermissionFormModal
ref="permissionFormModalRef"
@success="handleModalSuccess"
/>
</Page>
</template>