目 录CONTENT

文章目录

韵达快递散单接口对接

芈亓
2022-03-14 / 0 评论 / 4 点赞 / 867 阅读 / 20,960 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

韵达快递开放平台:http://open.yundaex.com/index
申请好资质认证通过之后申请对应的接口即可
image-1647246483300
具体开发要求是否更新看API文档。下面是本人开发的接口,范围包括:

预估运费、散件下单、取消散件下单和运单推送四个部分。

此代码中main方法为韵达官方提供DEMO,在API鉴权说明中,需要的jar也在此处下载。如有不明之处可联系本人在线答疑

package com.cn.hnust.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.hnust.service.ExpressOrderService;
import com.cn.hnust.shunfen.ExpressOrder;
import com.cn.hnust.util.HttpUtil;
import com.cn.hnust.util.IdGen;
import com.cn.hnust.util.JsonMapper;
import com.sun.xml.internal.ws.message.StringHeader;
import com.yundasys.api.DefaultOpenapiClient;
import com.yundasys.api.OpenapiConstants;
import com.yundasys.api.OpenapiException;
import com.yundasys.api.request.OpenApiDefaultRequest;
import com.yundasys.api.response.OpenApiDefaultResponse;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/yunDa")
public class YunDaController {
	@Autowired
	private ExpressOrderService expressOrderService;

	// 正式版请求地址:https://openapi.yundaex.com/
	private static final String url = "https://devkyweixin.yundasys.com/"; // 测试地址
	
	private static final String backUrl = "https:///";
	
	private static final String appKey = "999999";  
	
	private static final String appSecret = "04d4ad40eeec11e9bad2d962f53dda9d";  
	
	private static final long time = System.currentTimeMillis();
	
	/**
	 * 预估运费
	 * @param request
	 * @param response
	 * @return
	 * @throws OpenapiException 
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/getFreightInfo", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public String getFreightInfo(HttpServletRequest request,HttpServletResponse response) throws OpenapiException, UnsupportedEncodingException{
		Map<String, Object> map = new HashMap<String,Object>();
		String appkey = appKey;
		String appsecret = appSecret;
		String reqTime = String.valueOf(time);
		String startCity = request.getParameter("startCity");
		String endCity = request.getParameter("endCity");
		String weight = request.getParameter("weight");
		String serverUrl = url+"openapi-api/v1/order/getFreightInfo";
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(startCity))) {
			startCity = new String(startCity.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(endCity))) {
			endCity = new String(endCity.getBytes("ISO-8859-1"), "UTF-8");
		}
		map.put("startCity", startCity);
		map.put("endCity", endCity);
		map.put("weight", weight);
		DefaultOpenapiClient defaultOpenapiClient = new DefaultOpenapiClient(serverUrl, appkey, appsecret);
		OpenApiDefaultRequest openApiDefaultRequest = new OpenApiDefaultRequest();
	    //请求参数body, 指定JSON格式
	    String sourceContent =JsonMapper.getInstance().toJson(map);
	    
	    openApiDefaultRequest.setBizContent(sourceContent);
	    //生成的SIGN签名串
		String sign = defaultOpenapiClient.getSigner().sign(sourceContent, OpenapiConstants.SIGN_TYPE_MD5, OpenapiConstants.CHARSET_UTF8);
		openApiDefaultRequest.setHeadersMap(appkey, sign, reqTime);

        OpenApiDefaultResponse openApiDefaultResponse = defaultOpenapiClient.excute(openApiDefaultRequest);
        JSONObject json = JSONObject.fromObject(openApiDefaultResponse);
        System.out.println(json);
        if(openApiDefaultResponse.isSuccess()){
        	map.put("json", json);
            System.out.println("调用成功");
        }else{
        	map.put("json", json);
            System.out.println("调用失败");
        }
        System.out.println(openApiDefaultResponse.toString());
		return JsonMapper.getInstance().toJson(map);
	}
	
	/**
	 * 散件下单接口
	 * @param request
	 * @param response
	 * @return
	 * @throws OpenapiException 
	 * @throws UnsupportedEncodingException 
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/pushOrder", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public String pushOrder(HttpServletRequest request,HttpServletResponse response) throws OpenapiException, UnsupportedEncodingException, ParseException{
		List<Map<String, Object>> list_return = new ArrayList<Map<String,Object>>();
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, Object> map = new HashMap<String,Object>();Map<String, Object> map2 = new HashMap<String,Object>();
		String appid = appKey;   // 合作商app-key
		String appsecret = appSecret;
		String reqTime = String.valueOf(time);
		String orderid = request.getParameter("orderid");   // 合作商订单号
		String backurl = backUrl;   // 运单回传地址url
		String name = request.getParameter("name");   // 寄件人姓名
		String company = request.getParameter("company");   // 寄件人公司
		String province = request.getParameter("province");   // 寄件人省份
		String city = request.getParameter("city");   // 寄件人市
		String county = request.getParameter("county");   // 寄件人区/县
		String address = request.getParameter("address");   // 详细地址
		String postcode = request.getParameter("postcode");   // 邮编
		String phone = request.getParameter("phone");   // 固定电话(手机号固话必填一项)
		String mobile = request.getParameter("mobile");   // 手机号(手机号固话必填一项)
		String name1 = request.getParameter("name1");   // 收件人姓名
		String company1 = request.getParameter("company1");   // 收件人公司
		String province1 = request.getParameter("province1");   // 收件人省份
		String city1 = request.getParameter("city1");   // 收件人市
		String county1 = request.getParameter("county1");   // 收件人区/县
		String address1 = request.getParameter("address1");   // 详细地址
		String postcode1 = request.getParameter("postcode1");   // 邮编
		String phone1 = request.getParameter("phone1");   // 固定电话(手机号固话必填一项)
		String mobile1 = request.getParameter("mobile1");   // 手机号(手机号固话必填一项)
		String sendstarttime = request.getParameter("sendstarttime");   // 取件开始时间(yyyy-MM-dd HH:mm:ss )
		String sendendtime = request.getParameter("sendendtime");   // 取件结束时间
		String weight = request.getParameter("weight");   // 物品重量
		String size = request.getParameter("size");   // 货物大小(米),用半角的逗号来分隔长宽高
		String value = request.getParameter("value");   // 货物金额
		String freight = request.getParameter("freight");   // 运费
		String premium = request.getParameter("premium");   // 保险费
		String other_charges = request.getParameter("other_charges");   // 其他费用
		String items = request.getParameter("items");   // 商品信息集合List 里面包括 1 name 商品名称 2 number 数量  3 remark 说明
		String backparam = request.getParameter("backparam");   // 原样返回字段
		String special = request.getParameter("special");   // 商品类型(见商品类型字段) 0 文件类 1 电子产品类(包括家用电器) 2 办公用品,服装鞋帽,箱包类 3	化妆品,美容产品类 4	珠宝,手表,眼镜,贵重饰品类 5	食品,保健药品类 6	工艺品类(包括瓷器,茶具,烹饪用品) 7	玩具乐器类 8	其他类
		String remark = request.getParameter("remark");   // 备注
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(name))) {
			name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(name1))) {
			name1 = new String(name1.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(company))) {
			company = new String(company.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(company1))) {
			company1 = new String(company1.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(province))) {
			province = new String(province.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(province1))) {
			province1 = new String(province1.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(city))) {
			city = new String(city.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(city1))) {
			city1 = new String(city1.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(county))) {
			county = new String(county.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(county1))) {
			county1 = new String(county1.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(address))) {
			address = new String(address.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(address1))) {
			address1 = new String(address1.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(remark))) {
			remark = new String(remark.getBytes("ISO-8859-1"), "UTF-8");
		}
		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(backparam))) {
			backparam = new String(backparam.getBytes("ISO-8859-1"), "UTF-8");
		}
//		if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(cityName))) {
//			cityName = new String(cityName.getBytes("ISO-8859-1"), "UTF-8");
//		}
		if(!isNull(appid)){
			return "合作商App-Key不能为空";
		}
		if(!isNull(orderid)){
			return "合作商订单号不能为空";
		}
		if(!isNull(backurl)){
			return "运单回传地址url不能为空";
		}
		if(!isNull(name)){
			return "寄件人姓名不能为空";
		}
		if(!isNull(province)){
			return "寄件人省份不能为空";
		}
		if(!isNull(city)){
			return "寄件人市不能为空";
		}
		if(!isNull(county)){
			return "寄件人区/县不能为空";
		}
		if(!isNull(address)){
			return "详细地址不能为空";
		}
		if(!isNull(name1)){
			return "收件人姓名不能为空";
		}
		if(!isNull(province1)){
			return "收件人省份不能为空";
		}
		if(!isNull(city1)){
			return "收件人市不能为空";
		}
		if(!isNull(county1)){
			return "收件人区/县不能为空";
		}
		if(!isNull(address1)){
			return "详细地址不能为空";
		}
		if(!isNull(sendstarttime)){
			return "取件开始时间不能为空";
		}
		map.put("appid",appid );
		map.put("orderid",orderid );
		map.put("backurl",backurl );
		map.put("sendstarttime", sendstarttime);
		map.put("sendendtime", sendendtime);
		map.put("weight", weight);
		map.put("size", size);
		if (isNull(value)) {
			double value1 = Double.parseDouble(value);
			map.put("value", value1);
		}
		if (isNull(other_charges)) {
			double other_charges1 = Double.parseDouble(other_charges);
			map.put("other_charges", other_charges1);
		}
		if (isNull(premium)) {
			double premium1 = Double.parseDouble(premium);
			map.put("premium", premium1);
		}
		if(isNull(freight)){
			double freight1 = Double.parseDouble(freight);
			map.put("freight",freight1 );
		}
		map.put("backparam", backparam);
		map.put("special",special );
		map.put("remark",remark );
		JSONObject jObject = null;
		if (!isNull(items)) {
			jObject = new JSONObject();
		}else{
			 jObject = JSONObject.fromObject(items);
		}
		list_return.add(jObject);
		map.put("items", list_return);
		Map<String, Object> sender = new HashMap<String, Object>();
		sender.put("name", name);
		sender.put("company", company);
		sender.put("province", province);
		sender.put("city", city);
		sender.put("province", province);
		sender.put("postcode", postcode);
		sender.put("county", county);
		sender.put("address", address);
		sender.put("phone", phone);
		sender.put("mobile", mobile);
		Map<String, Object> receiver = new HashMap<String, Object>();
		receiver.put("name", name1);
		receiver.put("company", company1);
		receiver.put("province", province1);
		receiver.put("city", city1);
		receiver.put("province", province1);
		receiver.put("postcode", postcode1);
		receiver.put("county", county1);
		receiver.put("address", address1);
		receiver.put("phone", phone1);
		receiver.put("mobile", mobile1);
		map.put("sender", sender);
		map.put("receiver", receiver);
		String serverUrl = url+"openapi-api/v1/order/pushOrder";
		DefaultOpenapiClient defaultOpenapiClient = new DefaultOpenapiClient(serverUrl, appid, appsecret);

        OpenApiDefaultRequest openApiDefaultRequest = new OpenApiDefaultRequest();
		String sourceContent =JsonMapper.getInstance().toJson(map);        
		openApiDefaultRequest.setBizContent(sourceContent);
        //生成的SIGN签名串
        String sign = defaultOpenapiClient.getSigner().sign(sourceContent, OpenapiConstants.SIGN_TYPE_MD5, OpenapiConstants.CHARSET_UTF8);
        openApiDefaultRequest.setHeadersMap(appid, sign, reqTime);

        OpenApiDefaultResponse openApiDefaultResponse = defaultOpenapiClient.excute(openApiDefaultRequest);
        JSONObject json = JSONObject.fromObject(openApiDefaultResponse);
        System.out.println(json);
        if(openApiDefaultResponse.isSuccess()){
        	map.put("json", json);
        	ExpressOrder expressOrder = new ExpressOrder();
        	expressOrder.setId(IdGen.uuid());
        	expressOrder.setOrderId(orderid);
        	expressOrder.setjProvince(province);
        	expressOrder.setjCity(city);
        	expressOrder.setjCounty(county);
        	expressOrder.setjCompany(company);
        	expressOrder.setjMobile(mobile);
        	expressOrder.setjTel(phone);
        	expressOrder.setjAddress(address);
        	expressOrder.setjContact(name);
        	expressOrder.setdProvince(province1);
        	expressOrder.setdCity(city1);
        	expressOrder.setdCounty(county1);
        	expressOrder.setdCompany(company1);
        	expressOrder.setdMobile(mobile1);
        	expressOrder.setdTel(phone1);
        	expressOrder.setdAddress(address1);
        	expressOrder.setdContact(name1);
        	expressOrder.setWeight(weight);
        	expressOrder.setRemark(remark);
        	expressOrder.setSendStartTime(sFormat.parse(sendstarttime));
        	if (isNull(sendendtime)) {
        		expressOrder.setSendEndTime(sFormat.parse(sendendtime));
			}
        	expressOrder.setSize(size);
        	if (isNull(other_charges)) {
    			double other_charges1 = Double.parseDouble(other_charges);
    			expressOrder.setOtherCharges(other_charges1);
    		}
    		if (isNull(premium)) {
    			double premium1 = Double.parseDouble(premium);
    			expressOrder.setPremium(premium1);
    		}
    		if(isNull(freight)){
    			double freight1 = Double.parseDouble(freight);
    			expressOrder.setFreight(freight1);
    		}
    		if (isNull(value)) {
    			double value1 = Double.parseDouble(value);
    			expressOrder.setValue(value1);
    		}
    		expressOrder.setSpecial(special);
        	expressOrder.setCreateDate(new Date());
        	expressOrder.setUpdateDate(new Date());
        	expressOrder.setDelFlag("0");
        	expressOrderService.insert(expressOrder);
            System.out.println("调用成功");
        }else{
        	map.put("json", json);
            System.out.println("调用失败");
        }
        System.out.println(openApiDefaultResponse.toString());
		return JsonMapper.getInstance().toJson(map);
	}
	
	
	/**
	 * 取消散件订单
	 * @param request
	 * @param response
	 * @return
	 * @throws OpenapiException 
	 */
	@RequestMapping(value = "/cancelOrder", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public String cancelOrder(HttpServletRequest request,HttpServletResponse response) throws OpenapiException{
		Map<String, Object> map = new HashMap<String,Object>();
		String appkey = appKey;
		String appsecret = appSecret;
		String reqTime = String.valueOf(time);
		String backparam = request.getParameter("backparam");  // 原样返回字段 可不传
		String orderid = request.getParameter("orderid");  // 订单号
		map.put("appid",appkey );
		map.put("backparam", backparam);
		map.put("orderid", orderid);
		String serverUrl = url+"openapi-api/v1/order/cancelOrder";
		DefaultOpenapiClient defaultOpenapiClient = new DefaultOpenapiClient(serverUrl, appkey, appsecret);
		OpenApiDefaultRequest openApiDefaultRequest = new OpenApiDefaultRequest();
	    //请求参数body, 指定JSON格式
	    String sourceContent =JsonMapper.getInstance().toJson(map);
	    openApiDefaultRequest.setBizContent(sourceContent);
	    //生成的SIGN签名串
		String sign = defaultOpenapiClient.getSigner().sign(sourceContent, OpenapiConstants.SIGN_TYPE_MD5, OpenapiConstants.CHARSET_UTF8);
		openApiDefaultRequest.setHeadersMap(appkey, sign, reqTime);

        OpenApiDefaultResponse openApiDefaultResponse = defaultOpenapiClient.excute(openApiDefaultRequest);
        JSONObject json = JSONObject.fromObject(openApiDefaultResponse);
        System.out.println(json);
        if(openApiDefaultResponse.isSuccess()){
        	map.put("json", json);
        	ExpressOrder expressOrder = expressOrderService.findByOrderId(orderid);
        	expressOrder.setDelFlag("1");
        	expressOrderService.update(expressOrder);
            System.out.println("调用成功");
        }else{
        	map.put("json", json);
            System.out.println("调用失败");
        }
        System.out.println(openApiDefaultResponse.toString());
		return JsonMapper.getInstance().toJson(map);
	}
	
	/**
	 * 运单推送接口
	 * @param request
	 * @param response
	 * @return
	 * @throws OpenapiException 
	 */
	@RequestMapping(value = "/backUrlMsg", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
	@ResponseBody
	public String backUrlMsg(HttpServletRequest request,HttpServletResponse response) throws OpenapiException{
		Map<String, Object> map = new HashMap<String,Object>();
		String mailno = request.getParameter("mailno");
		String orderid = request.getParameter("orderid");
		String time = request.getParameter("time");
		String status = request.getParameter("status");
		String serverUrl = backUrl;
		DefaultOpenapiClient defaultOpenapiClient = new DefaultOpenapiClient(serverUrl, appKey, appSecret);
		OpenApiDefaultRequest openApiDefaultRequest = new OpenApiDefaultRequest();
	    //请求参数body, 指定JSON格式
	    String sourceContent =JsonMapper.getInstance().toJson(map);
	    openApiDefaultRequest.setBizContent(sourceContent);
	    //生成的SIGN签名串
		String sign = defaultOpenapiClient.getSigner().sign(sourceContent, OpenapiConstants.SIGN_TYPE_MD5, OpenapiConstants.CHARSET_UTF8);
		openApiDefaultRequest.setHeadersMap(appKey, sign, time);

        OpenApiDefaultResponse openApiDefaultResponse = defaultOpenapiClient.excute(openApiDefaultRequest);
        JSONObject json = JSONObject.fromObject(openApiDefaultResponse);
        System.out.println(json);
        if(openApiDefaultResponse.isSuccess()){
        	map.put("json", json);
        	ExpressOrder expressOrder = expressOrderService.findByOrderId(orderid);
        	expressOrder.setMailno(mailno);
        	expressOrder.setUpdateDate(new Date());
        	expressOrderService.update(expressOrder);
            System.out.println("调用成功");
        }else{
        	map.put("json", json);
            System.out.println("调用失败");
        }
        System.out.println(openApiDefaultResponse.toString());
		return JsonMapper.getInstance().toJson(map);
	}
	
	
	
	
	
	 public static void main(String[] args) throws OpenapiException {
	        //接口地址 测试地址
	        final String serverUrl = "https://devkyweixin.yundasys.com/openapi-api/v1/order/pushOrder";
	        //创建应用分配的appkey
	        final String appKey = "999999";
	        //创建应用分配的appsecret
	        final String appSecret = "04d4ad40eeec11e9bad2d962f53dda9d";

	        DefaultOpenapiClient defaultOpenapiClient = new DefaultOpenapiClient(serverUrl, appKey, appSecret);

	        OpenApiDefaultRequest openApiDefaultRequest = new OpenApiDefaultRequest();
	        //请求参数body, 指定JSON格式
	        String sourceContent ="{ \"appid\": \"999999\", \"backparam\": \"测试\", \"backurl\": \"http://127.0.0.1:8080/mkkMoblie\", \"freight\": 10, \"items\": [ { \"name\": \"衣服\", \"number\": 1, \"remark\": \"袜子\" } ], \"orderid\": \"2333098766\", \"other_charges\": 0, \"premium\": 1, \"receiver\": { \"address\": \"青浦区盈港东路 6679 号\", \"city\": \"上海市\", \"company\": \"string\", \"county\": \"青浦区\", \"mobile\": \"0553-9876542\", \"name\": \"李四\", \"phone\": 17601205970, \"postcode\": \"string\", \"province\": \"上海市\" }, \"remark\": \"string\", \"sendendtime\": \"2019-09-03 11:00:00\", \"sender\": { \"address\": \"盈港东路 7766 号\", \"city\": \"上海市\", \"company\": \"string\", \"county\": \"青浦区\", \"mobile\": \"0553-9876542\", \"name\": \"张三\", \"phone\": 17601205970, \"postcode\": \"string\", \"province\": \"上海市\" }, \"sendstarttime\": \"2019-09-03 10:00:00\", \"size\": \"0.12,0.23,0.11\", \"special\": 0, \"value\": 126.5, \"weight\": 0 }";
	        openApiDefaultRequest.setBizContent(sourceContent);
	        //生成的SIGN签名串
	        String sign = defaultOpenapiClient.getSigner().sign(sourceContent, OpenapiConstants.SIGN_TYPE_MD5, OpenapiConstants.CHARSET_UTF8);
	        openApiDefaultRequest.setHeadersMap(appKey, sign, String.valueOf(System.currentTimeMillis()));
	        System.out.println(openApiDefaultRequest);
	        OpenApiDefaultResponse openApiDefaultResponse = defaultOpenapiClient.excute(openApiDefaultRequest);
	        JSONObject json = JSONObject.fromObject(openApiDefaultResponse);
	        System.out.println(json);
	        if(openApiDefaultResponse.isSuccess()){
	            System.out.println("调用成功");
	        }else{
	            System.out.println("调用失败");
	        }
	        
	        System.out.println(openApiDefaultResponse.toString());
	    }
	 
	 private boolean isNull(String str){
			if(str == null || "".equals(str) || str.length()==0){
				return false;
			}
			return true;
		}
}
4

评论区