【安眼】缓存用户部门信息及登录逻辑
This commit is contained in:
+6
@@ -297,11 +297,17 @@ public class AnYanClientExecutor implements AnYanClient {
|
||||
@Override
|
||||
@Retryable(maxAttempts = 2)
|
||||
public AnYanUserInfoResult getAnYanUserInfo(String token, String username) {
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(AnYanConstants.AUTHORIZATION, AnYanConstants.TOKEN_TYPE + token);
|
||||
ResponseEntity<AnYanUserInfoResult> exchange = RestTemplateUtil.getExchange(AnYanApiEnum.USER_INFO_UNIT_V2, headers, AnYanUserInfoResult.class);
|
||||
redisClientUtil.set(new RedisKeyPrefix(-1, AnYanConstants.AN_YAN_API_USERINFO_PREFIX), username, exchange);
|
||||
return BeanUtil.toBean(exchange.getBody(), AnYanUserInfoResult.class);
|
||||
} catch (Exception e) {
|
||||
log.error("获取用户信息失败:{}", e.getMessage());
|
||||
ExceptionAssertsUtil.fail("获取用户信息失败");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+6
@@ -2,6 +2,7 @@ package com.renkang.restaurant.config;
|
||||
|
||||
import com.renkang.anyan.client.AnYanClient;
|
||||
import com.renkang.anyan.config.AnYanProperties;
|
||||
import com.renkang.anyan.enums.AnYanApiEnum;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.exception.ExceptionAssertsUtil;
|
||||
@@ -59,9 +60,14 @@ public class AnYanRestTemplateConfig {
|
||||
execute = clientHttpRequestExecution.execute(httpRequest, bytes);
|
||||
HttpStatus httpStatus = execute.getStatusCode();
|
||||
if (Arrays.asList(HttpStatus.UNAUTHORIZED, HttpStatus.FORBIDDEN).contains(httpStatus)) {
|
||||
// 获取请求URL
|
||||
String url = httpRequest.getURI().toString();
|
||||
// 排除特定接口不执行刷新token逻辑(获取token接口,根据token获取个人信息接口)
|
||||
if (!url.contains(AnYanApiEnum.QUERY_AUTH_TOKEN.getMethodPath()) && !url.contains(AnYanApiEnum.USER_INFO_UNIT_V2.getMethodPath())) {
|
||||
log.info("---------安眼token过期,触发刷新token机制-------已过期的token:{}", httpRequest.getHeaders().getFirst("authorization"));
|
||||
SpringContextUtils.getBean("anYanClientExecutor", AnYanClient.class).refreshToken();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("---------安眼平台API调用异常-------e{},接口:{}", e.getMessage(), httpRequest.getURI(), e);
|
||||
ExceptionAssertsUtil.fail("安眼平台API调用失败");
|
||||
|
||||
+40
-3
@@ -29,6 +29,7 @@ import org.jeecg.modules.system.bean.UserEmployee;
|
||||
import org.jeecg.modules.system.entity.HealthUserEmployeeEx;
|
||||
import org.jeecg.modules.system.entity.SysDepart;
|
||||
import org.jeecg.modules.system.entity.SysUser;
|
||||
import org.jeecg.modules.system.manager.DepartCacheManager;
|
||||
import org.jeecg.modules.system.model.SysLoginModel;
|
||||
import org.jeecg.modules.system.service.*;
|
||||
import org.jeecg.util.RSAEncryptUtils;
|
||||
@@ -58,6 +59,7 @@ public class LoginAnYanServiceImpl implements ILoginAnYanService {
|
||||
private final RedisUtil redisUtil;
|
||||
private final TaskExecutor taskExecutor;
|
||||
private final LoginService loginService;
|
||||
private final DepartCacheManager departCacheManager;
|
||||
|
||||
private static final String CLIENT_TYPE_PC = "pc";
|
||||
private static final String CLIENT_TYPE_APP = "app";
|
||||
@@ -116,14 +118,25 @@ public class LoginAnYanServiceImpl implements ILoginAnYanService {
|
||||
return Result.error(anYanAuthResult != null ? anYanAuthResult.getMessage() : "认证服务器无响应");
|
||||
}
|
||||
|
||||
AnYanUserInfoResult anYanUserInfo = null;
|
||||
try {
|
||||
// 授权成功获取用户信息(统一认证的平台接口没有做统一封装,成功和失败均返回一个不同的json字符串, 这里只能判断没有获取到用户信息则认为是失败)
|
||||
AnYanUserInfoResult anYanUserInfo = anYanClient.getAnYanUserInfo(anYanAuthResult.getAccess_token(), username);
|
||||
anYanUserInfo = anYanClient.getAnYanUserInfo(anYanAuthResult.getAccess_token(), username);
|
||||
if (anYanUserInfo == null || anYanUserInfo.getEmployeeUnitDTO() == null) {
|
||||
return Result.error("登录失败,请先联系本单位掌油管理员确认个人机构信息");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取用户信息异常: {}", e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查并更新用户信息
|
||||
if (anYanUserInfo != null) {
|
||||
checkUserAndUpdate(anYanUserInfo, decryptedPassword);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("检查并更新用户信息异常: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 获取本地用户信息(由于两个平台的密码可能不一致,所以去掉本平台的密码匹配)
|
||||
SysUser sysUser = sysUserService.getUserByNameOrWorkNo(username, null);
|
||||
@@ -344,12 +357,19 @@ public class LoginAnYanServiceImpl implements ILoginAnYanService {
|
||||
//新增时设置为叶子节点
|
||||
depart.setIzLeaf(CommonConstant.IS_LEAF);
|
||||
save = sysDepartService.save(depart);
|
||||
if (save) {
|
||||
//缓存
|
||||
departCacheManager.putCache(depart);
|
||||
}
|
||||
} else {
|
||||
save = sysDepartService.update(new LambdaUpdateWrapper<SysDepart>()
|
||||
.eq(SysDepart::getId, depart.getId())
|
||||
.set(SysDepart::getDepartName, orgName)
|
||||
.set(SysDepart::getUpdateTime, new Date())
|
||||
);
|
||||
if (save) {
|
||||
updateCache(depart);
|
||||
}
|
||||
}
|
||||
if (save) {
|
||||
SysDepart parentDepart = sysDepartService.getOne(new LambdaQueryWrapper<SysDepart>()
|
||||
@@ -357,20 +377,26 @@ public class LoginAnYanServiceImpl implements ILoginAnYanService {
|
||||
);
|
||||
// 判断父级组织是否为叶子节点,0不是叶子节点,1是叶子节点,是则更新为非叶子节点
|
||||
if (parentDepart != null && parentDepart.getIzLeaf().equals(CommonConstant.IS_LEAF)) {
|
||||
sysDepartService.update(new LambdaUpdateWrapper<SysDepart>()
|
||||
boolean update = sysDepartService.update(new LambdaUpdateWrapper<SysDepart>()
|
||||
.eq(SysDepart::getId, parentId)
|
||||
.set(SysDepart::getIzLeaf, CommonConstant.NOT_LEAF)
|
||||
);
|
||||
if (update) {
|
||||
updateCache(depart);
|
||||
}
|
||||
}
|
||||
long count = sysDepartService.count(new LambdaQueryWrapper<SysDepart>()
|
||||
.eq(SysDepart::getParentId, depart.getId())
|
||||
);
|
||||
// 判断当前节点是否存在子集,不存在则设置为叶子节点
|
||||
if (count == 0) {
|
||||
sysDepartService.update(new LambdaUpdateWrapper<SysDepart>()
|
||||
boolean update = sysDepartService.update(new LambdaUpdateWrapper<SysDepart>()
|
||||
.eq(SysDepart::getId, depart.getId())
|
||||
.set(SysDepart::getIzLeaf, CommonConstant.IS_LEAF)
|
||||
);
|
||||
if (update) {
|
||||
updateCache(depart);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 父id赋值
|
||||
@@ -381,6 +407,17 @@ public class LoginAnYanServiceImpl implements ILoginAnYanService {
|
||||
return orgId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author: pj
|
||||
* @description: 更新部门缓存
|
||||
* @date 2026/2/2 15:46
|
||||
*/
|
||||
public void updateCache(SysDepart depart) {
|
||||
departCacheManager.evictCache(depart);
|
||||
SysDepart byId = sysDepartService.getById(depart.getId());
|
||||
departCacheManager.putCache(byId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 更新员工其他信息
|
||||
* @author PengJ
|
||||
|
||||
Reference in New Issue
Block a user