Browse Source

快递100集成

qt5107 2 years ago
parent
commit
a1822a6853

+ 36 - 33
bladex/blade-gateway/src/main/java/org/springblade/gateway/provider/AuthProvider.java

@@ -28,40 +28,43 @@ import java.util.List;
  */
 public class AuthProvider {
 
-	public static final String AUTH_KEY = TokenConstant.HEADER;
-	private static final List<String> DEFAULT_SKIP_URL = new ArrayList<>();
+    public static final String AUTH_KEY = TokenConstant.HEADER;
+    private static final List<String> DEFAULT_SKIP_URL = new ArrayList<>();
 
-	static {
-		DEFAULT_SKIP_URL.add("/example");
-		DEFAULT_SKIP_URL.add("/oauth/token/**");
-		DEFAULT_SKIP_URL.add("/oauth/captcha/**");
-		DEFAULT_SKIP_URL.add("/oauth/clear-cache/**");
-		DEFAULT_SKIP_URL.add("/oauth/user-info");
-		DEFAULT_SKIP_URL.add("/oauth/render/**");
-		DEFAULT_SKIP_URL.add("/oauth/callback/**");
-		DEFAULT_SKIP_URL.add("/oauth/revoke/**");
-		DEFAULT_SKIP_URL.add("/oauth/refresh/**");
-		DEFAULT_SKIP_URL.add("/token/**");
-		DEFAULT_SKIP_URL.add("/actuator/**");
-		DEFAULT_SKIP_URL.add("/v2/api-docs/**");
-		DEFAULT_SKIP_URL.add("/auth/**");
-		DEFAULT_SKIP_URL.add("/log/**");
-		DEFAULT_SKIP_URL.add("/menu/routes");
-		DEFAULT_SKIP_URL.add("/menu/auth-routes");
-		DEFAULT_SKIP_URL.add("/menu/top-menu");
-		DEFAULT_SKIP_URL.add("/tenant/info");
-		DEFAULT_SKIP_URL.add("/process/resource-view");
-		DEFAULT_SKIP_URL.add("/process/diagram-view");
-		DEFAULT_SKIP_URL.add("/manager/check-upload");
-		DEFAULT_SKIP_URL.add("/error/**");
-		DEFAULT_SKIP_URL.add("/assets/**");
-	}
+    static {
+        DEFAULT_SKIP_URL.add("/example");
+        DEFAULT_SKIP_URL.add("/oauth/token/**");
+        DEFAULT_SKIP_URL.add("/oauth/captcha/**");
+        DEFAULT_SKIP_URL.add("/oauth/clear-cache/**");
+        DEFAULT_SKIP_URL.add("/oauth/user-info");
+        DEFAULT_SKIP_URL.add("/oauth/render/**");
+        DEFAULT_SKIP_URL.add("/oauth/callback/**");
+        DEFAULT_SKIP_URL.add("/oauth/revoke/**");
+        DEFAULT_SKIP_URL.add("/oauth/refresh/**");
+        DEFAULT_SKIP_URL.add("/token/**");
+        DEFAULT_SKIP_URL.add("/actuator/**");
+        DEFAULT_SKIP_URL.add("/v2/api-docs/**");
+        DEFAULT_SKIP_URL.add("/auth/**");
+        DEFAULT_SKIP_URL.add("/log/**");
+        DEFAULT_SKIP_URL.add("/menu/routes");
+        DEFAULT_SKIP_URL.add("/menu/auth-routes");
+        DEFAULT_SKIP_URL.add("/menu/top-menu");
+        DEFAULT_SKIP_URL.add("/tenant/info");
+        DEFAULT_SKIP_URL.add("/process/resource-view");
+        DEFAULT_SKIP_URL.add("/process/diagram-view");
+        DEFAULT_SKIP_URL.add("/manager/check-upload");
+        DEFAULT_SKIP_URL.add("/error/**");
+        DEFAULT_SKIP_URL.add("/assets/**");
 
-	/**
-	 * 默认无需鉴权的API
-	 */
-	public static List<String> getDefaultSkipUrl() {
-		return DEFAULT_SKIP_URL;
-	}
+        //第三方推送
+        DEFAULT_SKIP_URL.add("/**/third/party/push/**");
+    }
+
+    /**
+     * 默认无需鉴权的API
+     */
+    public static List<String> getDefaultSkipUrl() {
+        return DEFAULT_SKIP_URL;
+    }
 
 }

+ 13 - 0
hx-service/victoriatourist/src/main/java/com/fjhx/config/KD100Config.java

@@ -34,6 +34,11 @@ public class KD100Config {
      */
     private static String userid;
 
+    /**
+     * 推送回调地址
+     */
+    private static String callbackurl;
+
     public static String getKey() {
         return key;
     }
@@ -65,4 +70,12 @@ public class KD100Config {
     public void setUserid(String userid) {
         KD100Config.userid = userid;
     }
+
+    public static String getCallbackurl() {
+        return callbackurl;
+    }
+
+    public void setCallbackurl(String callbackurl) {
+        KD100Config.callbackurl = callbackurl;
+    }
 }

+ 48 - 0
hx-service/victoriatourist/src/main/java/com/fjhx/controller/thirdpartypush/ThirdPartyPushController.java

@@ -0,0 +1,48 @@
+package com.fjhx.controller.thirdpartypush;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fjhx.service.logistics.LogisticsInfoService;
+import com.fjhx.uitl.kd100.KD100Util;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * <p>
+ * 第三方推送 前端控制器
+ * </p>
+ *
+ * @author ${author}
+ * @since 2022-12-09
+ */
+@RestController
+@RequestMapping("/third/party/push")
+public class ThirdPartyPushController {
+
+    @Autowired
+    private LogisticsInfoService logisticsInfoService;
+
+    /**
+     * kd100回调
+     *
+     * @param data 物流信息
+     * @return
+     */
+    @PostMapping("/kd100")
+    public JSONObject kd100(@RequestBody String data) {
+        JSONObject returnResult = new JSONObject();
+        returnResult.put("result", true);
+        returnResult.put("returnCode", "200");
+        returnResult.put("message", "成功");
+
+        JSONObject result = KD100Util.parsePushData(data);
+
+        //更新物流信息
+        logisticsInfoService.updateStatusByCode(result.getString("nu"), result.getInteger("state"));
+
+        return returnResult;
+    }
+}
+

+ 8 - 0
hx-service/victoriatourist/src/main/java/com/fjhx/service/logistics/LogisticsInfoService.java

@@ -41,4 +41,12 @@ public interface LogisticsInfoService extends BaseService<LogisticsInfo> {
      * @return
      */
     Map<Long, List<LogisticsInfo>> getByPurchaseIdsToMap(List<Long> purchaseIds);
+
+    /**
+     * 根据物流单号跟新物流状态
+     *
+     * @param code  物流单号
+     * @param state 物流状态
+     */
+    void updateStatusByCode(String code, Integer state);
 }

+ 17 - 1
hx-service/victoriatourist/src/main/java/com/fjhx/service/logistics/impl/LogisticsInfoServiceImpl.java

@@ -56,7 +56,12 @@ public class LogisticsInfoServiceImpl extends ServiceImpl<LogisticsInfoMapper, L
 
         //查询快递100的物流信息
         JSONObject result = KD100Util.queryTrack(logisticsInfoVo.getLogisticsCompanyCode(), logisticsInfoVo.getCode());
-        logisticsInfoVo.setLogisticsStatus(result.getInteger("state"));
+        Integer state = result.getInteger("state");
+        if (state != LogisticsConstant.KD100Status.STATUS_3) {
+            //如果不是已签收状态,则开启订阅(物流状态跟踪并推送)
+            KD100Util.subscribe(logisticsInfoVo.getLogisticsCompanyCode(), logisticsInfoVo.getCode());
+        }
+        logisticsInfoVo.setLogisticsStatus(state);
 
         logisticsInfoVo.setStatus(LogisticsConstant.Status.STATUS_0);
         save(logisticsInfoVo);
@@ -111,4 +116,15 @@ public class LogisticsInfoServiceImpl extends ServiceImpl<LogisticsInfoMapper, L
         }
         return list.stream().collect(Collectors.groupingBy(LogisticsInfo::getPurchaseId));
     }
+
+    /**
+     * 根据物流单号跟新物流状态
+     *
+     * @param code  物流单号
+     * @param state 物流状态
+     */
+    @Override
+    public void updateStatusByCode(String code, Integer state) {
+        lambdaUpdate().set(LogisticsInfo::getStatus, state).eq(LogisticsInfo::getCode, code).update();
+    }
 }

+ 93 - 4
hx-service/victoriatourist/src/main/java/com/fjhx/uitl/kd100/KD100Util.java

@@ -4,10 +4,15 @@ import com.alibaba.fastjson.JSONObject;
 import com.fjhx.config.KD100Config;
 import com.google.gson.Gson;
 import com.kuaidi100.sdk.api.QueryTrack;
+import com.kuaidi100.sdk.api.Subscribe;
+import com.kuaidi100.sdk.contant.ApiInfoConstant;
 import com.kuaidi100.sdk.core.IBaseClient;
 import com.kuaidi100.sdk.pojo.HttpResult;
 import com.kuaidi100.sdk.request.QueryTrackParam;
 import com.kuaidi100.sdk.request.QueryTrackReq;
+import com.kuaidi100.sdk.request.SubscribeParam;
+import com.kuaidi100.sdk.request.SubscribeParameters;
+import com.kuaidi100.sdk.request.SubscribeReq;
 import com.kuaidi100.sdk.utils.SignUtils;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.StringUtils;
@@ -15,6 +20,8 @@ import org.springblade.core.log.exception.ServiceException;
 import org.springblade.core.tool.api.ResultCode;
 import org.springblade.core.tool.utils.Func;
 
+import java.net.URLDecoder;
+
 @Log4j2
 public class KD100Util {
 
@@ -44,25 +51,107 @@ public class KD100Util {
             log.error(e.toString());
             throw new ServiceException("请求第三方快递信息异常,请联系管理员!");
         }
-        return parseData(result);
+        return parseQueryResult(result);
+    }
+
+    /**
+     * 订阅接口
+     *
+     * @param com 快递公司编码
+     * @param num 快递单号
+     * @return
+     */
+    public static JSONObject subscribe(String com, String num) {
+        HttpResult result = new HttpResult();
+        try {
+            SubscribeParameters subscribeParameters = new SubscribeParameters();
+            subscribeParameters.setCallbackurl(KD100Config.getCallbackurl());
+
+            SubscribeParam subscribeParam = new SubscribeParam();
+            subscribeParam.setParameters(subscribeParameters);
+            subscribeParam.setCompany(com);
+            subscribeParam.setNumber(num);
+            subscribeParam.setKey(KD100Config.getKey());
+
+            SubscribeReq subscribeReq = new SubscribeReq();
+            subscribeReq.setSchema(ApiInfoConstant.SUBSCRIBE_SCHEMA);
+            subscribeReq.setParam(new Gson().toJson(subscribeParam));
+
+            IBaseClient subscribe = new Subscribe();
+            result = subscribe.execute(subscribeReq);
+        } catch (Exception e) {
+            log.error(e.toString());
+            throw new ServiceException("请求第三方快递订阅功能异常,请联系管理员!");
+        }
+        return parseSubscribeData(result);
     }
 
     /**
-     * 解析数据
+     * 解析数据:查询结果
      *
      * @param result 数据结果
      * @return
      */
-    private static JSONObject parseData(HttpResult result) {
+    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") + "】,请稍后重试!");
+                throw new ServiceException("操作失败,原因:物流信息【" + body.getString("message") + "】,请稍后重试,或者联系管理员!");
             }
         } else {
             throw new ServiceException("物流信息查询失败,请稍后重试,或者联系管理员!");
         }
     }
+
+    /**
+     * 解析数据:订阅结果
+     *
+     * @param result 数据结果
+     * @return
+     */
+    private static JSONObject parseSubscribeData(HttpResult result) {
+        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 {
+                throw new ServiceException("操作失败,原因:物流信息【" + body.getString("message") + "】,请稍后重试,或者联系管理员!");
+            }
+        } else {
+            throw new ServiceException("物流信息订阅失败,请稍后重试,或者联系管理员!");
+        }
+    }
+
+    /**
+     * 解析数据:推送结果
+     *
+     * @param data 物流信息
+     * @return
+     */
+    public static JSONObject parsePushData(String data) {
+        if (StringUtils.isNotBlank(data) && StringUtils.indexOf(data, "param=") != -1) {
+            data = StringUtils.substringAfter(data, "param=");
+            try {
+                data = URLDecoder.decode(data, "UTF-8");
+            } catch (Exception e) {
+                throw new ServiceException("物流信息推送失败,参数解码异常!");
+            }
+
+            JSONObject object = JSONObject.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")) {
+                    return body;
+                } else {
+                    throw new ServiceException("物流信息推送失败,原因:物流信息【" + body.getString("message") + "】!");
+                }
+            } else {
+                throw new ServiceException("物流信息推送失败,参数解析异常!");
+            }
+        } else {
+            throw new ServiceException("物流信息推送失败!");
+        }
+    }
 }