fix(SysDeptServiceImpl): 修复超级管理员部门为null查询部门列表报错问题

This commit is contained in:
郝先瑞 2022-01-27 22:08:44 +08:00
parent 92de8e35f7
commit 742d9b5b40
3 changed files with 51 additions and 9 deletions

View File

@ -9,7 +9,7 @@ package com.youlai.admin.constant;
public interface SystemConstants {
/**
* 根部门ID
* 根部门ID
*/
Long ROOT_DEPT_ID = 0l;
@ -23,6 +23,8 @@ public interface SystemConstants {
*/
String DEFAULT_USER_PASSWORD = "123456";
/**
* 超级管理员角色编码
*/
String ROOT_ROLE_CODE = "ROOT";
}

View File

@ -120,8 +120,24 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
.eq(SysDept::getStatus, GlobalConstants.STATUS_YES)
.orderByAsc(SysDept::getSort)
);
SysDept sysDept = this.getById(JwtUtils.getJwtPayload().getLong("deptId"));
List<IdLabelVO> deptSelectList = recursionTreeSelectList(sysDept.getParentId(), deptList);
boolean isRoot = JwtUtils.isRoot();
Long parentId;
if (isRoot) { // 超级管理员
parentId = SystemConstants.ROOT_DEPT_ID;
} else {
Long deptId = JwtUtils.getDeptId();
if (deptId == null) {
return Collections.emptyList();
}
SysDept dept = this.getById(deptId);
if (dept == null) {
return Collections.emptyList();
}
parentId = dept.getParentId();
}
List<IdLabelVO> deptSelectList = recursionTreeSelectList(parentId, deptList);
return deptSelectList;
}

View File

@ -1,5 +1,6 @@
package com.youlai.common.web.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.youlai.common.constant.SecurityConstants;
@ -11,6 +12,8 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -27,7 +30,7 @@ public class JwtUtils {
if (null == payload) {
throw new BizException("请传入认证头");
}
JSONObject jsonObject = JSONUtil.parseObj(URLDecoder.decode(payload,StandardCharsets.UTF_8.name()));
JSONObject jsonObject = JSONUtil.parseObj(URLDecoder.decode(payload, StandardCharsets.UTF_8.name()));
return jsonObject;
}
@ -41,6 +44,16 @@ public class JwtUtils {
return id;
}
/**
* 解析JWT获取用户ID
*
* @return
*/
public static Long getDeptId() {
Long id = getJwtPayload().getLong("deptId");
return id;
}
/**
* 解析JWT获取获取用户名
*
@ -52,18 +65,29 @@ public class JwtUtils {
}
/**
* JWT获取用户角色列表
*
* @return 角色列表
*/
public static List<String> getRoles() {
List<String> roles = null;
List<String> roles;
JSONObject payload = getJwtPayload();
if (payload.containsKey(SecurityConstants.JWT_AUTHORITIES_KEY)) {
roles = payload.getJSONArray(SecurityConstants.JWT_AUTHORITIES_KEY).toList(String.class);
} else {
roles = Collections.emptyList();
}
return roles;
}
/**
* 是否超级管理员
*
* @return
*/
public static boolean isRoot() {
List<String> roles = getRoles();
return CollectionUtil.isNotEmpty(roles) && roles.contains("ROOT");
}
}