feat:部门权限

This commit is contained in:
dzc committed 2025-07-04 18:03:24 +08:00
1 parent 78d2cac2aa
commit f774f7313f
3 files changed
+59 -29

No files matched your search

@@ -92,28 +92,10 @@ public class SysDeptController {
*/
@GetMapping(value = "/tree")
public R getTree(String deptName, Long parentId) {
Long deptId = null;
try {
PjcloudUser user = SecurityUtils.getUser();
deptId = user.getDeptId();
} catch (Exception e) {
log.error("获取当前用户ID异常", e);
}
if (ObjectUtil.isEmpty(parentId)) {
parentId = deptId;
}
return R.ok(sysDeptService.selectTree(deptName, parentId, DataScope.of()));
PjcloudUser user = SecurityUtils.getUser();
return R.ok(sysDeptService.selectTree(deptName, parentId, user));
}
/**
* 返回树形菜单集合
*/
@GetMapping(value = "/tree-select")
public R getTreeSelect(String deptName, Long parentId) {
return R.ok(sysDeptService.selectTree(deptName, parentId, null));
}
/**
* 查询所有,无权限过滤
* simpleTree
@@ -27,6 +27,7 @@ import com.bsd.pjcloud.admin.api.vo.DeptExcelVO;
import com.bsd.pjcloud.admin.api.vo.OrgTreeVO;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.security.service.PjcloudUser;
import jakarta.validation.Valid;
import org.springframework.validation.BindingResult;
@@ -49,7 +50,7 @@ public interface SysDeptService extends IService<SysDept> {
* @param deptName 部门名称
* @return 树
*/
List<Tree<Long>> selectTree(String deptName, Long parentId, DataScope dataScope);
List<Tree<Long>> selectTree(String deptName, Long parentId, PjcloudUser user);
List<Tree<Long>> selectSimpleTree(String deptName, Long parentId);
@@ -42,6 +42,8 @@ import com.bsd.pjcloud.common.core.constant.enums.YesNoEnum;
import com.bsd.pjcloud.common.core.util.R;
import com.bsd.pjcloud.common.data.datascope.DataScope;
import com.bsd.pjcloud.common.excel.vo.ErrorMessage;
import com.bsd.pjcloud.common.security.service.PjcloudUser;
import com.bsd.pjcloud.common.security.util.SecurityUtils;
import com.bsd.pjcloud.wallet.api.feign.RemoteWalletService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
@@ -132,11 +134,10 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
* @return 树 部门名称
*/
@Override
public List<Tree<Long>> selectTree(String deptName, Long parentId, DataScope dataScope) {
List<SysDept> deptAllList = new ArrayList<>();
public List<Tree<Long>> selectTree(String deptName, Long parentId, PjcloudUser user) {
// 查询全部部门
LambdaQueryWrapper<SysDept> queryWrapper = Wrappers.<SysDept>lambdaQuery().like(StrUtil.isNotBlank(deptName), SysDept::getName, deptName);
deptAllList = deptMapper.selectListByScope(queryWrapper, dataScope);
List<SysDept> deptAllList = deptMapper.selectList(queryWrapper);
// 查询数据权限内部门
List<Long> deptOwnIdList = deptMapper
.selectListByScope(
@@ -159,6 +160,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
// 有权限不返回标识
Map<String, Object> extra = new HashMap<>(8);
extra.put("isLock", !deptOwnIdList.contains(dept.getDeptId()));
extra.put("visible", false);
extra.put("createTime", dept.getCreateTime());
treeNode.setExtra(extra);
return treeNode;
@@ -174,13 +176,58 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
return tree;
}).collect(Collectors.toList());
}
if (ObjectUtil.isNotEmpty(parentId)) {
SysDept parentDept = getById(parentId);
if (ObjectUtil.isNotEmpty(parentDept) && ObjectUtil.isNotEmpty(parentDept.getParentId())) {
parentId = parentDept.getParentId();
List<Tree<Long>> tree = TreeUtil.build(collect, parentId == null ? 0 : parentId);
if (ObjectUtil.isNotEmpty(tree)) {
if (ObjectUtil.isNotEmpty(user) && ObjectUtil.isNotEmpty(user.getDeptId())) {
for (Tree<Long> rootNode : tree) {
markDeptAndRelatedVisible(rootNode, user.getDeptId());
}
}
}
return TreeUtil.build(collect, parentId == null ? 0 : parentId);
return tree;
}
/**
* 如果都是在targetDeptId这树上的visible都为true
*/
private void markDeptAndRelatedVisible(Tree<Long> node, Long targetDeptId) {
boolean shouldMarkVisible = isTargetOrAncestor(node, targetDeptId) || hasChildMatching(node, targetDeptId);
if (shouldMarkVisible) {
node.put("visible", true);
}
if (node.getChildren() != null) {
for (Tree<Long> child : node.getChildren()) {
markDeptAndRelatedVisible(child, targetDeptId);
}
}
}
/**
* 判断当前节点是否是目标节点,或者其父节点链上包含目标节点
*/
private boolean isTargetOrAncestor(Tree<Long> node, Long targetDeptId) {
if (node.getId().equals(targetDeptId)) {
return true;
}
Tree<Long> parent = node.getParent();
while (parent != null) {
if (parent.getId().equals(targetDeptId)) {
return true;
}
parent = parent.getParent();
}
return false;
}
/**
* 检查该节点及其子节点中是否存在目标节点
*/
private boolean hasChildMatching(Tree<Long> node, Long targetDeptId) {
if (node.getChildren() == null) {
return false;
}
return node.getChildren().stream().anyMatch(child -> child.getId().equals(targetDeptId) || hasChildMatching(child, targetDeptId));
}
@Override