Commit 0d47d6cead6ad1c56c4bbfdaaab5f54e71c965d3

Authored by YunaiV
1 parent 59d5fef5

mall + trade:code review 快递客户端的实现,没啥问题咧,搞下单测就 ok 拉

Showing 16 changed files with 74 additions and 65 deletions
yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java
@@ -53,9 +53,10 @@ public interface ErrorCodeConstants { @@ -53,9 +53,10 @@ public interface ErrorCodeConstants {
53 // ========== 物流 Express 模块 1011004000 ========== 53 // ========== 物流 Express 模块 1011004000 ==========
54 ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1011004000, "快递公司不存在"); 54 ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1011004000, "快递公司不存在");
55 ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011004001, "已经存在该编码的快递公司"); 55 ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011004001, "已经存在该编码的快递公司");
56 - ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011004002, "快递查询接口异常");  
57 - ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011004003, "快递查询返回失败,原因:{}");  
58 - ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011004004, "需要接入快递服务商,比如【快递100】"); 56 + ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011004002, "需要接入快递服务商,比如【快递100】");
  57 +
  58 + ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011004101, "快递查询接口异常");
  59 + ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011004102, "快递查询返回失败,原因:{}");
59 60
60 // ========== 物流 Template 模块 1011005000 ========== 61 // ========== 物流 Template 模块 1011005000 ==========
61 ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011005000, "已经存在该运费模板名"); 62 ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011005000, "已经存在该运费模板名");
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/config/ExpressClientConfig.java
@@ -8,13 +8,14 @@ import org.springframework.context.annotation.Configuration; @@ -8,13 +8,14 @@ import org.springframework.context.annotation.Configuration;
8 import org.springframework.web.client.RestTemplate; 8 import org.springframework.web.client.RestTemplate;
9 9
10 /** 10 /**
11 - * 快递客户端端配置类, 提供快递客户端工厂,默认的快递客户端实现 11 + * 快递客户端端配置类:
  12 + *
  13 + * 1. 快递客户端工厂 {@link ExpressClientFactory}
  14 + * 2. 默认的快递客户端实现 {@link ExpressClient}
12 * 15 *
13 * @author jason 16 * @author jason
14 */ 17 */
15 -@Configuration(  
16 - proxyBeanMethods = false  
17 -) 18 +@Configuration(proxyBeanMethods = false)
18 public class ExpressClientConfig { 19 public class ExpressClientConfig {
19 20
20 @Bean 21 @Bean
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/config/TradeExpressProperties.java
1 package cn.iocoder.yudao.module.trade.framework.delivery.config; 1 package cn.iocoder.yudao.module.trade.framework.delivery.config;
2 2
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientEnum; 3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.enums.ExpressClientEnum;
4 import lombok.Data; 4 import lombok.Data;
5 import org.springframework.boot.context.properties.ConfigurationProperties; 5 import org.springframework.boot.context.properties.ConfigurationProperties;
6 import org.springframework.stereotype.Component; 6 import org.springframework.stereotype.Component;
@@ -27,6 +27,7 @@ public class TradeExpressProperties { @@ -27,6 +27,7 @@ public class TradeExpressProperties {
27 * 默认不提供,需要提醒用户配置一个快递服务商。 27 * 默认不提供,需要提醒用户配置一个快递服务商。
28 */ 28 */
29 private ExpressClientEnum client = ExpressClientEnum.NOT_PROVIDE; 29 private ExpressClientEnum client = ExpressClientEnum.NOT_PROVIDE;
  30 +
30 /** 31 /**
31 * 快递鸟配置 32 * 快递鸟配置
32 */ 33 */
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/ExpressClient.java
1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client; 1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
2 2
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO; 3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
5 5
6 import java.util.List; 6 import java.util.List;
7 7
@@ -18,6 +18,6 @@ public interface ExpressClient { @@ -18,6 +18,6 @@ public interface ExpressClient {
18 * @param reqDTO 查询请求参数 18 * @param reqDTO 查询请求参数
19 */ 19 */
20 // TODO @jason:返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data 20 // TODO @jason:返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data
21 - List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO); 21 + List<ExpressTrackRespDTO> getExpressTrackList(ExpressTrackQueryReqDTO reqDTO);
22 22
23 } 23 }
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/ExpressClientFactory.java
1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client; 1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client;
2 2
  3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.enums.ExpressClientEnum;
  4 +
3 /** 5 /**
4 - * 快递客户端工厂接口,快递客户端工厂:用于创建和缓存快递客户端 6 + * 快递客户端工厂接口:用于创建和缓存快递客户端
5 * 7 *
6 * @author jason 8 * @author jason
7 */ 9 */
@@ -19,5 +21,4 @@ public interface ExpressClientFactory { @@ -19,5 +21,4 @@ public interface ExpressClientFactory {
19 */ 21 */
20 ExpressClient getOrCreateExpressClient(ExpressClientEnum clientEnum); 22 ExpressClient getOrCreateExpressClient(ExpressClientEnum clientEnum);
21 23
22 -  
23 } 24 }
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/convert/ExpressQueryConvert.java
1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert; 1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client.convert;
2 2
3 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;  
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO; 3 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
5 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO; 5 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
6 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO; 6 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
7 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO; 7 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO;
@@ -16,12 +16,12 @@ public interface ExpressQueryConvert { @@ -16,12 +16,12 @@ public interface ExpressQueryConvert {
16 16
17 ExpressQueryConvert INSTANCE = Mappers.getMapper(ExpressQueryConvert.class); 17 ExpressQueryConvert INSTANCE = Mappers.getMapper(ExpressQueryConvert.class);
18 18
19 - List<ExpressQueryRespDTO> convertList(List<KdNiaoExpressQueryRespDTO.ExpressTrack> expressTrackList); 19 + List<ExpressTrackRespDTO> convertList(List<KdNiaoExpressQueryRespDTO.ExpressTrack> expressTrackList);
20 20
21 - List<ExpressQueryRespDTO> convertList2(List<Kd100ExpressQueryRespDTO.ExpressTrack> expressTrackList); 21 + List<ExpressTrackRespDTO> convertList2(List<Kd100ExpressQueryRespDTO.ExpressTrack> expressTrackList);
22 22
23 - KdNiaoExpressQueryReqDTO convert(ExpressQueryReqDTO dto); 23 + KdNiaoExpressQueryReqDTO convert(ExpressTrackQueryReqDTO dto);
24 24
25 - Kd100ExpressQueryReqDTO convert2(ExpressQueryReqDTO dto); 25 + Kd100ExpressQueryReqDTO convert2(ExpressTrackQueryReqDTO dto);
26 26
27 } 27 }
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/ExpressQueryReqDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/ExpressTrackQueryReqDTO.java
@@ -4,12 +4,12 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO; @@ -4,12 +4,12 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
4 import lombok.Data; 4 import lombok.Data;
5 5
6 /** 6 /**
7 - * 快递查询 Req DTO 7 + * 快递轨迹的查询 Req DTO
8 * 8 *
9 * @author jason 9 * @author jason
10 */ 10 */
11 @Data 11 @Data
12 -public class ExpressQueryReqDTO { 12 +public class ExpressTrackQueryReqDTO {
13 13
14 /** 14 /**
15 * 快递公司编码 15 * 快递公司编码
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/ExpressQueryRespDTO.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/dto/ExpressTrackRespDTO.java
@@ -8,7 +8,7 @@ import lombok.Data; @@ -8,7 +8,7 @@ import lombok.Data;
8 * @author jason 8 * @author jason
9 */ 9 */
10 @Data 10 @Data
11 -public class ExpressQueryRespDTO { 11 +public class ExpressTrackRespDTO {
12 12
13 // TODO @jason:LocalDateTime 13 // TODO @jason:LocalDateTime
14 /** 14 /**
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/ExpressClientFactoryImpl.java
@@ -2,9 +2,12 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl; @@ -2,9 +2,12 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.hutool.core.lang.Assert; 3 import cn.hutool.core.lang.Assert;
4 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties; 4 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientEnum;  
6 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient; 5 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
  6 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.kd100.Kd100ExpressClient;
  7 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.kdniao.KdNiaoExpressClient;
  8 +import cn.iocoder.yudao.module.trade.framework.delivery.core.enums.ExpressClientEnum;
7 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory; 9 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClientFactory;
  10 +import lombok.AllArgsConstructor;
8 import org.springframework.web.client.RestTemplate; 11 import org.springframework.web.client.RestTemplate;
9 12
10 import java.util.Map; 13 import java.util.Map;
@@ -15,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap; @@ -15,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap;
15 * 18 *
16 * @author jason 19 * @author jason
17 */ 20 */
  21 +@AllArgsConstructor
18 public class ExpressClientFactoryImpl implements ExpressClientFactory { 22 public class ExpressClientFactoryImpl implements ExpressClientFactory {
19 23
20 private final Map<ExpressClientEnum, ExpressClient> clientMap = new ConcurrentHashMap<>(8); 24 private final Map<ExpressClientEnum, ExpressClient> clientMap = new ConcurrentHashMap<>(8);
@@ -22,12 +26,6 @@ public class ExpressClientFactoryImpl implements ExpressClientFactory { @@ -22,12 +26,6 @@ public class ExpressClientFactoryImpl implements ExpressClientFactory {
22 private final TradeExpressProperties tradeExpressProperties; 26 private final TradeExpressProperties tradeExpressProperties;
23 private final RestTemplate restTemplate; 27 private final RestTemplate restTemplate;
24 28
25 - public ExpressClientFactoryImpl(TradeExpressProperties tradeExpressProperties,  
26 - RestTemplate restTemplate) {  
27 - this.tradeExpressProperties = tradeExpressProperties;  
28 - this.restTemplate = restTemplate;  
29 - }  
30 -  
31 @Override 29 @Override
32 public ExpressClient getDefaultExpressClient() { 30 public ExpressClient getDefaultExpressClient() {
33 ExpressClient defaultClient = getOrCreateExpressClient(tradeExpressProperties.getClient()); 31 ExpressClient defaultClient = getOrCreateExpressClient(tradeExpressProperties.getClient());
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/NoProvideExpressClient.java
1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl; 1 package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient; 3 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
4 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;  
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO; 4 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
6 6
7 import java.util.List; 7 import java.util.List;
8 8
@@ -15,8 +15,10 @@ import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_CLI @@ -15,8 +15,10 @@ import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.EXPRESS_CLI
15 * @author jason 15 * @author jason
16 */ 16 */
17 public class NoProvideExpressClient implements ExpressClient { 17 public class NoProvideExpressClient implements ExpressClient {
  18 +
18 @Override 19 @Override
19 - public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) { 20 + public List<ExpressTrackRespDTO> getExpressTrackList(ExpressTrackQueryReqDTO reqDTO) {
20 throw exception(EXPRESS_CLIENT_NOT_PROVIDE); 21 throw exception(EXPRESS_CLIENT_NOT_PROVIDE);
21 } 22 }
  23 +
22 } 24 }
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/Kd100ExpressClient.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/kd100/Kd100ExpressClient.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.kd100;
2 2
3 import cn.hutool.core.collection.CollUtil; 3 import cn.hutool.core.collection.CollUtil;
4 import cn.hutool.core.util.HexUtil; 4 import cn.hutool.core.util.HexUtil;
@@ -6,8 +6,8 @@ import cn.hutool.crypto.digest.DigestUtil; @@ -6,8 +6,8 @@ import cn.hutool.crypto.digest.DigestUtil;
6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 6 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
7 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties; 7 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
8 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient; 8 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
9 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;  
10 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO; 9 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  10 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
11 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO; 11 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryReqDTO;
12 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO; 12 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kd100.Kd100ExpressQueryRespDTO;
13 import lombok.AllArgsConstructor; 13 import lombok.AllArgsConstructor;
@@ -36,29 +36,28 @@ import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.conve @@ -36,29 +36,28 @@ import static cn.iocoder.yudao.module.trade.framework.delivery.core.client.conve
36 public class Kd100ExpressClient implements ExpressClient { 36 public class Kd100ExpressClient implements ExpressClient {
37 37
38 private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do"; 38 private static final String REAL_TIME_QUERY_URL = "https://poll.kuaidi100.com/poll/query.do";
  39 +
39 private final RestTemplate restTemplate; 40 private final RestTemplate restTemplate;
40 private final TradeExpressProperties.Kd100Config config; 41 private final TradeExpressProperties.Kd100Config config;
41 42
42 @Override 43 @Override
43 - public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) { 44 + public List<ExpressTrackRespDTO> getExpressTrackList(ExpressTrackQueryReqDTO reqDTO) {
44 // 发起查询 45 // 发起查询
45 Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO); 46 Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
46 kd100ReqParam.setExpressCode(kd100ReqParam.getExpressCode().toLowerCase()); // 快递公司编码需要转成小写 47 kd100ReqParam.setExpressCode(kd100ReqParam.getExpressCode().toLowerCase()); // 快递公司编码需要转成小写
47 Kd100ExpressQueryRespDTO respDTO = requestExpressQuery(REAL_TIME_QUERY_URL, kd100ReqParam, 48 Kd100ExpressQueryRespDTO respDTO = requestExpressQuery(REAL_TIME_QUERY_URL, kd100ReqParam,
48 Kd100ExpressQueryRespDTO.class); 49 Kd100ExpressQueryRespDTO.class);
49 log.debug("[getExpressTrackList][快递 100 接口 查询接口返回 {}]", respDTO); 50 log.debug("[getExpressTrackList][快递 100 接口 查询接口返回 {}]", respDTO);
  51 +
50 // 处理结果 52 // 处理结果
51 if (Objects.equals("false", respDTO.getResult())) { 53 if (Objects.equals("false", respDTO.getResult())) {
52 log.error("[getExpressTrackList][快递 100 接口 返回失败 {}]", respDTO.getMessage()); 54 log.error("[getExpressTrackList][快递 100 接口 返回失败 {}]", respDTO.getMessage());
53 throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage()); 55 throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
54 } 56 }
55 - // TODO @jason:convertList2 如果空,应该返回 list 了; @芋艿 为了避免返回 null  
56 - if (CollUtil.isNotEmpty(respDTO.getTracks())) {  
57 - return INSTANCE.convertList2(respDTO.getTracks());  
58 - } else { 57 + if (CollUtil.isEmpty(respDTO.getTracks())) {
59 return Collections.emptyList(); 58 return Collections.emptyList();
60 } 59 }
61 - 60 + return INSTANCE.convertList2(respDTO.getTracks());
62 } 61 }
63 62
64 /** 63 /**
@@ -85,7 +84,6 @@ public class Kd100ExpressClient implements ExpressClient { @@ -85,7 +84,6 @@ public class Kd100ExpressClient implements ExpressClient {
85 log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody); 84 log.debug("[sendExpressQueryReq][快递 100 接口的请求参数: {}]", requestBody);
86 // 发送请求 85 // 发送请求
87 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers); 86 HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
88 - // TODO @jason:可以使用 restTemplate 的 post 方法哇 @芋艿 为了获取接口的原始返回。用exchange 便于查问题。  
89 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); 87 ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
90 log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity); 88 log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity);
91 89
@@ -98,7 +96,6 @@ public class Kd100ExpressClient implements ExpressClient { @@ -98,7 +96,6 @@ public class Kd100ExpressClient implements ExpressClient {
98 96
99 private String generateReqSign(String param, String key, String customer) { 97 private String generateReqSign(String param, String key, String customer) {
100 String plainText = String.format("%s%s%s", param, key, customer); 98 String plainText = String.format("%s%s%s", param, key, customer);
101 - // TODO @芋艿。 这里需要转换成大写, 没有对应方法  
102 return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false); 99 return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
103 } 100 }
104 101
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/KdNiaoExpressClient.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/kdniao/KdNiaoExpressClient.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.kdniao;
2 2
3 import cn.hutool.core.codec.Base64; 3 import cn.hutool.core.codec.Base64;
4 import cn.hutool.core.collection.CollUtil; 4 import cn.hutool.core.collection.CollUtil;
@@ -7,8 +7,8 @@ import cn.hutool.crypto.digest.DigestUtil; @@ -7,8 +7,8 @@ import cn.hutool.crypto.digest.DigestUtil;
7 import cn.iocoder.yudao.framework.common.util.json.JsonUtils; 7 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
8 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties; 8 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
9 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient; 9 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.ExpressClient;
10 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO;  
11 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryRespDTO; 10 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  11 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
12 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO; 12 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryReqDTO;
13 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryRespDTO; 13 import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.kdniao.KdNiaoExpressQueryRespDTO;
14 import lombok.AllArgsConstructor; 14 import lombok.AllArgsConstructor;
@@ -51,25 +51,27 @@ public class KdNiaoExpressClient implements ExpressClient { @@ -51,25 +51,27 @@ public class KdNiaoExpressClient implements ExpressClient {
51 * @param reqDTO 查询请求参数 51 * @param reqDTO 查询请求参数
52 */ 52 */
53 @Override 53 @Override
54 - public List<ExpressQueryRespDTO> getExpressTrackList(ExpressQueryReqDTO reqDTO) { 54 + public List<ExpressTrackRespDTO> getExpressTrackList(ExpressTrackQueryReqDTO reqDTO) {
55 KdNiaoExpressQueryReqDTO kdNiaoReqData = INSTANCE.convert(reqDTO); 55 KdNiaoExpressQueryReqDTO kdNiaoReqData = INSTANCE.convert(reqDTO);
56 // 快递公司编码需要转成大写 56 // 快递公司编码需要转成大写
57 kdNiaoReqData.setExpressCode(reqDTO.getExpressCode().toUpperCase()); 57 kdNiaoReqData.setExpressCode(reqDTO.getExpressCode().toUpperCase());
58 KdNiaoExpressQueryRespDTO respDTO = requestKdNiaoApi(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE, 58 KdNiaoExpressQueryRespDTO respDTO = requestKdNiaoApi(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
59 kdNiaoReqData, KdNiaoExpressQueryRespDTO.class); 59 kdNiaoReqData, KdNiaoExpressQueryRespDTO.class);
60 log.debug("[getExpressTrackList][快递鸟即时查询接口返回 {}]", respDTO); 60 log.debug("[getExpressTrackList][快递鸟即时查询接口返回 {}]", respDTO);
  61 +
  62 + // 处理结果
61 if (respDTO == null || !respDTO.getSuccess()) { 63 if (respDTO == null || !respDTO.getSuccess()) {
62 throw exception(EXPRESS_API_QUERY_FAILED, respDTO == null ? "" : respDTO.getReason()); 64 throw exception(EXPRESS_API_QUERY_FAILED, respDTO == null ? "" : respDTO.getReason());
63 } 65 }
64 if (CollUtil.isNotEmpty(respDTO.getTracks())) { 66 if (CollUtil.isNotEmpty(respDTO.getTracks())) {
65 - return INSTANCE.convertList(respDTO.getTracks());  
66 - } else {  
67 return Collections.emptyList(); 67 return Collections.emptyList();
68 } 68 }
  69 + return INSTANCE.convertList(respDTO.getTracks());
69 } 70 }
70 71
71 /** 72 /**
72 - * 快递鸟 通用的 API 请求, 暂时没有其他应用场景, 暂时放这里 73 + * 快递鸟 通用的 API 请求,暂时没有其他应用场景, 暂时放这里
  74 + *
73 * @param url 请求 url 75 * @param url 请求 url
74 * @param requestType 对应的请求指令 (快递鸟的RequestType) 76 * @param requestType 对应的请求指令 (快递鸟的RequestType)
75 * @param req 对应请求的请求参数 77 * @param req 对应请求的请求参数
@@ -77,8 +79,8 @@ public class KdNiaoExpressClient implements ExpressClient { @@ -77,8 +79,8 @@ public class KdNiaoExpressClient implements ExpressClient {
77 * @param <Req> 每个请求的请求结构 Req DTO 79 * @param <Req> 每个请求的请求结构 Req DTO
78 * @param <Resp> 每个请求的响应结构 Resp DTO 80 * @param <Resp> 每个请求的响应结构 Resp DTO
79 */ 81 */
80 - private <Req, Resp> Resp requestKdNiaoApi(String url, String requestType, Req req,  
81 - Class<Resp> respClass){ 82 + private <Req, Resp> Resp requestKdNiaoApi(String url, String requestType, Req req,
  83 + Class<Resp> respClass){
82 // 请求头 84 // 请求头
83 HttpHeaders headers = new HttpHeaders(); 85 HttpHeaders headers = new HttpHeaders();
84 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 86 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/ExpressClientEnum.java renamed to yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/framework/delivery/core/enums/ExpressClientEnum.java
1 -package cn.iocoder.yudao.module.trade.framework.delivery.core.client; 1 +package cn.iocoder.yudao.module.trade.framework.delivery.core.enums;
2 2
  3 +import lombok.AllArgsConstructor;
3 import lombok.Getter; 4 import lombok.Getter;
4 5
5 /** 6 /**
@@ -8,7 +9,9 @@ import lombok.Getter; @@ -8,7 +9,9 @@ import lombok.Getter;
8 * @author jason 9 * @author jason
9 */ 10 */
10 @Getter 11 @Getter
  12 +@AllArgsConstructor
11 public enum ExpressClientEnum { 13 public enum ExpressClientEnum {
  14 +
12 NOT_PROVIDE("not-provide","未提供"), 15 NOT_PROVIDE("not-provide","未提供"),
13 KD_NIAO("kd-niao", "快递鸟"), 16 KD_NIAO("kd-niao", "快递鸟"),
14 KD_100("kd-100", "快递100"); 17 KD_100("kd-100", "快递100");
@@ -17,14 +20,9 @@ public enum ExpressClientEnum { @@ -17,14 +20,9 @@ public enum ExpressClientEnum {
17 * 快递服务商唯一编码 20 * 快递服务商唯一编码
18 */ 21 */
19 private final String code; 22 private final String code;
20 -  
21 /** 23 /**
22 * 快递服务商名称 24 * 快递服务商名称
23 */ 25 */
24 private final String name; 26 private final String name;
25 27
26 - ExpressClientEnum(String code, String name) {  
27 - this.code = code;  
28 - this.name = name;  
29 - }  
30 } 28 }
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/Kd100ExpressClientTest.java
@@ -2,7 +2,8 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl; @@ -2,7 +2,8 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.iocoder.yudao.framework.common.exception.ServiceException; 3 import cn.iocoder.yudao.framework.common.exception.ServiceException;
4 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties; 4 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO; 5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  6 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.kd100.Kd100ExpressClient;
6 import org.junit.jupiter.api.BeforeEach; 7 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.Disabled; 8 import org.junit.jupiter.api.Disabled;
8 import org.junit.jupiter.api.Test; 9 import org.junit.jupiter.api.Test;
@@ -18,12 +19,14 @@ import javax.annotation.Resource; @@ -18,12 +19,14 @@ import javax.annotation.Resource;
18 import static org.junit.jupiter.api.Assertions.assertEquals; 19 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertThrows; 20 import static org.junit.jupiter.api.Assertions.assertThrows;
20 21
  22 +// TODO @jason:可以参考 AliyunSmsClientTest 写,纯 mockito,无需启动 spring 容器
21 /** 23 /**
22 * @author jason 24 * @author jason
23 */ 25 */
24 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Kd100ExpressClientTest.Application.class) 26 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = Kd100ExpressClientTest.Application.class)
25 @ActiveProfiles("unit-test") // 设置使用 trade-delivery-query 配置文件 27 @ActiveProfiles("unit-test") // 设置使用 trade-delivery-query 配置文件
26 public class Kd100ExpressClientTest { 28 public class Kd100ExpressClientTest {
  29 +
27 @Resource 30 @Resource
28 private RestTemplateBuilder builder; 31 private RestTemplateBuilder builder;
29 @Resource 32 @Resource
@@ -39,7 +42,7 @@ public class Kd100ExpressClientTest { @@ -39,7 +42,7 @@ public class Kd100ExpressClientTest {
39 @Disabled("需要 授权 key. 暂时忽略") 42 @Disabled("需要 授权 key. 暂时忽略")
40 void testRealTimeQueryExpressFailed() { 43 void testRealTimeQueryExpressFailed() {
41 ServiceException t = assertThrows(ServiceException.class, () -> { 44 ServiceException t = assertThrows(ServiceException.class, () -> {
42 - ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO(); 45 + ExpressTrackQueryReqDTO reqDTO = new ExpressTrackQueryReqDTO();
43 reqDTO.setExpressCode("yto"); 46 reqDTO.setExpressCode("yto");
44 reqDTO.setLogisticsNo("YT9383342193097"); 47 reqDTO.setLogisticsNo("YT9383342193097");
45 kd100ExpressClient.getExpressTrackList(reqDTO); 48 kd100ExpressClient.getExpressTrackList(reqDTO);
@@ -53,4 +56,4 @@ public class Kd100ExpressClientTest { @@ -53,4 +56,4 @@ public class Kd100ExpressClientTest {
53 @EnableConfigurationProperties(TradeExpressProperties.class) 56 @EnableConfigurationProperties(TradeExpressProperties.class)
54 public static class Application { 57 public static class Application {
55 } 58 }
56 -}  
57 \ No newline at end of file 59 \ No newline at end of file
  60 +}
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/KdNiaoExpressClientTest.java
@@ -2,7 +2,8 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl; @@ -2,7 +2,8 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl;
2 2
3 import cn.iocoder.yudao.framework.common.exception.ServiceException; 3 import cn.iocoder.yudao.framework.common.exception.ServiceException;
4 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties; 4 import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressProperties;
5 -import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressQueryReqDTO; 5 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackQueryReqDTO;
  6 +import cn.iocoder.yudao.module.trade.framework.delivery.core.client.impl.kdniao.KdNiaoExpressClient;
6 import org.junit.jupiter.api.BeforeEach; 7 import org.junit.jupiter.api.BeforeEach;
7 import org.junit.jupiter.api.Disabled; 8 import org.junit.jupiter.api.Disabled;
8 import org.junit.jupiter.api.Test; 9 import org.junit.jupiter.api.Test;
@@ -17,13 +18,16 @@ import javax.annotation.Resource; @@ -17,13 +18,16 @@ import javax.annotation.Resource;
17 18
18 import static org.junit.jupiter.api.Assertions.assertThrows; 19 import static org.junit.jupiter.api.Assertions.assertThrows;
19 20
20 -// TODO @芋艿:单测最后 review 21 +// TODO @jason:可以参考 AliyunSmsClientTest 写,纯 mockito,无需启动 spring 容器
21 /** 22 /**
  23 + * {@link KdNiaoExpressClient} 的单元测试
  24 + *
22 * @author jason 25 * @author jason
23 */ 26 */
24 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressClientTest.Application.class) 27 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressClientTest.Application.class)
25 @ActiveProfiles("unit-test") 28 @ActiveProfiles("unit-test")
26 public class KdNiaoExpressClientTest { 29 public class KdNiaoExpressClientTest {
  30 +
27 @Resource 31 @Resource
28 private RestTemplateBuilder builder; 32 private RestTemplateBuilder builder;
29 @Resource 33 @Resource
@@ -39,7 +43,7 @@ public class KdNiaoExpressClientTest { @@ -39,7 +43,7 @@ public class KdNiaoExpressClientTest {
39 @Disabled("需要 授权 key. 暂时忽略") 43 @Disabled("需要 授权 key. 暂时忽略")
40 void testRealTimeQueryExpressFailed() { 44 void testRealTimeQueryExpressFailed() {
41 assertThrows(ServiceException.class,() ->{ 45 assertThrows(ServiceException.class,() ->{
42 - ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO(); 46 + ExpressTrackQueryReqDTO reqDTO = new ExpressTrackQueryReqDTO();
43 reqDTO.setExpressCode("yy"); 47 reqDTO.setExpressCode("yy");
44 reqDTO.setLogisticsNo("YT9383342193097"); 48 reqDTO.setLogisticsNo("YT9383342193097");
45 kdNiaoExpressClient.getExpressTrackList(reqDTO); 49 kdNiaoExpressClient.getExpressTrackList(reqDTO);
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/framework/delivery/core/client/impl/NoProvideExpressClientTest.java
@@ -19,6 +19,7 @@ import javax.annotation.Resource; @@ -19,6 +19,7 @@ import javax.annotation.Resource;
19 import static org.junit.jupiter.api.Assertions.assertEquals; 19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows; 20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 21
  22 +// TODO @jason:可以参考 AliyunSmsClientTest 写,纯 mockito,无需启动 spring 容器
22 /** 23 /**
23 * @author jason 24 * @author jason
24 */ 25 */
@@ -49,4 +50,4 @@ public class NoProvideExpressClientTest { @@ -49,4 +50,4 @@ public class NoProvideExpressClientTest {
49 return builder.build(); 50 return builder.build();
50 } 51 }
51 } 52 }
52 -}  
53 \ No newline at end of file 53 \ No newline at end of file
  54 +}