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;
|
}
|
}
|