TmsApiDemo.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package demo;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import util.HttpClientHelper;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. /**
  8. * TMS接口调用实例,以 "获取登陆凭证接口"、 "创建订单接口"、 "批量查询订单接口" 为例
  9. * 其余接口调用方式相同;
  10. * 以下代码仅做基本的调用演示,逻辑并不严密,请客户方根据需求自行完善;
  11. * 参数只传了必填参数和部分非必填参数,完整的参数请查看文档
  12. */
  13. public class TmsApiDemo {
  14. //测试环境
  15. private static final String URL = "http://api.test.56ctms.com";
  16. private static final String USER_NAME = "19012345678";
  17. private static final String PASSWORD = "123456";
  18. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  19. public static void main(String[] args) {
  20. System.out.println("*********************获取登陆凭证接口*********************");
  21. JSONObject tokenParams = new JSONObject();
  22. tokenParams.put("userName", USER_NAME);
  23. tokenParams.put("password", PASSWORD);
  24. String result = HttpClientHelper.httpPost(URL + "/user/generate_access_token/v2", tokenParams);
  25. JSONObject tokenResponse = JSONObject.parseObject(result);
  26. JSONObject tokenInfo = tokenResponse.getJSONObject("data");
  27. //token,后续请求需要用到,获取一次即可,有效期1年
  28. String accessToken = tokenInfo.getString("accessToken");
  29. //刷新token,用于刷新token
  30. String refreshToken = tokenInfo.getString("refreshToken");
  31. System.out.println("ACCESS_TOKEN = " + accessToken);
  32. System.out.println("REFRESH_TOKEN = " + refreshToken);
  33. System.out.println();
  34. System.out.println("*********************创建订单接口*********************");
  35. JSONObject createOrderParams = new JSONObject();
  36. JSONArray orders = new JSONArray();
  37. createOrderParams.put("orderList", orders);
  38. JSONObject order = new JSONObject();
  39. //用于唯一性检测,此字段不在系统中展示:必填
  40. order.put("orderNumber", "KHY_TEST_123456");
  41. //发货人地址:必填
  42. order.put("consignerAddress", "浙江省杭州市西湖区计量大厦");
  43. //发货人姓名:必填
  44. order.put("consignerName", "快货运测试");
  45. //发货人电话:必填
  46. order.put("consignerPhone", "19012345678");
  47. //提货时间:时间戳,精确到秒:必填
  48. order.put("deliveryTime", System.currentTimeMillis() / 1000);
  49. //提送类型:选填, 1)自提,2)送货
  50. order.put("deliveryType", 1);
  51. //客户单号:选填
  52. order.put("customerOrderNumber", "KHY_TEST_123456");
  53. //预约送到时间:时间戳,精确到秒:选填
  54. order.put("appointArriveTime", System.currentTimeMillis() / 1000);
  55. //收货人地址:可不填
  56. order.put("consigneeAddress", "xxx");
  57. //收货人姓名:可不填
  58. order.put("consigneeName", "xxx");
  59. //收货人电话:可不填
  60. order.put("consigneePhone", "19012345679");
  61. orders.add(order);
  62. result = HttpClientHelper.httpPost(URL + "/order/create_order?access_token=" + accessToken, createOrderParams);
  63. JSONObject createOrderResponse = JSONObject.parseObject(result);
  64. //TMS系统生成的订单号
  65. JSONArray orderNumberList = createOrderResponse.getJSONArray("data");
  66. System.out.println("TMS生成的单号:" + orderNumberList);
  67. System.out.println();
  68. System.out.println("*********************批量查询订单接口*********************");
  69. JSONObject queryOrderParams = new JSONObject();
  70. //页码,1开始
  71. queryOrderParams.put("page", 1);
  72. //单页最大订单数,最大50
  73. queryOrderParams.put("size", 3);
  74. //订单状态,0已撤销,1已下单,2已接单,3已装货,4已签收,1000待处理,无法查看全部状态,不传则默认0已撤销
  75. queryOrderParams.put("state", 1000);
  76. //查询时间类型,created录入时间,deliveryed提货时间,appointArrived预约提货时间,signed签收时间
  77. queryOrderParams.put("timeType", "created");
  78. queryOrderParams.put("startDate", dateFormat.format(new Date(System.currentTimeMillis() - 24 * 3600 * 1000)));
  79. queryOrderParams.put("endDate", dateFormat.format(new Date()));
  80. result = HttpClientHelper.httpPost(URL + "/order/query_orders?access_token=" + accessToken, queryOrderParams);
  81. JSONObject queryOrderResponse = JSONObject.parseObject(result);
  82. JSONObject queryOrders = queryOrderResponse.getJSONObject("data");
  83. //总数据量
  84. Integer total = queryOrders.getInteger("total");
  85. //页数
  86. Integer page = queryOrders.getInteger("page");
  87. //分页大小
  88. Integer size = queryOrders.getInteger("size");
  89. //总页数
  90. Integer totalPagesCount = queryOrders.getInteger("totalPagesCount");
  91. //订单信息
  92. JSONArray orderList = queryOrders.getJSONArray("elements");
  93. System.out.println(orderList);
  94. }
  95. }