Browse Source

Merge branch 'dev_yantai_shandongtong' of http://git.elinkit.com.cn:7070/r/epmet-cloud

dev
wangxianzhang 3 years ago
parent
commit
f1ea757f35
  1. 72
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java

72
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java

@ -93,7 +93,8 @@ public class SdtServiceImpl implements SdtService {
// token刷新了以后,要刷新缓存中的token,这样下次才能获取到最新的token // token刷新了以后,要刷新缓存中的token,这样下次才能获取到最新的token
String key = RedisKeys.getSdtAccessToken(customerIdInner); String key = RedisKeys.getSdtAccessToken(customerIdInner);
redisUtils.set(key, access_token, expires_in); // 在过期时间前20分钟让token过期,重新获取
redisUtils.set(key, access_token, expires_in - 1200);
}); });
} }
@ -134,34 +135,42 @@ public class SdtServiceImpl implements SdtService {
public String getCachedAccessToken(String customerId) { public String getCachedAccessToken(String customerId) {
String key = RedisKeys.getSdtAccessToken(customerId); String key = RedisKeys.getSdtAccessToken(customerId);
String accessToken = redisUtils.getString(key); String accessToken = redisUtils.getString(key);
if (StringUtils.isNotBlank(accessToken)) { if (StringUtils.isBlank(accessToken)) {
return accessToken.replaceAll("\"", ""); refreshAccessToken(customerId);
accessToken = redisUtils.getString(key);
if (StringUtils.isBlank(accessToken)) {
log.error("【山东通】刷新token-从山东通刷新了token,依然取不到");
} }
// db中查询
SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId);
if (sdtAppInfoEntity == null) {
log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息");
return null;
} }
accessToken = sdtAppInfoEntity.getAccessToken(); return accessToken.replaceAll("\"", "");
Date expiresAt = sdtAppInfoEntity.getExpiresAt();
Long expireIn = -1l; // 只要缓存中没有,就直接去获取新的,不再查数据库了。
if (expiresAt != null) {
expireIn = getExpireIn(expiresAt);
}
if (StringUtils.isNotBlank(accessToken) && expireIn > 0) { // db中查询
// 没过期,可以用 //SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId);
redisUtils.set(key, accessToken, expireIn); //if (sdtAppInfoEntity == null) {
return accessToken; // log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息");
} else { // return null;
// 配置了appInfo,但是没有accessToken或者accessToken已经过期,刷新token,刷新缓存,然后重新从缓存中取 //}
refreshAccessToken(customerId); //
return redisUtils.getString(key); //accessToken = sdtAppInfoEntity.getAccessToken();
} //Date expiresAt = sdtAppInfoEntity.getExpiresAt();
//
//Long expireIn = -1l;
//if (expiresAt != null) {
// expireIn = getExpireIn(expiresAt);
//}
//
//if (StringUtils.isNotBlank(accessToken) && expireIn > 0) {
// // 没过期,可以用
// redisUtils.set(key, accessToken, expireIn);
// return accessToken;
//} else {
// // 配置了appInfo,但是没有accessToken或者accessToken已经过期,刷新token,刷新缓存,然后重新从缓存中取
// refreshAccessToken(customerId);
// return redisUtils.getString(key);
//}
} }
/** /**
@ -178,7 +187,7 @@ public class SdtServiceImpl implements SdtService {
} }
@Override @Override
public SdtStaffInfoResult getUserInfoByCode(String authCode, String customerId) { public SdtStaffInfoResult getUserInfoByCode(String authCode, String customerId, Integer requestTimes) {
SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId); SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId);
if (sdtAppInfoEntity == null) { if (sdtAppInfoEntity == null) {
log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息"); log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息");
@ -191,7 +200,7 @@ public class SdtServiceImpl implements SdtService {
log.info("【山东通】获取用户信息,code:{}, access_token:{}", authCode, getCachedAccessToken(customerId)); log.info("【山东通】获取用户信息,code:{}, access_token:{}", authCode, getCachedAccessToken(customerId));
Result<String> sdtResult = HttpClientManager.getInstance().sendGet(sdtAppInfoEntity.getApiAddress() + API_GET_USER_INFO_URL, params); Result<String> sdtResult = new Result<String>().ok("{\"errcode\":40014,\"errmsg\":\"invalid access_token [logid:]\"}"); //HttpClientManager.getInstance().sendGet(sdtAppInfoEntity.getApiAddress() + API_GET_USER_INFO_URL, params);
if (!sdtResult.success()) { if (!sdtResult.success()) {
// http状态判断 // http状态判断
log.error("【山东通登录】查询用户编码-http错误:{}", sdtResult.getMsg()); log.error("【山东通登录】查询用户编码-http错误:{}", sdtResult.getMsg());
@ -203,10 +212,19 @@ public class SdtServiceImpl implements SdtService {
SdtStaffInfoResult staffInfoResult = JSON.parseObject(sdtResult.getData(), SdtStaffInfoResult.class); SdtStaffInfoResult staffInfoResult = JSON.parseObject(sdtResult.getData(), SdtStaffInfoResult.class);
if (!staffInfoResult.success()) { if (!staffInfoResult.success()) {
// 山东通业务返回状态判断 // 山东通业务返回状态判断.如果是token问题,并且尝试次数<2(最多尝试2次),那么允许重试一次
if (staffInfoResult.isAccessTokenError() && requestTimes < 2) {
// 如果是token问题,则递归重新获取一下
refreshAccessToken(customerId);
staffInfoResult = getUserInfoByCode(authCode, customerId, ++requestTimes);
}
// 递归重试之后,再判断一下是否正常了,不正常就要报错
if (!staffInfoResult.success()) {
log.error("【山东通登录】查询用户编码失败,错误信息:{}", staffInfoResult.getErrmsg()); log.error("【山东通登录】查询用户编码失败,错误信息:{}", staffInfoResult.getErrmsg());
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null); throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null);
} }
}
return staffInfoResult; return staffInfoResult;
} }

Loading…
Cancel
Save