admin
2019-07-22 b4b421ba6b85b586f9b8171c876f716bab9840d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.yeshi.fanli.util.jd;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
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.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.JDOrderResult;
import com.yeshi.fanli.dto.jd.JDShopInfo;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.jd.JDOrder;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class JDApiUtil {
    public static String APP_ID = "1774659094";
    public static String APP_KEY = "7ba8e06b7d6fde3d6bd5db4b0026ecd7";
    public static String SECRET_KEY = "fb49bc6ecac5458ba5394fc2969d7c56";
    private static String SERVER_URL = "https://router.jd.com/api";
 
    public static Long POSITION_FANLI = 1834339426L;
    public static Long POSITION_SHARE = 1834289924L;
 
    // 订单查询类型
    public static int ORDER_TYPE_CREATETIME = 1;// 下单时间
    public static int ORDER_TYPE_FINISHTIME = 2;// 完成时间
    public static int ORDER_TYPE_UPDATETIME = 3;// 更新时间
 
    private static String post(String url, Map<String, String> params) {
        String baseUrl = url;
        List<String> paramsList = new ArrayList<>();
        if (params != null)
            for (Iterator<String> its = params.keySet().iterator(); its.hasNext();) {
                String key = its.next();
                try {
                    paramsList.add(key + "=" + URLEncoder.encode(params.get(key), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        baseUrl += "?" + StringUtil.concat(paramsList, "&");
        HttpClient client = new HttpClient();
        PostMethod pm = new PostMethod(baseUrl);
        try {
            client.executeMethod(pm);
            String result = pm.getResponseBodyAsString();
            return new String(result.getBytes("ISO-8859-1"), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    private static String getSign(Map<String, String> params) {
        List<String> list = new ArrayList<>();
        Iterator<String> its = params.keySet().iterator();
        while (its.hasNext()) {
            String key = its.next();
            list.add(key + params.get(key));
        }
        String str = "";
        Collections.sort(list);
        for (String st : list) {
            str += st;
        }
        return StringUtil.Md5(SECRET_KEY + str + SECRET_KEY).toUpperCase();
    }
 
    private static String baseRequest(String method, String accessToken, JSONObject params) {
        Map<String, String> baseMap = new HashMap<String, String>();
        baseMap.put("param_json", params.toString());
        baseMap.put("app_key", APP_KEY);
        baseMap.put("method", method);
        if (accessToken != null)
            baseMap.put("access_token", accessToken);
        baseMap.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
        baseMap.put("format", "json");
        baseMap.put("v", "1.0");
        baseMap.put("sign_method", "md5");
        baseMap.put("sign", getSign(baseMap));
        String result = post(SERVER_URL, baseMap);
        return result;
    }
 
    /**
     * 转链
     * 
     * @return
     */
    public static String convertLink(String materialId, String couponUrl, String pid) {
        JSONObject json = new JSONObject();
        json.put("materialId", materialId);
        json.put("siteId", APP_ID);
        if (couponUrl != null)
            json.put("couponUrl", couponUrl);
        // json.put("ext1", "437032_12");
        json.put("pid", pid);
 
        JSONObject root = new JSONObject();
        root.put("promotionCodeReq", json);
 
        String result = baseRequest("jd.union.open.promotion.common.get", null, json);
        JSONObject resultJson = JSONObject.fromObject(result);
        result = resultJson.optJSONObject("jd_union_open_promotion_common_get_response").optString("result");
        resultJson = JSONObject.fromObject(result);
        return resultJson.optJSONObject("data").optString("clickURL");
    }
 
    public static List<JDGoods> getGoodsDetail(List<Long> skuIdList) {
        List<JDGoods> list = new ArrayList<>();
        JSONObject json = new JSONObject();
        json.put("skuIds", StringUtil.concat(skuIdList, ","));
        String result = baseRequest("jd.union.open.goods.promotiongoodsinfo.query", null, json);
        JSONObject resultJson = JSONObject.fromObject(result);
        resultJson = resultJson.optJSONObject("jd_union_open_goods_promotiongoodsinfo_query_response");
        if (resultJson.optInt("code") == 0) {
            result = resultJson.optString("result");
            resultJson = JSONObject.fromObject(result);
            System.out.println(resultJson);
            JSONArray array = resultJson.optJSONArray("data");
            if (array != null)
                for (int i = 0; i < array.size(); i++) {
                    JDGoods goods = parseSimpleJDGoods(array.optJSONObject(i));
                    if (goods != null)
                        list.add(goods);
                }
        }
        return list;
    }
 
    private static JDGoods parseSimpleJDGoods(JSONObject json) {
        JDGoods goods = new JDGoods();
 
        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);
        goods.setComments(null);
        goods.setPrice(StringUtil.isNullOrEmpty(json.optString("wlUnitPrice"))
                ? new BigDecimal(json.optString("unitPrice")) : new BigDecimal(json.optString("wlUnitPrice")));
 
        JDCommissionInfo commissionInfo = new JDCommissionInfo();
        commissionInfo.setCommissionShare(new BigDecimal(json.optString("commisionRatioWl")));
        commissionInfo.setCommission(MoneyBigDecimalUtil
                .div(goods.getPrice().multiply(commissionInfo.getCommissionShare()), new BigDecimal(100)));
 
        goods.setCommissionInfo(commissionInfo);
        goods.setCouponInfo(null);
        goods.setGoodCommentsShare(null);
        List<String> imageList = new ArrayList<>();
        imageList.add(json.optString("imgUrl"));
        goods.setImageList(imageList);
        goods.setInOrderCount30Days(0L);
        goods.setIsHot(null);
        goods.setMaterialUrl(json.optString("materialUrl"));
        goods.setOwner(null);
        goods.setPinGouInfo(null);
 
        JDShopInfo shopInfo = new JDShopInfo();
        shopInfo.setShopId(json.optInt("shopId"));
        shopInfo.setShopName(null);
        goods.setShopInfo(shopInfo);
        goods.setSkuId(json.optLong("skuId"));
        goods.setSkuName(json.optString("goodsName"));
        goods.setTotalCount(null);
 
        return goods;
    }
 
    public static JDGoods getGoodsDetail(Long skuId) {
        List<Long> skuIdList = new ArrayList<>();
        skuIdList.add(skuId);
        List<JDGoods> list = getGoodsDetail(skuIdList);
        if (list != null && list.size() > 0)
            return list.get(0);
        return null;
    }
 
    public static JDOrderResult getOrder(int page, int pageSize, String 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", time);
 
        json.put("orderReq", orderReq);
        String result = baseRequest("jd.union.open.order.query", null, json);
        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) {
                String date = root.optJSONArray("data").toString();
                Type typeToken = new TypeToken<List<JDOrder>>() {
                }.getType();
                List<JDOrder> orderList = new Gson().fromJson(date, typeToken);
                if (orderList != null)
                    for (JDOrder order : orderList) {
                        for (int i = 0; i < order.getOrderItemList().size(); i++) {
                            order.getOrderItemList().get(i)
                                    .setTradeId(order.getOrderId() + "-" + order.getOrderItemList().get(i).getSkuId());
                            order.getOrderItemList().get(i).setOrderId(order.getOrderId());
                            order.getOrderItemList().get(i).setOrderBy(i + 1);
                        }
                    }
                return new JDOrderResult(hasMore, orderList);
            }
        }
        return null;
    }
 
}