|
|
@ -93,7 +93,8 @@ public class SdtServiceImpl implements SdtService { |
|
|
|
// token刷新了以后,要刷新缓存中的token,这样下次才能获取到最新的token
|
|
|
|
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) { |
|
|
|
String key = RedisKeys.getSdtAccessToken(customerId); |
|
|
|
String accessToken = redisUtils.getString(key); |
|
|
|
if (StringUtils.isNotBlank(accessToken)) { |
|
|
|
return accessToken.replaceAll("\"", ""); |
|
|
|
} |
|
|
|
|
|
|
|
// db中查询
|
|
|
|
SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId); |
|
|
|
if (sdtAppInfoEntity == null) { |
|
|
|
log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息"); |
|
|
|
return null; |
|
|
|
if (StringUtils.isBlank(accessToken)) { |
|
|
|
refreshAccessToken(customerId); |
|
|
|
accessToken = redisUtils.getString(key); |
|
|
|
if (StringUtils.isBlank(accessToken)) { |
|
|
|
log.error("【山东通】刷新token-从山东通刷新了token,依然取不到"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
accessToken = sdtAppInfoEntity.getAccessToken(); |
|
|
|
Date expiresAt = sdtAppInfoEntity.getExpiresAt(); |
|
|
|
return accessToken.replaceAll("\"", ""); |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
// db中查询
|
|
|
|
//SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId);
|
|
|
|
//if (sdtAppInfoEntity == null) {
|
|
|
|
// log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息");
|
|
|
|
// return null;
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//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 |
|
|
|
public SdtStaffInfoResult getUserInfoByCode(String authCode, String customerId) { |
|
|
|
public SdtStaffInfoResult getUserInfoByCode(String authCode, String customerId, Integer requestTimes) { |
|
|
|
SdtAppInfoEntity sdtAppInfoEntity = getSdtInfoByCustomerId(customerId); |
|
|
|
if (sdtAppInfoEntity == null) { |
|
|
|
log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息"); |
|
|
@ -191,7 +200,7 @@ public class SdtServiceImpl implements SdtService { |
|
|
|
|
|
|
|
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()) { |
|
|
|
// http状态判断
|
|
|
|
log.error("【山东通登录】查询用户编码-http错误:{}", sdtResult.getMsg()); |
|
|
@ -203,9 +212,18 @@ public class SdtServiceImpl implements SdtService { |
|
|
|
|
|
|
|
SdtStaffInfoResult staffInfoResult = JSON.parseObject(sdtResult.getData(), SdtStaffInfoResult.class); |
|
|
|
if (!staffInfoResult.success()) { |
|
|
|
// 山东通业务返回状态判断
|
|
|
|
log.error("【山东通登录】查询用户编码失败,错误信息:{}", staffInfoResult.getErrmsg()); |
|
|
|
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null); |
|
|
|
// 山东通业务返回状态判断.如果是token问题,并且尝试次数<2(最多尝试2次),那么允许重试一次
|
|
|
|
if (staffInfoResult.isAccessTokenError() && requestTimes < 2) { |
|
|
|
// 如果是token问题,则递归重新获取一下
|
|
|
|
refreshAccessToken(customerId); |
|
|
|
staffInfoResult = getUserInfoByCode(authCode, customerId, ++requestTimes); |
|
|
|
} |
|
|
|
|
|
|
|
// 递归重试之后,再判断一下是否正常了,不正常就要报错
|
|
|
|
if (!staffInfoResult.success()) { |
|
|
|
log.error("【山东通登录】查询用户编码失败,错误信息:{}", staffInfoResult.getErrmsg()); |
|
|
|
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return staffInfoResult; |
|
|
|