From d28bed1a1275131a5ca37f7da37961e2b518ac07 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期一, 26 八月 2019 13:44:00 +0800
Subject: [PATCH] 淘礼金创建异常处理

---
 fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java |  480 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 466 insertions(+), 14 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java
index a49fd1b..ea2b1cc 100644
--- a/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java
+++ b/fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java
@@ -1,8 +1,20 @@
 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.URLEncoder;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+
+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;
@@ -10,10 +22,21 @@
 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 {
@@ -85,24 +108,69 @@
 		System.out.println(result);
 	}
 
+	/**
+	 * 鎼滅储缃戦〉
+	 * 
+	 * @param searchFilter
+	 * @return
+	 */
 	@SuppressWarnings("deprecation")
-	public static void searchByKey(String key) {
+	public static JDSearchResult searchByKey(JDSearchFilter searchFilter) {
+
+		JDSearchResult searchResult = new JDSearchResult();
+
+		List<JDGoods> goodsList = new ArrayList<>();
 		JSONObject params = new JSONObject();
-		params.put("pageNo", 1);
-		params.put("pageSize", 20);
+		params.put("pageNo", searchFilter.getPageNo());
+		params.put("pageSize", searchFilter.getPageSize());
 		params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + ""));
+
 		JSONObject data = new JSONObject();
-		data.put("deliveryType", "0");
-		data.put("hasCoupon", "0");
-		data.put("isCare", "0");
-		data.put("isPinGou", "0");
-		data.put("isZY", "0");
-		data.put("key", key);
-		data.put("keywordType", "kt1");
-		data.put("lock", "");
-		data.put("orientationFlag", "");
-		data.put("searchType", "st1");
+		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");
@@ -112,10 +180,394 @@
 		try {
 			client.executeMethod(pm);
 			String result = pm.getResponseBodyAsString();
-			System.out.println(result);
+			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;
+	}
+
+	/**
+	 * 鍟嗗搧浣i噾璁$畻
+	 * 
+	 * @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;
+	}
+
+	/**
+	 * 鏍规嵁閾炬帴鎻愬彇鍟嗗搧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;
+				String goodsId = in.invokeFunction("getGoodsId", url).toString();
+				if (!StringUtil.isNullOrEmpty(goodsId))
+					return goodsId.trim();
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return null;
 	}
 
 }

--
Gitblit v1.8.0