feat: 新增系统权限树接口及页面(多级表格展示)

后端:
- PermissionService: 新增 getSystemPermissionTree() 只返回系统管理分支
- PermissionController: 新增 GET /permission/system/tree

前端:
- 新增 getSystemPermissionTreeApi()
- 新增路由 SystemPlatformSystemPermission(系统权限)
- 新增 views/system/system-permission/index.vue 多级表格展示
This commit is contained in:
小蟑螂
2026-04-10 22:57:15 +08:00
parent 25fee35743
commit f531121578
6 changed files with 286 additions and 5 deletions
@@ -14,6 +14,7 @@ import top.xyquan.ecomdmbbackend.modules.sys.permission.model.dto.PermissionFilt
import top.xyquan.ecomdmbbackend.modules.sys.permission.model.dto.UpdatePermissionDTO;
import top.xyquan.ecomdmbbackend.modules.sys.permission.model.vo.PermissionVO;
import top.xyquan.ecomdmbbackend.modules.sys.permission.service.PermissionService;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import java.util.List;
@@ -26,9 +27,11 @@ import java.util.List;
public class PermissionController {
private final PermissionService permissionService;
private final CurrentUserContext currentUserContext;
public PermissionController(PermissionService permissionService) {
public PermissionController(PermissionService permissionService, CurrentUserContext currentUserContext) {
this.permissionService = permissionService;
this.currentUserContext = currentUserContext;
}
@Operation(summary = "创建权限", description = "创建新的权限")
@@ -91,11 +94,11 @@ public class PermissionController {
return ApiResponse.success(tree);
}
@Operation(summary = "获取公司可见的权限树", description = "只返回已分配权限及其祖先节点")
@GetMapping("/tree/company/{companyId}")
@Operation(summary = "获取公司可见的权限树", description = "只返回当前用户所属公司的已分配权限及其祖先节点")
@GetMapping("/tree/company")
@RequirePermission(value = "system:permission:read", description = "获取公司可见权限树", allowSuper = true)
public ApiResponse<List<PermissionVO>> getCompanyVisiblePermissionTree(
@Parameter(description = "公司ID") @PathVariable Long companyId) {
public ApiResponse<List<PermissionVO>> getCompanyVisiblePermissionTree() {
Long companyId = currentUserContext.getCurrentCompanyId();
List<PermissionVO> tree = permissionService.getCompanyVisiblePermissionTree(companyId);
return ApiResponse.success(tree);
}
@@ -108,4 +111,12 @@ public class PermissionController {
List<PermissionVO> tree = permissionService.getPositionVisiblePermissionTree(positionId);
return ApiResponse.success(tree);
}
@Operation(summary = "获取系统权限树", description = "只返回系统管理模块下的权限(平台超管专用)")
@GetMapping("/system/tree")
@RequirePermission(value = "system:permission:read", description = "获取系统权限树", allowSuper = true)
public ApiResponse<List<PermissionVO>> getSystemPermissionTree() {
List<PermissionVO> tree = permissionService.getSystemPermissionTree();
return ApiResponse.success(tree);
}
}
@@ -73,4 +73,9 @@ public interface PermissionService {
* 获取职位可见的权限树(只返回已分配权限及其祖先节点)
*/
List<PermissionVO> getPositionVisiblePermissionTree(Long positionId);
/**
* 获取系统权限树(只返回系统管理模块下的权限,用于平台超管查看)
*/
List<PermissionVO> getSystemPermissionTree();
}
@@ -238,6 +238,30 @@ public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permiss
return result;
}
@Override
public List<PermissionVO> getSystemPermissionTree() {
List<PermissionPO> all = permissionMapper.selectAllForTree();
List<PermissionVO> allVO = all.stream().map(this::convertToVO).collect(Collectors.toList());
// 按 parent_id 分组
Map<Long, List<PermissionVO>> byParent = allVO.stream()
.collect(Collectors.groupingBy(
vo -> vo.getParentId() != null ? vo.getParentId() : 0L,
Collectors.toList()));
// 根节点是 id=100 系统管理模块
List<PermissionVO> root = byParent.getOrDefault(0L, Collections.emptyList());
List<PermissionVO> systemRoot = root.stream()
.filter(n -> n.getId() != null && n.getId() == 100)
.collect(Collectors.toList());
// 递归构建子树(复用已有的 buildChildrenRecursive 方法)
for (PermissionVO node : systemRoot) {
buildChildrenRecursive(node, byParent);
}
return systemRoot;
}
private PermissionVO convertToVO(PermissionPO po) {
PermissionVO vo = new PermissionVO();
BeanUtils.copyProperties(po, vo);
@@ -55,3 +55,24 @@ export async function getAllPermissionsApi() {
export async function getPermissionTreeApi() {
return requestClient.get<PermissionVO.PermissionInfoVO[]>('/permission/tree');
}
/**
* 获取公司可见的权限树(只返回当前用户所属公司的已分配权限及其祖先节点)
*/
export async function getCompanyVisiblePermissionTreeApi() {
return requestClient.get<PermissionVO.PermissionInfoVO[]>(`/permission/tree/company`);
}
/**
* 获取职位可见的权限树(只返回已分配权限及其祖先节点)
*/
export async function getPositionVisiblePermissionTreeApi(positionId: number) {
return requestClient.get<PermissionVO.PermissionInfoVO[]>(`/permission/tree/position/${positionId}`);
}
/**
* 获取系统权限树(系统管理模块,仅平台超管查看)
*/
export async function getSystemPermissionTreeApi() {
return requestClient.get<PermissionVO.PermissionInfoVO[]>('/permission/system/tree');
}
@@ -31,6 +31,16 @@ const routes: RouteRecordRaw[] = [
requiresAuth: true,
},
},
{
name: 'SystemPlatformSystemPermission',
path: '/platform-admin/system-permission',
component: () => import('#/views/system/system-permission/index.vue'),
meta: {
icon: 'lucide:layoutDashboard',
title: '系统权限',
requiresAuth: true,
},
},
{
name: 'SystemPlatformField',
path: '/platform-admin/field',
@@ -0,0 +1,210 @@
<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { PermissionVO } from '#/api/system/permission/types';
import { onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { Spin, Tag, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getSystemPermissionTreeApi } from '#/api/system/permission';
defineOptions({ name: 'SystemPermissionPage' });
const loading = ref(false);
const permissionData = ref<PermissionVO.PermissionInfoVO[]>([]);
// 获取系统权限树
async function loadSystemPermissions() {
loading.value = true;
try {
const result = await getSystemPermissionTreeApi();
permissionData.value = result || [];
} catch (error: any) {
message.error(error?.message || '获取系统权限树失败');
} finally {
loading.value = false;
}
}
// 扁平化树数据(用于多级表格展示)
interface FlatNode {
id: number;
name: string;
code: string;
type: string;
status: number;
sortOrder: number;
parentId: number | null;
level: number;
children?: FlatNode[];
hasChildren: boolean;
}
function flattenTree(nodes: PermissionVO.PermissionInfoVO[], level = 0, result: FlatNode[] = []): FlatNode[] {
for (const node of nodes) {
const flat: FlatNode = {
id: node.id!,
name: node.name!,
code: node.code!,
type: node.type!,
status: node.status!,
sortOrder: node.sortOrder || 0,
parentId: node.parentId ?? null,
level,
hasChildren: !!(node.children && node.children.length > 0),
children: node.children ? flattenTree(node.children, level + 1, []) : undefined,
};
result.push(flat);
if (node.children && node.children.length > 0) {
flattenTree(node.children, level + 1, result);
}
}
return result;
}
// 表格配置
const gridOptions: VxeGridProps<FlatNode> = {
border: true,
stripe: true,
highlightHoverRow: true,
showOverflow: true,
size: 'small',
expandAll: true,
columnConfig: {
resizable: true,
},
rowConfig: {
isHover: true,
},
columns: [
{
type: 'seq',
width: 60,
title: '序号',
},
{
field: 'name',
title: '权限名称',
minWidth: 200,
slots: { default: 'name' },
},
{
field: 'code',
title: '权限码',
minWidth: 200,
slots: { default: 'code' },
},
{
field: 'type',
title: '类型',
width: 120,
slots: { default: 'type' },
},
{
field: 'sortOrder',
title: '排序',
width: 80,
},
{
field: 'status',
title: '状态',
width: 100,
slots: { default: 'status' },
},
],
data: [],
};
const [Grid] = useVbenVxeGrid({
gridOptions,
});
function getTypeText(type: string) {
const map: Record<string, string> = {
api: '接口权限',
button: '按钮权限',
data: '数据权限',
module: '模块分组',
};
return map[type] || type;
}
function getTypeColor(type: string) {
const map: Record<string, string> = {
api: 'blue',
button: 'green',
data: 'orange',
module: 'purple',
};
return map[type] || 'default';
}
function getTypeTagColor(type: string) {
const map: Record<string, string> = {
api: 'processing',
button: 'success',
data: 'warning',
module: 'purple',
};
return map[type] || 'default';
}
onMounted(() => {
loadSystemPermissions().then(() => {
const flatData = flattenTree(permissionData.value);
const grid = (Grid as any).grid;
if (grid) {
grid.loadData(flatData);
}
});
});
</script>
<template>
<Page title="系统权限" auto-content-height>
<template #description>
<div class="text-foreground/80 mt-2">
系统管理模块下的完整权限树,用于查看系统权限结构(平台超管专用)
</div>
</template>
<Spin :spinning="loading">
<Grid>
<template #name="{ row }">
<span
class="font-medium"
:class="row.type === 'module' ? 'text-[var(--text-primary)]' : 'text-[var(--text-secondary)]'"
:style="{ paddingLeft: (row.level * 20) + 'px' }"
>
<span v-if="row.hasChildren" class="mr-1">📁</span>
<span v-else class="mr-1">📄</span>
{{ row.name }}
</span>
</template>
<template #code="{ row }">
<code class="text-xs text-gray-500">{{ row.code }}</code>
</template>
<template #type="{ row }">
<Tag :color="getTypeTagColor(row.type)">
{{ getTypeText(row.type) }}
</Tag>
</template>
<template #status="{ row }">
<Tag :color="row.status === 1 ? 'green' : 'red'">
{{ row.status === 1 ? '启用' : '禁用' }}
</Tag>
</template>
</Grid>
<div v-if="!loading && permissionData.length === 0" class="py-12 text-center text-gray-400">
暂无系统权限数据
</div>
</Spin>
</Page>
</template>