package com.yeshi.fanli.util.jd;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.math.BigDecimal;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.jsoup.select.Elements;
|
import org.yeshi.utils.BigDecimalUtil;
|
import org.yeshi.utils.HttpUtil;
|
|
import com.yeshi.fanli.dto.jd.JDCommissionInfo;
|
import com.yeshi.fanli.dto.jd.JDCouponInfo;
|
import com.yeshi.fanli.dto.jd.JDPingouInfo;
|
import com.yeshi.fanli.dto.jd.JDSearchFilter;
|
import com.yeshi.fanli.dto.jd.JDSearchResult;
|
import com.yeshi.fanli.dto.jd.JDShopInfo;
|
import com.yeshi.fanli.entity.jd.JDGoods;
|
import com.yeshi.fanli.tag.PageEntity;
|
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class JDUtil {
|
|
public void test() {
|
Map<String, String> systemParams = new HashMap<>();
|
systemParams.put("method", "");
|
systemParams.put("access_token", "");
|
systemParams.put("app_key", "");
|
systemParams.put("timestamp", "");
|
systemParams.put("sign", "");
|
}
|
|
public void test2() {
|
try {
|
Document doc = Jsoup.connect("https://media.jd.com/gotoadv/goods?pageSize=50")
|
.header("Cookie",
|
"user-key=35f673a3-8deb-4db1-90bf-43d2b685b870; cn=0; ipLocation=%u91CD%u5E86; ipLoc-djd=4-51026-103-0; _jrda=1; 3AB9D23F7A4B3C9B=LYNZAJWVCG3JB2Y2DAFLL7AU7LJEKYHJJVLNSRH6XTXA6BG5LOLHDS7DGMOLFAGSC7IR522A23USQUITRAJPNEJG6Y; PCSYCityID=4; __jdu=525081455; pinId=K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; areaId=4; showNoticeWindow=false; assist=LGXT4ZUUMHZXTL3LU6SHBXHIRZLI6OXJQWZEEYHFZIWHLDNNBDTCNH3Q4XXGSXEMN7NE6HJKSX7HI43OROSTWQXGAC6CJKJAW6CNTU3DIFRBQ7IAX22A74W2JHBTDLWPYNWRIL24YXC2ZGEVTP53CLVCZ73NS5ZOZ74Z5FQSBVDOU6I6APXRILVG5MCYPSLUYUT2WXRPWFZZKHAXGAWASAHGPI; isvName=%E5%B8%83%E4%B8%B8; masterClose=yes; thor=A13BE3D5245C7CE481E9909D3D6A771DC2FCAFB44DF9FD097DDFAD8F40A3B03D20FCD92E6728B2BB5512DCEABCBA620E2CFA885DBA2B4ECDD4ACCA0B6B1E1385C2D293F5F314639621BFE86734829975B7446B875B5F50055F10D1DDB48C16C166F0F33ECDB6036EA53BFC8BCAF98978A7E4E6AB5444C31D973161E1E0EE9556; __jdv=108460702|baidu|-|organic|not set|1520243688557; __jda=108460702.525081455.1480070037.1520238446.1520243689.43; __jdc=108460702; __jdb=108460702.4.525081455|43.1520243689")
|
.header("Referer", "https://media.jd.com/index/overview").header("Upgrade-Insecure-Requests", "1")
|
.header("User-Agent",
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36")
|
.get();
|
Elements els = doc.getElementsByClass("product-list-b").get(0).getElementsByTag("ul").get(0)
|
.getElementsByTag("li");
|
for (int i = 0; i < els.size(); i++) {
|
System.out.println(els.get(0).attr("skuid"));
|
getGoodsDetail("https://item.jd.com/" + els.get(i).attr("skuid") + ".html");
|
try {
|
Thread.sleep(10);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
public void getGoodsDetail(String url) {
|
try {
|
Document doc = Jsoup.connect(url).get();
|
|
String skuName = doc.getElementsByClass("itemInfo-wrap").get(0).getElementsByClass("sku-name").text();
|
Element els = doc.getElementById("choose-attrs");
|
Elements items = els.getElementsByClass("p-choose");
|
System.out.println(skuName);
|
for (int i = 0; i < items.size(); i++) {
|
|
String title = items.get(i).getElementsByClass("dt").text();
|
String content = items.get(i).getElementsByClass("selected").get(0).attr("data-value");
|
System.out.println("--" + title + ":" + content);
|
}
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
public void getCoupon() {
|
Map<String, String> map = new HashMap<>();
|
map.put("wareId", "6008133");
|
Map<String, String> headers = new HashMap<>();
|
headers.put("origin", "https://item.m.jd.com");
|
// headers.put("", "");
|
// headers.put("", "");
|
String result = HttpUtil.post("https://item.m.jd.com/coupon/coupon.json", map, headers);
|
System.out.println(result);
|
}
|
|
|
/**
|
* 搜索网页
|
* @param searchFilter
|
* @return
|
*/
|
@SuppressWarnings("deprecation")
|
public static JDSearchResult searchByKey(JDSearchFilter searchFilter) {
|
|
JDSearchResult searchResult = new JDSearchResult();
|
|
List<JDGoods> goodsList = new ArrayList<>();
|
JSONObject params = new JSONObject();
|
params.put("pageNo", searchFilter.getPageNo());
|
params.put("pageSize", searchFilter.getPageSize());
|
params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + ""));
|
|
JSONObject data = new JSONObject();
|
data.put("lock", "0");
|
data.put("deliveryType", searchFilter.getDeliveryType());
|
data.put("hasCoupon", searchFilter.getHasCoupon());
|
data.put("isCare", searchFilter.getIsCare());
|
data.put("isPinGou", searchFilter.getIsPinGou());
|
data.put("isZY", searchFilter.getIsZY());
|
data.put("orientationFlag", searchFilter.getOrientationFlag());
|
|
// 分类
|
data.put("categoryId", searchFilter.getCategoryId());
|
data.put("cat2Id", searchFilter.getCat2Id());
|
data.put("cat3Id", searchFilter.getCat3Id());
|
|
data.put("fromCommissionRatio", searchFilter.getFromCommissionRatio());
|
data.put("toCommissionRatio", searchFilter.getToCommissionRatio());
|
data.put("fromPrice", searchFilter.getFromPrice());
|
data.put("toPrice", searchFilter.getToPrice());
|
|
String key = searchFilter.getKey();
|
if (StringUtil.isNullOrEmpty(key)) {
|
data.put("key", "");
|
data.put("keywordType", "kt0");
|
data.put("searchType", "st3");
|
} else {
|
data.put("key", key);
|
data.put("keywordType", "kt1");
|
data.put("searchType", "st3");
|
}
|
|
String sortName = searchFilter.getSortName();
|
if (StringUtil.isNullOrEmpty(sortName)) {
|
data.put("sort", null);
|
data.put("sortName", null);
|
} else {
|
data.put("sortName", sortName);
|
String sort = searchFilter.getSort();
|
if (StringUtil.isNullOrEmpty(sort)) {
|
sort = JDSearchFilter.SORT_DESC;
|
}
|
data.put("sort", sort);
|
}
|
params.put("data", data);
|
|
System.out.println(params.toString());
|
|
HttpClient client = new HttpClient();
|
PostMethod pm = new PostMethod("https://union.jd.com/api/goods/search");
|
pm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
|
pm.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
pm.setRequestHeader("Referer", "https://union.jd.com/proManager/index?pageNo=1&keywords=%E9%9E%8B%E5%AD%90");
|
pm.setRequestBody(params.toString());
|
try {
|
client.executeMethod(pm);
|
String result = pm.getResponseBodyAsString();
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 200) {
|
JSONObject pageJson = json.optJSONObject("data").optJSONObject("page");
|
PageEntity entity = new PageEntity(pageJson.optInt("pageNo"), pageJson.optInt("pageSize"),
|
pageJson.optInt("totalCount"));
|
searchResult.setPageEntity(entity);
|
|
if (json.optJSONObject("data") != null) {
|
JSONArray array = json.optJSONObject("data").optJSONArray("unionGoods");
|
if (array != null) {
|
for (int i = 0; i < array.size(); i++) {
|
JDGoods parseGoods = parseGoods(array.optJSONArray(i).optJSONObject(0).toString());
|
if (parseGoods != null) {
|
goodsList.add(parseGoods);
|
}
|
}
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
searchResult.setGoodsList(goodsList);
|
return searchResult;
|
}
|
|
|
/**
|
* 搜索网页
|
* @param searchFilter
|
* @return
|
*/
|
@SuppressWarnings("deprecation")
|
public static List<JDGoods> getRecommendGoodsById(Long skuId) {
|
List<JDGoods> goodsList = new ArrayList<>();
|
JSONObject params = new JSONObject();
|
params.put("pageNo", 1);
|
params.put("pageSize", 60);
|
params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + ""));
|
|
JSONObject data = new JSONObject();
|
data.put("lock", "0");
|
data.put("deliveryType", 0);
|
data.put("hasCoupon",0);
|
data.put("isCare", 0);
|
data.put("isPinGou", 0);
|
data.put("isZY", 0);
|
data.put("orientationFlag", 0);
|
|
data.put("key", skuId);
|
data.put("keywordType", "kt0");
|
data.put("searchType", "st2");
|
params.put("data", data);
|
|
HttpClient client = new HttpClient();
|
PostMethod pm = new PostMethod("https://union.jd.com/api/goods/search");
|
pm.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
|
pm.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
|
pm.setRequestHeader("Referer", "https://union.jd.com/proManager/index?pageNo=1&keywords=%E9%9E%8B%E5%AD%90");
|
pm.setRequestBody(params.toString());
|
try {
|
client.executeMethod(pm);
|
String result = pm.getResponseBodyAsString();
|
JSONObject json = JSONObject.fromObject(result);
|
if (json.optInt("code") == 200) {
|
if (json.optJSONObject("data") != null) {
|
JSONArray array = json.optJSONObject("data").optJSONArray("unionRecommendGoods");
|
if (array != null) {
|
for (int i = 0; i < array.size(); i++) {
|
JDGoods parseGoods = parseGoods(array.optJSONArray(i).optJSONObject(0).toString());
|
if (parseGoods != null) {
|
goodsList.add(parseGoods);
|
}
|
}
|
}
|
}
|
}
|
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return goodsList;
|
}
|
|
public static JDGoods getGoodsDetail(Long skuId) {
|
JDSearchFilter sf = new JDSearchFilter();
|
sf.setKey(skuId + "");
|
sf.setPageNo(1);
|
sf.setPageSize(20);
|
JDSearchResult result = searchByKey(sf);
|
if (result != null && result.getGoodsList() != null && result.getGoodsList().size() > 0)
|
return result.getGoodsList().get(0);
|
return null;
|
}
|
|
|
private static JDGoods parseGoods(String data) {
|
JDGoods goods = new JDGoods();
|
JSONObject json = JSONObject.fromObject(data);
|
|
BigDecimal wlCommissionRatio = new BigDecimal(json.optString("wlCommissionRatio"));
|
if (wlCommissionRatio.compareTo(new BigDecimal(0)) > 0) {
|
JDCommissionInfo commission = new JDCommissionInfo();
|
commission.setCommission(new BigDecimal(json.optString("wlCommission")));
|
commission.setCommissionShare(wlCommissionRatio);
|
goods.setCommissionInfo(commission);
|
} else {
|
return null; // 过滤无返利商品
|
}
|
|
goods.setSkuId(json.optLong("skuId"));
|
goods.setSkuName(json.optString("skuName"));
|
|
JDShopInfo shopInfo = new JDShopInfo();
|
shopInfo.setShopId(json.optInt("shopId"));
|
shopInfo.setShopName(json.optString("shopName"));
|
goods.setShopInfo(shopInfo);
|
goods.setPrice(new BigDecimal(json.optString("wlPrice")));
|
|
goods.setInOrderCount30Days(json.optLong("inOrderCount30Days"));
|
|
if (json.optInt("isZY") == 1)
|
goods.setOwner("g");
|
else
|
goods.setOwner("p");
|
|
goods.setMaterialUrl("https://item.jd.com/"+ json.optLong("skuId") +".html");
|
goods.setPicUrl("http://img14.360buyimg.com/n1/" + json.optString("materialUrl"));
|
|
goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare")));
|
|
// 团购信息
|
if (json.optString("isPinGou") != null && json.optInt("isPinGou") == 1) {
|
JDPingouInfo jdPinGouInfo = new JDPingouInfo();
|
jdPinGouInfo.setPingouPrice(new BigDecimal(json.optString("pingouPrice")));
|
jdPinGouInfo.setPingouTmCount(json.optLong("pingouTmCount"));
|
goods.setPinGouInfo(jdPinGouInfo);
|
}
|
|
|
String finalPrice = json.optString("finalPrice");
|
if (!StringUtil.isNullOrEmpty(finalPrice)) {
|
// 券信息
|
BigDecimal finalPriceDecimal = new BigDecimal(json.optString("finalPrice"));
|
if (finalPriceDecimal != null && json.optString("hasCoupon") != null && json.optInt("hasCoupon") == 1
|
&& json.optInt("couponRemainCnt") > 0) {
|
JDCouponInfo coupon = new JDCouponInfo();
|
coupon.setDiscount(new BigDecimal(json.optString("couponDiscount")));
|
coupon.setQuota(new BigDecimal(json.optString("couponQuota")));
|
coupon.setLink("https:" + json.optString("couponLink"));
|
goods.setCouponInfo(coupon);
|
}
|
}
|
|
|
return goods;
|
}
|
|
|
/**
|
* 商品佣金计算
|
* @param goods
|
* @param rate
|
* @return
|
*/
|
public static BigDecimal getGoodsFanLiMoney(JDGoods goods, BigDecimal rate) {
|
BigDecimal money = null;
|
|
JDCommissionInfo commissionInfo = goods.getCommissionInfo();
|
if (commissionInfo == null) {
|
return money;
|
}
|
|
BigDecimal price = null;
|
JDPingouInfo pinGouInfo = goods.getPinGouInfo();
|
if (pinGouInfo == null) {
|
price = goods.getPrice();
|
} else {
|
price = pinGouInfo.getPingouPrice();
|
}
|
|
JDCouponInfo couponInfo = goods.getCouponInfo();
|
if (couponInfo == null) {
|
money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(
|
MoneyBigDecimalUtil.mul(price,commissionInfo.getCommissionShare()),
|
new BigDecimal("0.01")), MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
} else {
|
BigDecimal quota = couponInfo.getQuota();
|
BigDecimal discount = couponInfo.getDiscount();
|
if (quota.compareTo(price) <= 0 && price.compareTo(discount) > 0) {
|
|
BigDecimal finalPrice = price.subtract(discount);
|
money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil
|
.mul(MoneyBigDecimalUtil.mul(finalPrice, commissionInfo.getCommissionShare()), new BigDecimal("0.01")),
|
MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
|
} else {// 不能用券
|
money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(
|
MoneyBigDecimalUtil.mul(price,commissionInfo.getCommissionShare()),
|
new BigDecimal("0.01")), MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
}
|
|
}
|
return BigDecimalUtil.getWithNoZera(money);
|
}
|
|
|
/**
|
* 计算商品券后价,没有券则返回原价
|
*
|
* @param goodsBrief
|
* @return
|
*/
|
public static BigDecimal getQuanPrice(JDGoods jdGoods) {
|
JDCouponInfo couponInfo = jdGoods.getCouponInfo();
|
if (couponInfo == null) {
|
return jdGoods.getPrice();
|
}
|
|
BigDecimal discount = couponInfo.getDiscount();
|
BigDecimal price = null;
|
JDPingouInfo pinGouInfo = jdGoods.getPinGouInfo();
|
if (pinGouInfo == null) {
|
price = jdGoods.getPrice();
|
} else {
|
price = pinGouInfo.getPingouPrice();
|
}
|
// 计算券后价
|
BigDecimal sub = price.subtract(couponInfo.getQuota());
|
if (sub.compareTo(BigDecimal.valueOf(0)) >= 0) {
|
BigDecimal quanPrice = MoneyBigDecimalUtil.sub(price, discount);
|
return quanPrice;
|
} else {
|
return jdGoods.getPrice();
|
}
|
}
|
|
|
/**
|
* 销量处理
|
* @param count
|
* @return
|
*/
|
public static String getSaleCount(long count) {
|
String salesCountMidea = null;
|
if (count < 10000) {
|
salesCountMidea = count + "";
|
} else {
|
double sales = count;
|
salesCountMidea = String.format("%.1f", sales / 10000);
|
salesCountMidea = salesCountMidea + "万";
|
}
|
return salesCountMidea;
|
}
|
|
/**
|
* 搜索候选词
|
* @param key
|
* @return
|
*/
|
public static List<String> suggestSearch(String key) {
|
List<String> list = new ArrayList<>();
|
if (StringUtil.isNullOrEmpty(key))
|
return list;
|
String url = null;
|
try {
|
url = String.format(
|
"https://wq.jd.com/bases/searchdropdown/getdropdown?terminal=m&zip=1&key=%s&newjson=1&_=%s&sceneval=2",
|
URLEncoder.encode(key, "UTF-8"), System.currentTimeMillis() + "");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
|
if (url == null)
|
return list;
|
try {
|
String result = HttpUtil.get(url);
|
result = result.replace("callback(", "").replace(")", "");
|
JSONArray array = JSONArray.fromObject(result);
|
for (int i = 0; i < array.size(); i++) {
|
String sk = array.optJSONObject(i).optString("key");
|
if (!StringUtil.isNullOrEmpty(sk))
|
list.add(sk);
|
}
|
} catch (Exception e) {
|
}
|
return list;
|
}
|
|
}
|