yujian
2019-10-29 9b55262bb45c81c997ab04e55e8de13e14b8ca7d
fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoBaoUtil.java
@@ -72,6 +72,7 @@
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.TaoBaoHttpUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.vo.msg.ClientTextStyleVO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -782,7 +783,41 @@
      // labels.add(new ClientTextStyleVO("标签内容", "#00FF00"));
      // tg.setLabels(labels);
      // }
      return tg;
      return loadYuShouInfo(tg, tb);
   }
   private static TaoBaoGoodsBriefExtra loadYuShouInfo(TaoBaoGoodsBriefExtra extra, TaoBaoGoodsBrief goods) {
      if (goods != null && extra != null) {
         if (isYUShou(goods)) {// 预售商品
//            extra.setZkPrice(new BigDecimal(goods.getPresaleDeposit()));
            // 预售
            if (extra.getLabels() == null)
               extra.setLabels(new ArrayList<>());
            extra.getLabels().add(new ClientTextStyleVO("预售", "#FF2B4E"));
         }
      }
      return extra;
   }
   /**
    * 是否为预售商品
    *
    * @param goods
    * @return
    */
   public static boolean isYUShou(TaoBaoGoodsBrief goods) {
      if (!StringUtil.isNullOrEmpty(goods.getPresaleDeposit())
            && !StringUtil.isNullOrEmpty(goods.getPresaleStartTime())
            && !StringUtil.isNullOrEmpty(goods.getPresaleEndTime())) {
         long startTime = Long.parseLong(goods.getPresaleStartTime());
         long endTime = Long.parseLong(goods.getPresaleEndTime());
         long now = System.currentTimeMillis();
         if (now >= startTime && now < endTime) {
            return true;
         }
      }
      return false;
   }
   /**
@@ -1337,6 +1372,41 @@
      return taoBaoGoodsBrief;
   }
   public static String getGoodsIdByPhoneShareUrl(String burl) {
      try {
         Connection connect = Jsoup.connect(burl);
         Document document = connect.get();
         org.jsoup.nodes.Element element = document.getElementsByTag("head").get(0).getElementsByTag("Script")
               .get(1);
         String html = element.toString();
         int index = html.indexOf("var url = ");
         int endIndex = html.lastIndexOf("'");
         html = html.substring(index, endIndex);
         int index2 = html.indexOf("'");
         html = html.substring(index2 + 1);
         int indexId = html.indexOf("id=");
         String id = "";
         if (html.contains("a.m.taobao.com")) {
            indexId = html.indexOf("com/i");
            html = html.substring(indexId);
            html = html.substring(5, html.indexOf("."));
            id = html;
         } else {
            String reg = "[?|&]{1}[i]{1}[d]{1}[=]{1}\\d+[&]?";
            Pattern p = Pattern.compile(reg);
            Matcher matcher = p.matcher(html);
            if (matcher.find()) {
               id = matcher.group();
            }
            id = id.substring(id.indexOf("=") + 1, id.lastIndexOf("&"));
         }
         return id;
      } catch (Exception e) {
         LogHelper.error("无法解析到淘宝商品ID:" + burl);
         return null;
      }
   }
   /**
    * 获取商品的用户分成比例
    * 
@@ -1363,7 +1433,6 @@
    */
   public static BigDecimal getGoodsHongBaoMoney(TaoBaoGoodsBrief goodsBrief, BigDecimal rate) {
      BigDecimal money = null;
      if (StringUtil.isNullOrEmpty(goodsBrief.getCouponInfo())
            || goodsBrief.getCouponInfo().trim().equalsIgnoreCase("无")) {
         money = MoneyBigDecimalUtil.mul(
@@ -1926,6 +1995,7 @@
   /**
    * 查询天猫商品图片、标题
    *
    * @param auctionId
    * @return
    */
@@ -1941,12 +2011,17 @@
               if (content.contains("var _DATA_Mdskip")) {
                  content = content.replace("<script>", "");
                  content = content.replace("</script>", "");
                  content+=";function getData(){ var json={title:_DATA_Mdskip.item.title,pictUrl:_DATA_Mdskip.item.videoDetail.videoThumbnailURL};   return JSON.stringify(json);}";
                  if (content.contains("videoDetail")) {
                     content += ";function getData(){ var json={title:_DATA_Mdskip.item.title,pictUrl:_DATA_Mdskip.item.videoDetail.videoThumbnailURL};   return JSON.stringify(json);}";
                  } else {
                     content += ";function getData(){ var json={title:_DATA_Mdskip.item.title,pictUrl:_DATA_Mdskip.item.videos[0].videoThumbnailURL};   return JSON.stringify(json);}";
                  }
                  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"));
@@ -1956,7 +2031,7 @@
                     taoBaoGoodsBrief.setTitle(title.toString());
                     taoBaoGoodsBrief.setPictUrl(pictUrl.toString());
                  }
                  break;
               }
            }
@@ -1966,22 +2041,24 @@
      }
      return taoBaoGoodsBrief;
   }
   /**
    * 抖音返利local 获取真实淘宝链接
    * @param url https://s.click.taobao.com/t?e=m%3D2%26s%3DT9DFpHQCT7Rw4vFB6t2Z2ueEDrYVVa64XoO8tOebS+dRAdhuF14FMR8
    *
    * @param url
    *            https://s.click.taobao.com/t?e=m%3D2%26s%
    *            3DT9DFpHQCT7Rw4vFB6t2Z2ueEDrYVVa64XoO8tOebS+dRAdhuF14FMR8
    * @return
    */
   public static String getRealUrl(String url) {
      if (StringUtil.isNullOrEmpty(url))
         return null;
      try {
         HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
         client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
         client.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)"); // 让服务器认为是IE
         client.getParams().setParameter(HttpMethodParams.USER_AGENT,
               "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)"); // 让服务器认为是IE
         GetMethod get = new GetMethod(url);
         get.setFollowRedirects(false); // 禁止自动重定向
         int iGetResultCode = client.executeMethod(get);
@@ -1993,9 +2070,58 @@
         return get2.getResponseHeader("location").getValue(); // 真实地址
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      }
      return null;
   }
   /**
    * 淘口令过滤
    *
    * @param token
    * @return
    */
   public static String filterTaoToken(String token) {
      if (StringUtil.isNullOrEmpty(token))
         return token;
      if (token.contains("¥") || token.contains("€")) {
         String newToken = "";
         for (int i = 0; i < token.length(); i++) {
            if (token.charAt(i) == '¥' || token.charAt(i) == '€') {
               if (!newToken.contains("("))
                  newToken += "(";
               else
                  newToken += ")";
            } else
               newToken += token.charAt(i) + "";
         }
         return newToken;
      }
      return token;
   }
   /**
    * 提取自有格式的淘口令
    *
    * @param str
    * @return
    */
   public static String parseSystemTaoToken(String str) {
      String pattern = "(\\({1}[A-Za-z0-9]+\\){1})";
      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(str);
      while (m.find()) {
         String group = m.group(0);
         if (!StringUtil.isNullOrEmpty(group))
            return group;
      }
      return null;
   }
   public static boolean isSpecialGoods(Integer materialLibType) {
      if (materialLibType != null && materialLibType == 1)
         return true;
      else
         return false;
   }
}