| | |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | import com.sun.istack.NotNull; |
| | | import com.yeshi.fanli.dto.jd.*; |
| | | import com.yeshi.fanli.entity.jd.JDBackUpOrder; |
| | | import com.yeshi.fanli.util.Constant; |
| | | import com.yeshi.fanli.util.taobao.SearchFilterUtil; |
| | | import com.yeshi.goods.facade.utils.taobao.DaTaoKeApiUtil; |
| | | import org.apache.commons.httpclient.HttpClient; |
| | | import org.apache.commons.httpclient.methods.PostMethod; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.yeshi.fanli.dto.jd.JDCategoryInfo; |
| | | import com.yeshi.fanli.dto.jd.JDCommissionInfo; |
| | | import com.yeshi.fanli.dto.jd.JDCouponInfo; |
| | | import com.yeshi.fanli.dto.jd.JDFilter; |
| | | import com.yeshi.fanli.dto.jd.JDOrderResult; |
| | | import com.yeshi.fanli.dto.jd.JDPingouInfo; |
| | | import com.yeshi.fanli.dto.jd.JDSearchResult; |
| | | import com.yeshi.fanli.dto.jd.JDShopInfo; |
| | | import com.yeshi.fanli.entity.bus.clazz.GoodsClass; |
| | | import com.yeshi.fanli.entity.jd.JDGoods; |
| | | import com.yeshi.fanli.entity.jd.JDOrder; |
| | | import com.yeshi.fanli.entity.jd.JDOrderItem; |
| | | import com.yeshi.fanli.log.LogHelper; |
| | | import com.yeshi.common.entity.PageEntity; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.yeshi.utils.MoneyBigDecimalUtil; |
| | | import com.yeshi.fanli.util.StringUtil; |
| | | import org.yeshi.utils.TimeUtil; |
| | |
| | | import net.sf.json.JSONObject; |
| | | |
| | | public class JDApiUtil { |
| | | |
| | | final static Logger logger = LoggerFactory.getLogger("jdApiLogger"); |
| | | |
| | | public static String APP_ID = "1864778416"; |
| | | public static String APP_KEY = "388f5133d13a5ea290aa5c44f3ebcc00"; |
| | | public static String SECRET_KEY = "e22367fc866746d98b50d47b4c521781"; |
| | | public static String SECRET_KEY = "85e7e72110564efab6e13d302a4419da"; |
| | | private static String SERVER_URL = "https://router.jd.com/api"; |
| | | |
| | | // public static long POSITION_FANLI = 1869704794L; |
| | | // public static long POSITION_SHARE = 1869542990L; |
| | | // public static long POSITION_COUPON = 1869704796L; |
| | | |
| | | // 订单查询类型 |
| | | public static int ORDER_TYPE_CREATETIME = 1;// 下单时间 |
| | |
| | | return resultJson.optJSONObject("data").optString("clickURL"); |
| | | } |
| | | |
| | | public static String convertLinkWithSubUnionId(@NotNull String materialId, String couponUrl, String giftCouponKey, @NotNull String positionId, |
| | | String subUnionId) { |
| | | public static String convertLinkWithSubUnionId(String materialId, String couponUrl, String giftCouponKey, String positionId, |
| | | String subUnionId) throws Exception { |
| | | String log = "京东转链出错:"; |
| | | try { |
| | | JSONObject json = new JSONObject(); |
| | | JSONObject json = new JSONObject(); |
| | | |
| | | json.put("siteId", APP_ID); |
| | | json.put("positionId", positionId); |
| | | json.put("materialId", materialId); |
| | | json.put("siteId", APP_ID); |
| | | json.put("positionId", positionId); |
| | | json.put("materialId", materialId); |
| | | |
| | | if (!StringUtil.isNullOrEmpty(giftCouponKey)) { |
| | | json.put("giftCouponKey", giftCouponKey); |
| | | } |
| | | if (!StringUtil.isNullOrEmpty(couponUrl)) |
| | | json.put("couponUrl", couponUrl); |
| | | if (!StringUtil.isNullOrEmpty(giftCouponKey)) { |
| | | json.put("giftCouponKey", giftCouponKey); |
| | | } |
| | | if (!StringUtil.isNullOrEmpty(couponUrl)) |
| | | json.put("couponUrl", couponUrl); |
| | | |
| | | if (!StringUtil.isNullOrEmpty(subUnionId)) |
| | | json.put("subUnionId", subUnionId); |
| | | if (!StringUtil.isNullOrEmpty(subUnionId)) |
| | | json.put("subUnionId", subUnionId); |
| | | |
| | | // 1:长链, 2 :短链 ,3: 长链+短链 |
| | | json.put("chainType", 3); |
| | | JSONObject root = new JSONObject(); |
| | | root.put("promotionCodeReq", json); |
| | | // 1:长链, 2 :短链 ,3: 长链+短链 |
| | | json.put("chainType", 3); |
| | | JSONObject root = new JSONObject(); |
| | | root.put("promotionCodeReq", json); |
| | | |
| | | String result = baseRequest2("jd.union.open.promotion.bysubunionid.get", null, root); |
| | | String result = baseRequest2("jd.union.open.promotion.bysubunionid.get", null, root); |
| | | |
| | | log += json.toString() + "####" + result; |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | result = resultJson.optJSONObject("jd_union_open_promotion_bysubunionid_get_response").optString("result"); |
| | | System.out.println(result); |
| | | if (result == null) { |
| | | return null; |
| | | } |
| | | |
| | | resultJson = JSONObject.fromObject(result); |
| | | return resultJson.optJSONObject("data").optString("shortURL"); |
| | | } catch (Exception e) { |
| | | LogHelper.error(log); |
| | | log += json.toString() + "####" + result; |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | result = resultJson.optJSONObject("jd_union_open_promotion_bysubunionid_get_response").optString("result"); |
| | | System.out.println(result); |
| | | if (result == null) { |
| | | return null; |
| | | } |
| | | |
| | | return null; |
| | | resultJson = JSONObject.fromObject(result); |
| | | return resultJson.optJSONObject("data").optString("shortURL"); |
| | | } |
| | | |
| | | public static JDGoods queryGoodsDetail(Long skuId) { |
| | |
| | | * @return |
| | | */ |
| | | public static JDSearchResult queryByKey(JDFilter filter) { |
| | | logger.info(String.format("queryByKey:%s %s", filter.getKeyword(), StringUtil.concat(filter.getListId(), ","))); |
| | | |
| | | JDSearchResult searchResult = new JDSearchResult(); |
| | | |
| | | List<JDGoods> list = new ArrayList<>(); |
| | |
| | | json.put("owner", filter.getOwner()); |
| | | |
| | | System.out.println(json.toString()); |
| | | String result = ""; |
| | | |
| | | |
| | | JSONObject jsonDTO = new JSONObject(); |
| | | jsonDTO.put("goodsReqDTO", json); |
| | | |
| | | String result = baseRequest2("jd.union.open.goods.query", null, jsonDTO); |
| | | result = baseRequest2("jd.union.open.goods.query", null, jsonDTO); |
| | | System.out.println(result); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | resultJson = resultJson.optJSONObject("jd_union_open_goods_query_response"); |
| | | if (resultJson.optInt("code") == 0) { |
| | | if (resultJson != null && resultJson.optInt("code") == 0) { |
| | | result = resultJson.optString("result"); |
| | | resultJson = JSONObject.fromObject(result); |
| | | String message = resultJson.optString("message"); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | long totalCount = resultJson.optLong("totalCount"); |
| | | PageEntity pageEntity = new PageEntity(); |
| | | pageEntity.setTotalCount(totalCount); |
| | | |
| | | searchResult.setPageEntity(pageEntity); |
| | | } else { |
| | | result = DaTaoKeApiUtil.searchJDGoods(json); |
| | | resultJson = JSONObject.fromObject(result); |
| | | if (resultJson.optInt("code") == 0) { |
| | | JSONObject data = resultJson.optJSONObject("data"); |
| | | JSONArray array = data.optJSONArray("list"); |
| | | for (int i = 0; i < array.size(); i++) { |
| | | JSONObject item = array.optJSONObject(i); |
| | | JDGoods goods = parseDaTaoKeJDGoods(item); |
| | | if (goods != null) |
| | | list.add(goods); |
| | | } |
| | | |
| | | long totalCount = data.optLong("totalNum"); |
| | | PageEntity pageEntity = new PageEntity(); |
| | | pageEntity.setTotalCount(totalCount); |
| | | searchResult.setPageEntity(pageEntity); |
| | | } |
| | | System.out.printf(result); |
| | | |
| | | } |
| | | |
| | | |
| | | searchResult.setGoodsList(list); |
| | | return searchResult; |
| | | } |
| | |
| | | |
| | | return goods; |
| | | } |
| | | |
| | | |
| | | private static JDGoods parseDaTaoKeJDGoods(JSONObject json) { |
| | | JDGoods goods = new JDGoods(); |
| | | |
| | | // 佣金信息 |
| | | BigDecimal commissionShare = new BigDecimal(json.optString("commissionShare")); |
| | | if (commissionShare.compareTo(new BigDecimal(0)) > 0) { |
| | | JDCommissionInfo commissionInfo = new JDCommissionInfo(); |
| | | commissionInfo.setCommission(new BigDecimal(json.optString("commission"))); |
| | | commissionInfo.setCommissionShare(commissionShare); |
| | | goods.setCommissionInfo(commissionInfo); |
| | | } else { |
| | | return null; |
| | | } |
| | | |
| | | goods.setComments(json.optLong("comments")); |
| | | goods.setBrandCode(json.optString("brandCode")); |
| | | goods.setBrandName(json.optString("brandName")); |
| | | goods.setGoodCommentsShare(new BigDecimal(json.optString("goodCommentsShare"))); |
| | | goods.setInOrderCount30Days(json.optLong("inOrderCount30Days")); |
| | | goods.setOwner(json.optString("owner")); |
| | | goods.setMaterialUrl(json.optString("materialUrl")); |
| | | goods.setSkuId(json.optLong("skuId")); |
| | | goods.setSkuName(json.optString("skuName")); |
| | | goods.setIsHot(json.optInt("isHot")); |
| | | |
| | | // 价格信息 |
| | | goods.setPrice(new BigDecimal(json.optString("price"))); |
| | | |
| | | // 店铺信息 |
| | | JDShopInfo jdshopInfo = new JDShopInfo(); |
| | | jdshopInfo.setShopId(json.optInt("shopId")); |
| | | jdshopInfo.setShopName(json.optString("shopName")); |
| | | goods.setShopInfo(jdshopInfo); |
| | | |
| | | // 分类信息 |
| | | JDCategoryInfo categoryInfo = new JDCategoryInfo(); |
| | | categoryInfo.setCid1(json.optLong("cid1")); |
| | | categoryInfo.setCid1Name(json.optString("cid1Name")); |
| | | categoryInfo.setCid2(json.optLong("cid2")); |
| | | categoryInfo.setCid2Name(json.optString("cid2Name")); |
| | | categoryInfo.setCid3(json.optLong("cid3")); |
| | | categoryInfo.setCid3Name(json.optString("cid3Name")); |
| | | goods.setCategoryInfo(categoryInfo); |
| | | |
| | | // 图片信息 |
| | | List<String> imageList = new ArrayList<>(); |
| | | JSONArray imagesArray = json.optJSONArray("imageUrlList"); |
| | | for (int i = 0; i < imagesArray.size(); i++) { |
| | | imageList.add(imagesArray.optString(i)); |
| | | } |
| | | goods.setImageList(imageList); |
| | | |
| | | if (imageList.size() > 0) { |
| | | goods.setPicUrl(imageList.get(0)); |
| | | } |
| | | |
| | | BigDecimal price = new BigDecimal(json.optString("price")); |
| | | // 拼购信息 |
| | | Object pinGouInfo = json.get("pinGouInfo"); |
| | | if (pinGouInfo != null) { |
| | | JSONObject pinGouInfoJson = JSONObject.fromObject(pinGouInfo); |
| | | if (!pinGouInfoJson.isEmpty()) { |
| | | JDPingouInfo jdPinGouInfo = new JDPingouInfo(); |
| | | price = new BigDecimal(pinGouInfoJson.optString("pingouPrice")); |
| | | jdPinGouInfo.setPingouPrice(price); |
| | | jdPinGouInfo.setPingouTmCount(pinGouInfoJson.optLong("pingouTmCount")); |
| | | jdPinGouInfo.setPingouUrl(pinGouInfoJson.optString("pingouUrl")); |
| | | jdPinGouInfo.setTotalCount(pinGouInfoJson.optLong("totalCount")); |
| | | jdPinGouInfo.setPingouEndTime(pinGouInfoJson.optLong("pingouEndTime")); |
| | | jdPinGouInfo.setPingouStartTime(pinGouInfoJson.optLong("pingouStartTime")); |
| | | goods.setPinGouInfo(jdPinGouInfo); |
| | | } |
| | | } |
| | | |
| | | // 券信息 |
| | | Object coupon = json.get("couponInfo"); |
| | | if (coupon != null) { |
| | | JSONArray couponArray = json.optJSONArray("couponList"); |
| | | if (couponArray != null) { |
| | | List<JDCouponInfo> couponInfoList = new ArrayList<>(); |
| | | for (int i = 0; i < couponArray.size(); i++) { |
| | | JDCouponInfo couponInfo = parseJDCouponInfo(couponArray.optJSONObject(i)); |
| | | if (couponInfo != null) |
| | | couponInfoList.add(couponInfo); |
| | | } |
| | | |
| | | // 根据消费条件排序 |
| | | Comparator<JDCouponInfo> cm = new Comparator<JDCouponInfo>() { |
| | | @Override |
| | | public int compare(JDCouponInfo o1, JDCouponInfo o2) { |
| | | return o1.getQuota().compareTo(o2.getQuota()); |
| | | } |
| | | }; |
| | | Collections.sort(couponInfoList, cm); |
| | | // if (couponInfoList.size() > 0) { |
| | | // // 设置券信息为最接近的一个 |
| | | // for (int i = couponInfoList.size() - 1; i >= 0; i--) { |
| | | // if |
| | | // (couponInfoList.get(i).getQuota().compareTo(goods.getPrice()) |
| | | // <= 0) { |
| | | // goods.setCouponInfo(couponInfoList.get(i)); |
| | | // break; |
| | | // } |
| | | // } |
| | | // if (goods.getCouponInfo() == null) |
| | | // goods.setCouponInfo(couponInfoList.get(0)); |
| | | // } |
| | | // |
| | | goods.setCouponInfoList(couponInfoList); |
| | | |
| | | } |
| | | } |
| | | |
| | | return goods; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取推广商品信息接口 |
| | |
| | | root = JSONObject.fromObject(root.optString("result")); |
| | | if (root.optInt("code") == 200 && root.optJSONArray("data") != null) { |
| | | |
| | | String date = root.optJSONArray("data").toString(); |
| | | JSONArray data = root.optJSONArray("data"); |
| | | Type typeToken = new TypeToken<List<JDOrder>>() { |
| | | }.getType(); |
| | | List<JDOrder> orderList = new Gson().fromJson(date, typeToken); |
| | | List<JDOrder> orderList = new Gson().fromJson(data.toString(), typeToken); |
| | | if (orderList != null) |
| | | for (JDOrder order : orderList) { |
| | | Map<Long, List<JDOrderItem>> map = new HashMap<>(); |
| | |
| | | |
| | | } |
| | | return new JDOrderResult(hasMore, orderList); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static JDOrderForBackUpResult getOrderListForBackUp(int page, int pageSize, Date time, int type) { |
| | | JSONObject json = new JSONObject(); |
| | | JSONObject orderReq = new JSONObject(); |
| | | orderReq.put("pageNo", page); |
| | | orderReq.put("pageSize", pageSize); |
| | | orderReq.put("type", type); |
| | | orderReq.put("time", TimeUtil.getGernalTime(time.getTime(), "yyyyMMddHH")); |
| | | |
| | | json.put("orderReq", orderReq); |
| | | String result = baseRequest2("jd.union.open.order.query", null, json); |
| | | System.out.println(result); |
| | | JSONObject root = JSONObject.fromObject(result).optJSONObject("jd_union_open_order_query_response"); |
| | | if (root.optInt("code") == 0) { |
| | | boolean hasMore = root.optBoolean("hasMore"); |
| | | root = JSONObject.fromObject(root.optString("result")); |
| | | if (root.optInt("code") == 200 && root.optJSONArray("data") != null) { |
| | | List<JDBackUpOrder> orderList = new ArrayList<>(); |
| | | JSONArray data = root.optJSONArray("data"); |
| | | for (int i = 0; i < data.size(); i++) { |
| | | JSONObject item = data.optJSONObject(i); |
| | | String orderId = item.optString("orderId"); |
| | | Long orderTime = item.optLong("orderTime"); |
| | | JDBackUpOrder order = new JDBackUpOrder(); |
| | | order.setId(orderId); |
| | | order.setContent(item.toString()); |
| | | order.setOrderTime(new Date(orderTime)); |
| | | orderList.add(order); |
| | | } |
| | | return new JDOrderForBackUpResult(hasMore, orderList); |
| | | } |
| | | } |
| | | return null; |
| | |
| | | System.out.println(result); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // JDFilter filter=new JDFilter(); |
| | | // filter.setKeyword("花卉"); |
| | | // filter.setPageIndex(1); |
| | | // filter.setPageSize(20); |
| | | // queryByKey(filter); |
| | | |
| | | long now = System.currentTimeMillis(); |
| | | |
| | | JDOrderResult result = JDApiUtil.getOrderList(1, 200, new Date(now), JDApiUtil.ORDER_TYPE_CREATETIME); |
| | | System.out.printf(result.toString()); |
| | | } |
| | | |
| | | } |