| | |
| | | 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.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;
|
| | | import org.jsoup.Connection;
|
| | | import org.jsoup.Jsoup;
|
| | | import org.jsoup.nodes.Document;
|
| | | import org.jsoup.nodes.Element;
|
| | |
| | | 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;
|
| | |
| | | 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<>();
|
| | |
| | | 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")));
|
| | | }
|
| | |
|
| | | |
| | | /**
|
| | | * 商品佣金计算
|
| | | * |
| | | * @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")));
|
| | | }
|
| | | |
| | | /**
|
| | | * 计算商品券后价,没有券则返回原价
|
| | | *
|
| | |
| | |
|
| | | /**
|
| | | * 根据链接提取商品ID
|
| | | * |
| | | * @param url
|
| | | * @return
|
| | | */
|
| | |
| | | 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;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 查询天猫商品图片、标题
|
| | | * |
| | | * @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;
|
| | | }
|
| | | |
| | | }
|