|
|
@@ -14,12 +14,11 @@ import com.kym.common.utils.CommUtil;
|
|
|
import com.kym.common.utils.PlatformAesUtil;
|
|
|
import com.kym.entity.common.RedisKeys;
|
|
|
import com.kym.entity.enplus.EnRespQueryToken;
|
|
|
-import com.kym.entity.enplus.response.EnResponse;
|
|
|
+import com.kym.entity.enplus.response.PlatformResponse;
|
|
|
import com.kym.service.cache.PlatformCache;
|
|
|
-import com.kym.service.enplus.EnPlusService;
|
|
|
+import com.kym.service.enplus.PlatformApiService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -30,14 +29,13 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author skyline
|
|
|
- * @description EN+平台接口服务
|
|
|
- * @date 2023-07-29 14:43
|
|
|
+ * 互联互通平台接口服务
|
|
|
*/
|
|
|
@Service
|
|
|
-public class EnPlusServiceImpl implements EnPlusService {
|
|
|
+@Slf4j
|
|
|
+public class PlatformApiServiceImpl implements PlatformApiService {
|
|
|
|
|
|
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
- private static final Logger LOGGER = LoggerFactory.getLogger(EnPlusServiceImpl.class);
|
|
|
static OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder().build();
|
|
|
private final RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
@@ -48,7 +46,7 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
@Value("${en-plus.sigSecret}")
|
|
|
private String SigSecret;
|
|
|
|
|
|
- public EnPlusServiceImpl(RedisTemplate<String, String> redisTemplate) {
|
|
|
+ public PlatformApiServiceImpl(RedisTemplate<String, String> redisTemplate) {
|
|
|
this.redisTemplate = redisTemplate;
|
|
|
}
|
|
|
|
|
|
@@ -74,7 +72,7 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public EnResponse platformPost(String platformName, String url, String params) {
|
|
|
+ public PlatformResponse platformPost(String platformName, String url, String params) {
|
|
|
// token获取
|
|
|
var token = queryPlatformToken(platformName);
|
|
|
Headers headers = Headers.of("Authorization", "Bearer ".concat(token));
|
|
|
@@ -84,12 +82,12 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
.post(requestBody)
|
|
|
.url(url)
|
|
|
.build();
|
|
|
- var response = parse(synchronizedCall(request), EnResponse.class);
|
|
|
+ var response = parse(synchronizedCall(request), PlatformResponse.class);
|
|
|
|
|
|
if (0 == response.getRet()) {
|
|
|
return response;
|
|
|
} else {
|
|
|
- LOGGER.error(":url:{}\n params:{}\ntoken:{}\n返回信息:{}", url, params, token, response);
|
|
|
+ log.error(":url:{}\n params:{}\ntoken:{}\n返回信息:{}", url, params, token, response);
|
|
|
if (4002 == response.getRet()) {
|
|
|
// 如果返回Ret=4002,token错误的情况下,删除redis中的token,如果是预约订单则将此订单设置为延迟启动
|
|
|
redisTemplate.delete(RedisKeys.EN_PLUS_TOKEN + platformName);
|
|
|
@@ -106,8 +104,8 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
* @param platformName
|
|
|
* @return
|
|
|
*/
|
|
|
- JSONObject parsePlatformResponseData(EnResponse response, String platformName) {
|
|
|
- return JSONObject.parseObject(PlatformAesUtil.getUtil(PlatformCache.INSTANCE.getPlatformByName(platformName)).decrypt(response.getData()));
|
|
|
+ JSONObject parsePlatformResponseData(PlatformResponse response, String platformName) {
|
|
|
+ return JSONObject.parseObject(PlatformAesUtil.decrypt(PlatformCache.INSTANCE.getPlatformByName(platformName), response.getData()));
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -119,11 +117,11 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
*/
|
|
|
@Override
|
|
|
public String queryPlatformToken(String platformName) {
|
|
|
- LOGGER.info("查询互联互通平台token:{}", platformName);
|
|
|
+ log.info("查询互联互通平台token:{}", platformName);
|
|
|
var token = redisTemplate.opsForValue().get(RedisKeys.EN_PLUS_TOKEN + platformName);
|
|
|
if (CommUtil.isNotEmptyAndNull(token)) {
|
|
|
// 不同平台不同key
|
|
|
- LOGGER.debug("从缓存中查询到token:{},ttl:{}", token, redisTemplate.getExpire(RedisKeys.EN_PLUS_TOKEN + platformName));
|
|
|
+ log.debug("从缓存中查询到token:{},ttl:{}", token, redisTemplate.getExpire(RedisKeys.EN_PLUS_TOKEN + platformName));
|
|
|
return token;
|
|
|
}
|
|
|
var data = """
|
|
|
@@ -135,36 +133,36 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
|
|
|
var requestParams = buildPlatformParams(platformName, data);
|
|
|
|
|
|
- var response = enGetToken(EnPlusApi.EN_PLUS_QUERY_TOKEN.getApi(), requestParams);
|
|
|
+ var response = platformGetToken(EnPlusApi.EN_PLUS_QUERY_TOKEN.getApi(), requestParams);
|
|
|
|
|
|
if (response != null && 0 == response.getRet()) {
|
|
|
// 解密Data获取token
|
|
|
var jsonObject = parsePlatformResponseData(response, platformName);
|
|
|
- var platformRespQueryToken = parse(jsonObject.getString("Data"), EnRespQueryToken.class);
|
|
|
- LOGGER.debug("{}接口AccessToken:{}", platformName, platformRespQueryToken.toString());
|
|
|
+ var platformRespQueryToken = parse(jsonObject.toJSONString(), EnRespQueryToken.class);
|
|
|
+ log.debug("{}接口AccessToken:{}", platformName, platformRespQueryToken.toString());
|
|
|
// 缓存token不同平台不同key,有效期7天(我们这里每次请求en+获取token的有效期并不是从7天开始,有效期是在en+的剩余有效时间)
|
|
|
redisTemplate.opsForValue().set(RedisKeys.EN_PLUS_TOKEN + platformName, platformRespQueryToken.getAccessToken(), platformRespQueryToken.getTokenAvailableTime(), TimeUnit.SECONDS);
|
|
|
return platformRespQueryToken.getAccessToken();
|
|
|
} else {
|
|
|
// 记录错误码,返回错误信息
|
|
|
- LOGGER.error("{}接口错误:接口名{}:返回信息:{}", platformName, "query_token", response);
|
|
|
+ log.error("{}接口错误:接口名{}:返回信息:{}", platformName, "query_token", response);
|
|
|
throw new BusinessException(ResponseEnum.EN_PLUS_QUERY_TOKEN_ERROR);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public EnResponse enGetToken(String url, String params) {
|
|
|
+ public PlatformResponse platformGetToken(String url, String params) {
|
|
|
// token获取
|
|
|
RequestBody requestBody = RequestBody.create(params, JSON);
|
|
|
Request request = new Request.Builder()
|
|
|
.post(requestBody)
|
|
|
.url(url)
|
|
|
.build();
|
|
|
- var response = parse(synchronizedCall(request), EnResponse.class);
|
|
|
+ var response = parse(synchronizedCall(request), PlatformResponse.class);
|
|
|
|
|
|
if (response != null && 0 == response.getRet()) {
|
|
|
return response;
|
|
|
} else {
|
|
|
- LOGGER.error("EN+接口数据异常:url:{}\n params:{}\n返回信息:{}", url, params, response);
|
|
|
+ log.error("互联互通接口数据异常:url:{}\n params:{}\n返回信息:{}", url, params, response);
|
|
|
throw new BusinessException(ResponseEnum.EN_PLUS_API_EXCEPTION);
|
|
|
}
|
|
|
}
|
|
|
@@ -180,7 +178,7 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
@Override
|
|
|
public String buildPlatformParams(String platformName, String params) {
|
|
|
// 使用DataSecret对data加密
|
|
|
- var dataStr = PlatformAesUtil.getUtil(PlatformCache.INSTANCE.getPlatformByName(platformName)).encrypt(params);
|
|
|
+ var dataStr = PlatformAesUtil.encrypt(PlatformCache.INSTANCE.getPlatformByName(platformName), params);
|
|
|
// 时间戳
|
|
|
var timeStamp = LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN);
|
|
|
// 自增序列
|
|
|
@@ -222,13 +220,13 @@ public class EnPlusServiceImpl implements EnPlusService {
|
|
|
var sign = mac.digestHex(signString).toUpperCase();
|
|
|
if (sign.equals(Sig)) {
|
|
|
// 解密数据
|
|
|
- return PlatformAesUtil.getUtil(PlatformCache.INSTANCE.getPlatformByName(platformName)).decrypt(Data);
|
|
|
+ return PlatformAesUtil.decrypt(PlatformCache.INSTANCE.getPlatformByName(platformName), Data);
|
|
|
} else {
|
|
|
// 验签失败
|
|
|
- EnResponse enResponse = new EnResponse();
|
|
|
+ PlatformResponse enResponse = new PlatformResponse();
|
|
|
enResponse.setRet(4001);
|
|
|
enResponse.setMsg(ResponseEnum.EN_PLUS_PUSH_SIGN_FAIL.getMessage());
|
|
|
- LOGGER.error("EN+推送数据验签失败,数据:{}", json);
|
|
|
+ log.error("{}推送数据验签失败,数据:{}", platformName, json);
|
|
|
throw new EnPushException(ResponseEnum.EN_PLUS_PUSH_SIGN_FAIL, enResponse);
|
|
|
}
|
|
|
|