mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
mall + trade:code review 快递计算逻辑
This commit is contained in:
parent
e1a8e45ac7
commit
dcb1660880
@ -110,19 +110,19 @@ public class YudaoWebAutoConfiguration implements WebMvcConfigurer {
|
|||||||
return createFilterBean(new DemoFilter(), WebFilterOrderEnum.DEMO_FILTER);
|
return createFilterBean(new DemoFilter(), WebFilterOrderEnum.DEMO_FILTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建 RestTemplate 实例
|
|
||||||
* @param restTemplateBuilder {@link RestTemplateAutoConfiguration#restTemplateBuilder}
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){
|
|
||||||
return restTemplateBuilder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
|
public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
|
||||||
FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
|
FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
|
||||||
bean.setOrder(order);
|
bean.setOrder(order);
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 RestTemplate 实例
|
||||||
|
*
|
||||||
|
* @param restTemplateBuilder {@link RestTemplateAutoConfiguration#restTemplateBuilder}
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
|
||||||
|
return restTemplateBuilder.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@ import org.springframework.validation.annotation.Validated;
|
|||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
|
// TODO @jason:TradeExpressProperties;更通用哈
|
||||||
|
// TODO @芋艿:未来要不要放数据库中?考虑 saas 多租户时,不同租户使用不同的配置?
|
||||||
/**
|
/**
|
||||||
* 交易快递查询的配置项
|
* 交易快递查询的配置项
|
||||||
*
|
*
|
||||||
@ -21,9 +23,13 @@ import javax.validation.constraints.NotEmpty;
|
|||||||
public class TradeExpressQueryProperties {
|
public class TradeExpressQueryProperties {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递查询服务商, 如果未配置,默认使用快递鸟
|
* 快递查询服务商
|
||||||
|
*
|
||||||
|
* 如果未配置,默认使用快递鸟
|
||||||
*/
|
*/
|
||||||
private ExpressQueryProviderEnum expressQueryProvider;
|
// TODO @jason:可以把 expressQueryProvider 改成 client 变量,更简洁一点;
|
||||||
|
private ExpressQueryProviderEnum expressQueryProvider; // TODO @jaosn:默认值可以通过属性直接赋值哈;
|
||||||
|
// TODO @jason:需要考虑下,用户只配置了其中一个;
|
||||||
/**
|
/**
|
||||||
* 快递鸟配置
|
* 快递鸟配置
|
||||||
*/
|
*/
|
||||||
@ -46,12 +52,12 @@ public class TradeExpressQueryProperties {
|
|||||||
*/
|
*/
|
||||||
@NotEmpty(message = "快递鸟用户 ID 配置项不能为空")
|
@NotEmpty(message = "快递鸟用户 ID 配置项不能为空")
|
||||||
private String businessId;
|
private String businessId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递鸟 API Key
|
* 快递鸟 API Key
|
||||||
*/
|
*/
|
||||||
@NotEmpty(message = "快递鸟 Api Key 配置项不能为空")
|
@NotEmpty(message = "快递鸟 Api Key 配置项不能为空")
|
||||||
private String apiKey;
|
private String apiKey;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,6 +65,7 @@ public class TradeExpressQueryProperties {
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public static class Kd100Config {
|
public static class Kd100Config {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递 100 授权码
|
* 快递 100 授权码
|
||||||
*/
|
*/
|
||||||
@ -69,7 +76,7 @@ public class TradeExpressQueryProperties {
|
|||||||
*/
|
*/
|
||||||
@NotEmpty(message = "快递 100 授权 Key 配置项不能为空")
|
@NotEmpty(message = "快递 100 授权 Key 配置项不能为空")
|
||||||
private String key;
|
private String key;
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRes
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
// TODO @jason:可以改成 ExpressClient,未来可能还对接别的接口噢
|
||||||
/**
|
/**
|
||||||
* 快递查询客户端
|
* 快递查询客户端
|
||||||
*
|
*
|
||||||
@ -17,5 +18,7 @@ public interface ExpressQueryClient {
|
|||||||
*
|
*
|
||||||
* @param reqDTO 查询请求参数
|
* @param reqDTO 查询请求参数
|
||||||
*/
|
*/
|
||||||
|
// TODO @jason:可以改成 getExpressTrackList。返回字段可以参考 https://doc.youzanyun.com/detail/API/0/5 响应的 data
|
||||||
List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO);
|
List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,12 @@ import java.util.List;
|
|||||||
* @author jason
|
* @author jason
|
||||||
*/
|
*/
|
||||||
public interface ExpressQueryProvider {
|
public interface ExpressQueryProvider {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递实时查询
|
* 快递实时查询
|
||||||
*
|
*
|
||||||
* @param reqDTO 查询请求参数
|
* @param reqDTO 查询请求参数
|
||||||
*/
|
*/
|
||||||
List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO);
|
List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,12 @@ import lombok.Getter;
|
|||||||
* @author jason
|
* @author jason
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
|
|
||||||
public enum ExpressQueryProviderEnum {
|
public enum ExpressQueryProviderEnum {
|
||||||
|
|
||||||
KD_NIAO("kd-niao", "快递鸟"),
|
KD_NIAO("kd-niao", "快递鸟"),
|
||||||
KD_100("kd-100", "快递100");
|
KD_100("kd-100", "快递100");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递服务商唯一编码
|
* 快递服务商唯一编码
|
||||||
*/
|
*/
|
||||||
@ -21,8 +24,10 @@ public enum ExpressQueryProviderEnum {
|
|||||||
*/
|
*/
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
// TODO @jaosn:@AllArgsConstructor 可以替代哈
|
||||||
ExpressQueryProviderEnum(String code, String name) {
|
ExpressQueryProviderEnum(String code, String name) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,18 @@ package cn.iocoder.yudao.module.trade.framework.delivery.core;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递服务商工厂,用于创建和缓存快递服务商服务
|
* 快递服务商工厂,用于创建和缓存快递服务商服务
|
||||||
|
*
|
||||||
* @author jason
|
* @author jason
|
||||||
*/
|
*/
|
||||||
public interface ExpressQueryProviderFactory {
|
public interface ExpressQueryProviderFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过枚举获取快递查询服务商, 如果不存在。就创建一个对应的快递查询服务商
|
* 通过枚举获取快递查询服务商
|
||||||
|
*
|
||||||
|
* 如果不存在,就创建一个对应的快递查询服务商
|
||||||
|
*
|
||||||
* @param queryProviderEnum 快递服务商枚举
|
* @param queryProviderEnum 快递服务商枚举
|
||||||
*/
|
*/
|
||||||
ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum);
|
ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,4 +23,5 @@ public interface ExpressQueryConvert {
|
|||||||
KdNiaoExpressQueryReqDTO convert(ExpressQueryReqDTO dto);
|
KdNiaoExpressQueryReqDTO convert(ExpressQueryReqDTO dto);
|
||||||
|
|
||||||
Kd100ExpressQueryReqDTO convert2(ExpressQueryReqDTO dto);
|
Kd100ExpressQueryReqDTO convert2(ExpressQueryReqDTO dto);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,9 @@ public class ExpressQueryReqDTO {
|
|||||||
/**
|
/**
|
||||||
* 快递公司编码
|
* 快递公司编码
|
||||||
*
|
*
|
||||||
* 对应 {@link DeliveryExpressDO#getCode()} }
|
* 对应 {@link DeliveryExpressDO#getCode()}
|
||||||
*/
|
*/
|
||||||
|
// TODO @jaosn:要不改成 expressCode;项目里使用这个哈
|
||||||
private String expressCompanyCode;
|
private String expressCompanyCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,4 +28,5 @@ public class ExpressQueryReqDTO {
|
|||||||
* 收、寄件人的电话号码
|
* 收、寄件人的电话号码
|
||||||
*/
|
*/
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,15 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class ExpressQueryRespDTO {
|
public class ExpressQueryRespDTO {
|
||||||
|
|
||||||
|
// TODO @jason:LocalDateTime
|
||||||
/**
|
/**
|
||||||
* 发生时间
|
* 发生时间
|
||||||
*/
|
*/
|
||||||
private String time;
|
private String time;
|
||||||
|
// TODO @jason:其它字段可能要补充下
|
||||||
/**
|
/**
|
||||||
* 快递状态
|
* 快递状态
|
||||||
*/
|
*/
|
||||||
private String state;
|
private String state;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import lombok.Data;
|
|||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class Kd100ExpressQueryReqDTO {
|
public class Kd100ExpressQueryReqDTO {
|
||||||
|
|
||||||
|
// TODO @jaosn:要不改成 expressCode;项目里使用这个哈
|
||||||
/**
|
/**
|
||||||
* 快递公司编码
|
* 快递公司编码
|
||||||
*/
|
*/
|
||||||
@ -29,19 +30,20 @@ public class Kd100ExpressQueryReqDTO {
|
|||||||
* 收、寄件人的电话号码
|
* 收、寄件人的电话号码
|
||||||
*/
|
*/
|
||||||
private String phone;
|
private String phone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 出发地城市
|
* 出发地城市
|
||||||
*/
|
*/
|
||||||
private String from;
|
private String from;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 目的地城市,到达目的地后会加大监控频率
|
* 目的地城市,到达目的地后会加大监控频率
|
||||||
*/
|
*/
|
||||||
private String to;
|
private String to;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回结果排序:desc降序(默认),asc 升序
|
* 返回结果排序
|
||||||
|
*
|
||||||
|
* desc 降序(默认), asc 升序
|
||||||
*/
|
*/
|
||||||
private String order;
|
private String order;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,23 +18,22 @@ public class Kd100ExpressQueryRespDTO {
|
|||||||
*/
|
*/
|
||||||
@JsonProperty("com")
|
@JsonProperty("com")
|
||||||
private String expressCompanyCode;
|
private String expressCompanyCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递单号
|
* 快递单号
|
||||||
*/
|
*/
|
||||||
@JsonProperty("nu")
|
@JsonProperty("nu")
|
||||||
private String logisticsNo;
|
private String logisticsNo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递单当前状态
|
* 快递单当前状态
|
||||||
*/
|
*/
|
||||||
private String state;
|
private String state;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询结果, 失败返回 "false"
|
* 查询结果
|
||||||
|
*
|
||||||
|
* 失败返回 "false"
|
||||||
*/
|
*/
|
||||||
private String result;
|
private String result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询结果失败时的错误信息
|
* 查询结果失败时的错误信息
|
||||||
*/
|
*/
|
||||||
@ -56,4 +55,5 @@ public class Kd100ExpressQueryRespDTO {
|
|||||||
@JsonProperty("context")
|
@JsonProperty("context")
|
||||||
private String state;
|
private String state;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,21 +12,22 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class KdNiaoExpressQueryReqDTO {
|
public class KdNiaoExpressQueryReqDTO {
|
||||||
|
|
||||||
|
// TODO @jaosn:要不改成 expressCode;项目里使用这个哈
|
||||||
/**
|
/**
|
||||||
* 快递公司编码
|
* 快递公司编码
|
||||||
*/
|
*/
|
||||||
@JsonProperty("ShipperCode")
|
@JsonProperty("ShipperCode")
|
||||||
private String expressCompanyCode;
|
private String expressCompanyCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递单号
|
* 快递单号
|
||||||
*/
|
*/
|
||||||
@JsonProperty("LogisticCode")
|
@JsonProperty("LogisticCode")
|
||||||
private String logisticsNo;
|
private String logisticsNo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 订单编号
|
* 订单编号
|
||||||
*/
|
*/
|
||||||
@JsonProperty("OrderCode")
|
@JsonProperty("OrderCode")
|
||||||
private String orderNo;
|
private String orderNo;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,15 @@ import java.util.List;
|
|||||||
|
|
||||||
import static cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum.KD_NIAO;
|
import static cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum.KD_NIAO;
|
||||||
|
|
||||||
|
// TODO @jason:可以把整体包结构调整下;参考 sms client 的方式;
|
||||||
|
// + config
|
||||||
|
// + core
|
||||||
|
// client
|
||||||
|
// + dto
|
||||||
|
// + impl:里面可以放 kdniaoclient、kd100client
|
||||||
|
// ExpressClient
|
||||||
|
// ExpressClientFactory: 通过它直接获取默认和创建默认的 Client
|
||||||
|
// enums
|
||||||
/**
|
/**
|
||||||
* 快递查询客户端实现
|
* 快递查询客户端实现
|
||||||
*
|
*
|
||||||
@ -25,29 +34,32 @@ import static cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQuery
|
|||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ExpressQueryClientImpl implements ExpressQueryClient {
|
public class ExpressQueryClientImpl implements ExpressQueryClient {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private ExpressQueryProviderFactory expressQueryProviderFactory;
|
private ExpressQueryProviderFactory expressQueryProviderFactory;
|
||||||
@Resource
|
@Resource
|
||||||
private TradeExpressQueryProperties tradeExpressQueryProperties;
|
private TradeExpressQueryProperties tradeExpressQueryProperties;
|
||||||
|
|
||||||
private ExpressQueryProvider expressQueryProvider;
|
private ExpressQueryProvider expressQueryProvider;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void init() {
|
private void init() {
|
||||||
|
// 如果未设置,默认使用快递鸟
|
||||||
ExpressQueryProviderEnum queryProvider = tradeExpressQueryProperties.getExpressQueryProvider();
|
ExpressQueryProviderEnum queryProvider = tradeExpressQueryProperties.getExpressQueryProvider();
|
||||||
if (queryProvider == null) {
|
if (queryProvider == null) {
|
||||||
// 如果未设置,默认使用快递鸟
|
|
||||||
queryProvider = KD_NIAO;
|
queryProvider = KD_NIAO;
|
||||||
}
|
}
|
||||||
|
// 创建客户端
|
||||||
expressQueryProvider = expressQueryProviderFactory.getOrCreateExpressQueryProvider(queryProvider);
|
expressQueryProvider = expressQueryProviderFactory.getOrCreateExpressQueryProvider(queryProvider);
|
||||||
if (expressQueryProvider == null) {
|
if (expressQueryProvider == null) {
|
||||||
// 记录错误日志
|
|
||||||
log.error("获取创建快递查询服务商{}失败,请检查相关配置", queryProvider);
|
log.error("获取创建快递查询服务商{}失败,请检查相关配置", queryProvider);
|
||||||
}
|
}
|
||||||
Assert.notNull(expressQueryProvider, "快递查询服务商不能为空");
|
Assert.notNull(expressQueryProvider, "快递查询服务商不能为空");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO) {
|
public List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO) {
|
||||||
return expressQueryProvider.realTimeQueryExpress(reqDTO);
|
return expressQueryProvider.realTimeQueryExpress(reqDTO);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,14 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* // TODO @jason:注释不全
|
||||||
* @author jason
|
* @author jason
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class ExpressQueryProviderFactoryImpl implements ExpressQueryProviderFactory {
|
public class ExpressQueryProviderFactoryImpl implements ExpressQueryProviderFactory {
|
||||||
|
|
||||||
private final Map<ExpressQueryProviderEnum, ExpressQueryProvider> providerMap = new ConcurrentHashMap<>(8);
|
private final Map<ExpressQueryProviderEnum, ExpressQueryProvider> providerMap = new ConcurrentHashMap<>(8);
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private TradeExpressQueryProperties tradeExpressQueryProperties;
|
private TradeExpressQueryProperties tradeExpressQueryProperties;
|
||||||
@Resource
|
@Resource
|
||||||
@ -31,6 +33,7 @@ public class ExpressQueryProviderFactoryImpl implements ExpressQueryProviderFact
|
|||||||
|
|
||||||
private ExpressQueryProvider createExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum,
|
private ExpressQueryProvider createExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum,
|
||||||
TradeExpressQueryProperties tradeExpressQueryProperties) {
|
TradeExpressQueryProperties tradeExpressQueryProperties) {
|
||||||
|
// TODO @jason:是不是直接 return 就好啦,更简洁一点
|
||||||
ExpressQueryProvider result = null;
|
ExpressQueryProvider result = null;
|
||||||
switch (queryProviderEnum) {
|
switch (queryProviderEnum) {
|
||||||
case KD_NIAO:
|
case KD_NIAO:
|
||||||
|
@ -21,10 +21,11 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
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_FAILED;
|
|
||||||
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.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE;
|
import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE;
|
||||||
|
|
||||||
|
// TODO @jason:可以参考 KdNiaoExpressQueryProvider 建议改改哈
|
||||||
/**
|
/**
|
||||||
* 快递 100 服务商
|
* 快递 100 服务商
|
||||||
*
|
*
|
||||||
@ -34,8 +35,8 @@ import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.Expr
|
|||||||
public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
||||||
|
|
||||||
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;
|
private final TradeExpressQueryProperties.Kd100Config config;
|
||||||
|
|
||||||
public Kd100ExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.Kd100Config config) {
|
public Kd100ExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.Kd100Config config) {
|
||||||
@ -45,16 +46,19 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
|
public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
|
||||||
|
// 发起查询
|
||||||
Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
|
Kd100ExpressQueryReqDTO kd100ReqParam = INSTANCE.convert2(reqDTO);
|
||||||
// 快递公司编码需要转成小写
|
kd100ReqParam.setExpressCompanyCode(kd100ReqParam.getExpressCompanyCode().toLowerCase()); // 快递公司编码需要转成小写
|
||||||
kd100ReqParam.setExpressCompanyCode(kd100ReqParam.getExpressCompanyCode().toLowerCase());
|
|
||||||
Kd100ExpressQueryRespDTO respDTO = sendExpressQueryReq(REAL_TIME_QUERY_URL, kd100ReqParam,
|
Kd100ExpressQueryRespDTO respDTO = sendExpressQueryReq(REAL_TIME_QUERY_URL, kd100ReqParam,
|
||||||
Kd100ExpressQueryRespDTO.class);
|
Kd100ExpressQueryRespDTO.class);
|
||||||
log.debug("快递 100 接口 查询接口返回 {}", respDTO);
|
log.debug("[realTimeQueryExpress][快递 100 接口 查询接口返回 {}]", respDTO);
|
||||||
|
// 处理结果
|
||||||
if (Objects.equals("false", respDTO.getResult())) {
|
if (Objects.equals("false", respDTO.getResult())) {
|
||||||
log.error("快递 100 接口 返回失败 {} ", respDTO.getMessage());
|
log.error("[realTimeQueryExpress][快递 100 接口 返回失败 {}]", respDTO.getMessage());
|
||||||
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
|
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getMessage());
|
||||||
|
// TODO @json:else 可以不用写哈;
|
||||||
} else {
|
} else {
|
||||||
|
// TODO @jason:convertList2 如果空,应该返回 list 了;
|
||||||
if (CollUtil.isNotEmpty(respDTO.getTracks())) {
|
if (CollUtil.isNotEmpty(respDTO.getTracks())) {
|
||||||
return INSTANCE.convertList2(respDTO.getTracks());
|
return INSTANCE.convertList2(respDTO.getTracks());
|
||||||
} else {
|
} else {
|
||||||
@ -65,12 +69,14 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送快递 100 实时快递查询请求,可以作为通用快递 100 通用请求接口。 目前没有其它场景需要使用。暂时放这里
|
* 发送快递 100 实时快递查询请求,可以作为通用快递 100 通用请求接口。 目前没有其它场景需要使用。暂时放这里
|
||||||
|
*
|
||||||
* @param url 请求 url
|
* @param url 请求 url
|
||||||
* @param req 对应请求的请求参数
|
* @param req 对应请求的请求参数
|
||||||
* @param respClass 对应请求的响应 class
|
* @param respClass 对应请求的响应 class
|
||||||
* @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 sendExpressQueryReq(String url, Req req, Class<Resp> respClass) {
|
||||||
// 请求头
|
// 请求头
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
@ -78,19 +84,20 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
// 生成签名
|
// 生成签名
|
||||||
String param = JsonUtils.toJsonString(req);
|
String param = JsonUtils.toJsonString(req);
|
||||||
String sign = generateReqSign(param, config.getKey(), config.getCustomer());
|
String sign = generateReqSign(param, config.getKey(), config.getCustomer());
|
||||||
log.debug("快递 100 快递 接口生成签名的: {}", sign);
|
|
||||||
// 请求体
|
// 请求体
|
||||||
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
|
||||||
requestBody.add("customer", config.getCustomer());
|
requestBody.add("customer", config.getCustomer());
|
||||||
requestBody.add("sign", sign);
|
requestBody.add("sign", sign);
|
||||||
requestBody.add("param", param);
|
requestBody.add("param", param);
|
||||||
log.debug("快递 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 方法哇?
|
||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||||
log.debug("快递 100 接口响应结果 {}", responseEntity);
|
log.debug("[sendExpressQueryReq][快递 100 接口响应结果 {}]", responseEntity);
|
||||||
|
|
||||||
// 处理响应
|
// 处理响应
|
||||||
|
// TODO @jason:if return 原则;if (!responseEntity.getStatusCode().is2xxSuccessful()) 抛出异常;接着处理成功的
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
String response = responseEntity.getBody();
|
String response = responseEntity.getBody();
|
||||||
return JsonUtils.parseObject(response, respClass);
|
return JsonUtils.parseObject(response, respClass);
|
||||||
@ -101,7 +108,8 @@ public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
|
|
||||||
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);
|
||||||
log.debug("快递 100 接口待签名的数据 {}", plainText);
|
// TODO @jason:DigestUtil.md5Hex(plainText);
|
||||||
return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
|
return HexUtil.encodeHexStr(DigestUtil.md5(plainText), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -32,21 +32,27 @@ import static cn.iocoder.yudao.module.trade.framework.delivery.core.convert.Expr
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
|
public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
|
||||||
|
|
||||||
private static final String REAL_TIME_QUERY_URL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
|
private static final String REAL_TIME_QUERY_URL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递鸟即时查询免费版 RequestType
|
* 快递鸟即时查询免费版 RequestType
|
||||||
*/
|
*/
|
||||||
private static final String REAL_TIME_FREE_REQ_TYPE = "1002";
|
private static final String REAL_TIME_FREE_REQ_TYPE = "1002";
|
||||||
|
|
||||||
private final RestTemplate restTemplate;
|
private final RestTemplate restTemplate;
|
||||||
private final TradeExpressQueryProperties.KdNiaoConfig config;
|
private final TradeExpressQueryProperties.KdNiaoConfig config;
|
||||||
|
|
||||||
|
// TODO @jason:可以改成 lombok 哈
|
||||||
public KdNiaoExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.KdNiaoConfig config) {
|
public KdNiaoExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.KdNiaoConfig config) {
|
||||||
this.restTemplate = restTemplate;
|
this.restTemplate = restTemplate;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 快递鸟即时查询免费版本 参见 <a href="https://www.yuque.com/kdnjishuzhichi/dfcrg1/wugo6k">快递鸟接口文档</a>
|
* 快递鸟即时查询免费版本
|
||||||
|
*
|
||||||
|
* @see <a href="https://www.yuque.com/kdnjishuzhichi/dfcrg1/wugo6k">快递鸟接口文档</a>
|
||||||
* @param reqDTO 查询请求参数
|
* @param reqDTO 查询请求参数
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -56,7 +62,7 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
kdNiaoReqData.setExpressCompanyCode(reqDTO.getExpressCompanyCode().toUpperCase());
|
kdNiaoReqData.setExpressCompanyCode(reqDTO.getExpressCompanyCode().toUpperCase());
|
||||||
KdNiaoExpressQueryRespDTO respDTO = sendKdNiaoApiRequest(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
|
KdNiaoExpressQueryRespDTO respDTO = sendKdNiaoApiRequest(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
|
||||||
kdNiaoReqData, KdNiaoExpressQueryRespDTO.class);
|
kdNiaoReqData, KdNiaoExpressQueryRespDTO.class);
|
||||||
log.debug("快递鸟即时查询接口返回 {}", respDTO);
|
log.debug("[realTimeQueryExpress][快递鸟即时查询接口返回 {}]", respDTO);
|
||||||
if(!respDTO.getSuccess()){
|
if(!respDTO.getSuccess()){
|
||||||
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getReason());
|
throw exception(EXPRESS_API_QUERY_FAILED, respDTO.getReason());
|
||||||
}else{
|
}else{
|
||||||
@ -85,16 +91,16 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
// 请求体
|
// 请求体
|
||||||
String reqData = JsonUtils.toJsonString(req);
|
String reqData = JsonUtils.toJsonString(req);
|
||||||
String dataSign = generateDataSign(reqData, config.getApiKey());
|
String dataSign = generateDataSign(reqData, config.getApiKey());
|
||||||
log.trace("得到快递鸟接口 RequestType : {} 的 签名: {}", requestType, dataSign);
|
|
||||||
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
|
||||||
requestBody.add("RequestData", reqData);
|
requestBody.add("RequestData", reqData);
|
||||||
requestBody.add("DataType", "2");
|
requestBody.add("DataType", "2");
|
||||||
requestBody.add("EBusinessID", config.getBusinessId());
|
requestBody.add("EBusinessID", config.getBusinessId());
|
||||||
requestBody.add("DataSign", dataSign);
|
requestBody.add("DataSign", dataSign);
|
||||||
requestBody.add("RequestType", requestType);
|
requestBody.add("RequestType", requestType);
|
||||||
log.debug("快递鸟接口 RequestType : {}, 的请求参数 {}", requestType, requestBody);
|
log.debug("[sendKdNiaoApiRequest][快递鸟接口 RequestType : {}, 的请求参数 {}]", requestType, requestBody);
|
||||||
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
|
||||||
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||||
log.debug("快递鸟接口 RequestType : {}, 的响应结果 {}", requestType, responseEntity);
|
log.debug("快递鸟接口 RequestType : {}, 的响应结果 {}", requestType, responseEntity);
|
||||||
// 处理响应
|
// 处理响应
|
||||||
@ -113,7 +119,7 @@ public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
|
|||||||
*/
|
*/
|
||||||
private String generateDataSign(String reqData, String apiKey) {
|
private String generateDataSign(String reqData, String apiKey) {
|
||||||
String plainText = String.format("%s%s", reqData, apiKey);
|
String plainText = String.format("%s%s", reqData, apiKey);
|
||||||
log.trace("签名前的数据 {}", plainText);
|
|
||||||
return URLEncodeUtil.encode(Base64.encode(DigestUtil.md5Hex(plainText)));
|
return URLEncodeUtil.encode(Base64.encode(DigestUtil.md5Hex(plainText)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,12 @@ import javax.annotation.Resource;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
// TODO @芋艿:单测最后 review
|
||||||
/**
|
/**
|
||||||
* @author jason
|
* @author jason
|
||||||
*/
|
*/
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressQueryProviderTest.Application.class)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressQueryProviderTest.Application.class)
|
||||||
@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件
|
@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件 TODO @jason:可以直接写到 application-unit-test.yaml 配置文件里
|
||||||
public class KdNiaoExpressQueryProviderTest {
|
public class KdNiaoExpressQueryProviderTest {
|
||||||
@Resource
|
@Resource
|
||||||
private RestTemplateBuilder builder;
|
private RestTemplateBuilder builder;
|
||||||
|
Loading…
Reference in New Issue
Block a user