对接抖店API-03 获取订单列表

官方文档 : 抖店开放平台

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

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

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

/***************************************   订单详情查询     **********************************/
    public void searchList(Integer day, PlatformManagementStore store) {
        String method = "order.searchList";
        String methodStrUrl = "/order/searchList";
        String shopId = store.getShopId();
        //之前获取到的token
        //从redis获取
        if (redisUtils.hasKey("tikTokAccessToken" + shopId)) {
            accessToken = redisUtils.get("tikTokAccessToken" + shopId).toString();
        } else {
            JSONObject data = tokenCreate(shopId);
            if (null == data || "".equals(data)) {
                System.out.println("accessToken为空,请检查请求API是否正常获取到");
                return;
            }
            accessToken = data.getString("access_token");
            String expires_in = data.getString("expires_in");
            redisUtils.set("tikTokAccessToken" + shopId, accessToken, Long.valueOf(expires_in));
        }


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());

        //参数加密
        JSONObject param_json = new JSONObject(true);

        //当前时间
        Date now = DateUtils.getNowDate();
        //day天之前
        Calendar calendar = DateUtils.toCalendar(now);
        calendar.add(Calendar.DATE, -day);
        long endTime = now.getTime();
        long beginTime = calendar.getTimeInMillis();
        //下单结束时间
        param_json.put("create_time_end", String.valueOf(endTime / 1000));
        //下单开始时间
        param_json.put("create_time_start", String.valueOf(beginTime / 1000));
        //分页
        param_json.put("page", page.toString());
        param_json.put("size", pageSize.toString());

        //获取签名
        String sign = fetchSign(method, param_json.toString(), time);

        //地址后的参数
        StringBuffer param = new StringBuffer();
        param.append("app_key").append("=").append(APP_KEY).append("&");
        param.append("method").append("=").append(method).append("&");
        param.append("access_token").append("=").append(accessToken).append("&");
        param.append("param_json").append("=").append(param_json).append("&");
        param.append("timestamp").append("=").append(time).append("&");
        param.append("v").append("=").append("2").append("&");
        param.append("sign").append("=").append(sign).append("&");

        // 发送请求
        String response = HttpUtils.sendPost(DOUDIAN_URL + methodStrUrl, param.toString());

        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject.getIntValue("err_no") == 0) {
            JSONObject data = jsonObject.getJSONObject("data");
            String shopOrderList = data.getString("shop_order_list");
            List tiktokOrders = JSON.parseArray(shopOrderList, TiktokOrder.class);

            //获取对象
            tiktokOrderList.addAll(tiktokOrders);
            try {
                if (tiktokOrders.size() == pageSize) {
                    page++;
                    //由于有分页迭代获取订单
                    searchList(day, store);
                } else {
                    //创建订单列表, 订单详细, 订单地址三个List
                    List orderList = new ArrayList<>();
                    List orderDetailList = new ArrayList<>();
                    List orderAddressList = new ArrayList<>();
                    //迭代完毕后再保存
                    System.out.println("解析订单成功");
                    for (TiktokOrder order : tiktokOrderList) {
                        //判断订单状态 2备货中
                        if (null == order || order.getOrderStatus() != 2) {
                            continue;
                        }
                        // 查询订单详情接口
                        skuOrderDetailList = orderDetail(order.getOrderId(), shopId);
                        if (null != skuOrderDetailList && skuOrderDetailList.size() > 0) {
                            for (TiktokOrderDetail detail : skuOrderDetailList) {
                                detail.setParentOrderId(order.getOrderId());
                                orderDetailList.add(detail);
                            }
                            skuOrderDetailList = null;
                        }
                        //保存订单地址
                        List addressList = order.getPostAddr();
                        for (TiktokOrderAddress address : addressList) {
                            address.setOrderId(order.getOrderId());
                            orderAddressList.add(address);
                        }

                        /**
                         * 保存关于店铺的信息
                         */
                        order.setStoreName(store.getStoreName());
                        order.setStoreId(store.getStoreId());
                        order.setStoreCode(store.getStoreCode());
                        //保存订单
                        orderList.add(order);
                    }
                    
                    //执行保存到数据库操作
                    if (null != orderList && orderList.size() > 0) {
                        tiktokOrderService.batchInsertTiktokOrder(orderList);
                    }
                    if (null != orderDetailList && orderDetailList.size() > 0) {
                        tiktokOrderDetailService.batchInsertTiktokOrderDetail(orderDetailList);
                    }
                    if (null != orderAddressList && orderAddressList.size() > 0) {
                        tiktokOrderAddressService.batchInsertTiktokOrderAddress(orderAddressList);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //这个步骤好像是不用的 ,可以移除
                page = 0;
                tiktokOrderList = new ArrayList<>();
            }
        }






/***************************************   获取订单详情接口     **********************************/
    public List orderDetail(String shopOrderId, String shopId) {
        //如果之前存储过 ,则不继续存储订单详情
        String method = "order.orderDetail";
        String methodStrUrl = "/order/orderDetail";
        //之前获取到的token
        //从redis获取
        if (redisUtils.hasKey("tikTokAccessToken" + shopId)) {
            accessToken = redisUtils.get("tikTokAccessToken" + shopId).toString();
        } else {
            JSONObject data = tokenCreate(shopId);
            if (null == data || "".equals(data)) {
                System.out.println("accessToken为空,请检查请求API是否正常获取到");
                return null;
            }
            accessToken = data.getString("access_token");
            String expires_in = data.getString("expires_in");
            redisUtils.set("tikTokAccessToken" + shopId, accessToken, Long.valueOf(expires_in));
        }


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());

        //参数加密
        JSONObject param_json = new JSONObject(true);
        //订单id - orderID
        param_json.put("shop_order_id", shopOrderId);

        //获取签名
        String sign = fetchSign(method, param_json.toString(), time);

        //地址后的参数
        StringBuffer param = new StringBuffer();
        param.append("app_key").append("=").append(APP_KEY).append("&");
        param.append("method").append("=").append(method).append("&");
        param.append("access_token").append("=").append(accessToken).append("&");
        param.append("param_json").append("=").append(param_json).append("&");
        param.append("timestamp").append("=").append(time).append("&");
        param.append("v").append("=").append("2").append("&");
        param.append("sign").append("=").append(sign);

        // 发送请求
        String response = HttpUtils.sendPost(DOUDIAN_URL + methodStrUrl, param.toString());

        JSONObject jsonObject = JSONObject.parseObject(response);
        if (jsonObject.getIntValue("err_no") == 0) {
            JSONObject data = jsonObject.getJSONObject("data").getJSONObject("shop_order_detail");
            if (null != data && data.size() > 0) {
                String skuOrderList = data.getString("sku_order_list");
                List tiktokOrders = JSON.parseArray(skuOrderList, TiktokOrderDetail.class);
                return tiktokOrders;
            }
        }
        return null;
    }

你可能感兴趣的:(对接抖音开发,java)