admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoUtil.java
@@ -1,173 +1,724 @@
package com.yeshi.fanli.util.pinduoduo;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.yeshi.utils.BigDecimalUtil;
import org.yeshi.utils.HttpUtil;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class PinDuoDuoUtil {
   /**
    * 商品佣金计算
    * @param goods
    * @param rate
    * @return
    */
   public static BigDecimal getGoodsFanLiMoney(PDDGoodsDetail goods, BigDecimal rate) {
      BigDecimal money = null;
      BigDecimal price = new BigDecimal(goods.getMinNormalPrice());
      BigDecimal promotionRate = new BigDecimal(goods.getPromotionRate());
      Boolean hasCoupon = goods.getHasCoupon();
      if (hasCoupon == null || !hasCoupon) {
         money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(
               MoneyBigDecimalUtil.mul(price, promotionRate),new BigDecimal("0.001")),
               MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
      } else {
         BigDecimal amount = new BigDecimal(goods.getCouponDiscount());
         BigDecimal startFree = new BigDecimal(goods.getCouponMinOrderAmount());
         if (startFree.compareTo(price) <= 0 && price.compareTo(amount) > 0) {
            BigDecimal finalPrice = price.subtract(amount);
            money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil
                  .mul(MoneyBigDecimalUtil.mul(finalPrice, promotionRate), new BigDecimal("0.001")),
                  MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
         } else {// 不能用券
            money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(
                  MoneyBigDecimalUtil.mul(price,promotionRate), new BigDecimal("0.001")),
                  MoneyBigDecimalUtil.div(rate, new BigDecimal(100)));
         }
      }
      return BigDecimalUtil.getWithNoZera(money);
   }
   /**
    * 计算商品券后价,没有券则返回原价
    *
    * @param goodsBrief
    * @return
    */
   public static BigDecimal getQuanPrice(PDDGoodsDetail goods) {
      BigDecimal price = new BigDecimal(goods.getMinNormalPrice());
      Boolean hasCoupon = goods.getHasCoupon();
      if (hasCoupon == null || !hasCoupon) {
         return price;
      }
      BigDecimal amount = new BigDecimal(goods.getCouponDiscount());
      BigDecimal startFree = new BigDecimal(goods.getCouponMinOrderAmount());
      if (startFree.compareTo(price) <= 0) {
         BigDecimal quanPrice = MoneyBigDecimalUtil.sub(price, amount);
         return quanPrice;
      } else {
         return price;
      }
   }
   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;
   }
   public static List<String> getDetailImages(Long id) {
      List<String> imgList = new ArrayList<>();
      try {
         Document doc = Jsoup.connect("http://yangkeduo.com/goods.html?goods_id=" + id)
               .userAgent(
                     "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
               .get();
         Elements els = doc.getElementsByTag("script");
         for (int i = 0; i < els.size(); i++) {
            if (els.get(i).html().contains("window.rawData")) {
               String dataJS = els.get(i).html().replace("window.", "var ");
               dataJS += "function getData(){return JSON.stringify(rawData);}";
               ScriptEngineManager manager = new ScriptEngineManager();
               ScriptEngine engine = manager.getEngineByName("javascript");
               try {
                  engine.eval(dataJS);
                  if (engine instanceof Invocable) {
                     Invocable in = (Invocable) engine;
                     String jsonStr = in.invokeFunction("getData").toString();
                     JSONObject json = JSONObject.fromObject(jsonStr);
                     JSONArray array = json.optJSONObject("store").optJSONObject("initDataObj")
                           .optJSONObject("goods").optJSONArray("detailGallery");
                     for (int j = 0; j < array.size(); j++) {
                        imgList.add("http:" + array.optJSONObject(j).optString("url"));
                     }
                  }
               } catch (Exception e) {
                  e.printStackTrace();
               }
               break;
            }
         }
      } catch (Exception e1) {
         e1.printStackTrace();
      }
      return imgList;
   }
   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("http://apiv3.yangkeduo.com/search_suggest?query=%s&pdduid=0",
               URLEncoder.encode(key, "UTF-8"));
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      }
      if (url == null)
         return list;
      try {
         String result = HttpUtil.get(url);
         JSONObject dataJSON = JSONObject.fromObject(result);
         JSONArray array = dataJSON.optJSONArray("suggest_list");
         for (int i = 0; i < array.size(); i++) {
            String sk = array.optJSONObject(i).optJSONObject("item_data").optString("suggestion");
            if (!StringUtil.isNullOrEmpty(sk))
               list.add(sk);
         }
      } catch (Exception e) {
      }
      return list;
   }
}
package com.yeshi.fanli.util.pinduoduo;
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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.yeshi.utils.BigDecimalUtil;
import org.yeshi.utils.HttpUtil;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.dto.pdd.PDDGoodsResult;
import com.yeshi.fanli.dto.pdd.PDDSearchFilter;
import com.yeshi.fanli.log.LogHelper;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.jd.JDUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.yeshi.utils.TimeUtil;
public class PinDuoDuoUtil {
    /**
     * 商品佣金计算
     *
     * @param goods
     * @param rate
     * @return
     */
    public static BigDecimal getGoodsFanLiMoney(PDDGoodsDetail goods, BigDecimal rate) {
        BigDecimal money = null;
        BigDecimal hundred = new BigDecimal(100);
        rate = MoneyBigDecimalUtil.div(rate, hundred);
        BigDecimal price = MoneyBigDecimalUtil.div(new BigDecimal(goods.getMinGroupPrice()), hundred).setScale(2);
        BigDecimal promotionRate = MoneyBigDecimalUtil.div3(new BigDecimal(goods.getPromotionRate()),
                new BigDecimal(1000));
        if (goods.getPredictPromotionRate() != null)
            promotionRate = MoneyBigDecimalUtil.div3(goods.getPredictPromotionRate(),
                    new BigDecimal(1000));
        Boolean hasCoupon = goods.getHasCoupon();
        if (hasCoupon == null || !hasCoupon) {
            money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, promotionRate), rate);
        } else {
            BigDecimal amount = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponDiscount()), hundred);
            BigDecimal startFree = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponMinOrderAmount()), hundred);
            if (startFree.compareTo(price) <= 0 && price.compareTo(amount) > 0) {
                BigDecimal finalPrice = price.subtract(amount);
                money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(finalPrice, promotionRate), rate);
            } else {// 不能用券
                money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, promotionRate), rate);
            }
        }
        return BigDecimalUtil.getWithNoZera(money).setScale(2);
    }
    public static BigDecimal getGoodsShareMoney(PDDGoodsDetail goods, BigDecimal rate) {
        BigDecimal money = null;
        BigDecimal hundred = new BigDecimal(100);
        rate = MoneyBigDecimalUtil.div(rate, hundred);
        BigDecimal price = MoneyBigDecimalUtil.div(new BigDecimal(goods.getMinGroupPrice()), hundred).setScale(2);
        BigDecimal promotionRate = MoneyBigDecimalUtil.div3(new BigDecimal(goods.getPromotionRate()),
                new BigDecimal(1000));
        Boolean hasCoupon = goods.getHasCoupon();
        if (hasCoupon == null || !hasCoupon) {
            money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, promotionRate), rate);
        } else {
            BigDecimal amount = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponDiscount()), hundred);
            BigDecimal startFree = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponMinOrderAmount()), hundred);
            if (startFree.compareTo(price) <= 0 && price.compareTo(amount) > 0) {
                BigDecimal finalPrice = price.subtract(amount);
                money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(finalPrice, promotionRate), rate);
            } else {// 不能用券
                money = MoneyBigDecimalUtil.mul(MoneyBigDecimalUtil.mul(price, promotionRate), rate);
            }
        }
        return BigDecimalUtil.getWithNoZera(money).setScale(2);
    }
    /**
     * 计算商品券后价,没有券则返回原价
     *
     * @param goodsBrief
     * @return
     */
    public static BigDecimal getCouponPrice(PDDGoodsDetail goods) {
        BigDecimal hundred = new BigDecimal(100);
        BigDecimal price = MoneyBigDecimalUtil.div(new BigDecimal(goods.getMinGroupPrice()), hundred);
        Boolean hasCoupon = goods.getHasCoupon();
        if (hasCoupon == null || !hasCoupon) {
            return price.setScale(2);
        }
        BigDecimal amount = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponDiscount()), hundred);
        BigDecimal startFree = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCouponMinOrderAmount()), hundred);
        if (startFree.compareTo(price) <= 0) {
            BigDecimal quanPrice = MoneyBigDecimalUtil.sub(price, amount);
            return quanPrice.setScale(2);
        } else {
            return price.setScale(2);
        }
    }
    public static List<String> getDetailImages(Long id) {
        List<String> imgList = new ArrayList<>();
        try {
            Document doc = Jsoup.connect("http://yangkeduo.com/goods.html?goods_id=" + id)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
                    .get();
            Elements els = doc.getElementsByTag("script");
            for (int i = 0; i < els.size(); i++) {
                if (els.get(i).html().contains("window.rawData")) {
                    String dataJS = els.get(i).html().replace("window.", "var ");
                    dataJS += "function getData(){return JSON.stringify(rawData);}";
                    ScriptEngineManager manager = new ScriptEngineManager();
                    ScriptEngine engine = manager.getEngineByName("javascript");
                    try {
                        engine.eval(dataJS);
                        if (engine instanceof Invocable) {
                            Invocable in = (Invocable) engine;
                            String jsonStr = in.invokeFunction("getData").toString();
                            JSONObject json = JSONObject.fromObject(jsonStr);
                            JSONArray array = json.optJSONObject("store").optJSONObject("initDataObj")
                                    .optJSONObject("goods").optJSONArray("detailGallery");
                            for (int j = 0; j < array.size(); j++) {
                                String u = array.optJSONObject(j).optString("url");
                                if (!u.startsWith("http"))
                                    imgList.add("http:" + u);
                                else
                                    imgList.add(u);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return imgList;
    }
    public static List<Long> getRecommendGoodsId(Long id) {
        List<Long> list = new ArrayList<Long>();
        JSONObject params = new JSONObject();
        params.put("pageNo", 1);
        params.put("show_tags", 1);
        params.put("goods_id", id);
        params.put("app_name", "goods_detail");
        params.put("list_id", "goods_detail_HgfiMc");
        params.put("pdduid", StringUtil.Md5(System.currentTimeMillis() + ""));
        HttpClient client = new HttpClient();
        PostMethod pm = new PostMethod("https://mobile.yangkeduo.com/proxy/api/api/tesla/query");
        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://jinbao.pinduoduo.com/promotion/brand-promotion");
        pm.setRequestBody(params.toString());
        try {
            client.executeMethod(pm);
            String result = pm.getResponseBodyAsString();
            LogHelper.test("拼多多猜你喜欢:" + result);
            JSONObject json = JSONObject.fromObject(result);
            JSONArray array = json.optJSONArray("data");
            if (array != null) {
                for (int i = 0; i < array.size(); i++) {
                    list.add(array.optJSONObject(i).optLong("goods_id"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 搜索候选词
     *
     * @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("http://apiv3.yangkeduo.com/search_suggest?query=%s&pdduid=0",
                    URLEncoder.encode(key, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (url == null)
            return list;
        try {
            String result = HttpUtil.get(url);
            JSONObject dataJSON = JSONObject.fromObject(result);
            JSONArray array = dataJSON.optJSONArray("suggest_list");
            for (int i = 0; i < array.size(); i++) {
                String sk = array.optJSONObject(i).optJSONObject("item_data").optString("suggestion");
                if (!StringUtil.isNullOrEmpty(sk))
                    list.add(sk);
            }
        } catch (Exception e) {
        }
        return list;
    }
    /**
     * 多多进宝爬取数据-品牌好货
     *
     * @param sf
     * @return
     */
    public static PDDGoodsResult getBrandGoods(PDDSearchFilter sf) {
        JSONObject params = new JSONObject();
        params.put("pageNumber", sf.getPage());
        params.put("pageSize", sf.getPageSize());
        if (sf.getSortType() != null) {
            params.put("sortType", sf.getSortType());
        }
        if (sf.getHasCoupon() != null) {
            params.put("withCoupon", sf.getHasCoupon() ? 1 : 0);
        }
        if (sf.getKw() != null) {
            params.put("keyword", sf.getKw());
        } else {
            params.put("keyword", "");
        }
        if (sf.getOptId() != null) {
            params.put("optId", sf.getOptId());
        }
        if (sf.getCatId() != null) {
            params.put("categoryId", sf.getCatId());
        }
        System.out.println(params.toString());
        HttpClient client = new HttpClient();
        PostMethod pm = new PostMethod("https://jinbao.pinduoduo.com/network/api/common/brand/goodsList");
        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://jinbao.pinduoduo.com/promotion/brand-promotion");
        pm.setRequestBody(params.toString());
        PDDGoodsResult goodsResult = null;
        try {
            client.executeMethod(pm);
            String result = pm.getResponseBodyAsString();
            System.out.println(result);
            JSONObject json = JSONObject.fromObject(result);
            Boolean code = json.optBoolean("success");
            if (code != null && code) {
                JSONObject root = json.optJSONObject("result");
                if (root == null) {
                    return null;
                }
                JSONArray array = root.optJSONArray("goodsList");
                if (array == null) {
                    return null;
                }
                List<PDDGoodsDetail> goodsList = new ArrayList<PDDGoodsDetail>();
                for (int i = 0; i < array.size(); i++) {
                    PDDGoodsDetail parseGoods = parseGoods(array.getJSONObject(i));
                    if (parseGoods != null) {
                        goodsList.add(parseGoods);
                    }
                }
                int totalCount = root.optInt("total");
                goodsResult = new PDDGoodsResult();
                goodsResult.setGoodsList(goodsList);
                goodsResult.setTotalCount(totalCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goodsResult;
    }
    private static PDDGoodsDetail parseGoods(JSONObject json) {
        PDDGoodsDetail goods = new PDDGoodsDetail();
        goods.setMallName(json.optString("mallName"));
        goods.setMerchantType(json.optInt("merchantType"));
        goods.setGoodsId(json.optLong("goodsId"));
        goods.setGoodsName(json.optString("goodsName"));
        goods.setGoodsDesc(json.optString("goodsDesc"));
        goods.setSalesTip(json.optString("salesTip"));
        if (json.optString("goodsImageUrl") != null) {
            goods.setGoodsImageUrl("http:" + json.optString("goodsImageUrl"));
        }
        if (json.optString("goodsThumbnailUrl") != null) {
            goods.setGoodsThumbnailUrl("http:" + json.optString("goodsThumbnailUrl"));
        }
        if (json.optString("minGroupPrice") != null) {
            goods.setMinGroupPrice(json.getLong("minGroupPrice") / 10);
        }
        if (json.optString("categoryId") != null) {
            goods.setCategoryId(json.getLong("categoryId"));
        }
        goods.setCategoryName(json.optString("categoryName"));
        goods.setHasCoupon(json.optBoolean("hasCoupon"));
        if (json.optString("couponMinOrderAmount") != null) {
            goods.setCouponMinOrderAmount(json.getLong("couponMinOrderAmount") / 10);
        }
        if (json.optString("couponDiscount") != null) {
            goods.setCouponDiscount(json.getLong("couponDiscount") / 10);
        }
        if (json.optString("couponTotalQuantity") != null) {
            goods.setCouponTotalQuantity(json.getLong("couponTotalQuantity"));
        }
        if (json.optString("couponRemainQuantity") != null) {
            goods.setCouponRemainQuantity(json.getLong("couponRemainQuantity"));
        }
        if (json.optString("couponStartTime") != null) {
            goods.setCouponStartTime(json.getLong("couponStartTime"));
        }
        if (json.optString("couponEndTime") != null) {
            goods.setCouponEndTime(json.getLong("couponEndTime"));
        }
        if (json.optString("promotionRate") != null) {
            goods.setPromotionRate(json.getLong("promotionRate"));
        }
        if (json.optString("optId") != null) {
            goods.setOptId(json.getLong("optId"));
        }
        return goods;
    }
    // 获取Android打开原生APP的uri
    public static String getAndroidNativeURI(String url) {
        if (url.contains("duo_coupon_landing.html?")) {
            int index = url.indexOf("duo_coupon_landing.html?");
            if (index >= 0) {
                return "pinduoduo://com.xunmeng.pinduoduo/" + url.substring(index, url.length());
            }
        }
        return null;
    }
    /**
     * 多多进宝爬取数据-品牌好货
     *
     * @param sf
     * @return
     */
    public static PDDGoodsResult getTodaySaleGoods() {
        JSONObject params = new JSONObject();
        params.put("type", 1);
        params.put("sortType", 3);
        HttpClient client = new HttpClient();
        PostMethod pm = new PostMethod("https://jinbao.pinduoduo.com/network/api/common/queryTopGoodsList");
        pm.setRequestHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3730.400 QQBrowser/10.5.3805.400");
        pm.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
        pm.setRequestHeader("Referer", "https://jinbao.pinduoduo.com/promotion/hot-promotion");
        pm.setRequestBody(params.toString());
        PDDGoodsResult goodsResult = null;
        try {
            client.executeMethod(pm);
            String result = pm.getResponseBodyAsString();
            JSONObject json = JSONObject.fromObject(result);
            Boolean code = json.optBoolean("success");
            if (code != null && code) {
                JSONObject root = json.optJSONObject("result");
                if (root == null) {
                    return null;
                }
                JSONArray array = root.optJSONArray("list");
                if (array == null) {
                    return null;
                }
                List<PDDGoodsDetail> goodsList = new ArrayList<PDDGoodsDetail>();
                for (int i = 0; i < array.size(); i++) {
                    PDDGoodsDetail parseGoods = parseTodaySaleGoods(array.getJSONObject(i));
                    if (parseGoods != null) {
                        goodsList.add(parseGoods);
                    }
                }
                int totalCount = root.optInt("total");
                goodsResult = new PDDGoodsResult();
                goodsResult.setGoodsList(goodsList);
                goodsResult.setTotalCount(totalCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goodsResult;
    }
    private static PDDGoodsDetail parseTodaySaleGoods(JSONObject json) {
        PDDGoodsDetail goods = new PDDGoodsDetail();
        goods.setMallName(json.optString("mallName"));
        goods.setMerchantType(json.optInt("merchantType"));
        goods.setGoodsId(json.optLong("goodsId"));
        goods.setGoodsName(json.optString("goodsName"));
        goods.setGoodsDesc(json.optString("goodsDesc"));
        goods.setSalesTip(json.optString("salesTip"));
        if (json.optString("mallId") != null) {
            goods.setMallId(json.getLong("mallId"));
        }
        if (json.optString("goodsImageUrl") != null) {
            goods.setGoodsImageUrl(json.optString("goodsImageUrl"));
        }
        if (json.optString("goodsThumbnailUrl") != null) {
            goods.setGoodsThumbnailUrl(json.optString("goodsThumbnailUrl"));
        }
        if (json.optString("minGroupPrice") != null) {
            goods.setMinGroupPrice(json.getLong("minGroupPrice"));
        }
        if (json.optString("minNormalPrice") != null) {
            goods.setMinNormalPrice(json.getLong("minNormalPrice"));
        }
        if (json.optString("categoryId") != null) {
            goods.setCategoryId(json.getLong("categoryId"));
        }
        goods.setCategoryName(json.optString("categoryName"));
        goods.setHasCoupon(json.optBoolean("hasCoupon"));
        if (json.optString("couponMinOrderAmount") != null) {
            goods.setCouponMinOrderAmount(json.getLong("couponMinOrderAmount"));
        }
        if (json.optString("couponDiscount") != null) {
            goods.setCouponDiscount(json.getLong("couponDiscount"));
        }
        if (json.optString("couponTotalQuantity") != null) {
            goods.setCouponTotalQuantity(json.getLong("couponTotalQuantity"));
        }
        if (json.optString("couponRemainQuantity") != null) {
            goods.setCouponRemainQuantity(json.getLong("couponRemainQuantity"));
        }
        if (json.optString("couponStartTime") != null) {
            goods.setCouponStartTime(json.getLong("couponStartTime"));
        }
        if (json.optString("couponEndTime") != null) {
            goods.setCouponEndTime(json.getLong("couponEndTime"));
        }
        if (json.optString("promotionRate") != null) {
            goods.setPromotionRate(json.getLong("promotionRate"));
        }
        if (json.optString("optId") != null) {
            goods.setOptId(json.getLong("optId"));
        }
        return goods;
    }
    public static String getJDGoodsJS() {
        InputStream input = JDUtil.class.getClassLoader().getResourceAsStream("pddGoods.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 (ScriptException e) {
            e.printStackTrace();
        }
    }
    /**
     * 是否为拼多多的链接
     *
     * @param link
     * @return
     */
    public static boolean isPDDLink(String link) {
        return link.contains("://p.pinduoduo.com/") || link.contains("yangkeduo.com/");
    }
    public static boolean isPDDShortLink(String link) {
        return link.contains("://p.pinduoduo.com/");
    }
    public static String getPDDGoodsId(String url) {
        if (url == null)
            return null;
        String link = url;
        if (isPDDShortLink(link)) {// 拼多多的短链
            HttpClient client = new HttpClient();
            client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
            PostMethod pm = new PostMethod(link);
            try {
                client.executeMethod(pm);
                Header location = pm.getResponseHeader("Location");
                if (location != null)
                    link = location.getValue();
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            if (engine instanceof Invocable) {
                Invocable in = (Invocable) engine;
                Object goodsId = in.invokeFunction("getGoodsId", link);
                if (goodsId != null)
                    return goodsId.toString().trim();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static PDDGoodsDetail getPDDGoodsInfo(String id) {
        try {
            Document doc = Jsoup.connect("http://yangkeduo.com/goods.html?goods_id=" + id)
                    .userAgent(
                            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
                    .get();
            Elements els = doc.getElementsByTag("script");
            for (int i = 0; i < els.size(); i++) {
                if (els.get(i).html().contains("window.rawData")) {
                    String dataJS = els.get(i).html().replace("window.", "var ");
                    dataJS += "function getData(){return JSON.stringify(rawData);}";
                    ScriptEngineManager manager = new ScriptEngineManager();
                    ScriptEngine engine = manager.getEngineByName("javascript");
                    try {
                        engine.eval(dataJS);
                        if (engine instanceof Invocable) {
                            Invocable in = (Invocable) engine;
                            String jsonStr = in.invokeFunction("getData").toString();
                            JSONObject json = JSONObject.fromObject(jsonStr);
                            JSONObject goods = json.optJSONObject("store").optJSONObject("initDataObj")
                                    .optJSONObject("goods");
                            String imageData = goods.optString("viewImageData");
                            imageData = imageData.replace("[", "").replace("]", "");
                            String[] images = imageData.split(",");
                            String image = images[0].trim();
                            if (image.startsWith("\""))
                                image = image.substring(1, image.length() - 1);
                            if (image.endsWith("\""))
                                image = image.substring(0, image.length() - 2);
                            PDDGoodsDetail goodsDetail = new PDDGoodsDetail();
                            goodsDetail.setGoodsImageUrl("http:" + image);
                            goodsDetail.setGoodsThumbnailUrl("http:" + image);
                            goodsDetail.setGoodsName(goods.optString("goodsName"));
                            goodsDetail.setGoodsId(goods.optLong("goodsID"));
                            return goodsDetail;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return null;
    }
    public static List<String> getPDDShortLinksFromText(String text) {
        String regex = "(https://p\\.pinduoduo\\.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;
    }
    public static String getCustomParams(Long uid) {
        return uid + "" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd");
    }
    public static String getUidFromCustomParams(String params) {
        if (params.contains("_")) {
            return params.split("_")[0];
        } else {
            if (params.length() > 12) {
                return params.substring(0, params.length() - 8);
            } else {
                return params;
            }
        }
    }
    public static void main(String[] args) {
        String link = "https://mobile.yangkeduo.com/goods.html?ps=Xy6iPwbIKZ";
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        GetMethod pm = new GetMethod(link);
        pm.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
        //pm.setRequestHeader("Sec-Fetch-Site","same-origin");
        pm.setRequestHeader("Upgrade-Insecure-Requests","1");
        pm.setRequestHeader("Referer","https://mobile.yangkeduo.com/goods.html?ps=Xy6iPwbIKZ");
        //pm.setRequestHeader("Priority","u=0, i");
        pm.setRequestHeader(":authority","mobile.yangkeduo.com");
        //pm.setRequestHeader("Cookie","api_uid=CkilZ2ZPIkIbkQBWBOpQAg==; _nano_fp=Xpman5TjX5gYX5XyX9_~Ae2Gbs0pL8h6jt_96NPO; webp=1; jrpl=KlZOYnFydoAazGlzTaZsZppQwiLSz3OM; njrpl=KlZOYnFydoAazGlzTaZsZppQwiLSz3OM; dilx=C2L6L~2oOr3ElrZKZ4_YW; pdd_vds=gaeAuFISuzLSnVdWuVbgspGgwjOXGqdpNMuzbXyFeJOXypsMOMlkLJnMGpOH");
        pm.setRequestHeader("Sec-Ch-Ua","\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"");
        pm.setRequestHeader("Sec-Ch-Ua-Mobile","?0");
        pm.setRequestHeader("Sec-Ch-Ua-Platform","\"Windows\"");
        pm.setRequestHeader("Sec-Fetch-Dest","document");
        pm.setRequestHeader("Sec-Fetch-Mode","navigate");
//        pm.setRequestHeader("","");
//        pm.setRequestHeader("","");
        try {
            client.executeMethod(pm);
            Header[]  headers = pm.getRequestHeaders();
            for(Header header:headers ){
                System.out.println(header.toString());
            }
            Header location = pm.getResponseHeader("Location");
            if (location != null) {
                link = location.getValue();
                System.out.println(link);
            }
            System.out.println(pm.getResponseBodyAsString());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}