mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-27 01:32:03 +08:00
快递查询、 通过快递鸟查询实现
This commit is contained in:
parent
5ffc1ac9f3
commit
45e5578cb4
@ -10,11 +10,14 @@ import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.filter.CorsFilter;
|
||||||
@ -107,6 +110,15 @@ 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);
|
||||||
|
@ -53,7 +53,8 @@ public interface ErrorCodeConstants {
|
|||||||
ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名");
|
ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名");
|
||||||
ErrorCode DELIVERY_EXPRESS_USER_ADDRESS_IS_EMPTY = new ErrorCode(1011003004, "计算快递运费时,收件人地址编号为空");
|
ErrorCode DELIVERY_EXPRESS_USER_ADDRESS_IS_EMPTY = new ErrorCode(1011003004, "计算快递运费时,收件人地址编号为空");
|
||||||
ErrorCode PRODUCT_EXPRESS_TEMPLATE_NOT_FOUND = new ErrorCode(1011003005, "找不到到商品对应的运费模板");
|
ErrorCode PRODUCT_EXPRESS_TEMPLATE_NOT_FOUND = new ErrorCode(1011003005, "找不到到商品对应的运费模板");
|
||||||
ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003006, "自提门店不存在");
|
ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003006, "快递接口查询失败");
|
||||||
|
ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003007, "自提门店不存在");
|
||||||
|
|
||||||
// ========== Price 相关 1011004000 ============
|
// ========== Price 相关 1011004000 ============
|
||||||
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0");
|
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0");
|
||||||
|
@ -0,0 +1,65 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.config;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 交易快递查询的配置项
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "yudao.trade.express.query")
|
||||||
|
@Data
|
||||||
|
@Validated
|
||||||
|
public class TradeExpressQueryProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询服务商, 如果未配置,默认使用快递鸟
|
||||||
|
*/
|
||||||
|
private ExpressQueryProviderEnum expressQueryProvider;
|
||||||
|
/**
|
||||||
|
* 快递鸟配置
|
||||||
|
*/
|
||||||
|
@Valid
|
||||||
|
private KdNiaoConfig kdNiao;
|
||||||
|
/**
|
||||||
|
* 快递 100 配置
|
||||||
|
*/
|
||||||
|
@Valid
|
||||||
|
private Kd100Config kd100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟配置项目
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public static class KdNiaoConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟用户 ID
|
||||||
|
*/
|
||||||
|
@NotEmpty(message = "快递鸟用户 ID 配置项不能为空")
|
||||||
|
private String businessId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟 API Key
|
||||||
|
*/
|
||||||
|
@NotEmpty(message = "快递鸟 Api Key 配置项不能为空")
|
||||||
|
private String apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递100 配置项 TODO
|
||||||
|
*/
|
||||||
|
public static class Kd100Config {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询客户端
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
public interface ExpressQueryClient {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递实时查询
|
||||||
|
*
|
||||||
|
* @param reqDTO 查询请求参数
|
||||||
|
*/
|
||||||
|
List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO);
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询服务商
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
public interface ExpressQueryProvider {
|
||||||
|
/**
|
||||||
|
* 快递实时查询
|
||||||
|
*
|
||||||
|
* @param reqDTO 查询请求参数
|
||||||
|
*/
|
||||||
|
List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO);
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询服务商枚举
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public enum ExpressQueryProviderEnum {
|
||||||
|
KD_NIAO("kd-niao", "快递鸟"),
|
||||||
|
KD_100("kd-100", "快递100");
|
||||||
|
/**
|
||||||
|
* 快递服务商唯一编码
|
||||||
|
*/
|
||||||
|
private final String code;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递服务商名称
|
||||||
|
*/
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
ExpressQueryProviderEnum(String code, String name) {
|
||||||
|
this.code = code;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递服务商工厂, 用于创建和缓存快递服务商服务
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
public interface ExpressQueryProviderFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过枚举获取快递查询服务商, 如果不存在。就创建一个对应的快递查询服务商
|
||||||
|
* @param queryProviderEnum 快递服务商枚举
|
||||||
|
*/
|
||||||
|
ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum);
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.convert;
|
||||||
|
|
||||||
|
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.kdniao.KdNiaoExpressQueryReqDTO;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryRespDTO;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ExpressQueryConvert {
|
||||||
|
|
||||||
|
ExpressQueryConvert INSTANCE = Mappers.getMapper(ExpressQueryConvert.class);
|
||||||
|
|
||||||
|
List<ExpressQueryRespDTO> convertList(List<KdNiaoExpressQueryRespDTO.ExpressTrack> expressTrackList);
|
||||||
|
|
||||||
|
KdNiaoExpressQueryReqDTO convert(ExpressQueryReqDTO dto);
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询 Req DTO
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class ExpressQueryReqDTO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递公司编码
|
||||||
|
*
|
||||||
|
* 对应 {@link DeliveryExpressDO#getCode()} }
|
||||||
|
*/
|
||||||
|
private String expressCompanyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发货快递单号
|
||||||
|
*/
|
||||||
|
private String logisticsNo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 收、寄件人的电话号码
|
||||||
|
*/
|
||||||
|
private String phone;
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询 Resp DTO
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class ExpressQueryRespDTO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发生时间
|
||||||
|
*/
|
||||||
|
private String time;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递状态
|
||||||
|
*/
|
||||||
|
private String state;
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟快递查询 Req DTO
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class KdNiaoExpressQueryReqDTO {
|
||||||
|
/**
|
||||||
|
* 快递公司编码
|
||||||
|
*/
|
||||||
|
@JsonProperty("ShipperCode")
|
||||||
|
private String expressCompanyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递单号
|
||||||
|
*/
|
||||||
|
@JsonProperty("LogisticCode")
|
||||||
|
private String logisticsNo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单编号
|
||||||
|
*/
|
||||||
|
@JsonProperty("OrderCode")
|
||||||
|
private String orderNo;
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟快递查询 Resp DTO 参见 <a href="https://www.yuque.com/kdnjishuzhichi/dfcrg1/wugo6k">快递鸟接口文档</a>
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class KdNiaoExpressQueryRespDTO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递公司编码
|
||||||
|
*/
|
||||||
|
@JsonProperty("ShipperCode")
|
||||||
|
private String expressCompanyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递单号
|
||||||
|
*/
|
||||||
|
@JsonProperty("LogisticCode")
|
||||||
|
private String logisticsNo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单编号
|
||||||
|
*/
|
||||||
|
@JsonProperty("OrderCode")
|
||||||
|
private String orderNo;
|
||||||
|
|
||||||
|
@JsonProperty("EBusinessID")
|
||||||
|
private String businessId;
|
||||||
|
@JsonProperty("State")
|
||||||
|
private String state;
|
||||||
|
/**
|
||||||
|
* 成功与否
|
||||||
|
*/
|
||||||
|
@JsonProperty("Success")
|
||||||
|
private Boolean success;
|
||||||
|
/**
|
||||||
|
* 失败原因
|
||||||
|
*/
|
||||||
|
@JsonProperty("Reason")
|
||||||
|
private String reason;
|
||||||
|
|
||||||
|
@JsonProperty("Traces")
|
||||||
|
private List<ExpressTrack> tracks;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class ExpressTrack {
|
||||||
|
/**
|
||||||
|
* 轨迹发生时间
|
||||||
|
*/
|
||||||
|
@JsonProperty("AcceptTime")
|
||||||
|
private String time;
|
||||||
|
/**
|
||||||
|
* 轨迹描述
|
||||||
|
*/
|
||||||
|
@JsonProperty("AcceptStation")
|
||||||
|
private String state;
|
||||||
|
}
|
||||||
|
|
||||||
|
// {
|
||||||
|
// "EBusinessID": "1237100",
|
||||||
|
// "Traces": [],
|
||||||
|
// "State": "0",
|
||||||
|
// "ShipperCode": "STO",
|
||||||
|
// "LogisticCode": "638650888018",
|
||||||
|
// "Success": true,
|
||||||
|
// "Reason": "暂无轨迹信息"
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryClient;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProvider;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderFactory;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderEnum.KD_NIAO;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递查询客户端实现
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class ExpressQueryClientImpl implements ExpressQueryClient {
|
||||||
|
@Resource
|
||||||
|
private ExpressQueryProviderFactory expressQueryProviderFactory;
|
||||||
|
@Resource
|
||||||
|
private TradeExpressQueryProperties tradeExpressQueryProperties;
|
||||||
|
|
||||||
|
private ExpressQueryProvider expressQueryProvider;
|
||||||
|
@PostConstruct
|
||||||
|
private void init(){
|
||||||
|
ExpressQueryProviderEnum queryProvider = tradeExpressQueryProperties.getExpressQueryProvider();
|
||||||
|
if (queryProvider == null) {
|
||||||
|
// 如果未设置,默认使用快递鸟
|
||||||
|
queryProvider = KD_NIAO;
|
||||||
|
}
|
||||||
|
expressQueryProvider = expressQueryProviderFactory.getOrCreateExpressQueryProvider(queryProvider);
|
||||||
|
if (expressQueryProvider == null) {
|
||||||
|
// 记录错误日志
|
||||||
|
log.error("获取创建快递查询服务商{}失败,请检查相关配置", queryProvider);
|
||||||
|
}
|
||||||
|
Assert.notNull(expressQueryProvider, "快递查询服务商不能为空");
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public List<ExpressQueryRespDTO> realTimeQuery(ExpressQueryReqDTO reqDTO) {
|
||||||
|
return expressQueryProvider.realTimeQueryExpress(reqDTO);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
|
||||||
|
|
||||||
|
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.ExpressQueryProviderEnum;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.ExpressQueryProviderFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class ExpressQueryProviderFactoryImpl implements ExpressQueryProviderFactory {
|
||||||
|
|
||||||
|
private final Map<ExpressQueryProviderEnum, ExpressQueryProvider> providerMap = new ConcurrentHashMap<>(8);
|
||||||
|
@Resource
|
||||||
|
private TradeExpressQueryProperties tradeExpressQueryProperties;
|
||||||
|
@Resource
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExpressQueryProvider getOrCreateExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum) {
|
||||||
|
return providerMap.computeIfAbsent(queryProviderEnum,
|
||||||
|
provider -> createExpressQueryProvider(provider, tradeExpressQueryProperties));
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExpressQueryProvider createExpressQueryProvider(ExpressQueryProviderEnum queryProviderEnum,
|
||||||
|
TradeExpressQueryProperties tradeExpressQueryProperties) {
|
||||||
|
ExpressQueryProvider result = null;
|
||||||
|
switch (queryProviderEnum) {
|
||||||
|
case KD_NIAO:
|
||||||
|
result = new KdNiaoExpressQueryProvider(restTemplate, tradeExpressQueryProperties.getKdNiao());
|
||||||
|
break;
|
||||||
|
case KD_100:
|
||||||
|
result = new Kd100ExpressQueryProvider(restTemplate, tradeExpressQueryProperties.getKd100());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
|
||||||
|
|
||||||
|
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 org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递 100 服务商 TODO
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
public class Kd100ExpressQueryProvider implements ExpressQueryProvider {
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
private final TradeExpressQueryProperties.Kd100Config config;
|
||||||
|
|
||||||
|
public Kd100ExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.Kd100Config config) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,129 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.codec.Base64;
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.net.URLEncodeUtil;
|
||||||
|
import cn.hutool.crypto.digest.DigestUtil;
|
||||||
|
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.kdniao.KdNiaoExpressQueryReqDTO;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.provider.kdniao.KdNiaoExpressQueryRespDTO;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
|
import org.springframework.util.MultiValueMap;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
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.framework.delivery.core.convert.ExpressQueryConvert.INSTANCE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟服务商
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class KdNiaoExpressQueryProvider implements ExpressQueryProvider {
|
||||||
|
private static final String REAL_TIME_QUERY_URL = "https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";
|
||||||
|
/**
|
||||||
|
* 快递鸟即时查询免费版 RequestType
|
||||||
|
*/
|
||||||
|
private static final String REAL_TIME_FREE_REQ_TYPE = "1002";
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
private final TradeExpressQueryProperties.KdNiaoConfig config;
|
||||||
|
|
||||||
|
public KdNiaoExpressQueryProvider(RestTemplate restTemplate, TradeExpressQueryProperties.KdNiaoConfig config) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟即时查询免费版本 参见 <a href="https://www.yuque.com/kdnjishuzhichi/dfcrg1/wugo6k">快递鸟接口文档</a>
|
||||||
|
* @param reqDTO 查询请求参数
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<ExpressQueryRespDTO> realTimeQueryExpress(ExpressQueryReqDTO reqDTO) {
|
||||||
|
KdNiaoExpressQueryReqDTO kdNiaoReqData = INSTANCE.convert(reqDTO);
|
||||||
|
// 快递公司编码需要转成大写
|
||||||
|
kdNiaoReqData.setExpressCompanyCode(reqDTO.getExpressCompanyCode().toUpperCase());
|
||||||
|
KdNiaoExpressQueryRespDTO respDTO = sendKdNiaoApiRequest(REAL_TIME_QUERY_URL, REAL_TIME_FREE_REQ_TYPE,
|
||||||
|
kdNiaoReqData, KdNiaoExpressQueryRespDTO.class);
|
||||||
|
log.debug("快递鸟即时查询接口返回 {}", respDTO);
|
||||||
|
if (respDTO.getSuccess() && CollUtil.isNotEmpty(respDTO.getTracks())) {
|
||||||
|
return INSTANCE.convertList(respDTO.getTracks());
|
||||||
|
}else{
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <Req, Resp> Resp sendKdNiaoApiRequest(String url, String requestType, Req req,
|
||||||
|
Class<Resp> respClass){
|
||||||
|
return sendKdNiaoApiRequest(url, requestType, req, respClass, null);
|
||||||
|
}
|
||||||
|
private <Req, Resp> Resp sendKdNiaoApiRequest(String url, String requestType, Req req,
|
||||||
|
TypeReference<Resp> typeRefResp){
|
||||||
|
return sendKdNiaoApiRequest(url, requestType, req, null, typeRefResp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟 通用的 API 请求
|
||||||
|
* @param url 请求 url
|
||||||
|
* @param requestType 对应的请求指令 (快递鸟的RequestType)
|
||||||
|
* @param req 对应请求的请求参数
|
||||||
|
* @param respClass 对应请求的响应 class
|
||||||
|
* @param typeRefResp 对应请求的响应 TypeReference
|
||||||
|
* @param <Req> 每个请求的请求结构 Req DTO
|
||||||
|
* @param <Resp> 每个请求的响应结构 Resp DTO
|
||||||
|
*/
|
||||||
|
private <Req, Resp> Resp sendKdNiaoApiRequest(String url, String requestType, Req req,
|
||||||
|
Class<Resp> respClass, TypeReference<Resp> typeRefResp){
|
||||||
|
// 请求头
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
|
// 请求体
|
||||||
|
String reqData = JsonUtils.toJsonString(req);
|
||||||
|
String dataSign = generateDataSign(reqData, config.getApiKey());
|
||||||
|
log.trace("得到快递鸟接口 RequestType : {} 的 签名: {}", requestType, dataSign);
|
||||||
|
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
|
||||||
|
requestBody.add("RequestData", reqData);
|
||||||
|
requestBody.add("DataType", "2");
|
||||||
|
requestBody.add("EBusinessID", config.getBusinessId());
|
||||||
|
requestBody.add("DataSign", dataSign);
|
||||||
|
requestBody.add("RequestType", requestType);
|
||||||
|
log.debug("快递鸟接口 RequestType : {}, 的请求参数 {}", requestType, requestBody);
|
||||||
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
|
||||||
|
// 发送请求
|
||||||
|
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||||
|
log.debug("快递鸟接口 RequestType : {}, 的响应结果 {}", requestType, responseEntity);
|
||||||
|
// 处理响应
|
||||||
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
String response = responseEntity.getBody();
|
||||||
|
if (respClass != null && typeRefResp == null) {
|
||||||
|
return JsonUtils.parseObject(response, respClass);
|
||||||
|
} else {
|
||||||
|
return JsonUtils.parseObject(response, typeRefResp);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw exception(EXPRESS_API_QUERY_FAILED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 快递鸟生成请求签名 参见 <a href="https://www.yuque.com/kdnjishuzhichi/dfcrg1/zes04h">签名说明</a>
|
||||||
|
* @param reqData 请求实体
|
||||||
|
* @param apiKey api Key
|
||||||
|
*/
|
||||||
|
private String generateDataSign(String reqData, String apiKey) {
|
||||||
|
String plainText = String.format("%s%s", reqData, apiKey);
|
||||||
|
log.trace("签名前的数据 {}", plainText);
|
||||||
|
return URLEncodeUtil.encode(Base64.encode(DigestUtil.md5Hex(plainText)));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
package cn.iocoder.yudao.module.trade.framework.delivery.core.impl;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.config.TradeExpressQueryProperties;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryReqDTO;
|
||||||
|
import cn.iocoder.yudao.module.trade.framework.delivery.core.dto.ExpressQueryRespDTO;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = KdNiaoExpressQueryProviderTest.Application.class)
|
||||||
|
@ActiveProfiles("trade-delivery-query") // 设置使用 trade-delivery-query 配置文件
|
||||||
|
public class KdNiaoExpressQueryProviderTest {
|
||||||
|
@Resource
|
||||||
|
private RestTemplateBuilder builder;
|
||||||
|
@Resource
|
||||||
|
private TradeExpressQueryProperties expressQueryProperties;
|
||||||
|
|
||||||
|
private KdNiaoExpressQueryProvider kdNiaoExpressQueryProvider;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void init(){
|
||||||
|
kdNiaoExpressQueryProvider = new KdNiaoExpressQueryProvider(builder.build(),expressQueryProperties.getKdNiao());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void queryExpress() {
|
||||||
|
ExpressQueryReqDTO reqDTO = new ExpressQueryReqDTO();
|
||||||
|
reqDTO.setExpressCompanyCode("yto");
|
||||||
|
reqDTO.setLogisticsNo("YT1764381060802");
|
||||||
|
List<ExpressQueryRespDTO> expressQueryRespDTOS = kdNiaoExpressQueryProvider.realTimeQueryExpress(reqDTO);
|
||||||
|
assertNotNull(expressQueryRespDTOS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Import({
|
||||||
|
RestTemplateAutoConfiguration.class
|
||||||
|
})
|
||||||
|
@EnableConfigurationProperties(TradeExpressQueryProperties.class)
|
||||||
|
public static class Application {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
spring:
|
||||||
|
main:
|
||||||
|
lazy-initialization: true # 开启懒加载,加快速度
|
||||||
|
banner-mode: off # 单元测试,禁用 Banner
|
||||||
|
|
||||||
|
--- #################### 数据库相关配置 ####################
|
||||||
|
|
||||||
|
yudao:
|
||||||
|
trade:
|
||||||
|
express:
|
||||||
|
query:
|
||||||
|
express-query-provider: kd_niao
|
||||||
|
kd-niao:
|
||||||
|
api-key: 8e22d97d-6a3d-442e-b243-2190c9f0cfdd
|
||||||
|
business-id: 1801700
|
Loading…
Reference in New Issue
Block a user