快递鸟 物流跟踪订阅 即时查询快递 预约取件(在线下单)

/**
 * 快递鸟
 * 物流跟踪:
 * 1. 除天天快递、申通快递外,其他主流的快递公司都支持;
 * 2. 顺丰速运:仅支持通过快递鸟预约取件接口/电子面单接口发货的顺丰单号查询。
 * 3. 承诺达:仅支持通过快递鸟预约取件接口/电子面单接口发货的承诺达单号查询。
 * @Author: liangkesai
 * @CreateDate: 2018/12/26 15:45
 */
public class ExpressUtils {


    public static String stateForStr(int state){
        StringBuffer sb = new StringBuffer();
        switch (state){
            case 0:
                sb.append("无轨迹");
                break;
            case 1:
                sb.append("已揽收");
                break;
            case 2:
                sb.append("在途中");
                break;
            case 3:
                sb.append("签收");
                break;
            case 4:
                sb.append("问题件");
                break;
        }
        return sb.toString();
    }


    /**
     * Json方式  物流跟踪信息订阅
     *
     * @throws Exception
     */
    public static JSONObject orderTracesSubByJson(String ShipperCode, String LogisticCode) {
        String str = "{'LogisticCode':'" + LogisticCode + "','ShipperCode':'" + ShipperCode + "'}";

        JSONObject json = null;
        try {
            Map params = new HashMap();
            params.put("RequestData", urlEncoder(str, "UTF-8"));
            params.put("EBusinessID", Express.EBUSINESSID);
            params.put("RequestType", "1008");
            String dataSign = encrypt(str, Express.APP_KEY, "UTF-8");
            params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
            params.put("DataType", "2");

            String result = UrlUtils.sendPost(Express.EXTERFACE_INVOKE, params);
            json = JSON.parseObject(result);
            if (json == null) {
                throw new RRException("请求返回为空");
            }
            if (!(boolean) json.get("Success")) {
                throw new RRException("物流信息订阅异常: " + json.get("Reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * Json方式 即时查询快递
     *
     * @throws Exception
     */
    public static JSONObject getOrderTracesByJson(String ShipperCode, String LogisticCode) {
        String requestData = "{'OrderCode':'','ShipperCode':'" + ShipperCode + "','LogisticCode':'" + LogisticCode + "'}";
        JSONObject json = null;
        try {
            Map params = new HashMap();
            params.put("RequestData", urlEncoder(requestData, "UTF-8"));
            params.put("EBusinessID", Express.EBUSINESSID);
            params.put("RequestType", "1002");
            String dataSign = encrypt(requestData, Express.APP_KEY, "UTF-8");
            params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
            params.put("DataType", "2");
            String result = UrlUtils.sendPost(Express.QUERY, params);
            json = JSON.parseObject(result);
            if (json == null) {
                throw new RRException("请求返回为空");
            }
            if (!(boolean) json.get("Success")) {
                throw new RRException("快递查询异常: " + json.get("Reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 预约取件(在线下单)
     * bean 转 json 会出现转义问题 依据官方demo 采用字符串拼接json
     *
     * @throws Exception
     */
    public static JSONObject orderOnlineByJson(RequestData requestData) {
        String str = "{'ShipperCode':'" + requestData.getShipperCode() + "','OrderCode':'" + requestData.getOrderCode() + "','PayType':'" + requestData.getPayType() + "','ExpType':'" + requestData.getExpType() + "'," +
                "'Receiver':{'Name':'" + requestData.getReceiver().getName() + "','Mobile':'" + requestData.getReceiver().getMobile() + "','ProvinceName':'" + requestData.getReceiver().getProvinceName() + "','CityName':'" + requestData.getReceiver().getCityName() + "','Address':'" + requestData.getReceiver().getAddress() + "'}," +
                "'Sender':{'Name':'" + requestData.getSender().getName() + "','Mobile':'" + requestData.getSender().getMobile() + "','ProvinceName':'" + requestData.getSender().getProvinceName() + "','CityName':'" + requestData.getSender().getCityName() + "','Address':'" + requestData.getSender().getAddress() + "'}," +
                "'StartDate:'" + requestData.getStartDate() + "',EndDate:'" + requestData.getEndDate() + "'," +
                "'Commodity':[{'GoodsName':'" + requestData.getCommodity().getGoodsName() + "'}]}";
        JSONObject json = null;
        try {
            Map params = new HashMap();
            params.put("RequestData", urlEncoder(str, "UTF-8"));
            params.put("EBusinessID", Express.EBUSINESSID);
            params.put("RequestType", "1001");
            String dataSign = encrypt(str, Express.APP_KEY, "UTF-8");
            params.put("DataSign", urlEncoder(dataSign, "UTF-8"));
            params.put("DataType", "2");
            SySUtils.check(3);
            String result = UrlUtils.sendPost(Express.OORDERSERVICE, params);
            json = JSON.parseObject(result);
            if (json == null) {
                throw new RRException("请求返回为空");
            }
            if (!(boolean) json.get("Success")) {
                throw new RRException("预约取件异常 : " + json.get("Reason"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * MD5加密
     *
     * @param str     内容
     * @param charset 编码方式
     * @throws Exception
     */
    private static String MD5(String str, String charset) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes(charset));
        byte[] result = md.digest();
        StringBuffer sb = new StringBuffer(32);
        for (int i = 0; i < result.length; i++) {
            int val = result[i] & 0xff;
            if (val <= 0xf) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(val));
        }
        return sb.toString().toLowerCase();
    }

    /**
     * base64编码
     *
     * @param str     内容
     * @param charset 编码方式
     * @throws UnsupportedEncodingException
     */
    public static String base64(String str, String charset) throws UnsupportedEncodingException {
        String encoded = Base64.encode(str.getBytes(charset));
        return encoded;
    }

    public static String urlEncoder(String str, String charset) throws UnsupportedEncodingException {
        String result = URLEncoder.encode(str, charset);
        return result;
    }

    /**
     * 判断是否从快递鸟进入的推送数据
     *
     * @param RequestData
     * @param DataSign
     * @return
     */
    public static boolean isFromKdniao(String RequestData, String DataSign) {
        try {
            return encrypt(RequestData, Express.APP_KEY, "utf-8").equals(DataSign);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }




    /**
     * 电商Sign签名生成
     *
     * @param content  内容
     * @param keyValue Appkey
     * @param charset  编码方式
     * @return DataSign签名
     * @throws UnsupportedEncodingException ,Exception
     */
    private static String encrypt(String content, String keyValue, String charset) throws UnsupportedEncodingException, Exception {
        if (keyValue != null) {
            return base64(MD5(content + keyValue, charset), charset);
        }
        return base64(MD5(content, charset), charset);
    }


    public static void main(String[] args) throws Exception {
        System.out.println(getOrderTracesByJson("YZPY","9894396360151"));
        System.out.println(getOrderTracesByJson("YZPY","9894394393456"));
    }


}

 

你可能感兴趣的:(java)