refactor: 获取不到用户信息时抛出异常,不使用默认用户

This commit is contained in:
曹鹏飞 2024-04-10 15:02:10 +08:00
parent 7a53dfce58
commit 33fa764e40
2 changed files with 47 additions and 28 deletions

View File

@ -440,8 +440,8 @@ public class EbomApi extends BaseApi {
dto.setRealName("郑军榕");
dto.setDepartName("信息流程中心/IT支持");
dto.setFullDeptName("信息流程中心/IT支持");
dto.setRowId(1585164668335439881l);
SessionUtil.loginUser.set(dto);
dto.setRowId(1585164668335439881L);
BaseApi.loginUser.set(dto);
return ResultVO.success();
}

View File

@ -1,10 +1,12 @@
package com.nflg.product.base.core.conmon.util;
import cn.hutool.core.util.StrUtil;
import com.nflg.product.base.core.api.BaseApi;
import com.nflg.product.base.core.exception.NflgBusinessException;
import nflg.product.common.constant.STATE;
import nflg.product.common.dto.LoginUserInfoDTO;
import java.util.*;
import java.util.List;
import java.util.Optional;
/**
* @decription
@ -12,7 +14,7 @@ import java.util.*;
* @Date 2022/7/15 9:04
**/
public class SessionUtil {
public static ThreadLocal<LoginUserInfoDTO> loginUser= BaseApi.loginUser;
//public static ThreadLocal<LoginUserInfoDTO> loginUser= BaseApi.loginUser;
/**
* 获取登入用户编号
@ -20,7 +22,8 @@ public class SessionUtil {
* @return
*/
public static Long getRowId() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getRowId).orElse(1585164668335439881L);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getRowId).orElse(1585164668335439881L);
return getUser().getRowId();
}
/**
@ -29,7 +32,9 @@ public class SessionUtil {
* @return
*/
public static String getUserCode() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getUserCode).orElse("QHI17062100");
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getUserCode).orElse("QHI17062100");
return getUser().getUserCode();
}
/**
@ -38,7 +43,8 @@ public class SessionUtil {
* @return
*/
public static String getUserName() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getUserName).orElse("郑军榕");
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getUserName).orElse("郑军榕");
return getUser().getUserName();
}
/**
@ -47,48 +53,61 @@ public class SessionUtil {
* @return
*/
public static Long getPartRowId() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getDepartRowId).orElse(null);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getDepartRowId).orElse(null);
return getUser().getDepartRowId();
}
public static String getRealName() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getRealName).orElse("郑军榕");
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getRealName).orElse("郑军榕");
return getUser().getRealName();
}
public static String getDepartName() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getDepartName).orElse(null);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getDepartName).orElse(null);
return getUser().getDepartName();
}
public static List<Long> getAruList() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getAruList).orElse(null);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getAruList).orElse(null);
return getUser().getAruList();
}
// 获取岗位代码
public static Integer getQueryData() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getQueryData).orElse(null);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getQueryData).orElse(null);
return getUser().getQueryData();
}
public static String getFullDeptName() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getFullDeptName).orElse(null);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getFullDeptName).orElse(null);
return getUser().getFullDeptName();
}
//获取2级部门以下
public static String getSecondFullDeptName() {
String depName = Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getFullDeptName).orElse("");
depName= StrUtil.replace(depName,"OU=","").replace("福建南方路面机械有限公司,DC=nflg","");
List<String> depts = new ArrayList<>(Arrays.asList(StrUtil.split(depName, ",")));
depts.remove("");
Collections.reverse(depts );
return StrUtil.join("/",depts);
}
// //获取2级部门以下
// public static String getSecondFullDeptName() {
// String depName = Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getFullDeptName).orElse("");
//
// depName= StrUtil.replace(depName,"OU=","").replace("福建南方路面机械有限公司,DC=nflg","");
// List<String> depts = new ArrayList<>(Arrays.asList(StrUtil.split(depName, ",")));
// depts.remove("");
// Collections.reverse(depts );
// return StrUtil.join("/",depts);
//
//
// }
public static String getLanguage() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getLanguage).orElse("");
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getLanguage).orElse("");
return getUser().getLanguage();
}
public static String getSessionKey() {
return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getSessionKey).orElse(null);
//return Optional.ofNullable(loginUser.get()).map(LoginUserInfoDTO::getSessionKey).orElse(null);
return getUser().getSessionKey();
}
private static LoginUserInfoDTO getUser() {
return Optional.ofNullable(BaseApi.loginUser.get())
.orElseThrow(() -> new NflgBusinessException(STATE.LoginError, "请登录"));
}
}