| | |
| | | package com.yeshi.fanli.util.jd;
|
| | |
|
| | | import java.io.BufferedReader;
|
| | | import java.io.IOException;
|
| | | import java.io.InputStream;
|
| | | import java.io.InputStreamReader;
|
| | | import java.io.UnsupportedEncodingException;
|
| | | import java.math.BigDecimal;
|
| | | import java.net.URLDecoder;
|
| | | import java.net.URLEncoder;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Collections;
|
| | | import java.util.Comparator;
|
| | | import java.util.HashMap;
|
| | | import java.util.Iterator;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.regex.Matcher;
|
| | | import java.util.regex.Pattern;
|
| | |
|
| | | import javax.script.Invocable;
|
| | | import javax.script.ScriptEngine;
|
| | | import javax.script.ScriptEngineManager;
|
| | |
|
| | | import org.apache.commons.httpclient.HttpClient;
|
| | | import org.apache.commons.httpclient.methods.PostMethod;
|
| | | import org.jsoup.Connection;
|
| | | 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 org.yeshi.utils.NumberUtil;
|
| | |
|
| | | import com.yeshi.fanli.dto.GoodsClassDTO;
|
| | | 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.log.LogHelper;
|
| | | import com.yeshi.fanli.tag.PageEntity;
|
| | | import com.yeshi.fanli.util.MoneyBigDecimalUtil;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | |
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class JDUtil {
|
| | |
|
| | | public static final String TM_PHONE_URL = "https://item.m.jd.com/product/%s.html";
|
| | |
|
| | | public void test() {
|
| | | Map<String, String> systemParams = new HashMap<>();
|
| | |
| | | System.out.println(result);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 搜索网页
|
| | | * |
| | | * @param searchFilter
|
| | | * @return
|
| | | */
|
| | | @SuppressWarnings("deprecation")
|
| | | public static JDSearchResult searchByKey(JDSearchFilter searchFilter) {
|
| | |
|
| | |
| | | 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("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("keywordType", "kt1");
|
| | | data.put("searchType", "st3");
|
| | | }
|
| | | |
| | |
|
| | | String sortName = searchFilter.getSortName();
|
| | | if (StringUtil.isNullOrEmpty(sortName)) {
|
| | | data.put("sort", null);
|
| | |
| | | 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");
|
| | |
| | | JSONArray array = json.optJSONObject("data").optJSONArray("unionGoods");
|
| | | if (array != null) {
|
| | | for (int i = 0; i < array.size(); i++) {
|
| | | goodsList.add(parseGoods(array.optJSONArray(i).optJSONObject(0).toString()));
|
| | | JDGoods parseGoods = parseGoods(array.optJSONArray(i).optJSONObject(0).toString());
|
| | | if (parseGoods != null) {
|
| | | goodsList.add(parseGoods);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | json.optJSONObject("data").optJSONArray("unionGoods");
|
| | | }
|
| | |
|
| | | System.out.println(result);
|
| | | } 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) {
|
| | |
| | | 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")));
|
| | |
|
| | | if (!StringUtil.isNullOrEmpty(json.optString("wlPrice")))
|
| | | goods.setPrice(new BigDecimal(json.optString("wlPrice")));
|
| | | else
|
| | | goods.setPrice(new BigDecimal(json.optString("finalPrice")));
|
| | | |
| | | 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.setMaterialUrl("https://item.jd.com/" + json.optLong("skuId") + ".html");
|
| | | goods.setPicUrl("http://img14.360buyimg.com/n1/" + json.optString("materialUrl"));
|
| | | JDCommissionInfo commission = new JDCommissionInfo();
|
| | | commission.setCommission(new BigDecimal(json.optString("wlCommission")));
|
| | | commission.setCommissionShare(new BigDecimal(json.optString("wlCommissionRatio")));
|
| | | goods.setCommissionInfo(commission);
|
| | |
|
| | | goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare")));
|
| | |
|
| | | if (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);
|
| | | } else {
|
| | | // 无券
|
| | | // 团购信息
|
| | | 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) {
|
| | | List<JDCouponInfo> couponList = new ArrayList<>();
|
| | | JDCouponInfo coupon = new JDCouponInfo();
|
| | | coupon.setDiscount(new BigDecimal(json.optString("couponDiscount")));
|
| | | coupon.setQuota(new BigDecimal(json.optString("couponQuota")));
|
| | | coupon.setLink("https:" + json.optString("couponLink"));
|
| | | couponList.add(coupon);
|
| | | goods.setCouponInfoList(couponList);
|
| | | }
|
| | | }
|
| | |
|
| | | 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;
|
| | | }
|
| | | |
| | | JDCouponInfo couponInfo = goods.getCouponInfo();
|
| | | if (couponInfo == null) {
|
| | | // 佣金
|
| | | BigDecimal commission = commissionInfo.getCommission();
|
| | | return MoneyBigDecimalUtil.mul(commission, MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
| | |
|
| | | BigDecimal price = null;
|
| | | JDPingouInfo pinGouInfo = goods.getPinGouInfo();
|
| | | if (pinGouInfo == null) {
|
| | | price = goods.getPrice();
|
| | | } else {
|
| | | BigDecimal price = goods.getPrice();
|
| | | price = pinGouInfo.getPingouPrice();
|
| | | }
|
| | |
|
| | | JDCouponInfo couponInfo = getShowCouponInfo(goods.getCouponInfoList(), price);
|
| | | 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)));
|
| | | }
|
| | | }
|
| | | // 返利比例为原来的90%
|
| | | return BigDecimalUtil.getWithNoZera(MoneyBigDecimalUtil.mul(money, new BigDecimal("0.9")));
|
| | | }
|
| | |
|
| | | /**
|
| | | * 商品佣金计算
|
| | | * |
| | | * @param goods
|
| | | * @param rate
|
| | | * @return
|
| | | */
|
| | | public static BigDecimal getGoodsFanLiMoneyPlus(JDGoods goods, BigDecimal rate) {
|
| | | BigDecimal money = new BigDecimal(0);
|
| | | BigDecimal commissionPlus = goods.getCommissionPlus();
|
| | | if (commissionPlus == null) {
|
| | | return money;
|
| | | }
|
| | |
|
| | | BigDecimal price = null;
|
| | | JDPingouInfo pinGouInfo = goods.getPinGouInfo();
|
| | | if (pinGouInfo == null) {
|
| | | price = goods.getPrice();
|
| | | } else {
|
| | | price = pinGouInfo.getPingouPrice();
|
| | | }
|
| | |
|
| | | JDCouponInfo couponInfo = getShowCouponInfo(goods.getCouponInfoList(), price);
|
| | | if (couponInfo == null) {
|
| | | money = MoneyBigDecimalUtil.mul(
|
| | | MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, commissionPlus), 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")),
|
| | | .mul(MoneyBigDecimalUtil.mul(finalPrice, commissionPlus), 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)));
|
| | | money = MoneyBigDecimalUtil.mul(
|
| | | MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, commissionPlus), new BigDecimal("0.01")),
|
| | | MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
|
| | | }
|
| | | |
| | | }
|
| | | return BigDecimalUtil.getWithNoZera(money);
|
| | | // 返利比例为原来的90%
|
| | | return BigDecimalUtil.getWithNoZera(MoneyBigDecimalUtil.mul(money, new BigDecimal("0.9")));
|
| | | }
|
| | | |
| | | |
| | |
|
| | | /**
|
| | | * 计算商品券后价,没有券则返回原价
|
| | | *
|
| | |
| | | * @return
|
| | | */
|
| | | public static BigDecimal getQuanPrice(JDGoods jdGoods) {
|
| | | JDCouponInfo couponInfo = jdGoods.getCouponInfo();
|
| | | BigDecimal price = null;
|
| | | JDPingouInfo pinGouInfo = jdGoods.getPinGouInfo();
|
| | | if (pinGouInfo == null) {
|
| | | price = jdGoods.getPrice();
|
| | | } else {
|
| | | price = pinGouInfo.getPingouPrice();
|
| | | }
|
| | | JDCouponInfo couponInfo = getShowCouponInfo(jdGoods.getCouponInfoList(), price);
|
| | | if (couponInfo == null) {
|
| | | return jdGoods.getPrice();
|
| | | } |
| | | |
| | | }
|
| | |
|
| | | BigDecimal discount = couponInfo.getDiscount();
|
| | | BigDecimal price = jdGoods.getPrice();
|
| | | // 计算券后价
|
| | | BigDecimal sub = price.subtract(couponInfo.getQuota());
|
| | | if (sub.compareTo(BigDecimal.valueOf(0)) >= 0) {
|
| | |
| | | return jdGoods.getPrice();
|
| | | }
|
| | | }
|
| | | |
| | | |
| | |
|
| | | /**
|
| | | * 销量处理
|
| | | * |
| | | * @param count
|
| | | * @return
|
| | | */
|
| | |
| | | }
|
| | | return salesCountMidea;
|
| | | }
|
| | | |
| | |
|
| | | /**
|
| | | * 搜索候选词
|
| | | * |
| | | * @param key
|
| | | * @return
|
| | | */
|
| | | public static List<String> suggestSearch(String key) {
|
| | | List<String> list = new ArrayList<>();
|
| | | if (StringUtil.isNullOrEmpty(key))
|
| | |
| | | return list;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据链接提取商品ID
|
| | | * |
| | | * @param url
|
| | | * @return
|
| | | */
|
| | | public static String parseJDSkuIdByUrl(String url) {
|
| | | try {
|
| | | if (url.startsWith("https://item.m.jd.com/product/") || url.startsWith("http://item.m.jd.com/product/")
|
| | | || url.startsWith("https://item.jd.com/") || url.startsWith("http://item.jd.com/")) {
|
| | | String preUrl = url.split("\\?")[0];
|
| | | String index = preUrl.split("/")[preUrl.split("/").length - 1];
|
| | | index = index.split("\\.")[0];
|
| | | return index.trim();
|
| | | }
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | public static JDGoods getGoodsFromWeb(Long skuId) {
|
| | | String url = String.format("https://item.m.jd.com/product/%s.html", skuId + "");
|
| | | try {
|
| | | Document doc = Jsoup.connect(url).get();
|
| | | Elements scripts = doc.getElementsByTag("script");
|
| | | for (int i = 0; i < scripts.size(); i++) {
|
| | | String content = scripts.get(i).html();
|
| | | if (content.contains("window._itemOnly =")) {
|
| | | try {
|
| | | String jsContent = "var windowData={};function getData(){return JSON.stringify(windowData._itemOnly);}"
|
| | | + "var _itemOnly={}; " + content.replace("window.", "windowData.");
|
| | | System.out.println(jsContent);
|
| | | ScriptEngineManager m = new ScriptEngineManager();
|
| | | ScriptEngine engine = m.getEngineByName("JavaScript");
|
| | | engine.eval(jsContent);
|
| | | if (engine instanceof Invocable) {
|
| | | Invocable in = (Invocable) engine;
|
| | | String data = in.invokeFunction("getData").toString();
|
| | | JSONObject root = JSONObject.fromObject(data);
|
| | | root = root.optJSONObject("item");
|
| | | JDGoods goods = new JDGoods();
|
| | | goods.setSkuId(root.optLong("skuId"));
|
| | | goods.setPicUrl("http://img14.360buyimg.com/n1/" + root.optJSONArray("image").optString(0));
|
| | | goods.setSkuName(root.optString("skuName"));
|
| | | return goods;
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | return null;
|
| | | }
|
| | |
|
| | | public static String getJDGoodsJS() {
|
| | | InputStream input = JDUtil.class.getClassLoader().getResourceAsStream("jdGoods.js");
|
| | | StringBuilder sb = new StringBuilder();
|
| | | String line;
|
| | | BufferedReader br = new BufferedReader(new InputStreamReader(input));
|
| | | try {
|
| | | while ((line = br.readLine()) != null) {
|
| | | sb.append(line);
|
| | | }
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return sb.toString();
|
| | | }
|
| | |
|
| | | static String jdGoodsJs = null;
|
| | | static ScriptEngine engine = null;
|
| | | static {
|
| | | if (jdGoodsJs == null)
|
| | | jdGoodsJs = getJDGoodsJS();
|
| | | ScriptEngineManager manager = new ScriptEngineManager();
|
| | | engine = manager.getEngineByName("javascript");
|
| | | try {
|
| | | engine.eval(jdGoodsJs);
|
| | | } catch (Exception e) {
|
| | | }
|
| | | }
|
| | |
|
| | | public static String getJDGoodsId(String url) {
|
| | | try {
|
| | | if (engine instanceof Invocable) {
|
| | | Invocable in = (Invocable) engine;
|
| | | Object goodsId = in.invokeFunction("getGoodsId", url);
|
| | | if (goodsId != null)
|
| | | return goodsId.toString().trim();
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 通过链接id
|
| | | * |
| | | * @param url
|
| | | * @return
|
| | | */
|
| | | public static String getJDGoodsIdByWeiXin(String url) {
|
| | | if (StringUtil.isNullOrEmpty(url) || url.indexOf("jd.com") < 0)
|
| | | return null;
|
| | |
|
| | | try {
|
| | | String id = null;
|
| | | int indexOf = url.indexOf("?");
|
| | | url = url.substring(indexOf + 1, url.length() - 1);
|
| | | String[] temp = url.split("&");
|
| | | for (int i = 0; i < temp.length; i++) {
|
| | | String content = temp[i];
|
| | | if (content.startsWith("sku")) {
|
| | | id = content.split("=")[content.split("=").length - 1];
|
| | | break;
|
| | | }
|
| | | }
|
| | | return id;
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据 u.jd短连接获取真实的商品id
|
| | | * |
| | | * @param url
|
| | | * @return
|
| | | */
|
| | | public static String getJDGoodsIdByUJD(String url) {
|
| | | try {
|
| | | String body = HttpUtil.get(url);
|
| | | Document doc = Jsoup.parse(body);
|
| | | Elements els = doc.getElementsByTag("script");
|
| | | for (int i = 0; i < els.size(); i++) {
|
| | | String content = els.get(i).html();
|
| | | if (content.indexOf("var hrl") > -1) {
|
| | | ScriptEngineManager manager = new ScriptEngineManager();
|
| | | ScriptEngine engine = manager.getEngineByName("javascript");
|
| | |
|
| | | int indexOf = content.indexOf("var hrl");
|
| | | content = content.substring(indexOf, content.length());
|
| | |
|
| | | int indexOf2 = content.indexOf(";");
|
| | | content = content.substring(0, indexOf2 + 1);
|
| | |
|
| | | String js = "function getData(){return JSON.stringify(hrl);}" + content;
|
| | |
|
| | | engine.eval(js);
|
| | | if (engine instanceof Invocable) {
|
| | | Invocable in = (Invocable) engine;
|
| | | String data = in.invokeFunction("getData").toString();
|
| | | String link = data.substring(1, data.length() - 1);
|
| | | // 将链接中未encode的参数encode
|
| | | String param = link.split("\\?")[1];
|
| | | String host = link.split("\\?")[0];
|
| | | String[] params = param.split("&");
|
| | | String paramStr = "";
|
| | | for (String p : params) {
|
| | | String value = p.substring(p.indexOf("=") + 1);
|
| | | String key = p.substring(0, p.indexOf("="));
|
| | | if (URLDecoder.decode(value, "UTF-8").equalsIgnoreCase(value)) {
|
| | | value = URLEncoder.encode(value, "UTF-8");
|
| | | }
|
| | | paramStr += key + "=" + value + "&";
|
| | | }
|
| | | link = host + "?" + paramStr;
|
| | | String realLink = HttpUtil.getLocation(link);
|
| | | Map<String, String> paramsMap = new HashMap<>();
|
| | |
|
| | | param = realLink.split("\\?")[1];
|
| | | params = param.split("&");
|
| | | for (String p : params) {
|
| | | String value = p.substring(p.indexOf("=") + 1);
|
| | | String key = p.substring(0, p.indexOf("="));
|
| | | paramsMap.put(key, value);
|
| | | }
|
| | | if (!StringUtil.isNullOrEmpty(paramsMap.get("sku"))) {
|
| | | return paramsMap.get("sku");
|
| | | }
|
| | |
|
| | | if (!StringUtil.isNullOrEmpty(paramsMap.get("wareId"))) {
|
| | | return paramsMap.get("wareId");
|
| | | }
|
| | |
|
| | | // 判断纯数字大于4位的返回
|
| | | for (Iterator<String> its = paramsMap.keySet().iterator(); its.hasNext();) {
|
| | | String key = its.next();
|
| | | String value = paramsMap.get(key);
|
| | | if (NumberUtil.isNumeric(value) && value.length() >= 4) {
|
| | | return value;
|
| | | }
|
| | | }
|
| | | String id = JDUtil.getJDGoodsId(realLink);
|
| | | if (!StringUtil.isNullOrEmpty(id)) {
|
| | | return id;
|
| | | }
|
| | | LogHelper.error("京东短接解析失败:" + url);
|
| | | return null;
|
| | | }
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | LogHelper.errorDetailInfo(e, "京东短链获取商品ID失败:", url);
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 查询天猫商品图片、标题
|
| | | * |
| | | * @param auctionId
|
| | | * @return
|
| | | */
|
| | | public static JDGoods getSimpleGoodsInfo(String goodsId) {
|
| | | JDGoods jdGoods = null;
|
| | | try {
|
| | | Connection connect = Jsoup.connect(String.format(TM_PHONE_URL, goodsId));
|
| | | Document document = connect.get();
|
| | | Elements elements = document.getElementsByTag("script");
|
| | | if (elements.size() >= 0) {
|
| | | for (int i = 0; i < elements.size(); i++) {
|
| | | String content = elements.get(i).toString();
|
| | | if (content.contains("window._itemOnly")) {
|
| | | System.out.println("存在");
|
| | | content = content.replace("<script>", "");
|
| | | content = content.replace("</script>", "");
|
| | | int indexOf = content.indexOf("window._isLogin");
|
| | | content = content.substring(0, indexOf);
|
| | |
|
| | | int indexItem = content.indexOf("\"item\":");
|
| | | content = content.substring(indexItem, content.length() - 1);
|
| | | content = content.replace("\"item\":", "var item =");
|
| | | content = content.replace("});", "").trim();
|
| | |
|
| | | content += ";function getData() {return item;}";
|
| | | ScriptEngineManager manager = new ScriptEngineManager();
|
| | | ScriptEngine engine = manager.getEngineByName("javascript");
|
| | | engine.eval(content);
|
| | |
|
| | | if (engine instanceof Invocable) {
|
| | | Invocable in = (Invocable) engine;
|
| | | JSONObject json = JSONObject.fromObject(in.invokeFunction("getData"));
|
| | |
|
| | | jdGoods = new JDGoods();
|
| | | jdGoods.setSkuName(json.getString("skuName"));
|
| | | JSONObject jsonImage = json.getJSONObject("image");
|
| | | if (jsonImage != null && jsonImage.size() > 0) {
|
| | | String picUrl = jsonImage.getString("0");
|
| | | jdGoods.setPicUrl("http://m.360buyimg.com/mobilecms/s750x750_" + picUrl);
|
| | | }
|
| | | }
|
| | |
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return jdGoods;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取类目
|
| | | * |
| | | * @param level
|
| | | * 等级
|
| | | * @param parentId
|
| | | * 上级id
|
| | | * @return
|
| | | */
|
| | | public static List<GoodsClassDTO> getCategoryList(int level, int parentId) {
|
| | | try {
|
| | | // 请求地址
|
| | | String requestUrl = "https://union.jd.com/api/help/getCategoryList?level=%s&parentId=%s";
|
| | | // 执行请求
|
| | | String result = HttpUtil.get(String.format(requestUrl, level, parentId));
|
| | |
|
| | | if (!StringUtil.isNullOrEmpty(result)) {
|
| | | JSONObject json = JSONObject.fromObject(result);
|
| | | JSONArray jsonArray = json.getJSONArray("data");
|
| | | if (jsonArray != null && jsonArray.size() > 0) {
|
| | | List<GoodsClassDTO> list = new ArrayList<GoodsClassDTO>();
|
| | | for (int i = 0; i < jsonArray.size(); i++) {
|
| | | Object object = jsonArray.get(i);
|
| | | if (object == null)
|
| | | continue;
|
| | | JSONObject jsonClass = JSONObject.fromObject(object);
|
| | |
|
| | | GoodsClassDTO goodsClass = new GoodsClassDTO();
|
| | | goodsClass.setId(jsonClass.optString("id"));
|
| | | goodsClass.setName(jsonClass.optString("categoryName"));
|
| | | list.add(goodsClass);
|
| | | }
|
| | | return list;
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取类目
|
| | | * |
| | | * @param level
|
| | | * 等级
|
| | | * @param parentId
|
| | | * 上级id
|
| | | * @return
|
| | | */
|
| | | public static List<GoodsClassDTO> getCategoryCommiPlus(int level, int parentId) {
|
| | | try {
|
| | | // 请求地址
|
| | | String requestUrl = "https://union.jd.com/api/help/getCategoryCommiPlus?level=%s&parentId=%s";
|
| | | // 执行请求
|
| | | String result = HttpUtil.get(String.format(requestUrl, level, parentId));
|
| | |
|
| | | if (!StringUtil.isNullOrEmpty(result)) {
|
| | | JSONObject json = JSONObject.fromObject(result);
|
| | | JSONArray jsonArray = json.getJSONArray("data");
|
| | | if (jsonArray != null && jsonArray.size() > 0) {
|
| | | List<GoodsClassDTO> list = new ArrayList<GoodsClassDTO>();
|
| | | for (int i = 0; i < jsonArray.size(); i++) {
|
| | | Object object = jsonArray.get(i);
|
| | | if (object == null)
|
| | | continue;
|
| | | JSONObject jsonClass = JSONObject.fromObject(object);
|
| | |
|
| | | GoodsClassDTO goodsClass = new GoodsClassDTO();
|
| | | goodsClass.setId(jsonClass.optString("id"));
|
| | | goodsClass.setName(jsonClass.optString("name"));
|
| | |
|
| | | String selfMobileComm = jsonClass.optString("selfMobileComm");
|
| | | if (!StringUtil.isNullOrEmpty(selfMobileComm))
|
| | | goodsClass.setSelfComm(new BigDecimal(selfMobileComm));
|
| | |
|
| | | String popMobileComm = jsonClass.optString("popMobileComm");
|
| | | if (!StringUtil.isNullOrEmpty(popMobileComm))
|
| | | goodsClass.setPopComm(new BigDecimal(popMobileComm));
|
| | |
|
| | | list.add(goodsClass);
|
| | | }
|
| | | return list;
|
| | | }
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 获取需要展示的券
|
| | | * |
| | | * @param couponInfoList
|
| | | * 券列表
|
| | | * @param price
|
| | | * 商品价格
|
| | | * @return
|
| | | */
|
| | | public static JDCouponInfo getShowCouponInfo(List<JDCouponInfo> couponInfoList, BigDecimal price) {
|
| | | if (couponInfoList != null && couponInfoList.size() > 0) {
|
| | | // 根据消费条件排序
|
| | | Comparator<JDCouponInfo> cm = new Comparator<JDCouponInfo>() {
|
| | | @Override
|
| | | public int compare(JDCouponInfo o1, JDCouponInfo o2) {
|
| | | return o1.getQuota().compareTo(o2.getQuota());
|
| | | }
|
| | | };
|
| | | Collections.sort(couponInfoList, cm);
|
| | |
|
| | | // 设置券信息为最接近的一个
|
| | | for (int i = couponInfoList.size() - 1; i >= 0; i--) {
|
| | | if (couponInfoList.get(i).getQuota().compareTo(price) <= 0) {
|
| | | return couponInfoList.get(i);
|
| | | }
|
| | | }
|
| | | return couponInfoList.get(0);
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | public static JDCouponInfo getShowCouponInfo(JDGoods goods) {
|
| | | if (goods == null)
|
| | | return null;
|
| | | BigDecimal price = null;
|
| | | JDPingouInfo pinGouInfo = goods.getPinGouInfo();
|
| | | if (pinGouInfo == null) {
|
| | | price = goods.getPrice();
|
| | | } else {
|
| | | price = pinGouInfo.getPingouPrice();
|
| | | }
|
| | | return getShowCouponInfo(goods.getCouponInfoList(), price);
|
| | | }
|
| | |
|
| | | public static List<String> getJDShortLinksFromText(String text) {
|
| | | String regex = "(https://u\\.jd\\.com/)[0-9A-Za-z]{1,20}";
|
| | | Pattern pattern = Pattern.compile(regex);
|
| | | Matcher m = pattern.matcher(text);
|
| | | List<String> urlList = new ArrayList<>();
|
| | | while (m.find()) {
|
| | | urlList.add(m.group());
|
| | | }
|
| | | return urlList;
|
| | | }
|
| | |
|
| | | }
|