feat(dept): 新增部门树形组件功能

- 创建 CuteDeptTreeVo 模型类用于部门树形结构数据传输
- 实现 CuteDeptTreeService 服务类处理部门树形数据查询逻辑
- 开发 CuteDeptTreeApi 接口提供部门树形数据查询功能
- 将 SystemDeptService 中的 filterRootDeptList 和 queryDeptByArgs 方法改为 public 以便外部调用
- 前端创建 cute-dept-tree API 接口文件封装请求方法
- 更新 CuteDeptTree 组件使用新的部门树形接口并调整数据处理逻辑
- 修改组件 props 类型从 Number/String 改为 String 类型
This commit is contained in:
2026-01-17 09:34:42 +08:00
parent d113ebfa2f
commit 4eaa383717
6 changed files with 164 additions and 11 deletions
@@ -0,0 +1,12 @@
import request from '@/utils/request'
export function listMetadata(query) {
return request({
url: 'api/component/CuteDeptTree/listMetadata',
method: 'post',
data: query
})
}
export default { listMetadata }
@@ -30,13 +30,13 @@
<script>
import { searchDept } from '@/api/system/dept'
import CuteDeptTreeApi from '@/api/application/cute-dept-tree'
export default {
name: 'CuteDeptTree',
props: {
value: {
type: [Number, String],
type: String,
required: false,
default: null
}
@@ -61,15 +61,11 @@ export default {
params['pid'] = node.data.id
}
setTimeout(() => {
searchDept({
page: 1,
size: 1000,
args: params
}).then(res => {
CuteDeptTreeApi.listMetadata(params).then(res => {
if (resolve) {
resolve(res.content)
resolve(res)
} else {
this.deptData = res.content
this.deptData = res
}
})
}, 100)
@@ -0,0 +1,32 @@
package cn.odboy.system.application;
import cn.odboy.system.application.model.CuteDeptTreeVo;
import cn.odboy.system.application.service.CuteDeptTreeService;
import cn.odboy.system.dal.dataobject.SystemDeptTb;
import cn.odboy.system.dal.model.request.SystemQueryDeptArgs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@Api(tags = "系统组件:CuteDeptTree")
@RequestMapping("/api/component/CuteDeptTree")
public class CuteDeptTreeApi {
@Autowired
private CuteDeptTreeService cuteDeptTreeService;
@ApiOperation("查询部门")
@PostMapping(value = "/listMetadata")
public ResponseEntity<List<CuteDeptTreeVo>> listMetadata(@Validated @RequestBody SystemQueryDeptArgs args) throws Exception {
List<CuteDeptTreeVo> depts = cuteDeptTreeService.listMetadata(args);
return ResponseEntity.ok(depts);
}
}
@@ -0,0 +1,48 @@
package cn.odboy.system.application.model;
import cn.odboy.base.KitObject;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CuteDeptTreeVo extends KitObject {
/**
* 部门ID
*/
private String id;
/**
* 部门名称
*/
private String name;
/**
* 上级部门ID
*/
private String pid;
/**
* 子节点数目
*/
private Integer subCount = 0;
/**
* 是否有子节点
*/
public Boolean getHasChildren() {
return subCount > 0;
}
/**
* 是否为叶子
*/
public Boolean getLeaf() {
return subCount <= 0;
}
/**
* 标签名称
*/
public String getLabel() {
return name;
}
}
@@ -0,0 +1,65 @@
package cn.odboy.system.application.service;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.odboy.system.application.model.CuteDeptTreeVo;
import cn.odboy.system.constant.SystemDataScopeEnum;
import cn.odboy.system.dal.dataobject.SystemDeptTb;
import cn.odboy.system.dal.model.request.SystemQueryDeptArgs;
import cn.odboy.system.framework.permission.core.KitSecurityHelper;
import cn.odboy.system.service.SystemDeptService;
import cn.odboy.util.KitClassUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CuteDeptTreeService {
@Autowired
private SystemDeptService systemDeptService;
public List<CuteDeptTreeVo> listMetadata(SystemQueryDeptArgs args) throws Exception {
String dataScopeType = KitSecurityHelper.getDataScopeType();
if (dataScopeType.equals(SystemDataScopeEnum.ALL.getValue())) {
args.setPidIsNull(true);
}
List<Field> fields = KitClassUtil.getAllFields(args.getClass(), new ArrayList<>());
List<String> fieldNames = new ArrayList<>() {{
add("pidIsNull");
add("enabled");
}};
for (Field field : fields) {
// 设置对象的访问权限, 保证对private的属性的访问
field.setAccessible(true);
Object val = field.get(args);
if (fieldNames.contains(field.getName())) {
continue;
}
if (ObjectUtil.isNotNull(val)) {
args.setPidIsNull(null);
break;
}
}
// 数据权限
args.setIds(KitSecurityHelper.getCurrentUserDataScope());
List<SystemDeptTb> list = systemDeptService.queryDeptByArgs(args);
// 如果为空, 就代表为自定义权限或者本级权限, 就需要去重, 不理解可以注释掉,看查询结果
if (StrUtil.isBlank(dataScopeType)) {
return systemDeptService.filterRootDeptList(list).stream().map(this::convertToVo).collect(Collectors.toList());
}
return list.stream().map(this::convertToVo).collect(Collectors.toList());
}
private CuteDeptTreeVo convertToVo(SystemDeptTb record) {
CuteDeptTreeVo treeVo = new CuteDeptTreeVo();
treeVo.setId(Long.toString(record.getId()));
treeVo.setName(record.getName());
treeVo.setPid(record.getPid() == null ? null : Long.toString(record.getPid()));
treeVo.setSubCount(record.getSubCount());
return treeVo;
}
}
@@ -403,7 +403,7 @@ public class SystemDeptService {
/**
* 筛选出 list 中没有父部门(即 pid 不在其他部门 id 列表中的部门)的部门列表
*/
private List<SystemDeptTb> filterRootDeptList(List<SystemDeptTb> list) {
public List<SystemDeptTb> filterRootDeptList(List<SystemDeptTb> list) {
List<SystemDeptTb> deptList = new ArrayList<>();
// 使用 Set 存储所有部门的 id
Set<Long> idSet = list.stream().map(SystemDeptTb::getId).collect(Collectors.toSet());
@@ -422,7 +422,7 @@ public class SystemDeptService {
* @param args /
* @return /
*/
private List<SystemDeptTb> queryDeptByArgs(SystemQueryDeptArgs args) {
public List<SystemDeptTb> queryDeptByArgs(SystemQueryDeptArgs args) {
LambdaQueryWrapper<SystemDeptTb> wrapper = new LambdaQueryWrapper<>();
if (args != null) {
wrapper.in(CollUtil.isNotEmpty(args.getIds()), SystemDeptTb::getId, args.getIds());