查询登录用户权限信息,使用UserUtils

This commit is contained in:
2023-11-11 15:21:25 +08:00
parent 6fa87d6fc2
commit 5f44c35d03
14 changed files with 109 additions and 56 deletions

View File

@@ -0,0 +1,75 @@
package com.jsowell.pile.util;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.jsowell.common.core.domain.entity.SysDept;
import com.jsowell.common.core.domain.entity.SysUser;
import com.jsowell.common.core.domain.model.LoginUser;
import com.jsowell.common.core.domain.vo.AuthorizedDeptVO;
import com.jsowell.common.util.SecurityUtils;
import com.jsowell.pile.service.IMemberBasicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 后管登录用户工具类
*/
// 1.将工具类注入到容器中
@Slf4j
@Component
public class UserUtils {
// 2.注入想引用的对象的静态实例
private static IMemberBasicInfoService memberBasicInfoService;
// 3.初始化有参构造器
@Autowired
public UserUtils(IMemberBasicInfoService memberBasicInfoService) {
UserUtils.memberBasicInfoService = memberBasicInfoService;
}
// 4.使用
// 查询登录用户权限
public static AuthorizedDeptVO getAuthorizedMap() {
// 登录用户
LoginUser loginUser = SecurityUtils.getLoginUser();
if (loginUser == null) {
return null;
}
// 用户
SysUser user = loginUser.getUser();
if (user == null) {
return null;
}
AuthorizedDeptVO resultVO = AuthorizedDeptVO.builder().build();
// 登录用户所属部门(运营商或站点 根据父级id判断
SysDept dept = user.getDept();
if (dept == null) {
log.info("用户id:{}, 获取不到所属部门信息", user.getUserId());
return null;
}
String deptId = String.valueOf(dept.getDeptId());
resultVO.setDeptId(deptId);
log.info("用户id:{}, 所属部门信息:{}", user.getUserId(), JSONObject.toJSONString(dept));
// 登录用户角色
Long parentId = dept.getParentId();
resultVO.setParentId(String.valueOf(parentId));
List<String> merchantDeptIds = Lists.newArrayList();
List<String> stationDeptIds = Lists.newArrayList();
if (parentId == 0L) {
// 父级id是0表明是该账号挂在平台下面是系统管理员
} else if (parentId == 100L) {
// 父级id是100表明是该账号挂在运营商下面是运营商管理员
merchantDeptIds.add(deptId);
resultVO.setMerchantDeptIds(merchantDeptIds);
} else {
// 其他情况,表明是站点管理员
stationDeptIds.add(deptId);
resultVO.setStationDeptIds(stationDeptIds);
}
return resultVO;
}
}