package com.yeshi.fanli.util.suning;
|
|
import java.io.IOException;
|
import java.lang.reflect.Type;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.HttpException;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
|
import com.google.gson.Gson;
|
import com.google.gson.reflect.TypeToken;
|
import com.yeshi.fanli.dto.suning.SuningGoodsInfo;
|
import com.yeshi.fanli.dto.suning.SuningOrderQueryResultDTO;
|
import com.yeshi.fanli.dto.suning.SuningQueryModel;
|
import com.yeshi.fanli.entity.suning.SuningOrderInfo;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.TimeUtil;
|
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
|
public class SuningApiUtil {
|
|
public final static String APP_SECRET = "a217bc7018fd8ca112bb60f09057d996";
|
public final static String APP_KEY = "6fca015e83eff7ef150bebcad418ea32";
|
|
public final static String PID_SHARE = "666527";
|
public final static String PID_BUY = "666526";
|
|
private static String post(String url, String entity, Map<String, String> headers) {
|
String charset = "utf-8";
|
HttpClient client = new HttpClient();
|
PostMethod method = new PostMethod(url);
|
method.addRequestHeader("Content-Type", "text/html;charset=" + charset);
|
method.setRequestHeader("Content-Type", "text/html;charset=" + charset);
|
method.setRequestBody(entity);
|
for (Iterator<String> its = headers.keySet().iterator(); its.hasNext();) {
|
String key = its.next();
|
method.addRequestHeader(key, headers.get(key));
|
}
|
|
try {
|
client.executeMethod(method);
|
return method.getResponseBodyAsString();
|
} catch (HttpException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
private static String getSign(Map<String, String> baseParams, String resParams) {
|
StringBuffer sb = new StringBuffer();
|
try {
|
sb.append(APP_SECRET);
|
sb.append(baseParams.get("appMethod"));
|
sb.append(baseParams.get("appRequestTime"));
|
sb.append(baseParams.get("appKey"));
|
sb.append(baseParams.get("versionNo"));
|
} catch (Exception e) {
|
|
}
|
|
try {
|
sb.append(StringUtil.getBase64String(resParams));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return StringUtil.Md5(sb.toString());
|
}
|
|
private static String baseRequest(String method, JSONObject params) {
|
Map<String, String> baseParams = new HashMap<>();
|
baseParams.put("appMethod", method);
|
baseParams.put("appRequestTime", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
|
baseParams.put("format", "json");
|
baseParams.put("appKey", APP_KEY);
|
baseParams.put("versionNo", "v1.2");
|
String sign = getSign(baseParams, params.toString());
|
baseParams.put("signInfo", sign);
|
String result = post("https://open.suning.com/api/http/sopRequest/" + method, params.toString(), baseParams);
|
System.out.println(result);
|
return result;
|
}
|
|
private static String baseRequest(String method, String key, JSONObject params) {
|
JSONObject keyJSON = new JSONObject();
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext();) {
|
String k = its.next();
|
keyJSON.put(k, params.get(k));
|
}
|
JSONObject content = new JSONObject();
|
content.put(key, keyJSON);
|
JSONObject body = new JSONObject();
|
body.put("sn_body", content);
|
JSONObject request = new JSONObject();
|
request.put("sn_request", body);
|
return baseRequest(method, request);
|
}
|
|
/**
|
* 查询商品
|
* @Title: searchGoods
|
* @Description:
|
* @param model
|
* @return
|
* List<SuningGoodsInfo> 返回类型
|
* @throws
|
*/
|
public static List<SuningGoodsInfo> searchGoods(SuningQueryModel model) {
|
model.setPicWidth(320);
|
model.setPicHeight(320);
|
String key = "querySearchcommoditynew";
|
String json = new Gson().toJson(model);
|
String result = baseRequest("suning.netalliance.searchcommoditynew.query", key, JSONObject.fromObject(json));
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
JSONArray array = resultJSON.optJSONObject("sn_responseContent").optJSONObject("sn_body").optJSONObject(key)
|
.optJSONArray("commodityList");
|
if (array != null) {
|
Type type = new TypeToken<ArrayList<SuningGoodsInfo>>() {
|
}.getType();
|
List<SuningGoodsInfo> goodsList = new Gson().fromJson(array.toString(), type);
|
return goodsList;
|
}
|
|
return null;
|
}
|
|
public static SuningGoodsInfo getGoodsDetail(String goodsCode, String supplierCode) {
|
JSONObject model = new JSONObject();
|
model.put("commodityStr", goodsCode + "-" + supplierCode);
|
model.put("picWidth", 600);
|
model.put("picHeight", 600);
|
|
String key = "queryCommoditydetail";
|
String result = baseRequest("suning.netalliance.commoditydetail.query", key, model);
|
System.out.println(result);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
JSONArray array = resultJSON.optJSONObject("sn_responseContent").optJSONObject("sn_body").optJSONArray(key);
|
if (array != null && array.size() > 0) {
|
return new Gson().fromJson(array.optJSONObject(0).toString(), SuningGoodsInfo.class);
|
}
|
return null;
|
}
|
|
public static String convertLink(String productUrl, String quanUrl, String pid, String subUser) {
|
JSONObject model = new JSONObject();
|
if (productUrl != null)
|
model.put("productUrl", productUrl);
|
|
if (!StringUtil.isNullOrEmpty(quanUrl))
|
model.put("quanUrl", quanUrl);
|
|
if (pid != null)
|
model.put("promotionId", pid);
|
|
if (subUser != null)
|
model.put("subUser", subUser);
|
|
String key = "getExtensionlink";
|
String result = baseRequest("suning.netalliance.extensionlink.get", key, model);
|
System.out.println(result);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
JSONObject json = resultJSON.optJSONObject("sn_responseContent").optJSONObject("sn_body").optJSONObject(key);
|
String link = json.optString("shortLink");
|
return link;
|
}
|
|
/**
|
* 0
|
* @Title: getOrderList
|
* @Description:
|
* @param startTime
|
* @param endTime
|
* @param page
|
* @param orderLineStatus
|
* @return
|
* SuningOrderQueryResultDTO 返回类型
|
* @throws
|
*/
|
public static SuningOrderQueryResultDTO getOrderList(Date startTime, Date endTime, int page, int orderLineStatus) {
|
JSONObject model = new JSONObject();
|
model.put("startTime", TimeUtil.getGernalTime(startTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
|
model.put("endTime", TimeUtil.getGernalTime(endTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
|
model.put("pageSize", 20);
|
model.put("pageNo", page);
|
model.put("orderLineStatus", orderLineStatus);
|
|
String key = "queryOrder";
|
String result = baseRequest("suning.netalliance.order.query", key, model);
|
JSONObject resultJSON = JSONObject.fromObject(result);
|
resultJSON = resultJSON.optJSONObject("sn_responseContent");
|
JSONArray array = resultJSON.optJSONObject("sn_body").optJSONArray(key);
|
Gson gson = new Gson();
|
|
Type type = new TypeToken<ArrayList<SuningOrderInfo>>() {
|
}.getType();
|
|
if (array != null) {
|
List<SuningOrderInfo> orderList = new ArrayList<>();
|
for (int i = 0; i < array.size(); i++) {
|
JSONObject data = array.optJSONObject(i);
|
JSONArray itemArray = data.optJSONArray("orderDetail");
|
List<SuningOrderInfo> tempList = gson.fromJson(itemArray.toString(), type);
|
for(SuningOrderInfo info:tempList){
|
info.setOrderCode(data.optString("orderCode"));
|
orderList.add(info);
|
}
|
}
|
|
return new SuningOrderQueryResultDTO(resultJSON.optJSONObject("sn_head").optInt("totalSize"), orderList);
|
}
|
|
return null;
|
}
|
|
}
|