| | |
| | | package com.yeshi.fanli.util.jd;
|
| | |
|
| | | import java.io.IOException;
|
| | | 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 org.apache.commons.httpclient.HttpClient;
|
| | | import org.apache.commons.httpclient.methods.PostMethod;
|
| | | import org.jsoup.Jsoup;
|
| | | import org.jsoup.nodes.Document;
|
| | | import org.jsoup.nodes.Element;
|
| | | import org.jsoup.select.Elements;
|
| | | import org.yeshi.utils.HttpUtil;
|
| | |
|
| | | import com.yeshi.fanli.dto.jd.JDCommissionInfo;
|
| | | import com.yeshi.fanli.dto.jd.JDCouponInfo;
|
| | | import com.yeshi.fanli.dto.jd.JDSearchFilter;
|
| | | 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.tag.PageEntity;
|
| | | import com.yeshi.fanli.util.StringUtil;
|
| | |
|
| | | import net.sf.json.JSONArray;
|
| | | import net.sf.json.JSONObject;
|
| | |
|
| | | public class JDUtil {
|
| | |
|
| | |
| | | System.out.println(result);
|
| | | }
|
| | |
|
| | | @SuppressWarnings("deprecation")
|
| | | public static JDSearchResult searchByKey(JDSearchFilter searchFilter) {
|
| | |
|
| | | JDSearchResult searchResult = new JDSearchResult();
|
| | |
|
| | | List<JDGoods> goodsList = new ArrayList<>();
|
| | | JSONObject params = new JSONObject();
|
| | | params.put("pageNo", searchFilter.getPageNo());
|
| | | params.put("pageSize", searchFilter.getPageSize());
|
| | | params.put("searchUUID", StringUtil.Md5(System.currentTimeMillis() + ""));
|
| | | JSONObject data = new JSONObject();
|
| | | data.put("deliveryType", "0");
|
| | | data.put("hasCoupon", searchFilter.getHasCoupon());
|
| | | data.put("isCare", "0");
|
| | | data.put("isPinGou", "0");
|
| | | data.put("isZY", searchFilter.getIsZY());
|
| | | data.put("key", searchFilter.getKey());
|
| | | data.put("keywordType", "kt1");
|
| | | data.put("lock", "");
|
| | | data.put("orientationFlag", "");
|
| | | data.put("searchType", "st1");
|
| | | params.put("data", data);
|
| | | HttpClient client = new HttpClient();
|
| | | PostMethod pm = new PostMethod("https://union.jd.com/api/goods/search");
|
| | | 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://union.jd.com/proManager/index?pageNo=1&keywords=%E9%9E%8B%E5%AD%90");
|
| | | pm.setRequestBody(params.toString());
|
| | | try {
|
| | | client.executeMethod(pm);
|
| | | String result = pm.getResponseBodyAsString();
|
| | | JSONObject json = JSONObject.fromObject(result);
|
| | | if (json.optInt("code") == 200) {
|
| | | JSONObject pageJson = json.optJSONObject("data").optJSONObject("page");
|
| | | PageEntity entity = new PageEntity(pageJson.optInt("pageNo"), pageJson.optInt("pageSize"),
|
| | | pageJson.optInt("totalCount"));
|
| | | searchResult.setPageEntity(entity);
|
| | |
|
| | | if (json.optJSONObject("data") != null) {
|
| | | JSONArray array = json.optJSONObject("data").optJSONArray("unionGoods");
|
| | | if (array != null) {
|
| | | for (int i = 0; i < array.size(); i++) {
|
| | | goodsList.add(parseGoods(array.optJSONArray(i).optJSONObject(0).toString()));
|
| | | }
|
| | | }
|
| | | }
|
| | | json.optJSONObject("data").optJSONArray("unionGoods");
|
| | | }
|
| | |
|
| | | System.out.println(result);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | searchResult.setGoodsList(goodsList);
|
| | | return searchResult;
|
| | | }
|
| | |
|
| | | public static JDGoods getGoodsDetail(Long skuId) {
|
| | | JDSearchFilter sf = new JDSearchFilter();
|
| | | sf.setKey(skuId + "");
|
| | | sf.setPageNo(1);
|
| | | sf.setPageSize(20);
|
| | | JDSearchResult result = searchByKey(sf);
|
| | | if (result != null && result.getGoodsList() != null && result.getGoodsList().size() > 0)
|
| | | return result.getGoodsList().get(0);
|
| | | return null;
|
| | | }
|
| | |
|
| | | private static JDGoods parseGoods(String data) {
|
| | | JDGoods goods = new JDGoods();
|
| | | JSONObject json = JSONObject.fromObject(data);
|
| | | goods.setSkuId(json.optLong("skuId"));
|
| | | goods.setSkuName(json.optString("skuName"));
|
| | | JDShopInfo shopInfo = new JDShopInfo();
|
| | | shopInfo.setShopId(json.optInt("shopId"));
|
| | | shopInfo.setShopName(json.optString("shopName"));
|
| | | goods.setShopInfo(shopInfo);
|
| | |
|
| | | if (!StringUtil.isNullOrEmpty(json.optString("wlPrice")))
|
| | | goods.setPrice(new BigDecimal(json.optString("wlPrice")));
|
| | | else
|
| | | goods.setPrice(new BigDecimal(json.optString("finalPrice")));
|
| | | goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare")));
|
| | | goods.setInOrderCount30Days(json.optLong("inOrderCount30Days"));
|
| | | if (json.optInt("isZY") == 1)
|
| | | goods.setOwner("g");
|
| | | else
|
| | | goods.setOwner("p");
|
| | | goods.setMaterialUrl("http://img14.360buyimg.com/n1/" + json.optString("materialUrl"));
|
| | | JDCommissionInfo commission = new JDCommissionInfo();
|
| | | commission.setCommission(new BigDecimal(json.optString("wlCommission")));
|
| | | commission.setCommissionShare(new BigDecimal(json.optString("wlCommissionRatio")));
|
| | | goods.setCommissionInfo(commission);
|
| | | goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare")));
|
| | |
|
| | | if (json.optString("hasCoupon") != null && json.optInt("hasCoupon") == 1
|
| | | && json.optInt("couponRemainCnt") > 0) {
|
| | | // 有券
|
| | | 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);
|
| | | } else {
|
| | | // 无券
|
| | | }
|
| | | return goods;
|
| | | }
|
| | |
|
| | | 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(
|
| | | "https://wq.jd.com/bases/searchdropdown/getdropdown?terminal=m&zip=1&key=%s&newjson=1&_=%s&sceneval=2",
|
| | | URLEncoder.encode(key, "UTF-8"), System.currentTimeMillis() + "");
|
| | | } catch (UnsupportedEncodingException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | if (url == null)
|
| | | return list;
|
| | | try {
|
| | | String result = HttpUtil.get(url);
|
| | | result = result.replace("callback(", "").replace(")", "");
|
| | | JSONArray array = JSONArray.fromObject(result);
|
| | | for (int i = 0; i < array.size(); i++) {
|
| | | String sk = array.optJSONObject(i).optString("key");
|
| | | if (!StringUtil.isNullOrEmpty(sk))
|
| | | list.add(sk);
|
| | | }
|
| | | } catch (Exception e) {
|
| | | }
|
| | | return list;
|
| | | }
|
| | |
|
| | | }
|