当前位置:   article > 正文

对接抖店API-03 获取订单列表_抖音达人订单api

抖音达人订单api

官方文档 : 抖店开放平台

DOUDIAN_URL :https://openapi-fxg.jinritemai.com

如果前面获取accessToken 和签名都完成了, 那获取订单会简单很多,毕竟已经熟悉了需要的数据结构。

订单列表中只需要注意一下参数排序问题,分页过程就行, 因为接口返回的数据只有100条 ,所以需要多次请求, 跟分页查询类似,只到你查到想要的所有数据 ,然后在存在本地。

  1. /*************************************** 订单详情查询 **********************************/
  2. public void searchList(Integer day, PlatformManagementStore store) {
  3. String method = "order.searchList";
  4. String methodStrUrl = "/order/searchList";
  5. String shopId = store.getShopId();
  6. //之前获取到的token
  7. //从redis获取
  8. if (redisUtils.hasKey("tikTokAccessToken" + shopId)) {
  9. accessToken = redisUtils.get("tikTokAccessToken" + shopId).toString();
  10. } else {
  11. JSONObject data = tokenCreate(shopId);
  12. if (null == data || "".equals(data)) {
  13. System.out.println("accessToken为空,请检查请求API是否正常获取到");
  14. return;
  15. }
  16. accessToken = data.getString("access_token");
  17. String expires_in = data.getString("expires_in");
  18. redisUtils.set("tikTokAccessToken" + shopId, accessToken, Long.valueOf(expires_in));
  19. }
  20. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  21. String time = simpleDateFormat.format(new Date());
  22. //参数加密
  23. JSONObject param_json = new JSONObject(true);
  24. //当前时间
  25. Date now = DateUtils.getNowDate();
  26. //day天之前
  27. Calendar calendar = DateUtils.toCalendar(now);
  28. calendar.add(Calendar.DATE, -day);
  29. long endTime = now.getTime();
  30. long beginTime = calendar.getTimeInMillis();
  31. //下单结束时间
  32. param_json.put("create_time_end", String.valueOf(endTime / 1000));
  33. //下单开始时间
  34. param_json.put("create_time_start", String.valueOf(beginTime / 1000));
  35. //分页
  36. param_json.put("page", page.toString());
  37. param_json.put("size", pageSize.toString());
  38. //获取签名
  39. String sign = fetchSign(method, param_json.toString(), time);
  40. //地址后的参数
  41. StringBuffer param = new StringBuffer();
  42. param.append("app_key").append("=").append(APP_KEY).append("&");
  43. param.append("method").append("=").append(method).append("&");
  44. param.append("access_token").append("=").append(accessToken).append("&");
  45. param.append("param_json").append("=").append(param_json).append("&");
  46. param.append("timestamp").append("=").append(time).append("&");
  47. param.append("v").append("=").append("2").append("&");
  48. param.append("sign").append("=").append(sign).append("&");
  49. // 发送请求
  50. String response = HttpUtils.sendPost(DOUDIAN_URL + methodStrUrl, param.toString());
  51. JSONObject jsonObject = JSONObject.parseObject(response);
  52. if (jsonObject.getIntValue("err_no") == 0) {
  53. JSONObject data = jsonObject.getJSONObject("data");
  54. String shopOrderList = data.getString("shop_order_list");
  55. List<TiktokOrder> tiktokOrders = JSON.parseArray(shopOrderList, TiktokOrder.class);
  56. //获取对象
  57. tiktokOrderList.addAll(tiktokOrders);
  58. try {
  59. if (tiktokOrders.size() == pageSize) {
  60. page++;
  61. //由于有分页迭代获取订单
  62. searchList(day, store);
  63. } else {
  64. //创建订单列表, 订单详细, 订单地址三个List
  65. List<TiktokOrder> orderList = new ArrayList<>();
  66. List<TiktokOrderDetail> orderDetailList = new ArrayList<>();
  67. List<TiktokOrderAddress> orderAddressList = new ArrayList<>();
  68. //迭代完毕后再保存
  69. System.out.println("解析订单成功");
  70. for (TiktokOrder order : tiktokOrderList) {
  71. //判断订单状态 2备货中
  72. if (null == order || order.getOrderStatus() != 2) {
  73. continue;
  74. }
  75. // 查询订单详情接口
  76. skuOrderDetailList = orderDetail(order.getOrderId(), shopId);
  77. if (null != skuOrderDetailList && skuOrderDetailList.size() > 0) {
  78. for (TiktokOrderDetail detail : skuOrderDetailList) {
  79. detail.setParentOrderId(order.getOrderId());
  80. orderDetailList.add(detail);
  81. }
  82. skuOrderDetailList = null;
  83. }
  84. //保存订单地址
  85. List<TiktokOrderAddress> addressList = order.getPostAddr();
  86. for (TiktokOrderAddress address : addressList) {
  87. address.setOrderId(order.getOrderId());
  88. orderAddressList.add(address);
  89. }
  90. /**
  91. * 保存关于店铺的信息
  92. */
  93. order.setStoreName(store.getStoreName());
  94. order.setStoreId(store.getStoreId());
  95. order.setStoreCode(store.getStoreCode());
  96. //保存订单
  97. orderList.add(order);
  98. }
  99. //执行保存到数据库操作
  100. if (null != orderList && orderList.size() > 0) {
  101. tiktokOrderService.batchInsertTiktokOrder(orderList);
  102. }
  103. if (null != orderDetailList && orderDetailList.size() > 0) {
  104. tiktokOrderDetailService.batchInsertTiktokOrderDetail(orderDetailList);
  105. }
  106. if (null != orderAddressList && orderAddressList.size() > 0) {
  107. tiktokOrderAddressService.batchInsertTiktokOrderAddress(orderAddressList);
  108. }
  109. }
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. } finally {
  113. //这个步骤好像是不用的 ,可以移除
  114. page = 0;
  115. tiktokOrderList = new ArrayList<>();
  116. }
  117. }
  118. /*************************************** 获取订单详情接口 **********************************/
  119. public List<TiktokOrderDetail> orderDetail(String shopOrderId, String shopId) {
  120. //如果之前存储过 ,则不继续存储订单详情
  121. String method = "order.orderDetail";
  122. String methodStrUrl = "/order/orderDetail";
  123. //之前获取到的token
  124. //从redis获取
  125. if (redisUtils.hasKey("tikTokAccessToken" + shopId)) {
  126. accessToken = redisUtils.get("tikTokAccessToken" + shopId).toString();
  127. } else {
  128. JSONObject data = tokenCreate(shopId);
  129. if (null == data || "".equals(data)) {
  130. System.out.println("accessToken为空,请检查请求API是否正常获取到");
  131. return null;
  132. }
  133. accessToken = data.getString("access_token");
  134. String expires_in = data.getString("expires_in");
  135. redisUtils.set("tikTokAccessToken" + shopId, accessToken, Long.valueOf(expires_in));
  136. }
  137. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  138. String time = simpleDateFormat.format(new Date());
  139. //参数加密
  140. JSONObject param_json = new JSONObject(true);
  141. //订单id - orderID
  142. param_json.put("shop_order_id", shopOrderId);
  143. //获取签名
  144. String sign = fetchSign(method, param_json.toString(), time);
  145. //地址后的参数
  146. StringBuffer param = new StringBuffer();
  147. param.append("app_key").append("=").append(APP_KEY).append("&");
  148. param.append("method").append("=").append(method).append("&");
  149. param.append("access_token").append("=").append(accessToken).append("&");
  150. param.append("param_json").append("=").append(param_json).append("&");
  151. param.append("timestamp").append("=").append(time).append("&");
  152. param.append("v").append("=").append("2").append("&");
  153. param.append("sign").append("=").append(sign);
  154. // 发送请求
  155. String response = HttpUtils.sendPost(DOUDIAN_URL + methodStrUrl, param.toString());
  156. JSONObject jsonObject = JSONObject.parseObject(response);
  157. if (jsonObject.getIntValue("err_no") == 0) {
  158. JSONObject data = jsonObject.getJSONObject("data").getJSONObject("shop_order_detail");
  159. if (null != data && data.size() > 0) {
  160. String skuOrderList = data.getString("sku_order_list");
  161. List<TiktokOrderDetail> tiktokOrders = JSON.parseArray(skuOrderList, TiktokOrderDetail.class);
  162. return tiktokOrders;
  163. }
  164. }
  165. return null;
  166. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/357872
推荐阅读
相关标签
  

闽ICP备14008679号