From f4a0f2acc63d7785eab108419a4e16f5f688cb95 Mon Sep 17 00:00:00 2001 From: yujian <yujian@163.com> Date: 星期六, 18 一月 2020 12:06:27 +0800 Subject: [PATCH] 用户注册信息 --- fanli/src/main/java/com/yeshi/fanli/util/jd/JDUtil.java | 253 +++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 217 insertions(+), 36 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 87870ae..da4e698 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 @@ -6,11 +6,17 @@ 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; @@ -25,6 +31,7 @@ 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; @@ -34,6 +41,7 @@ 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; @@ -330,11 +338,13 @@ 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")); - goods.setCouponInfo(coupon); + couponList.add(coupon); + goods.setCouponInfoList(couponList); } } @@ -364,7 +374,7 @@ price = pinGouInfo.getPingouPrice(); } - JDCouponInfo couponInfo = goods.getCouponInfo(); + JDCouponInfo couponInfo = getShowCouponInfo(goods.getCouponInfoList(), price); if (couponInfo == null) { money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil .mul(MoneyBigDecimalUtil.mul(price, commissionInfo.getCommissionShare()), new BigDecimal("0.01")), @@ -390,7 +400,6 @@ return BigDecimalUtil.getWithNoZera(MoneyBigDecimalUtil.mul(money, new BigDecimal("0.9"))); } - /** * 鍟嗗搧浣i噾璁$畻 * @@ -404,8 +413,6 @@ if (commissionPlus == null) { return money; } - - BigDecimal price = null; JDPingouInfo pinGouInfo = goods.getPinGouInfo(); @@ -415,10 +422,10 @@ price = pinGouInfo.getPingouPrice(); } - JDCouponInfo couponInfo = goods.getCouponInfo(); + JDCouponInfo couponInfo = getShowCouponInfo(goods.getCouponInfoList(), price); if (couponInfo == null) { - money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil - .mul(MoneyBigDecimalUtil.mul(price, commissionPlus), new BigDecimal("0.01")), + money = MoneyBigDecimalUtil.mul( + MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, commissionPlus), new BigDecimal("0.01")), MoneyBigDecimalUtil.div(rate, new BigDecimal(100))); } else { BigDecimal quota = couponInfo.getQuota(); @@ -426,21 +433,20 @@ 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))); + 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.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"))); } - + /** * 璁$畻鍟嗗搧鍒稿悗浠凤紝娌℃湁鍒稿垯杩斿洖鍘熶环 * @@ -448,12 +454,6 @@ * @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) { @@ -461,6 +461,12 @@ } else { price = pinGouInfo.getPingouPrice(); } + JDCouponInfo couponInfo = getShowCouponInfo(jdGoods.getCouponInfoList(), price); + if (couponInfo == null) { + return jdGoods.getPrice(); + } + + BigDecimal discount = couponInfo.getDiscount(); // 璁$畻鍒稿悗浠� BigDecimal sub = price.subtract(couponInfo.getQuota()); if (sub.compareTo(BigDecimal.valueOf(0)) >= 0) { @@ -626,6 +632,121 @@ } /** + * 閫氳繃閾炬帴id + * + * @param url + * @return + */ + public static String getJDGoodsIdByWeiXin(String url) { + if (StringUtil.isNullOrEmpty(url)) + 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); + // 灏嗛摼鎺ヤ腑鏈猠ncode鐨勫弬鏁癳ncode + 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("浜笢鐭帴瑙f瀽澶辫触:" + url); + return null; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + LogHelper.errorDetailInfo(e, "浜笢鐭摼鑾峰彇鍟嗗搧ID澶辫触:", url); + } + return null; + } + + /** * 鏌ヨ澶╃尗鍟嗗搧鍥剧墖銆佹爣棰� * * @param auctionId @@ -679,12 +800,14 @@ } return jdGoods; } - - + /** * 鑾峰彇绫荤洰 - * @param level 绛夌骇 - * @param parentId 涓婄骇id + * + * @param level + * 绛夌骇 + * @param parentId + * 涓婄骇id * @return */ public static List<GoodsClassDTO> getCategoryList(int level, int parentId) { @@ -699,12 +822,12 @@ 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 ++ ) { + 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")); @@ -721,8 +844,11 @@ /** * 鑾峰彇绫荤洰 - * @param level 绛夌骇 - * @param parentId 涓婄骇id + * + * @param level + * 绛夌骇 + * @param parentId + * 涓婄骇id * @return */ public static List<GoodsClassDTO> getCategoryCommiPlus(int level, int parentId) { @@ -737,24 +863,24 @@ 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 ++ ) { + 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)) + if (!StringUtil.isNullOrEmpty(selfMobileComm)) goodsClass.setSelfComm(new BigDecimal(selfMobileComm)); - + String popMobileComm = jsonClass.optString("popMobileComm"); - if (!StringUtil.isNullOrEmpty(popMobileComm)) + if (!StringUtil.isNullOrEmpty(popMobileComm)) goodsClass.setPopComm(new BigDecimal(popMobileComm)); - + list.add(goodsClass); } return list; @@ -765,5 +891,60 @@ } 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; + } + } -- Gitblit v1.8.0