mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
refactor: 系统权限页改用 VxeTable 树形表格展示
- 去掉表格/树状视图切换,只保留树形表格 - 数据源改为 getSystemPermissionTreeApi()(系统管理分支) - 使用 VxeTable treeConfig.transform 扁平数据自动构建树 - 去掉分页、搜索表单
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
<script setup lang="ts">
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { PermissionDTO, PermissionVO } from '#/api/system/permission';
|
||||
import type { PageQueryDTO } from '#/types/common';
|
||||
import type { PermissionVO } from '#/api/system/permission';
|
||||
|
||||
import { computed, ref, onMounted } from 'vue';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import { Button, message, Popconfirm, Space, Tag, Spin, Tree } from 'ant-design-vue';
|
||||
import { Button, message, Popconfirm, Space, Tag, Spin } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import {
|
||||
deletePermissionApi,
|
||||
getAllPermissionsApi,
|
||||
getPermissionPageApi,
|
||||
getSystemPermissionTreeApi,
|
||||
} from '#/api/system/permission';
|
||||
|
||||
@@ -28,87 +25,41 @@ const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const permissionFormModalRef = ref();
|
||||
const viewMode = ref<'table' | 'tree'>('table');
|
||||
|
||||
// 树数据
|
||||
const permissionTree = ref<PermissionVO.PermissionInfoVO[]>([]);
|
||||
const treeLoading = ref(false);
|
||||
const expandedKeys = ref<string[]>([]); // 树状视图预展开
|
||||
// 扁平化树数据(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;
|
||||
}
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
code: undefined as string | undefined,
|
||||
name: undefined as string | undefined,
|
||||
type: undefined as string | undefined,
|
||||
status: undefined as number | undefined,
|
||||
});
|
||||
|
||||
// 状态选项
|
||||
const statusOptions = [
|
||||
{ label: '启用', value: 1 },
|
||||
{ label: '禁用', value: 0 },
|
||||
];
|
||||
|
||||
// 权限类型选项
|
||||
const typeOptions = [
|
||||
{ label: '接口权限', value: 'api' },
|
||||
{ label: '按钮权限', value: 'button' },
|
||||
{ label: '数据权限', value: 'data' },
|
||||
{ label: '模块', value: 'module' },
|
||||
];
|
||||
|
||||
// 搜索表单配置
|
||||
const formOptions = computed(() => ({
|
||||
schemas: [
|
||||
{
|
||||
fieldName: 'code',
|
||||
label: '权限码',
|
||||
},
|
||||
{
|
||||
fieldName: 'name',
|
||||
label: '权限名称',
|
||||
},
|
||||
{
|
||||
fieldName: 'type',
|
||||
label: '权限类型',
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
options: typeOptions,
|
||||
placeholder: '请选择权限类型',
|
||||
},
|
||||
},
|
||||
{
|
||||
fieldName: 'status',
|
||||
label: '状态',
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
options: statusOptions,
|
||||
placeholder: '请选择状态',
|
||||
},
|
||||
},
|
||||
],
|
||||
showDefaultActions: true,
|
||||
collapsed: true,
|
||||
collapsedRows: 1,
|
||||
}));
|
||||
|
||||
// VbenVxeGrid 表格配置
|
||||
// VxeGrid 树形表格配置
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 'auto',
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '权限名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true, // 启用树节点显示
|
||||
},
|
||||
{
|
||||
field: 'code',
|
||||
title: '权限码',
|
||||
minWidth: 200,
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '权限名称',
|
||||
minWidth: 150,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'type',
|
||||
@@ -116,6 +67,11 @@ const gridOptions: VxeGridProps<any> = {
|
||||
minWidth: 120,
|
||||
slots: { default: 'type' },
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
@@ -128,11 +84,6 @@ const gridOptions: VxeGridProps<any> = {
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
@@ -151,55 +102,28 @@ const gridOptions: VxeGridProps<any> = {
|
||||
highlightHoverRow: true,
|
||||
showOverflow: true,
|
||||
size: 'small',
|
||||
expandAll: false,
|
||||
toolbarConfig: {
|
||||
custom: true,
|
||||
refresh: true,
|
||||
zoom: true,
|
||||
},
|
||||
data: [],
|
||||
pagerConfig: {
|
||||
enabled: true,
|
||||
pageSizes: [10, 20, 50, 100],
|
||||
layouts: [
|
||||
'PrevJump',
|
||||
'PrevPage',
|
||||
'Number',
|
||||
'NextPage',
|
||||
'NextJump',
|
||||
'Sizes',
|
||||
'FullJump',
|
||||
'Total',
|
||||
] as (
|
||||
| 'FullJump'
|
||||
| 'NextJump'
|
||||
| 'NextPage'
|
||||
| 'Number'
|
||||
| 'PrevJump'
|
||||
| 'PrevPage'
|
||||
| 'Sizes'
|
||||
| 'Total'
|
||||
)[],
|
||||
treeConfig: {
|
||||
transform: true,
|
||||
rowField: 'id',
|
||||
parentField: 'parentId',
|
||||
},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }) => {
|
||||
query: async () => {
|
||||
try {
|
||||
loading.value = true;
|
||||
const params: PageQueryDTO<PermissionDTO.PermissionFilterDTO> = {
|
||||
page: page.currentPage,
|
||||
pageSize: page.pageSize,
|
||||
...searchForm.value,
|
||||
};
|
||||
const response = await getPermissionPageApi(params);
|
||||
return {
|
||||
items: response.records,
|
||||
total: response.total,
|
||||
};
|
||||
const treeData = await getSystemPermissionTreeApi();
|
||||
const flatData = flattenTree(treeData || []);
|
||||
return { items: flatData, total: flatData.length };
|
||||
} catch (error: any) {
|
||||
console.error('查询数据失败:', error);
|
||||
const errorMessage =
|
||||
error?.response?.data?.message || error?.message || '查询数据失败';
|
||||
message.error(errorMessage);
|
||||
message.error(error?.message || '获取权限树失败');
|
||||
return { items: [], total: 0 };
|
||||
} finally {
|
||||
loading.value = false;
|
||||
@@ -210,83 +134,39 @@ const gridOptions: VxeGridProps<any> = {
|
||||
};
|
||||
|
||||
// 表格实例
|
||||
const [Grid, gridApi] = useVbenVxeGrid({
|
||||
gridOptions,
|
||||
formOptions: formOptions.value,
|
||||
showSearchForm: true,
|
||||
tableTitle: '权限管理',
|
||||
});
|
||||
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
|
||||
|
||||
// 计算属性
|
||||
const hasSelected = computed(() => {
|
||||
const vxeTable = (gridApi as any).grid;
|
||||
return vxeTable?.getCheckboxRecords?.()?.length > 0;
|
||||
});
|
||||
|
||||
// 刷新表格数据
|
||||
// 刷新表格
|
||||
function refreshTable() {
|
||||
if (gridApi) gridApi.reload();
|
||||
}
|
||||
|
||||
// 加载权限树
|
||||
async function loadPermissionTree() {
|
||||
treeLoading.value = true;
|
||||
try {
|
||||
const result = await getSystemPermissionTreeApi();
|
||||
permissionTree.value = result || [];
|
||||
// 默认展开所有节点
|
||||
expandedKeys.value = permissionTree.value.map((n) => String(n.id));
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '获取权限树失败');
|
||||
} finally {
|
||||
treeLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 切换视图
|
||||
function switchView(mode: 'table' | 'tree') {
|
||||
viewMode.value = mode;
|
||||
if (mode === 'tree') loadPermissionTree();
|
||||
}
|
||||
|
||||
// 打开新增
|
||||
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('删除成功');
|
||||
if (viewMode.value === 'tree') loadPermissionTree();
|
||||
else refreshTable();
|
||||
} catch (error: any) {
|
||||
message.error(error?.response?.data?.message || error?.message || '删除失败');
|
||||
}
|
||||
}
|
||||
|
||||
async function handleBatchDelete() {
|
||||
const vxeTable = (gridApi as any).grid;
|
||||
const selectedRows = vxeTable?.getCheckboxRecords?.();
|
||||
if (!selectedRows?.length) {
|
||||
message.warning('请选择要删除的数据');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await Promise.all(selectedRows.map((row: PermissionVO.PermissionInfoVO) => deletePermissionApi(row.id)));
|
||||
message.success(`成功删除 ${selectedRows.length} 条记录`);
|
||||
refreshTable();
|
||||
} catch (error: any) {
|
||||
message.error(error?.response?.data?.message || error?.message || '批量删除失败');
|
||||
message.error(
|
||||
error?.response?.data?.message || error?.message || '删除失败',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 表单弹窗成功回调
|
||||
function handleModalSuccess() {
|
||||
if (viewMode.value === 'tree') loadPermissionTree();
|
||||
else refreshTable();
|
||||
refreshTable();
|
||||
}
|
||||
|
||||
// 类型文本
|
||||
@@ -309,52 +189,23 @@ const getTypeColor = (type: string) => {
|
||||
};
|
||||
return colorMap[type] || 'default';
|
||||
};
|
||||
|
||||
// Ant Design Tree 格式转换
|
||||
function toAntdTreeData(nodes: PermissionVO.PermissionInfoVO[]): any[] {
|
||||
return nodes.map((node) => ({
|
||||
key: String(node.id),
|
||||
title: `${node.name} (${node.code})`,
|
||||
children: node.children ? toAntdTreeData(node.children) : undefined,
|
||||
}));
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (viewMode.value === 'tree') loadPermissionTree();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page title="系统权限" auto-content-height>
|
||||
<template #description>
|
||||
<div class="text-foreground/80 mt-2">
|
||||
管理系统权限码,支持权限的增删改查操作
|
||||
系统管理模块下的完整权限树结构
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- 视图切换 -->
|
||||
<div class="mb-4 flex items-center gap-3">
|
||||
<Space>
|
||||
<Button :type="viewMode === 'table' ? 'primary' : 'default'" @click="switchView('table')">
|
||||
表格视图
|
||||
</Button>
|
||||
<Button :type="viewMode === 'tree' ? 'primary' : 'default'" @click="switchView('tree')">
|
||||
树状视图
|
||||
</Button>
|
||||
</Space>
|
||||
</div>
|
||||
|
||||
<!-- 表格视图 -->
|
||||
<Grid v-if="viewMode === 'table'" :loading="loading">
|
||||
<Grid>
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加权限
|
||||
</Button>
|
||||
<Button v-if="hasSelected" danger @click="handleBatchDelete">
|
||||
批量删除
|
||||
</Button>
|
||||
</Space>
|
||||
</template>
|
||||
|
||||
@@ -373,67 +224,16 @@ onMounted(() => {
|
||||
<template #action="{ row }">
|
||||
<Space>
|
||||
<Button size="small" @click="handleEdit(row)">编辑</Button>
|
||||
<Popconfirm title="确定要删除这个权限吗?" @confirm="handleDelete(row)">
|
||||
<Popconfirm
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button size="small" danger>删除</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
<!-- 树状视图 -->
|
||||
<div v-if="viewMode === 'tree'" class="rounded border bg-white p-4 dark:bg-neutral-800">
|
||||
<div class="mb-3 flex items-center justify-between">
|
||||
<span class="text-sm text-gray-500">权限树(共 {{ permissionTree.length }} 个顶级模块)</span>
|
||||
<Button size="small" @click="handleAdd">添加权限</Button>
|
||||
</div>
|
||||
<Spin :spinning="treeLoading">
|
||||
<Tree
|
||||
v-if="permissionTree.length > 0"
|
||||
:tree-data="toAntdTreeData(permissionTree)"
|
||||
:default-expanded-keys="expandedKeys"
|
||||
:selectable="false"
|
||||
class="min-h-[400px]"
|
||||
>
|
||||
<template #title="{ key, dataRef }">
|
||||
<div class="flex items-center justify-between gap-4 pr-4">
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="font-medium">{{ dataRef.name }}</span>
|
||||
<span class="text-xs text-gray-400">{{ dataRef.code }}</span>
|
||||
</div>
|
||||
<Space size="small">
|
||||
<Tag :color="getTypeColor(dataRef.type)" class="text-xs">
|
||||
{{ getTypeText(dataRef.type) }}
|
||||
</Tag>
|
||||
<Tag :color="dataRef.status === 1 ? 'green' : 'red'" class="text-xs">
|
||||
{{ dataRef.status === 1 ? '启用' : '禁用' }}
|
||||
</Tag>
|
||||
<Button
|
||||
v-if="dataRef.type !== 'module'"
|
||||
size="small"
|
||||
type="text"
|
||||
@click.stop="handleEdit(dataRef)"
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
v-if="dataRef.type !== 'module'"
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDelete(dataRef)"
|
||||
>
|
||||
<Button size="small" type="text" danger @click.stop>
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
</div>
|
||||
</template>
|
||||
</Tree>
|
||||
<div v-else-if="!treeLoading" class="py-8 text-center text-gray-400">
|
||||
暂无权限数据
|
||||
</div>
|
||||
</Spin>
|
||||
</div>
|
||||
|
||||
<!-- 权限表单弹窗 -->
|
||||
<PermissionFormModal
|
||||
ref="permissionFormModalRef"
|
||||
|
||||
Reference in New Issue
Block a user