admin
2023-07-24 24587fd1b4af52469d1e122d2a495eea79d6865e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package com.yeshi.fanli.util.goods.douyin;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.entity.dy.DYOrder;
import com.yeshi.fanli.util.goods.douyin.vo.*;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.TimeUtil;
 
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
 
/**
 * @author hxh
 * @title: CSJCPSApiUtil
 * @description: 穿山甲商品联盟
 * 文档:https://lf3-plat.pglstatp-toutiao.com/obj/union-platform/ef1828077907ddfacd07b35665ddd6c3.pdf
 * @date 2022/8/8 15:45
 */
public class CSJCPSApiUtil {
    private static Logger debugLog = LoggerFactory.getLogger("debugLog");
 
    private static String post(String url, String entity) {
        HttpClient client = new HttpClient();
        //client.getHostConfiguration().setProxy("192.168.3.122", 8888);
        PostMethod method = new PostMethod(url);
        method.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        method.setRequestBody(entity);
        try {
            client.executeMethod(method);
            return method.getResponseBodyAsString();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
 
    private static String baseRequest(String path, JSONObject data, CSJAppInfo appInfo) {
        String url = "http://ecom.pangolin-sdk-toutiao.com" + path;
 
        debugLog.info("抖音请求开始:链接-{}", url);
 
        com.alibaba.fastjson.JSONObject params = new com.alibaba.fastjson.JSONObject();
        params.put("app_id", appInfo.getAppId());
        params.put("timestamp", System.currentTimeMillis() / 1000);
        params.put("req_id", UUID.randomUUID().toString());
        params.put("data", data.toString());
        List<String> list = new ArrayList<>();
        for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            list.add(key + "=" + params.get(key));
        }
        Collections.sort(list);
        String signStr = StringUtil.concat(list, "&") + appInfo.getSecureKey();
        String sign = StringUtil.Md5(signStr);
        params.put("sign", sign);
 
        return post(url, params.toString());
    }
 
    private static String baseRequest(String path, JSONObject data) {
        String appId = "5171164";
        String secureKey = "8a3061d15290ba953f4278a2251b03f4";
        return baseRequest(path, data, new CSJAppInfo(appId, secureKey));
    }
 
    /**
     * @return void
     * @author hxh
     * @description 获取直播列表
     * @date 15:09 2022/9/26
     * @param: page
     * @param: pageSize
     * @param: sortBy 排序字段: 1-综合;2-销量;3-佣⾦率;4-粉丝数。
     * @param: status
     **/
    public static ListResult listLive(DYLiveSearchFilter filter) {
        JSONObject data = new JSONObject();
        data.put("page", filter.getPage());
        data.put("page_size", filter.getPageSize());
        if (filter.getSortBy() != null) {
            data.put("sort_by", filter.getSortBy());
        }
        if (filter.getSortType() != null) {
            data.put("sort_type", filter.getSortType());
        }
        if (filter.getStatus() != null) {
            data.put("status", filter.getStatus());
        }
        String result = baseRequest("/live/search", data);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        int count = dataJson.optInt("total");
        Type type = new TypeToken<List<DYLiveInfo>>() {
        }.getType();
        return new ListResult(count, new Gson().fromJson(dataJson.optJSONArray("live_infos").toString(), type));
    }
 
    /**
     * @return void
     * @author hxh
     * @description 直播转链
     * @date 16:51 2022/9/26
     * @param: authorOpenId  直播间列表接⼝返回的author_openid
     * @param: extra_info ⾃定义字段,只允许 数字、字⺟和_,限制⻓度为20
     * @param: live_ext 直播间列表接⼝下发的live_info.ext
     **/
    public static DYConvertResult liveConvert(String authorOpenId, String extra_info, String live_ext) {
        JSONObject data = new JSONObject();
        data.put("author_openid", authorOpenId);
        data.put("external_info", extra_info);
        data.put("live_ext", live_ext);
        //转链类型:1、抖⾳ deep link;2、抖
        //⾳⼆维码;3、抖⾳⼝令。
        //默认返回抖⾳deeplink
        data.put("share_type", "[1,2,3]");
        String result = baseRequest("/live/link", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        DYConvertResult bean = new DYConvertResult();
        bean.setDeeplink(dataJson.optString("dy_deeplink"));
        bean.setPassword(dataJson.optString("dy_password"));
 
        if (dataJson.optJSONObject("dy_qr_code") != null) {
            bean.setQrCode(dataJson.optJSONObject("dy_qr_code").optString("url"));
        }
        return bean;
    }
 
    /**
     * @return void
     * @author hxh
     * @description 获取商品分类
     * @date 14:04 2022/9/27
     * @param: parentId  本接⼝是通过上级类⽬,查询下级类⽬。如
     * 果要查询⼀级类⽬,则该字段填写 0 即可。
     * 查询⼆级类⽬,输⼊相应的⼀级类⽬即可。
     * 若未传,则默认为0
     **/
    public static ListResult goodsCategory(Integer parentId) {
        JSONObject data = new JSONObject();
        if (parentId != null) {
            data.put("parent_id", parentId);
        }
        String result = baseRequest("/product/category", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        Type type = new TypeToken<List<DYGoodsCategory>>() {
        }.getType();
        return new ListResult(dataJson.optInt("total"), new Gson().fromJson(dataJson.optJSONArray("category_list").toString(), type));
    }
 
 
    /**
     * @return com.yeshi.fanli.util.goods.douyin.vo.ListResult
     * @author hxh
     * @description 商品搜索
     * @date 10:20 2022/9/28
     * @param: filter
     **/
    public static ListResult searchGoods(DYGoodsSearchFilter filter) {
        JSONObject data = new JSONObject();
        data.put("page", filter.getPage());
        data.put("page_size", filter.getPageSize());
        if (filter.getTitle() != null) {
            data.put("title", filter.getTitle());
        }
 
        if (filter.getFirst_cids() != null) {
            data.put("first_cids", JSONObject.fromObject(filter.getFirst_cids()));
        }
 
        if (filter.getSecond_cids() != null) {
            data.put("second_cids", JSONObject.fromObject(filter.getSecond_cids()));
        }
 
        if (filter.getThird_cids() != null) {
            data.put("third_cids", new Gson().toJson(filter.getThird_cids()));
        }
 
        if (filter.getPrice_min() != null) {
            data.put("price_min", filter.getPrice_min());
        }
 
        if (filter.getPrice_max() != null) {
            data.put("price_max", filter.getPrice_max());
        }
        if (filter.getSell_num_min() != null) {
            data.put("sell_num_min", filter.getSell_num_min());
        }
        if (filter.getSell_num_max() != null) {
            data.put("sell_num_max", filter.getSell_num_max());
        }
 
        if (filter.getSearch_type() != null) {
            data.put("search_type", filter.getSearch_type());
        }
 
        if (filter.getOrder_type() != null) {
            data.put("order_type", filter.getOrder_type());
        }
 
        if (filter.getCos_fee_min() != null) {
            data.put("cos_fee_min", filter.getCos_fee_min());
        }
 
        if (filter.getCos_fee_max() != null) {
            data.put("cos_fee_max", filter.getCos_fee_max());
        }
 
        if (filter.getCos_ratio_min() != null) {
            data.put("cos_ratio_min", filter.getCos_ratio_min());
        }
 
        if (filter.getCos_ratio_max() != null) {
            data.put("cos_ratio_max", filter.getCos_fee_max());
        }
 
 
        String result = baseRequest("/product/search", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        Type type = new TypeToken<List<DYGoods>>() {
        }.getType();
        return new ListResult(dataJson.optInt("total"), new Gson().fromJson(dataJson.optJSONArray("products").toString(), type));
    }
 
 
    /**
     * @return com.yeshi.fanli.util.goods.douyin.vo.DYConvertResult
     * @author hxh
     * @description
     * @date 10:22 2022/9/28
     * @param: product_url 商品url。与商品接⼝ detail_url⼀致
     * @param: extra_info 媒体传递扩展参数的字段, 字符只允许字⺟⼤⼩写、数字、 下划线,⻓度不超过20
     * @param: product_ext 商品搜索接⼝返回的 product.ext 字段, 尽量填写
     **/
    public static DYConvertResult goodsConvert(String product_url, String extra_info, String product_ext) throws Exception {
        JSONObject data = new JSONObject();
        data.put("product_url", product_url);
        data.put("external_info", extra_info);
        data.put("product_ext", product_ext);
        //转链类型:1、抖⾳ deep link;2、抖
        //⾳⼆维码;3、抖⾳⼝令。
        //默认返回抖⾳deeplink
        data.put("share_type", "[1,2,3]");
        String result = baseRequest("/product/link", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            throw new Exception(resultJson.optString("desc"));
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        DYConvertResult bean = new DYConvertResult();
        bean.setDeeplink(dataJson.optString("dy_deeplink"));
        bean.setPassword(dataJson.optString("dy_password"));
 
        if (dataJson.optJSONObject("dy_qr_code") != null) {
            bean.setQrCode(dataJson.optJSONObject("dy_qr_code").optString("url"));
        }
        return bean;
    }
 
    /**
     * @return java.util.List<com.yeshi.fanli.util.goods.douyin.vo.DYGoodsDetail>
     * @author hxh
     * @description 商品详情
     * @date 14:09 2022/9/28
     * @param: goodsIds
     **/
    public static List<DYGoodsDetail> goodsDetails(List<Long> goodsIds) {
        JSONObject data = new JSONObject();
        data.put("product_ids", new Gson().toJson(goodsIds));
        String result = baseRequest("/product/detail", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        Type type = new TypeToken<List<DYGoodsDetail>>() {
        }.getType();
        return new Gson().fromJson(dataJson.optJSONArray("products").toString(), type);
    }
 
 
    public static DYGoodsDetail goodsDetail(Long goodsId) {
        List<DYGoodsDetail> list = goodsDetails(Arrays.asList(new Long[]{goodsId}));
        if (list == null || list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }
 
 
    public static DYOrderResult orderList(DYOrderFilter filter) {
        JSONObject data = JSONObject.fromObject(new Gson().toJson(filter));
 
 
        String result = baseRequest("/order/search", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        return new Gson().fromJson(dataJson.toString(), DYOrderResult.class);
    }
 
    public static DYOrder getOrderDetail(String orderId, int orderType) {
        DYOrderFilter filter = new DYOrderFilter();
        filter.setOrder_type(orderType);
        filter.setOrder_ids(Arrays.asList(new String[]{orderId}));
        DYOrderResult result = orderList(filter);
        if (result == null) {
            return null;
        }
 
        if (result.getOrders() == null || result.getOrders().size() <= 0) {
            return null;
        }
        return result.getOrders().get(0);
    }
 
    /**
     * @return com.yeshi.fanli.util.goods.douyin.vo.DYAggregateH5Result
     * @author hxh
     * @description 聚合页H5
     * @date 15:48 2022/9/30
     * @param: filter
     **/
    public static DYAggregateH5Result aggregateH5(DYAggregateH5Filter filter) {
        JSONObject data = new JSONObject();
        if (filter.getDevice() != null) {
            data.put("device", new Gson().toJson(filter.getDevice()));
        }
        if (filter.getExternal_info() != null) {
            data.put("external_info", filter.getExternal_info());
        }
 
        if (filter.getMaterial_id() != null) {
            data.put("material_id", filter.getMaterial_id());
        }
 
        if (filter.getProduct_url() != null) {
            data.put("product_url", filter.getProduct_url());
        }
 
 
        String result = baseRequest("/aggregate/h5", data);
        System.out.println(result);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") != 0) {
            return null;
        }
        JSONObject dataJson = resultJson.optJSONObject("data");
        return new Gson().fromJson(dataJson.toString(), DYAggregateH5Result.class);
    }
 
 
    public static void main(String[] args) throws Exception {
        DYGoodsDetail goods = CSJCPSApiUtil.goodsDetail(Long.parseLong("3610681443753307139"));
        DYConvertResult link = CSJCPSApiUtil.goodsConvert(goods.getDetail_url(), DYUtil.createFanLiExtraInfo(437032L),
                "");
        System.out.println(link);
    }
 
 
}
 
class CSJAppInfo {
    private String appId;
    private String secureKey;
 
    public CSJAppInfo() {
    }
 
    public CSJAppInfo(String appId, String secureKey) {
        this.appId = appId;
        this.secureKey = secureKey;
    }
 
    public String getAppId() {
        return appId;
    }
 
    public void setAppId(String appId) {
        this.appId = appId;
    }
 
    public String getSecureKey() {
        return secureKey;
    }
 
    public void setSecureKey(String secureKey) {
        this.secureKey = secureKey;
    }
}