All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hn.utils.kuaidi.KuaidiUtil Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.utils.kuaidi;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.utils.AssertUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 快递100
 * 官网
 * @author admin
 */
public class KuaidiUtil {

    private static final Log log = LogFactory.get();

    /**
     * 快递100实时查询接口
     */
    private static final String QUERY_URL="https://poll.kuaidi100.com/poll/query.do";

    /**
     * 快递100智能判断接口,根据订单号获取物流公司编码
     */
    private static final String AUTO_NUMBER_URL="http://www.kuaidi100.com/autonumber/auto";

    /**
     *  授权key
     */
    private String key;

    /**
     * 公司编号
     */
    private String customer;

    public static KuaidiUtil create(String key, String customer){
        return new KuaidiUtil(key,customer);
    }

    private KuaidiUtil(String key, String customer) {
        this.key = key;
        this.customer = customer;
    }

    /**
     * 快递100实时查询接口
     * @param com 快递公司编码 {@link KuaidiCompany}
     * @param num 快递单号
     * @return KuaidiResult {@link KuaidiResult}
     */
    public KuaidiResult query(String com,String num){
        AssertUtils.notNull(com,"快递公司必填");
        AssertUtils.notNull(num,"快递单号必填");
        KuaidiParam param = new KuaidiParam();
        param.setCom(com);
        param.setNum(num);
        return query(param);
    }

    /**
     * 快递100实时查询接口
     * @param com 快递公司编码 {@link KuaidiCompany}
     * @param num 快递单号
     * @return KuaidiResult {@link KuaidiResult}
     */
    public KuaidiResult query(KuaidiCompany com,String num){
        return query(com.getCode(),num);
    }

    /**
     * 快递100实时查询接口
     * 接口文档
     * 接口详细文档(需要登录)
     * @param param {@link KuaidiParam}
     * @return KuaidiResult {@link KuaidiResult}
     */
    public KuaidiResult query(KuaidiParam param) {
        Map reqMap = new HashMap<>(3);
        reqMap.put("customer",customer);
        String paramStr = JSONUtil.toJsonStr(param);
        reqMap.put("sign",sign(paramStr));
        reqMap.put("param",paramStr);
        log.info("[快递100]请求参数:{}",JSONUtil.toJsonStr(reqMap));
        String result = HttpUtil.post(QUERY_URL, reqMap);
        AssertUtils.notNull(result,"[快递100]请求失败");
        log.info("[快递100]返回结果:{}",result);
        JSONObject resultObject = JSONUtil.parseObj(result);
        if(resultObject.containsKey("result")){
            throw new IllegalArgumentException("[快递100]"+resultObject.getStr("message"));
        }

        return JSONUtil.toBean(resultObject, KuaidiResult.class);
    }

    /**
     * 快递100智能判断接口
     * 根据客户提交的快递单号,判断该单号可能所属的快递公司编码,返回的数据是多个可能的快递公司列表,相似度高的快递公司排名靠前
     * 现在取的是第一个
     * @param num 快递单号
     * @return 物流公司编码
     */
    public String getComByNum(String num){
        Map reqMap = new HashMap<>(2);
        reqMap.put("num",num);
        reqMap.put("key",key);
        String result = HttpUtil.get(AUTO_NUMBER_URL, reqMap);
        AssertUtils.notNull(result,"[快递100]请求失败");
        log.info("[快递100]智能判断接口 返回结果:{}",result);
        JSONObject resultObj = JSONUtil.parseObj(result);
        if(resultObj.containsKey("result")){
            throw new IllegalArgumentException("[快递100]"+resultObj.getStr("message"));
        }

        JSONArray jsonArray = JSONUtil.parseArray(result);
        if(jsonArray.isEmpty()){
            return null;
        }
        JSONObject data = (JSONObject)jsonArray.get(0);
        return data.getStr("comCode");
    }

    /**
     * 加签
     * @param param 参数
     * @return 加签后的字符串
     */
    private String sign(String param){
        AssertUtils.notNull(param,"[快递100]参数param为空");
        String signStr = SecureUtil.md5(param + this.key + this.customer);
        AssertUtils.notNull(signStr,"[快递100]数据加签失败:"+param);
        return signStr.toUpperCase();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy