|
@@ -1,15 +1,16 @@
|
|
-package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
|
|
|
|
|
|
+package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.util.HexUtil;
|
|
import cn.hutool.core.util.HexUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
-import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
|
|
|
|
-import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;
|
|
|
|
-import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
|
|
|
|
-import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
|
|
|
|
-import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryReqDTO;
|
|
|
|
-import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kd100.Kd100ExpressQueryRespDTO;
|
|
|
|
|
|
+import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
|
|
|
|
+import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
|
|
|
|
+import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;
|
|
|
|
+import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO;
|
|
|
|
+import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
|
|
|
|
+import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.*;
|
|
import org.springframework.http.*;
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
@@ -23,48 +24,41 @@ import java.util.Objects;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
|
|
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_ERROR;
|
|
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
|
|
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_API_QUERY_FAILED;
|
|
-import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE;
|
|
|
|
|
|
+import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert.ExpressQueryConvert.INSTANCE;
|
|
|
|
|
|
-// TODO @jason:可以参考 KdNiaoExpressQueryProvider 建议改改哈
|
|
|
|
/**
|
|
/**
|
|
- * 快递 100 服务商
|
|
|
|
|
|
+ * 快递 100 客户端
|
|
*
|
|
*
|
|
* @author jason
|
|
* @author jason
|
|
*/
|
|
*/
|
|
@Slf4j
|
|
@Slf4j
|
|
-public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|
|
|
|
|
+@AllArgsConstructor
|
|
|
|
+public class Kd100ExpressClient implements ExpressClient {
|
|
|
|
|
|
private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";
|
|
private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";
|
|
-
|
|
|
|
private final RestTemplate restTemplate;
|
|
private final RestTemplate restTemplate;
|
|
- private final TradeExpressQueryProperties.Kd100Config config;
|
|
|
|
-
|
|
|
|
- public Kd100ExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.Kd100Config config) {
|
|
|
|
- this.restTemplate = restTemplate;
|
|
|
|
- this.config = config;
|
|
|
|
- }
|
|
|
|
|
|
+ private final TradeExpressProperties.Kd100Config config;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
|
|
|
|
|
|
+ public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) {
|
|
// 发起查询
|
|
// 发起查询
|
|
Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
|
|
Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
|
|
- kd100ReqParam.setExpressCompanyCode(kd100ReqParam.getExpressCompanyCode().toLowerCase()); // 快递公司编码需要转成小写
|
|
|
|
- Kd100ExpressQueryRespDTO respDTO = sendExpressQueryReq(REAL_TIME_QUERY_URL, kd100ReqParam,
|
|
|
|
|
|
+ kd100ReqParam.setExpressCode(kd100ReqParam.getExpressCode().toLowerCase()); // 快递公司编码需要转成小写
|
|
|
|
+ Kd100ExpressQueryRespDTO respDTO = requestExpressQuery(REAL_TIME_QUERY_URL, kd100ReqParam,
|
|
Kd100ExpressQueryRespDTO.class);
|
|
Kd100ExpressQueryRespDTO.class);
|
|
- log.debug("[realTimeQueryExpress][快递 100 接口 查询接口返回 {}]", respDTO);
|
|
|
|
|
|
+ log.debug("[getExpressTrackList][快递 100 接口 查询接口返回 {}]", respDTO);
|
|
// 处理结果
|
|
// 处理结果
|
|
if (Objects.equals("false", respDTO.getResult())) {
|
|
if (Objects.equals("false", respDTO.getResult())) {
|
|
- log.error("[realTimeQueryExpress][快递 100 接口 返回失败 {}]", respDTO.getMessage());
|
|
|
|
|
|
+ log.error("[getExpressTrackList][快递 100 接口 返回失败 {}]", respDTO.getMessage());
|
|
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
|
|
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
|
|
- // TODO @json:else 可以不用写哈;
|
|
|
|
|
|
+ }
|
|
|
|
+ // TODO @jason:convertList2 如果空,应该返回 list 了; @芋艿 为了避免返回 null
|
|
|
|
+ if (CollUtil.isNotEmpty(respDTO.getTracks())) {
|
|
|
|
+ return INSTANCE.convertList2(respDTO.getTracks());
|
|
} else {
|
|
} else {
|
|
- // TODO @jason:convertList2 如果空,应该返回 list 了;
|
|
|
|
- if (CollUtil.isNotEmpty(respDTO.getTracks())) {
|
|
|
|
- return INSTANCE.convertList2(respDTO.getTracks());
|
|
|
|
- } else {
|
|
|
|
- return Collections.emptyList();
|
|
|
|
- }
|
|
|
|
|
|
+ return Collections.emptyList();
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -76,8 +70,7 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|
* @param <Req> 每个请求的请求结构 Req DTO
|
|
* @param <Req> 每个请求的请求结构 Req DTO
|
|
* @param <Resp> 每个请求的响应结构 Resp DTO
|
|
* @param <Resp> 每个请求的响应结构 Resp DTO
|
|
*/
|
|
*/
|
|
- // TODO @jason:可以改成 request,发起请求哈;
|
|
|
|
- private <Req, Resp> Resp sendExpressQueryReq(String url, Req req, Class<Resp> respClass) {
|
|
|
|
|
|
+ private <Req, Resp> Resp requestExpressQuery(String url, Req req, Class<Resp> respClass) {
|
|
// 请求头
|
|
// 请求头
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
@@ -92,23 +85,20 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|
log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody);
|
|
log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody);
|
|
// 发送请求
|
|
// 发送请求
|
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
|
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
|
|
- // TODO @jason:可以使用 restTemplate 的 post 方法哇?
|
|
|
|
|
|
+ // TODO @jason:可以使用 restTemplate 的 post 方法哇 @芋艿 为了获取接口的原始返回。用exchange 便于查问题。
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
|
log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity);
|
|
log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity);
|
|
|
|
|
|
// 处理响应
|
|
// 处理响应
|
|
- // TODO @jason:if return 原则;if (!responseEntity.getStatusCode().is2xxSuccessful()) 抛出异常;接着处理成功的
|
|
|
|
- if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
|
|
|
- String response = responseEntity.getBody();
|
|
|
|
- return JsonUtils.parseObject(response, respClass);
|
|
|
|
- } else {
|
|
|
|
|
|
+ if (!responseEntity.getStatusCode().is2xxSuccessful()) {
|
|
throw exception(EXPRESS_API_QUERY_ERROR);
|
|
throw exception(EXPRESS_API_QUERY_ERROR);
|
|
}
|
|
}
|
|
|
|
+ return JsonUtils.parseObject(responseEntity.getBody(), respClass);
|
|
}
|
|
}
|
|
|
|
|
|
private String generateReqSign(String param, String key, String customer) {
|
|
private String generateReqSign(String param, String key, String customer) {
|
|
String plainText = String.format("%s%s%s", param, key, customer);
|
|
String plainText = String.format("%s%s%s", param, key, customer);
|
|
- // TODO @jason:DigestUtil.md5Hex(plainText);
|
|
|
|
|
|
+ // TODO @芋艿。 这里需要转换成大写, 没有对应方法
|
|
return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
|
|
return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
|
|
}
|
|
}
|
|
|
|
|