mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 01:09:19 +08:00
refactor: 系统权限页改用 VxeTable 树形表格展示
- 去掉表格/树状视图切换,只保留树形表格 - 数据源改为 getSystemPermissionTreeApi()(系统管理分支) - 使用 VxeTable treeConfig.transform 扁平数据自动构建树 - 去掉分页、搜索表单
This commit is contained in:
@@ -1,20 +1,17 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||||
import type { PermissionDTO, PermissionVO } from '#/api/system/permission';
|
import type { PermissionVO } from '#/api/system/permission';
|
||||||
import type { PageQueryDTO } from '#/types/common';
|
|
||||||
|
|
||||||
import { computed, ref, onMounted } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
import { Page } from '@vben/common-ui';
|
import { Page } from '@vben/common-ui';
|
||||||
import { createIconifyIcon } from '@vben/icons';
|
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 { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||||
import {
|
import {
|
||||||
deletePermissionApi,
|
deletePermissionApi,
|
||||||
getAllPermissionsApi,
|
|
||||||
getPermissionPageApi,
|
|
||||||
getSystemPermissionTreeApi,
|
getSystemPermissionTreeApi,
|
||||||
} from '#/api/system/permission';
|
} from '#/api/system/permission';
|
||||||
|
|
||||||
@@ -28,87 +25,41 @@ const PlusIcon = createIconifyIcon('lucide:plus');
|
|||||||
// 响应式数据
|
// 响应式数据
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const permissionFormModalRef = ref();
|
const permissionFormModalRef = ref();
|
||||||
const viewMode = ref<'table' | 'tree'>('table');
|
|
||||||
|
|
||||||
// 树数据
|
// 扁平化树数据(VxeTable treeConfig.transform 需要扁平数据)
|
||||||
const permissionTree = ref<PermissionVO.PermissionInfoVO[]>([]);
|
function flattenTree(
|
||||||
const treeLoading = ref(false);
|
nodes: PermissionVO.PermissionInfoVO[],
|
||||||
const expandedKeys = ref<string[]>([]); // 树状视图预展开
|
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 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 表格配置
|
|
||||||
const gridOptions: VxeGridProps<any> = {
|
const gridOptions: VxeGridProps<any> = {
|
||||||
columns: [
|
columns: [
|
||||||
{
|
{
|
||||||
type: 'seq' as const,
|
type: 'seq' as const,
|
||||||
minWidth: 'auto',
|
minWidth: 60,
|
||||||
title: '序号',
|
title: '序号',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
field: 'name',
|
||||||
|
title: '权限名称',
|
||||||
|
minWidth: 200,
|
||||||
|
showOverflow: 'tooltip' as const,
|
||||||
|
treeNode: true, // 启用树节点显示
|
||||||
|
},
|
||||||
{
|
{
|
||||||
field: 'code',
|
field: 'code',
|
||||||
title: '权限码',
|
title: '权限码',
|
||||||
minWidth: 200,
|
minWidth: 200,
|
||||||
},
|
showOverflow: 'tooltip' as const,
|
||||||
{
|
|
||||||
field: 'name',
|
|
||||||
title: '权限名称',
|
|
||||||
minWidth: 150,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'type',
|
field: 'type',
|
||||||
@@ -116,6 +67,11 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
minWidth: 120,
|
minWidth: 120,
|
||||||
slots: { default: 'type' },
|
slots: { default: 'type' },
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
field: 'sortOrder',
|
||||||
|
title: '排序',
|
||||||
|
minWidth: 80,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
field: 'status',
|
field: 'status',
|
||||||
title: '状态',
|
title: '状态',
|
||||||
@@ -128,11 +84,6 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
minWidth: 200,
|
minWidth: 200,
|
||||||
showOverflow: 'tooltip' as const,
|
showOverflow: 'tooltip' as const,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
field: 'sortOrder',
|
|
||||||
title: '排序',
|
|
||||||
minWidth: 80,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
field: 'createTime',
|
field: 'createTime',
|
||||||
title: '创建时间',
|
title: '创建时间',
|
||||||
@@ -151,55 +102,28 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
highlightHoverRow: true,
|
highlightHoverRow: true,
|
||||||
showOverflow: true,
|
showOverflow: true,
|
||||||
size: 'small',
|
size: 'small',
|
||||||
|
expandAll: false,
|
||||||
toolbarConfig: {
|
toolbarConfig: {
|
||||||
custom: true,
|
custom: true,
|
||||||
refresh: true,
|
refresh: true,
|
||||||
zoom: true,
|
zoom: true,
|
||||||
},
|
},
|
||||||
data: [],
|
data: [],
|
||||||
pagerConfig: {
|
treeConfig: {
|
||||||
enabled: true,
|
transform: true,
|
||||||
pageSizes: [10, 20, 50, 100],
|
rowField: 'id',
|
||||||
layouts: [
|
parentField: 'parentId',
|
||||||
'PrevJump',
|
|
||||||
'PrevPage',
|
|
||||||
'Number',
|
|
||||||
'NextPage',
|
|
||||||
'NextJump',
|
|
||||||
'Sizes',
|
|
||||||
'FullJump',
|
|
||||||
'Total',
|
|
||||||
] as (
|
|
||||||
| 'FullJump'
|
|
||||||
| 'NextJump'
|
|
||||||
| 'NextPage'
|
|
||||||
| 'Number'
|
|
||||||
| 'PrevJump'
|
|
||||||
| 'PrevPage'
|
|
||||||
| 'Sizes'
|
|
||||||
| 'Total'
|
|
||||||
)[],
|
|
||||||
},
|
},
|
||||||
proxyConfig: {
|
proxyConfig: {
|
||||||
ajax: {
|
ajax: {
|
||||||
query: async ({ page }) => {
|
query: async () => {
|
||||||
try {
|
try {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
const params: PageQueryDTO<PermissionDTO.PermissionFilterDTO> = {
|
const treeData = await getSystemPermissionTreeApi();
|
||||||
page: page.currentPage,
|
const flatData = flattenTree(treeData || []);
|
||||||
pageSize: page.pageSize,
|
return { items: flatData, total: flatData.length };
|
||||||
...searchForm.value,
|
|
||||||
};
|
|
||||||
const response = await getPermissionPageApi(params);
|
|
||||||
return {
|
|
||||||
items: response.records,
|
|
||||||
total: response.total,
|
|
||||||
};
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('查询数据失败:', error);
|
message.error(error?.message || '获取权限树失败');
|
||||||
const errorMessage =
|
|
||||||
error?.response?.data?.message || error?.message || '查询数据失败';
|
|
||||||
message.error(errorMessage);
|
|
||||||
return { items: [], total: 0 };
|
return { items: [], total: 0 };
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
@@ -210,83 +134,39 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 表格实例
|
// 表格实例
|
||||||
const [Grid, gridApi] = useVbenVxeGrid({
|
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
|
||||||
gridOptions,
|
|
||||||
formOptions: formOptions.value,
|
|
||||||
showSearchForm: true,
|
|
||||||
tableTitle: '权限管理',
|
|
||||||
});
|
|
||||||
|
|
||||||
// 计算属性
|
// 刷新表格
|
||||||
const hasSelected = computed(() => {
|
|
||||||
const vxeTable = (gridApi as any).grid;
|
|
||||||
return vxeTable?.getCheckboxRecords?.()?.length > 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 刷新表格数据
|
|
||||||
function refreshTable() {
|
function refreshTable() {
|
||||||
if (gridApi) gridApi.reload();
|
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() {
|
function handleAdd() {
|
||||||
permissionFormModalRef.value?.open();
|
permissionFormModalRef.value?.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 打开编辑
|
||||||
function handleEdit(row: PermissionVO.PermissionInfoVO) {
|
function handleEdit(row: PermissionVO.PermissionInfoVO) {
|
||||||
permissionFormModalRef.value?.open(row);
|
permissionFormModalRef.value?.open(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 删除
|
||||||
async function handleDelete(row: PermissionVO.PermissionInfoVO) {
|
async function handleDelete(row: PermissionVO.PermissionInfoVO) {
|
||||||
try {
|
try {
|
||||||
await deletePermissionApi(row.id);
|
await deletePermissionApi(row.id);
|
||||||
message.success('删除成功');
|
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();
|
refreshTable();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
message.error(error?.response?.data?.message || error?.message || '批量删除失败');
|
message.error(
|
||||||
|
error?.response?.data?.message || error?.message || '删除失败',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 表单弹窗成功回调
|
||||||
function handleModalSuccess() {
|
function handleModalSuccess() {
|
||||||
if (viewMode.value === 'tree') loadPermissionTree();
|
refreshTable();
|
||||||
else refreshTable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 类型文本
|
// 类型文本
|
||||||
@@ -309,52 +189,23 @@ const getTypeColor = (type: string) => {
|
|||||||
};
|
};
|
||||||
return colorMap[type] || 'default';
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<Page title="系统权限" auto-content-height>
|
<Page title="系统权限" auto-content-height>
|
||||||
<template #description>
|
<template #description>
|
||||||
<div class="text-foreground/80 mt-2">
|
<div class="text-foreground/80 mt-2">
|
||||||
管理系统权限码,支持权限的增删改查操作
|
系统管理模块下的完整权限树结构
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<!-- 视图切换 -->
|
<Grid>
|
||||||
<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">
|
|
||||||
<template #toolbar-tools>
|
<template #toolbar-tools>
|
||||||
<Space>
|
<Space>
|
||||||
<Button type="primary" @click="handleAdd">
|
<Button type="primary" @click="handleAdd">
|
||||||
<PlusIcon />
|
<PlusIcon />
|
||||||
添加权限
|
添加权限
|
||||||
</Button>
|
</Button>
|
||||||
<Button v-if="hasSelected" danger @click="handleBatchDelete">
|
|
||||||
批量删除
|
|
||||||
</Button>
|
|
||||||
</Space>
|
</Space>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -373,67 +224,16 @@ onMounted(() => {
|
|||||||
<template #action="{ row }">
|
<template #action="{ row }">
|
||||||
<Space>
|
<Space>
|
||||||
<Button size="small" @click="handleEdit(row)">编辑</Button>
|
<Button size="small" @click="handleEdit(row)">编辑</Button>
|
||||||
<Popconfirm title="确定要删除这个权限吗?" @confirm="handleDelete(row)">
|
<Popconfirm
|
||||||
|
title="确定要删除这个权限吗?"
|
||||||
|
@confirm="handleDelete(row)"
|
||||||
|
>
|
||||||
<Button size="small" danger>删除</Button>
|
<Button size="small" danger>删除</Button>
|
||||||
</Popconfirm>
|
</Popconfirm>
|
||||||
</Space>
|
</Space>
|
||||||
</template>
|
</template>
|
||||||
</Grid>
|
</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
|
<PermissionFormModal
|
||||||
ref="permissionFormModalRef"
|
ref="permissionFormModalRef"
|
||||||
|
|||||||
Reference in New Issue
Block a user