| | |
| | | |
| | | /** |
| | | * TODO 未完成 唯品会接口 |
| | | * |
| | | * @author Administrator |
| | | * |
| | | * @author Administrator |
| | | */ |
| | | public class VipShopApiUtil { |
| | | |
| | | private final static String appKey = "f9e7f22f"; |
| | | private final static String appSecret = "9B2291352497FAF42B2DF44BFCF62316"; |
| | | private final static String appKey = "f9e7f22f"; |
| | | private final static String appSecret = "9B2291352497FAF42B2DF44BFCF62316"; |
| | | |
| | | /** |
| | | * 获取签名 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | private static String getSign(Map<String, String> systemParams, JSONObject taskParams) { |
| | | List<String> list = new ArrayList<>(); |
| | | for (Iterator<String> its = systemParams.keySet().iterator(); its.hasNext();) { |
| | | String key = its.next(); |
| | | String value = systemParams.get(key); |
| | | list.add(key + value); |
| | | } |
| | | Collections.sort(list); |
| | | String source = ""; |
| | | for (String st : list) |
| | | source += st; |
| | | source += taskParams.toString(); |
| | | return StringUtil.MD5Hmac(source, appSecret); |
| | | } |
| | | //是否采用订单侠转链 |
| | | private final static boolean CONVERT_DINGDANXIA = true; |
| | | |
| | | private static Map<String, String> getSystemParams(String service, String method) { |
| | | Map<String, String> params = new HashMap<String, String>(); |
| | | params.put("service", service); |
| | | params.put("method", method); |
| | | params.put("version", "1.0"); |
| | | params.put("timestamp", System.currentTimeMillis() / 1000 + ""); |
| | | params.put("format", "json"); |
| | | params.put("appKey", appKey); |
| | | return params; |
| | | } |
| | | /** |
| | | * 获取签名 |
| | | * |
| | | * @param params |
| | | * @return |
| | | */ |
| | | private static String getSign(Map<String, String> systemParams, JSONObject taskParams) { |
| | | List<String> list = new ArrayList<>(); |
| | | for (Iterator<String> its = systemParams.keySet().iterator(); its.hasNext(); ) { |
| | | String key = its.next(); |
| | | String value = systemParams.get(key); |
| | | list.add(key + value); |
| | | } |
| | | Collections.sort(list); |
| | | String source = ""; |
| | | for (String st : list) |
| | | source += st; |
| | | source += taskParams.toString(); |
| | | return StringUtil.MD5Hmac(source, appSecret); |
| | | } |
| | | |
| | | /** |
| | | * 基础请求 |
| | | * |
| | | * @param service |
| | | * @param method |
| | | * @param taskParams |
| | | * @return |
| | | */ |
| | | private static String baseRequest(String service, String method, JSONObject taskParams) { |
| | | Map<String, String> systemParams = getSystemParams(service, method); |
| | | String sign = getSign(systemParams, taskParams); |
| | | systemParams.put("sign", sign); |
| | | String baseUrl = "https://gw.vipapis.com"; |
| | | baseUrl += "?"; |
| | | for (Iterator<String> its = systemParams.keySet().iterator(); its.hasNext();) { |
| | | String key = its.next(); |
| | | String value = ""; |
| | | try { |
| | | value = URLEncoder.encode(systemParams.get(key), "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | baseUrl += key + "=" + value + "&"; |
| | | } |
| | | baseUrl = baseUrl.endsWith("&") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; |
| | | String result = post(baseUrl, taskParams.toString()); |
| | | return result; |
| | | } |
| | | private static Map<String, String> getSystemParams(String service, String method) { |
| | | Map<String, String> params = new HashMap<String, String>(); |
| | | params.put("service", service); |
| | | params.put("method", method); |
| | | params.put("version", "1.0"); |
| | | params.put("timestamp", System.currentTimeMillis() / 1000 + ""); |
| | | params.put("format", "json"); |
| | | params.put("appKey", appKey); |
| | | return params; |
| | | } |
| | | |
| | | @SuppressWarnings("deprecation") |
| | | private static String post(String url, String body) { |
| | | HttpClient client = new HttpClient(); |
| | | PostMethod method = new PostMethod(url); |
| | | method.addRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
| | | method.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
| | | method.setRequestBody(body); |
| | | try { |
| | | client.executeMethod(method); |
| | | return method.getResponseBodyAsString(); |
| | | } catch (HttpException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 基础请求 |
| | | * |
| | | * @param service |
| | | * @param method |
| | | * @param taskParams |
| | | * @return |
| | | */ |
| | | private static String baseRequest(String service, String method, JSONObject taskParams) { |
| | | Map<String, String> systemParams = getSystemParams(service, method); |
| | | String sign = getSign(systemParams, taskParams); |
| | | systemParams.put("sign", sign); |
| | | String baseUrl = "https://gw.vipapis.com"; |
| | | baseUrl += "?"; |
| | | for (Iterator<String> its = systemParams.keySet().iterator(); its.hasNext(); ) { |
| | | String key = its.next(); |
| | | String value = ""; |
| | | try { |
| | | value = URLEncoder.encode(systemParams.get(key), "UTF-8"); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | baseUrl += key + "=" + value + "&"; |
| | | } |
| | | baseUrl = baseUrl.endsWith("&") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; |
| | | String result = post(baseUrl, taskParams.toString()); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 搜索 |
| | | * @Title: search |
| | | * @Description: |
| | | * @param searchFilter |
| | | * @return |
| | | * VIPSearchResult 返回类型 |
| | | * @throws |
| | | */ |
| | | public static VIPSearchResult search(VIPSearchFilter searchFilter) { |
| | | Map<String, String> taskParams = new HashMap<>(); |
| | | taskParams.put("keyword", searchFilter.getKeyword()); |
| | | taskParams.put("page", searchFilter.getPage() + ""); |
| | | taskParams.put("pageSize", searchFilter.getPageSize() + ""); |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | if (searchFilter.getPriceStart() != null) |
| | | taskParams.put("priceStart", searchFilter.getPriceStart()); |
| | | @SuppressWarnings("deprecation") |
| | | private static String post(String url, String body) { |
| | | HttpClient client = new HttpClient(); |
| | | PostMethod method = new PostMethod(url); |
| | | method.addRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
| | | method.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
| | | method.setRequestBody(body); |
| | | try { |
| | | client.executeMethod(method); |
| | | return method.getResponseBodyAsString(); |
| | | } catch (HttpException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | if (searchFilter.getPriceEnd() != null) |
| | | taskParams.put("priceEnd", searchFilter.getPriceEnd()); |
| | | if (searchFilter.getOrder() != null) |
| | | taskParams.put("order", searchFilter.getOrder() + ""); |
| | | if (searchFilter.getFieldName() != null) |
| | | taskParams.put("fieldName", searchFilter.getFieldName() + ""); |
| | | /** |
| | | * 搜索 |
| | | * |
| | | * @param searchFilter |
| | | * @return VIPSearchResult 返回类型 |
| | | * @throws |
| | | * @Title: search |
| | | * @Description: |
| | | */ |
| | | public static VIPSearchResult search(VIPSearchFilter searchFilter) { |
| | | Map<String, String> taskParams = new HashMap<>(); |
| | | taskParams.put("keyword", searchFilter.getKeyword()); |
| | | taskParams.put("page", searchFilter.getPage() + ""); |
| | | taskParams.put("pageSize", searchFilter.getPageSize() + ""); |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | if (searchFilter.getPriceStart() != null) |
| | | taskParams.put("priceStart", searchFilter.getPriceStart()); |
| | | |
| | | JSONObject root = new JSONObject(); |
| | | root.put("request", JSONObject.fromObject(taskParams)); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "query", root); |
| | | System.out.println(result); |
| | | List<VIPGoodsInfo> goodsList = new ArrayList<>(); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | Gson gson = new Gson(); |
| | | if (resultJson.optInt("returnCode") == 0) { |
| | | resultJson = resultJson.optJSONObject("result"); |
| | | int count = resultJson.optInt("total"); |
| | | |
| | | JSONArray array = resultJson.optJSONArray("goodsInfoList"); |
| | | if (array != null && array.size() > 0) { |
| | | for (int i = 0; i < array.size(); i++) { |
| | | JSONObject optJSONObject = array.optJSONObject(i); |
| | | if (optJSONObject == null) { |
| | | continue; |
| | | } |
| | | VIPGoodsInfo info = gson.fromJson(array.optJSONObject(i).toString(), VIPGoodsInfo.class); |
| | | if (info != null) |
| | | goodsList.add(info); |
| | | } |
| | | } |
| | | |
| | | return new VIPSearchResult(goodsList, count); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | |
| | | public static VIPSearchResult getGoodsList(int channelType,int page) { |
| | | Map<String, String> taskParams = new HashMap<>(); |
| | | taskParams.put("channelType",channelType+""); |
| | | taskParams.put("page", page+ ""); |
| | | taskParams.put("pageSize", 20 + ""); |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | |
| | | if (searchFilter.getPriceEnd() != null) |
| | | taskParams.put("priceEnd", searchFilter.getPriceEnd()); |
| | | if (searchFilter.getOrder() != null) |
| | | taskParams.put("order", searchFilter.getOrder() + ""); |
| | | if (searchFilter.getFieldName() != null) |
| | | taskParams.put("fieldName", searchFilter.getFieldName() + ""); |
| | | |
| | | JSONObject root = new JSONObject(); |
| | | root.put("request", JSONObject.fromObject(taskParams)); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "goodsList", root); |
| | | System.out.println(result); |
| | | List<VIPGoodsInfo> goodsList = new ArrayList<>(); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | Gson gson = new Gson(); |
| | | if (resultJson.optInt("returnCode") == 0) { |
| | | resultJson = resultJson.optJSONObject("result"); |
| | | int count = resultJson.optInt("total"); |
| | | JSONArray array = resultJson.optJSONArray("goodsInfoList"); |
| | | for (int i = 0; i < array.size(); i++) { |
| | | VIPGoodsInfo info = gson.fromJson(array.optJSONObject(i).toString(), VIPGoodsInfo.class); |
| | | if (info != null) |
| | | goodsList.add(info); |
| | | } |
| | | return new VIPSearchResult(goodsList, count); |
| | | } |
| | | return null; |
| | | } |
| | | JSONObject root = new JSONObject(); |
| | | root.put("request", JSONObject.fromObject(taskParams)); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "query", root); |
| | | System.out.println(result); |
| | | List<VIPGoodsInfo> goodsList = new ArrayList<>(); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | Gson gson = new Gson(); |
| | | if (resultJson.optInt("returnCode") == 0) { |
| | | resultJson = resultJson.optJSONObject("result"); |
| | | int count = resultJson.optInt("total"); |
| | | |
| | | /** |
| | | * 商品ID转链 |
| | | * |
| | | * @param goodsId |
| | | */ |
| | | public static VIPConvertResultDTO convertLink(String goodsId, String tag) { |
| | | JSONObject taskParams = new JSONObject(); |
| | | JSONArray goodsIdArray = new JSONArray(); |
| | | goodsIdArray.add(goodsId); |
| | | taskParams.put("goodsIdList", goodsIdArray); |
| | | taskParams.put("chanTag", tag); |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionUrlService", "genByGoodsId", |
| | | JSONObject.fromObject(taskParams)); |
| | | System.out.println(result); |
| | | JSONObject resultJSON = JSONObject.fromObject(result); |
| | | if (resultJSON.optInt("returnCode") == 0) { |
| | | String re = resultJSON.optJSONObject("result").optJSONArray("urlInfoList").optJSONObject(0).toString(); |
| | | return new Gson().fromJson(re, VIPConvertResultDTO.class); |
| | | } |
| | | return null; |
| | | } |
| | | JSONArray array = resultJson.optJSONArray("goodsInfoList"); |
| | | if (array != null && array.size() > 0) { |
| | | for (int i = 0; i < array.size(); i++) { |
| | | JSONObject optJSONObject = array.optJSONObject(i); |
| | | if (optJSONObject == null) { |
| | | continue; |
| | | } |
| | | VIPGoodsInfo info = gson.fromJson(array.optJSONObject(i).toString(), VIPGoodsInfo.class); |
| | | if (info != null) |
| | | goodsList.add(info); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据商品ID获取商品详情 |
| | | * |
| | | * @param goodsIdList |
| | | * @return |
| | | */ |
| | | public static List<VIPGoodsInfo> getGoodsDetail(List<String> goodsIdList) { |
| | | JSONObject params = new JSONObject(); |
| | | params.put("goodsIdList", goodsIdList); |
| | | params.put("requestId", UUID.randomUUID()); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "getByGoodsIds", |
| | | JSONObject.fromObject(params)); |
| | | System.out.println(result); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | Gson gson = new Gson(); |
| | | List<VIPGoodsInfo> goodsList = new ArrayList<>(); |
| | | if (resultJson.optInt("returnCode") == 0) { |
| | | JSONArray array = resultJson.optJSONArray("result"); |
| | | for (int i = 0; i < array.size(); i++) { |
| | | VIPGoodsInfo info = gson.fromJson(array.optJSONObject(i).toString(), VIPGoodsInfo.class); |
| | | if (info != null) |
| | | goodsList.add(info); |
| | | } |
| | | return goodsList; |
| | | } |
| | | return null; |
| | | } |
| | | return new VIPSearchResult(goodsList, count); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取商品详情 |
| | | * |
| | | * @param goodsId |
| | | * @return |
| | | */ |
| | | public static VIPGoodsInfo getGoodsDetail(String goodsId) { |
| | | List<String> goodsIdList = new ArrayList<>(); |
| | | goodsIdList.add(goodsId); |
| | | List<VIPGoodsInfo> goodsList = getGoodsDetail(goodsIdList); |
| | | if (goodsList == null || goodsList.size() == 0) |
| | | return null; |
| | | return goodsList.get(0); |
| | | } |
| | | |
| | | public static VipShopQueryOrderResultDTO getOrderList(VipShopOrderQueryModel query) { |
| | | JSONObject params = JSONObject.fromObject(new Gson().toJson(query)); |
| | | params.put("requestId", UUID.randomUUID()); |
| | | JSONObject root = new JSONObject(); |
| | | root.put("queryModel", params); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionOrderService", "orderList", root); |
| | | System.out.println(result); |
| | | JSONObject json = JSONObject.fromObject(result); |
| | | if (json.optInt("returnCode") == 0) { |
| | | JSONObject resultJson = json.optJSONObject("result"); |
| | | int total = resultJson.optInt("total"); |
| | | JSONArray array = JSONArray.fromObject(resultJson.optJSONArray("orderInfoList")); |
| | | if (array != null) { |
| | | Type type = new TypeToken<ArrayList<VipShopOrder>>() { |
| | | }.getType(); |
| | | List<VipShopOrder> orderList = new Gson().fromJson(array.toString(), type); |
| | | return new VipShopQueryOrderResultDTO(orderList, total); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | public static VIPSearchResult getGoodsList(int channelType, int page) { |
| | | Map<String, String> taskParams = new HashMap<>(); |
| | | taskParams.put("channelType", channelType + ""); |
| | | taskParams.put("page", page + ""); |
| | | taskParams.put("pageSize", 20 + ""); |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | |
| | | |
| | | JSONObject root = new JSONObject(); |
| | | root.put("request", JSONObject.fromObject(taskParams)); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "goodsList", root); |
| | | System.out.println(result); |
| | | List<VIPGoodsInfo> goodsList = new ArrayList<>(); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | Gson gson = new Gson(); |
| | | if (resultJson.optInt("returnCode") == 0) { |
| | | resultJson = resultJson.optJSONObject("result"); |
| | | int count = resultJson.optInt("total"); |
| | | JSONArray array = resultJson.optJSONArray("goodsInfoList"); |
| | | for (int i = 0; i < array.size(); i++) { |
| | | VIPGoodsInfo info = gson.fromJson(array.optJSONObject(i).toString(), VIPGoodsInfo.class); |
| | | if (info != null) |
| | | goodsList.add(info); |
| | | } |
| | | return new VIPSearchResult(goodsList, count); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 商品ID转链 |
| | | * |
| | | * @param goodsId |
| | | */ |
| | | public static VIPConvertResultDTO convertLink(String goodsId, String tag) { |
| | | |
| | | if (CONVERT_DINGDANXIA) { |
| | | return DingDanXiaApiUtil.convertLink(goodsId, tag); |
| | | } |
| | | |
| | | JSONObject taskParams = new JSONObject(); |
| | | JSONArray goodsIdArray = new JSONArray(); |
| | | goodsIdArray.add(goodsId); |
| | | taskParams.put("goodsIdList", goodsIdArray); |
| | | taskParams.put("chanTag", tag); |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionUrlService", "genByGoodsId", |
| | | JSONObject.fromObject(taskParams)); |
| | | System.out.println(result); |
| | | JSONObject resultJSON = JSONObject.fromObject(result); |
| | | if (resultJSON.optInt("returnCode") == 0) { |
| | | String re = resultJSON.optJSONObject("result").optJSONArray("urlInfoList").optJSONObject(0).toString(); |
| | | return new Gson().fromJson(re, VIPConvertResultDTO.class); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | public static VIPConvertResultDTO convertLinkByUrl(String url, String tag) { |
| | | |
| | | if (CONVERT_DINGDANXIA) { |
| | | return DingDanXiaApiUtil.convertLinkByUrl(url, tag); |
| | | } |
| | | JSONObject taskParams = new JSONObject(); |
| | | JSONArray urlArray = new JSONArray(); |
| | | urlArray.add(url); |
| | | taskParams.put("urlList", urlArray); |
| | | if (tag != null) { |
| | | taskParams.put("chanTag", tag); |
| | | } |
| | | taskParams.put("requestId", System.currentTimeMillis() + "_" + UUID.randomUUID()); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionUrlService", "genByVIPUrl", |
| | | JSONObject.fromObject(taskParams)); |
| | | System.out.println(result); |
| | | JSONObject resultJSON = JSONObject.fromObject(result); |
| | | if (resultJSON.optInt("returnCode") == 0) { |
| | | String re = resultJSON.optJSONObject("result").optJSONArray("urlInfoList").optJSONObject(0).toString(); |
| | | return new Gson().fromJson(re, VIPConvertResultDTO.class); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 根据商品ID获取商品详情 |
| | | * |
| | | * @param goodsIdList |
| | | * @return |
| | | */ |
| | | public static List<VIPGoodsInfo> getGoodsDetail(List<String> goodsIdList) { |
| | | JSONObject params = new JSONObject(); |
| | | params.put("goodsIdList", goodsIdList); |
| | | params.put("requestId", UUID.randomUUID()); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionGoodsService", "getByGoodsIds", |
| | | JSONObject.fromObject(params)); |
| | | System.out.println(result); |
| | | JSONObject resultJson = JSONObject.fromObject(result); |
| | | Gson gson = new Gson(); |
| | | List<VIPGoodsInfo> goodsList = new ArrayList<>(); |
| | | if (resultJson.optInt("returnCode") == 0) { |
| | | JSONArray array = resultJson.optJSONArray("result"); |
| | | for (int i = 0; i < array.size(); i++) { |
| | | VIPGoodsInfo info = gson.fromJson(array.optJSONObject(i).toString(), VIPGoodsInfo.class); |
| | | if (info != null) |
| | | goodsList.add(info); |
| | | } |
| | | return goodsList; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取商品详情 |
| | | * |
| | | * @param goodsId |
| | | * @return |
| | | */ |
| | | public static VIPGoodsInfo getGoodsDetail(String goodsId) { |
| | | List<String> goodsIdList = new ArrayList<>(); |
| | | goodsIdList.add(goodsId); |
| | | List<VIPGoodsInfo> goodsList = getGoodsDetail(goodsIdList); |
| | | if (goodsList == null || goodsList.size() == 0) |
| | | return null; |
| | | return goodsList.get(0); |
| | | } |
| | | |
| | | public static VipShopQueryOrderResultDTO getOrderList(VipShopOrderQueryModel query) { |
| | | JSONObject params = JSONObject.fromObject(new Gson().toJson(query)); |
| | | params.put("requestId", UUID.randomUUID()); |
| | | JSONObject root = new JSONObject(); |
| | | root.put("queryModel", params); |
| | | String result = baseRequest("com.vip.adp.api.open.service.UnionOrderService", "orderList", root); |
| | | System.out.println(result); |
| | | JSONObject json = JSONObject.fromObject(result); |
| | | if (json.optInt("returnCode") == 0) { |
| | | JSONObject resultJson = json.optJSONObject("result"); |
| | | int total = resultJson.optInt("total"); |
| | | JSONArray array = JSONArray.fromObject(resultJson.optJSONArray("orderInfoList")); |
| | | if (array != null) { |
| | | Type type = new TypeToken<ArrayList<VipShopOrder>>() { |
| | | }.getType(); |
| | | List<VipShopOrder> orderList = new Gson().fromJson(array.toString(), type); |
| | | return new VipShopQueryOrderResultDTO(orderList, total); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | convertLinkByUrl("https://t.vip.com/T7RyKKCJLj9", "437032"); |
| | | } |
| | | |
| | | } |