|
@@ -1,12 +1,10 @@
|
|
|
package com.fjhx.uitl.kd100;
|
|
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.extra.spring.SpringUtil;
|
|
|
+import cn.hutool.http.HttpStatus;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fjhx.config.KD100Config;
|
|
|
-import com.fjhx.constants.RedisConstant;
|
|
|
import com.fjhx.constants.logistics.LogisticsConstant;
|
|
|
-import com.google.gson.Gson;
|
|
|
import com.kuaidi100.sdk.api.QueryTrack;
|
|
|
import com.kuaidi100.sdk.api.Subscribe;
|
|
|
import com.kuaidi100.sdk.contant.ApiInfoConstant;
|
|
@@ -17,46 +15,75 @@ import com.kuaidi100.sdk.utils.SignUtils;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
-import org.springblade.core.redis.cache.BladeRedis;
|
|
|
-import org.springblade.core.tool.api.ResultCode;
|
|
|
import org.springblade.core.tool.utils.Func;
|
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
-import java.util.Date;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
@Log4j2
|
|
|
public class KD100Util {
|
|
|
|
|
|
- private final static BladeRedis bladeRedis = SpringUtil.getBean(BladeRedis.class);
|
|
|
-
|
|
|
/**
|
|
|
* 实时快递查询接口
|
|
|
*
|
|
|
* @param com 快递公司编码
|
|
|
* @param num 快递单号
|
|
|
- * @return
|
|
|
*/
|
|
|
- public static JSONObject queryTrack(String com, String num) {
|
|
|
- HttpResult result;
|
|
|
+ public static KD100Result queryTrack(String com, String num) {
|
|
|
+ HttpResult httpResult;
|
|
|
+
|
|
|
try {
|
|
|
- QueryTrackReq queryTrackReq = new QueryTrackReq();
|
|
|
+ // 参数组合成的json对象
|
|
|
QueryTrackParam queryTrackParam = new QueryTrackParam();
|
|
|
queryTrackParam.setCom(com);
|
|
|
queryTrackParam.setNum(num);
|
|
|
- String param = new Gson().toJson(queryTrackParam);
|
|
|
+ String param = JSON.toJSONString(queryTrackParam);
|
|
|
|
|
|
+ // 快递100入参
|
|
|
+ QueryTrackReq queryTrackReq = new QueryTrackReq();
|
|
|
queryTrackReq.setParam(param);
|
|
|
queryTrackReq.setCustomer(KD100Config.getCustomer());
|
|
|
queryTrackReq.setSign(SignUtils.querySign(param, KD100Config.getKey(), KD100Config.getCustomer()));
|
|
|
|
|
|
- IBaseClient baseClient = new QueryTrack();
|
|
|
- result = baseClient.execute(queryTrackReq);
|
|
|
+ // 查询快递100
|
|
|
+ httpResult = new QueryTrack().execute(queryTrackReq);
|
|
|
} catch (Exception e) {
|
|
|
- log.error(e.toString());
|
|
|
- throw new ServiceException("请求第三方快递信息异常,请联系管理员!");
|
|
|
+ log.error("请求kd100发生异常", e);
|
|
|
+ throw new ServiceException("请求kd100发生异常,异常原因:" + e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Objects.equals(httpResult.getStatus(), HttpStatus.HTTP_OK)) {
|
|
|
+ return JSON.parseObject(httpResult.getBody()).toJavaObject(KD100Result.class);
|
|
|
+ } else {
|
|
|
+ log.error("查询快递100失败,失败原因:{}", httpResult.getBody());
|
|
|
+ throw new ServiceException("查询快递100失败,失败原因:" + httpResult.getBody());
|
|
|
}
|
|
|
- return parseQueryResult(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取快递状态并监听未到货快递
|
|
|
+ *
|
|
|
+ * @param com 快递公司编码
|
|
|
+ * @param num 快递单号
|
|
|
+ * @return state
|
|
|
+ */
|
|
|
+ public static Integer getStateAndMonitor(String com, String num) {
|
|
|
+ // 查询快递100的物流信息
|
|
|
+ KD100Result result = KD100Util.queryTrack(com, num);
|
|
|
+ String message = result.getMessage();
|
|
|
+ if (Objects.equals(message, "查询无结果,请隔段时间再查")) {
|
|
|
+ return LogisticsConstant.KD100Status.STATUS_N;
|
|
|
+ }
|
|
|
+ if (Objects.equals(message, "ok")) {
|
|
|
+ Integer state = result.getState();
|
|
|
+ if (!Objects.equals(state, LogisticsConstant.KD100Status.STATUS_3)) {
|
|
|
+ // 如果不是已签收状态,则开启订阅(物流状态跟踪并推送)
|
|
|
+ KD100Util.subscribe(com, num);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("快递100异常:" + message);
|
|
|
+ }
|
|
|
+ return result.getState();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -64,9 +91,8 @@ public class KD100Util {
|
|
|
*
|
|
|
* @param com 快递公司编码
|
|
|
* @param num 快递单号
|
|
|
- * @return
|
|
|
*/
|
|
|
- public static JSONObject subscribe(String com, String num, Date date) {
|
|
|
+ public static void subscribe(String com, String num) {
|
|
|
HttpResult result;
|
|
|
try {
|
|
|
SubscribeParameters subscribeParameters = new SubscribeParameters();
|
|
@@ -80,65 +106,23 @@ public class KD100Util {
|
|
|
|
|
|
SubscribeReq subscribeReq = new SubscribeReq();
|
|
|
subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
|
|
|
- subscribeReq.setParam(new Gson().toJson(subscribeParam));
|
|
|
+ subscribeReq.setParam(JSON.toJSONString(subscribeParam));
|
|
|
|
|
|
IBaseClient subscribe = new Subscribe();
|
|
|
result = subscribe.execute(subscribeReq);
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.toString());
|
|
|
- throw new ServiceException("请求第三方快递订阅功能异常,请联系管理员!");
|
|
|
- }
|
|
|
- return parseSubscribeData(result, com, num, date);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 解析数据:查询结果
|
|
|
- *
|
|
|
- * @param result 数据结果
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static JSONObject parseQueryResult(HttpResult result) {
|
|
|
- if (Func.isNotEmpty(result) && result.getStatus() == ResultCode.SUCCESS.getCode()) {
|
|
|
- JSONObject body = JSONObject.parseObject(result.getBody());
|
|
|
- if (Func.isNotEmpty(body.get("message")) && StringUtils.equalsIgnoreCase(body.getString("message"), "ok")) {
|
|
|
- return body;
|
|
|
- } else {
|
|
|
- throw new ServiceException("操作失败,原因:物流信息【" + body.getString("message") + "】,请稍后重试,或者联系管理员!");
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new ServiceException("物流信息查询失败,请稍后重试,或者联系管理员!");
|
|
|
+ throw new ServiceException("请求第三方快递订阅功能异常:" + e);
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 解析数据:订阅结果
|
|
|
- *
|
|
|
- * @param result 数据结果
|
|
|
- * @return
|
|
|
- */
|
|
|
- private static JSONObject parseSubscribeData(HttpResult result, String com, String num, Date date) {
|
|
|
- if (Func.isNotEmpty(result) && result.getStatus() == ResultCode.SUCCESS.getCode()) {
|
|
|
- JSONObject body = JSONObject.parseObject(result.getBody());
|
|
|
- if (Func.isNotEmpty(body) && body.getBoolean("result")) {
|
|
|
- return body;
|
|
|
- } else {
|
|
|
-
|
|
|
- if (DateUtil.betweenDay(new Date(), date, false) < 3) {
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("com", com);
|
|
|
- jsonObject.put("num", num);
|
|
|
- jsonObject.put("date", date);
|
|
|
- bladeRedis.set(RedisConstant.KD_100_SEND_SUBSCRIBE + com + ":" + num, jsonObject);
|
|
|
- }
|
|
|
-
|
|
|
- log.error("监听物流消息失败: com:{}, num:{}, messageL{}", com, num, body.toString());
|
|
|
|
|
|
- return body;
|
|
|
-
|
|
|
- // throw new ServiceException("操作失败,原因:物流信息【" + body.getString("message") + "】,请稍后重试,或者联系管理员!");
|
|
|
+ if (Objects.equals(result.getStatus(), HttpStatus.HTTP_OK)) {
|
|
|
+ JSONObject body = JSON.parseObject(result.getBody());
|
|
|
+ if (!Objects.equals(body.getBoolean("result"), Boolean.TRUE)) {
|
|
|
+ log.error("监听物流消息失败: com:{}, num:{}, message:{}", com, num, result.getBody());
|
|
|
+ throw new ServiceException("监听物流消息失败:" + result.getBody());
|
|
|
}
|
|
|
} else {
|
|
|
- throw new ServiceException("物流信息订阅失败,请稍后重试,或者联系管理员!");
|
|
|
+ throw new ServiceException("监听物流消息失败:" + result.getError());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -146,7 +130,6 @@ public class KD100Util {
|
|
|
* 解析数据:推送结果
|
|
|
*
|
|
|
* @param data 物流信息
|
|
|
- * @return
|
|
|
*/
|
|
|
public static JSONObject parsePushData(String data) {
|
|
|
if (StringUtils.isNotBlank(data) && StringUtils.indexOf(data, "param=") != -1) {
|
|
@@ -157,7 +140,7 @@ public class KD100Util {
|
|
|
throw new ServiceException("物流信息推送失败,参数解码异常!");
|
|
|
}
|
|
|
|
|
|
- JSONObject object = JSONObject.parseObject(data);
|
|
|
+ JSONObject object = JSON.parseObject(data);
|
|
|
if (Func.isNotEmpty(object) && Func.isNotEmpty(object.getJSONObject("lastResult"))) {
|
|
|
JSONObject body = object.getJSONObject("lastResult");
|
|
|
if (Func.isNotEmpty(body.get("message")) && StringUtils.equalsIgnoreCase(body.getString("message"), "ok")) {
|
|
@@ -173,25 +156,4 @@ public class KD100Util {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static Integer getLogisticsStatus(String logisticsCompanyCode, String code) {
|
|
|
- Integer itemState = -1;
|
|
|
-
|
|
|
- // 查询快递100的物流信息
|
|
|
- try {
|
|
|
- JSONObject result = KD100Util.queryTrack(logisticsCompanyCode, code);
|
|
|
- itemState = result.getInteger("state");
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- Integer state = itemState;
|
|
|
- new Thread(() -> {
|
|
|
- if (!Objects.equals(state, LogisticsConstant.KD100Status.STATUS_3)) {
|
|
|
- // 如果不是已签收状态,则开启订阅(物流状态跟踪并推送)
|
|
|
- KD100Util.subscribe(logisticsCompanyCode, code, new Date());
|
|
|
- }
|
|
|
- }).start();
|
|
|
- return state;
|
|
|
- }
|
|
|
-
|
|
|
}
|