mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 13:49:20 +08:00
feat: 权限树前端改造
- PermissionVO 新增 parentId、sortOrder、children 字段 - 新增 getPermissionTreeApi 接口 - 权限管理页新增树状视图(表格/树状切换) - 职位权限分配页改为树状多选(父→子级联勾选) - 公司权限分配页改为树状多选(父→子级联勾选) - 勾选父节点自动勾选所有子权限和父节点链
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
<script lang="ts" setup>
|
||||
<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 { computed, ref } from 'vue';
|
||||
import { computed, ref, onMounted } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import { Button, message, Popconfirm, Space, Tag } from 'ant-design-vue';
|
||||
import { Button, message, Popconfirm, Space, Tag, Spin, Tree } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid } from '#/adapter/vxe-table';
|
||||
import {
|
||||
deletePermissionApi,
|
||||
getAllPermissionsApi,
|
||||
getPermissionPageApi,
|
||||
getPermissionTreeApi,
|
||||
} from '#/api/system/permission';
|
||||
|
||||
import PermissionFormModal from './components/PermissionFormModal.vue';
|
||||
@@ -26,6 +28,12 @@ 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<number[]>([]);
|
||||
|
||||
// 搜索表单
|
||||
const searchForm = ref({
|
||||
@@ -46,6 +54,7 @@ const typeOptions = [
|
||||
{ label: '接口权限', value: 'api' },
|
||||
{ label: '按钮权限', value: 'button' },
|
||||
{ label: '数据权限', value: 'data' },
|
||||
{ label: '模块', value: 'module' },
|
||||
];
|
||||
|
||||
// 搜索表单配置
|
||||
@@ -119,6 +128,11 @@ const gridOptions: VxeGridProps<any> = {
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
@@ -166,7 +180,6 @@ const gridOptions: VxeGridProps<any> = {
|
||||
| 'Total'
|
||||
)[],
|
||||
},
|
||||
// 配置远程加载
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }) => {
|
||||
@@ -187,10 +200,7 @@ const gridOptions: VxeGridProps<any> = {
|
||||
const errorMessage =
|
||||
error?.response?.data?.message || error?.message || '查询数据失败';
|
||||
message.error(errorMessage);
|
||||
return {
|
||||
items: [],
|
||||
total: 0,
|
||||
};
|
||||
return { items: [], total: 0 };
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
@@ -215,89 +225,112 @@ const hasSelected = computed(() => {
|
||||
|
||||
// 刷新表格数据
|
||||
function refreshTable() {
|
||||
if (gridApi) {
|
||||
gridApi.reload();
|
||||
if (gridApi) gridApi.reload();
|
||||
}
|
||||
|
||||
// 加载权限树
|
||||
async function loadPermissionTree() {
|
||||
treeLoading.value = true;
|
||||
try {
|
||||
const result = await getPermissionTreeApi();
|
||||
permissionTree.value = result.data || [];
|
||||
// 默认展开所有节点
|
||||
expandedKeys.value = permissionTree.value.map((n) => n.id);
|
||||
} catch (error: any) {
|
||||
message.error(error?.message || '获取权限树失败');
|
||||
} finally {
|
||||
treeLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
const handleAdd = () => {
|
||||
// 切换视图
|
||||
function switchView(mode: 'table' | 'tree') {
|
||||
viewMode.value = mode;
|
||||
if (mode === 'tree') loadPermissionTree();
|
||||
}
|
||||
|
||||
function handleAdd() {
|
||||
permissionFormModalRef.value?.open();
|
||||
};
|
||||
}
|
||||
|
||||
const handleEdit = (row: PermissionVO.PermissionInfoVO) => {
|
||||
function handleEdit(row: PermissionVO.PermissionInfoVO) {
|
||||
permissionFormModalRef.value?.open(row);
|
||||
};
|
||||
}
|
||||
|
||||
const handleDelete = async (row: PermissionVO.PermissionInfoVO) => {
|
||||
async function handleDelete(row: PermissionVO.PermissionInfoVO) {
|
||||
try {
|
||||
await deletePermissionApi(row.id);
|
||||
message.success('删除成功');
|
||||
refreshTable();
|
||||
if (viewMode.value === 'tree') loadPermissionTree();
|
||||
else refreshTable();
|
||||
} catch (error: any) {
|
||||
console.error('删除失败:', error);
|
||||
const errorMessage =
|
||||
error?.response?.data?.message || error?.message || '删除失败';
|
||||
message.error(errorMessage);
|
||||
message.error(error?.response?.data?.message || error?.message || '删除失败');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const handleBatchDelete = async () => {
|
||||
async function handleBatchDelete() {
|
||||
const vxeTable = (gridApi as any).grid;
|
||||
const selectedRows = vxeTable?.getCheckboxRecords?.();
|
||||
if (!selectedRows?.length) {
|
||||
message.warning('请选择要删除的数据');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const deletePromises = selectedRows.map(
|
||||
(row: PermissionVO.PermissionInfoVO) => deletePermissionApi(row.id),
|
||||
);
|
||||
await Promise.all(deletePromises);
|
||||
await Promise.all(selectedRows.map((row: PermissionVO.PermissionInfoVO) => deletePermissionApi(row.id)));
|
||||
message.success(`成功删除 ${selectedRows.length} 条记录`);
|
||||
refreshTable();
|
||||
} catch (error: any) {
|
||||
console.error('批量删除失败:', error);
|
||||
const errorMessage =
|
||||
error?.response?.data?.message || error?.message || '批量删除失败';
|
||||
message.error(errorMessage);
|
||||
message.error(error?.response?.data?.message || error?.message || '批量删除失败');
|
||||
}
|
||||
};
|
||||
|
||||
// 处理模态框成功回调
|
||||
function handleModalSuccess() {
|
||||
refreshTable();
|
||||
}
|
||||
|
||||
// 获取权限类型文本
|
||||
function handleModalSuccess() {
|
||||
if (viewMode.value === 'tree') loadPermissionTree();
|
||||
else refreshTable();
|
||||
}
|
||||
|
||||
// 类型文本
|
||||
const getTypeText = (type: string) => {
|
||||
const typeMap = {
|
||||
const typeMap: Record<string, string> = {
|
||||
api: '接口权限',
|
||||
button: '按钮权限',
|
||||
data: '数据权限',
|
||||
module: '模块分组',
|
||||
};
|
||||
return typeMap[type as keyof typeof typeMap] || type;
|
||||
return typeMap[type] || type;
|
||||
};
|
||||
|
||||
// 获取权限类型颜色
|
||||
const getTypeColor = (type: string) => {
|
||||
const colorMap = {
|
||||
const colorMap: Record<string, string> = {
|
||||
api: 'blue',
|
||||
button: 'green',
|
||||
data: 'orange',
|
||||
module: 'purple',
|
||||
};
|
||||
return colorMap[type as keyof typeof colorMap] || 'default';
|
||||
return colorMap[type] || 'default';
|
||||
};
|
||||
|
||||
// 获取状态文本
|
||||
const getStatusText = (status: number) => {
|
||||
return status === 1 ? '启用' : '禁用';
|
||||
};
|
||||
// Ant Design Tree 格式转换
|
||||
function toAntdTreeData(nodes: PermissionVO.PermissionInfoVO[]): any[] {
|
||||
return nodes.map((node) => ({
|
||||
key: node.id,
|
||||
title: (
|
||||
<span class="flex items-center gap-2">
|
||||
<span class="font-medium">{node.name}</span>
|
||||
<span class="text-xs text-gray-400">({node.code})</span>
|
||||
<Tag color={node.status === 1 ? 'green' : 'red'} class="text-xs">
|
||||
{node.status === 1 ? '启用' : '禁用'}
|
||||
</Tag>
|
||||
</span>
|
||||
),
|
||||
children: node.children ? toAntdTreeData(node.children) : undefined,
|
||||
disabled: node.type === 'module',
|
||||
}));
|
||||
}
|
||||
|
||||
// 获取状态颜色
|
||||
const getStatusColor = (status: number) => {
|
||||
return status === 1 ? 'green' : 'red';
|
||||
};
|
||||
onMounted(() => {
|
||||
if (viewMode.value === 'tree') loadPermissionTree();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -308,9 +341,20 @@ const getStatusColor = (status: number) => {
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- VbenVxeGrid 表格 -->
|
||||
<Grid :loading="loading">
|
||||
<!-- 工具栏工具 -->
|
||||
<!-- 视图切换 -->
|
||||
<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>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
@@ -323,34 +367,82 @@ const getStatusColor = (status: number) => {
|
||||
</Space>
|
||||
</template>
|
||||
|
||||
<!-- 权限类型列 -->
|
||||
<template #type="{ row }">
|
||||
<Tag :color="getTypeColor(row.type)">
|
||||
{{ getTypeText(row.type) }}
|
||||
</Tag>
|
||||
</template>
|
||||
|
||||
<!-- 状态列 -->
|
||||
<template #status="{ row }">
|
||||
<Tag :color="getStatusColor(row.status)">
|
||||
{{ getStatusText(row.status) }}
|
||||
<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>
|
||||
<Button size="small" @click="handleEdit(row)">编辑</Button>
|
||||
<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)"
|
||||
: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