|
| 1 | +package net.kdks.handler; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.alibaba.fastjson.TypeReference; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Base64; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import net.kdks.config.JingdongConfig; |
| 10 | +import net.kdks.config.JituConfig; |
| 11 | +import net.kdks.constant.CommonConstant; |
| 12 | +import net.kdks.constant.JingdongConstant; |
| 13 | +import net.kdks.constant.JituConstant; |
| 14 | +import net.kdks.enums.ExpressCompanyCodeEnum; |
| 15 | +import net.kdks.enums.ExpressStateEnum; |
| 16 | +import net.kdks.model.CreateOrderParam; |
| 17 | +import net.kdks.model.ExpressData; |
| 18 | +import net.kdks.model.ExpressParam; |
| 19 | +import net.kdks.model.ExpressPriceParam; |
| 20 | +import net.kdks.model.ExpressPriceResult; |
| 21 | +import net.kdks.model.ExpressResponse; |
| 22 | +import net.kdks.model.ExpressResult; |
| 23 | +import net.kdks.model.OrderResult; |
| 24 | +import net.kdks.model.jd.JingdongResult; |
| 25 | +import net.kdks.model.jd.route.JingdongRouteItem; |
| 26 | +import net.kdks.model.jd.route.JingdongRouteResult; |
| 27 | +import net.kdks.model.jt.JituResult; |
| 28 | +import net.kdks.model.jt.price.JituPriceResult; |
| 29 | +import net.kdks.model.jt.route.JituRouteItem; |
| 30 | +import net.kdks.model.jt.route.JituRouteResult; |
| 31 | +import net.kdks.request.JingdongRequest; |
| 32 | +import net.kdks.request.JituRequest; |
| 33 | +import net.kdks.utils.Assert; |
| 34 | +import net.kdks.utils.DigestUtils; |
| 35 | +import net.kdks.utils.MapUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * 京东. |
| 39 | + * |
| 40 | + * @author Ze.Wang |
| 41 | + * @since 0.0.11 |
| 42 | + */ |
| 43 | +public class ExpressJingdongHandler implements ExpressHandler { |
| 44 | + |
| 45 | + |
| 46 | + private JingdongRequest jingdongRequest; |
| 47 | + |
| 48 | + private JingdongConfig jingdongConfig; |
| 49 | + |
| 50 | + public ExpressJingdongHandler(JingdongConfig jingdongConfig) { |
| 51 | + this.jingdongConfig = jingdongConfig; |
| 52 | + this.jingdongRequest = new JingdongRequest(jingdongConfig); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * 查询轨迹信息. |
| 57 | + * |
| 58 | + * @param expressParam 快递号、手机、快递公司编码 |
| 59 | + * @return 查询接口 |
| 60 | + */ |
| 61 | + @Override |
| 62 | + public ExpressResponse<List<ExpressResult>> getExpressInfo(ExpressParam expressParam) { |
| 63 | + List<ExpressResult> expressResults = new ArrayList<>(); |
| 64 | + for (String expressNo : expressParam.getExpressNos()) { |
| 65 | + Map<String, String> paramMap = MapUtils.newHashMap(3); |
| 66 | + paramMap.put("waybillCode", expressNo); |
| 67 | + paramMap.put("orderOrigin", "1"); |
| 68 | + paramMap.put("customerCode", jingdongConfig.getCustomerCode()); |
| 69 | + List<Map<String, String>> paramList = new ArrayList<>(); |
| 70 | + paramList.add(paramMap); |
| 71 | + String param = JSON.toJSONString(paramList); |
| 72 | + String responseData = jingdongRequest.queryRouteRequest(param, expressParam.getFormat()); |
| 73 | + ExpressResult expressResult = disposeResult(responseData, expressNo, expressParam); |
| 74 | + expressResults.add(expressResult); |
| 75 | + } |
| 76 | + return ExpressResponse.ok(expressResults); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 结果处理. |
| 81 | + * |
| 82 | + * @param responseData 响应 |
| 83 | + * @return 查询结果 |
| 84 | + */ |
| 85 | + private ExpressResult disposeResult(String responseData, String expressNo, |
| 86 | + ExpressParam expressParam) { |
| 87 | + |
| 88 | + ExpressResult expressResult = new ExpressResult(); |
| 89 | + if (expressParam.isViewOriginal()) { |
| 90 | + expressResult.setOriginalResult(responseData); |
| 91 | + } |
| 92 | + expressResult.setCom(ExpressCompanyCodeEnum.JD.getValue()); |
| 93 | + expressResult.setNu(expressNo); |
| 94 | + JingdongResult<JingdongRouteResult> result = |
| 95 | + JSON.parseObject(responseData, |
| 96 | + new TypeReference<JingdongResult<JingdongRouteResult>>() {}); |
| 97 | + if (!JingdongConstant.REQUEST_SUCCESS_CODE.equals(result.getCode()) |
| 98 | + || result.getData() == null) { |
| 99 | + expressResult.setState(ExpressStateEnum.NO_INFO.getValue()); |
| 100 | + expressResult.setMsg(result.getMsg()); |
| 101 | + return expressResult; |
| 102 | + } |
| 103 | + List<JingdongRouteItem> routes = result.getData().getTraceDetails(); |
| 104 | + if (routes == null || routes.isEmpty()) { |
| 105 | + expressResult.setState(ExpressStateEnum.NO_INFO.getValue()); |
| 106 | + expressResult.setMsg(CommonConstant.NO_INFO); |
| 107 | + return expressResult; |
| 108 | + } |
| 109 | + if (expressParam.isViewRoute()) { |
| 110 | + List<ExpressData> data = new ArrayList<>(routes.size()); |
| 111 | + data.addAll(routes); |
| 112 | + expressResult.setData(data); |
| 113 | + } |
| 114 | + expressResult.setState(routes.get(0).getStatus()); |
| 115 | + if (ExpressStateEnum.SIGNED.getValue().equals(expressResult.getState())) { |
| 116 | + expressResult.setIscheck(CommonConstant.YES); |
| 117 | + } |
| 118 | + return expressResult; |
| 119 | + |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * 运费预估. |
| 125 | + * |
| 126 | + * @param expressPriceParam 起始省份、起始城市、目的身份、目的城市、重量、长、宽、高 |
| 127 | + * @return 运费 |
| 128 | + */ |
| 129 | + @Override |
| 130 | + public ExpressResponse<ExpressPriceResult> getExpressPrice( |
| 131 | + ExpressPriceParam expressPriceParam) { |
| 132 | + return ExpressResponse.failed(CommonConstant.NO_SOPPORT); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * 创建订单. |
| 137 | + * |
| 138 | + * @param createOrderParam 下单参数,主要包含物品信息、收件人信息、寄件人信息等 |
| 139 | + * @return 快递单号等信息 |
| 140 | + */ |
| 141 | + @Override |
| 142 | + public ExpressResponse<OrderResult> createOrder(CreateOrderParam createOrderParam) { |
| 143 | + return ExpressResponse.failed(CommonConstant.NO_SOPPORT); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * 获取当前快递公司编码. |
| 148 | + * |
| 149 | + * @return 快递公司编码 |
| 150 | + */ |
| 151 | + @Override |
| 152 | + public String getExpressCompanyCode() { |
| 153 | + return ExpressCompanyCodeEnum.JD.getValue(); |
| 154 | + } |
| 155 | + |
| 156 | +} |
| 157 | + |
0 commit comments