From 394ee5b2dc3416634540b9f942c869574adb3d9c Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期三, 20 十一月 2019 14:47:32 +0800
Subject: [PATCH] 邀请订单到账
---
fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java | 254 +++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 236 insertions(+), 18 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 ff47881..87870ae 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,6 +1,9 @@
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;
@@ -15,6 +18,7 @@
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;
@@ -22,6 +26,7 @@
import org.yeshi.utils.BigDecimalUtil;
import org.yeshi.utils.HttpUtil;
+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;
@@ -37,6 +42,8 @@
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<>();
@@ -378,11 +385,62 @@
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")));
}
+
+ /**
+ * 鍟嗗搧浣i噾璁$畻
+ *
+ * @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 = goods.getCouponInfo();
+ 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, commissionPlus),
+ new BigDecimal("0.01")), MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
+
+ } else {// 涓嶈兘鐢ㄥ埜
+ money = MoneyBigDecimalUtil.mul(
+ MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price,commissionPlus),
+ new BigDecimal("0.01")),
+ MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
+ }
+ }
+ // 杩斿埄姣斾緥涓哄師鏉ョ殑90%
+ return BigDecimalUtil.getWithNoZera(MoneyBigDecimalUtil.mul(money, new BigDecimal("0.9")));
+ }
+
/**
* 璁$畻鍟嗗搧鍒稿悗浠凤紝娌℃湁鍒稿垯杩斿洖鍘熶环
*
@@ -524,28 +582,188 @@
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 (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();
- } else if (url.startsWith("https://item.m.jd.com/ware/view.action?")) {
- String preUrl = url.substring(url.indexOf("?") + 1, url.length());
- String[] sts = preUrl.split("&");
- for (String st : sts) {
- if (st.startsWith("wareId=")) {
- return st.split("=")[1].trim();
- }
- }
+ 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;
}
+ /**
+ * 鏌ヨ澶╃尗鍟嗗搧鍥剧墖銆佹爣棰�
+ *
+ * @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;
+ }
+
}
--
Gitblit v1.8.0