package com.yeshi.fanli.util.pinduoduo;
|
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.yeshi.utils.HttpUtil;
|
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
|
public class PinDuoDuoApiUtil {
|
private final static String CLIENT_ID = "9f6ee5ebd3b94c2080c4d51c2427e9fa";
|
private final static String CLIENT_SECRET = "95e1f1904385664bf4b87d4b34de12f9f31c505d";
|
|
private static String getSign(Map<String, String> map) {
|
List<String> keyList = new ArrayList<>();
|
Iterator<String> keys = map.keySet().iterator();
|
while (keys.hasNext()) {
|
String key = keys.next();
|
keyList.add(key);
|
}
|
Collections.sort(keyList);
|
String org = "";
|
for (String key : keyList) {
|
org += key + map.get(key);
|
}
|
return StringUtil.Md5(CLIENT_SECRET + org + CLIENT_SECRET).toUpperCase();
|
}
|
|
private static String baseRequest(Map<String, String> map) {
|
map.put("client_id", CLIENT_ID);
|
map.put("timestamp", System.currentTimeMillis() / 1000 + "");
|
map.put("sign", getSign(map));
|
Iterator<String> keys = map.keySet().iterator();
|
String url = "https://gw-api.pinduoduo.com/api/router?";
|
while (keys.hasNext()) {
|
String key = keys.next();
|
try {
|
url += key + "=" + URLEncoder.encode(map.get(key), "UTF-8") + "&";
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
if (url.endsWith("&"))
|
url = url.substring(0, url.length() - 1);
|
|
String result = HttpUtil.post(url);
|
return result;
|
}
|
|
public static void searchGoods(String key) {
|
Map<String, String> map = new HashMap<>();
|
map.put("type", "pdd.ddk.goods.search");
|
map.put("keyword", key);
|
String result = baseRequest(map);
|
System.out.println(result);
|
}
|
|
public static void convert(Long goodsId) {
|
Map<String, String> map = new HashMap<>();
|
map.put("type", "pdd.ddk.goods.promotion.url.generate");
|
map.put("p_id", "8590899_61877633");
|
JSONArray array = new JSONArray();
|
array.add(goodsId);
|
map.put("goods_id_list", array.toString());
|
map.put("custom_parameters", "437032");
|
|
String result = baseRequest(map);
|
System.out.println(result);
|
}
|
|
public static void createPid() {
|
Map<String, String> map = new HashMap<>();
|
map.put("type", "pdd.ddk.goods.pid.generate");
|
map.put("number", "1");
|
String result = baseRequest(map);
|
System.out.println(result);
|
}
|
|
public static void getOrders() {
|
Map<String, String> map = new HashMap<>();
|
map.put("type", "pdd.ddk.order.list.increment.get");
|
map.put("start_update_time", System.currentTimeMillis() / 1000 - 60 * 60 + "");
|
map.put("end_update_time", System.currentTimeMillis() / 1000 + "");
|
String result = baseRequest(map);
|
System.out.println(result);
|
}
|
|
/**
|
* 获取商品详情
|
*
|
* @param goodsId
|
*/
|
public static void getGoodsDetail(Long goodsId) {
|
JSONArray array = new JSONArray();
|
array.add(goodsId);
|
Map<String, String> map = new HashMap<>();
|
map.put("type", "pdd.ddk.goods.detail");
|
map.put("goods_id_list", array.toString());
|
String result = baseRequest(map);
|
System.out.println(result);
|
}
|
|
}
|