admin
2021-09-13 8ce7c720e4e7a604b0ff770349b5556f39d37759
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.yeshi.fanli.util.vipshop;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dto.vip.VIPConvertResultDTO;
import com.yeshi.fanli.dto.vip.VIPSearchFilter;
import com.yeshi.fanli.dto.vip.VIPSearchResult;
import com.yeshi.fanli.dto.vip.goods.VIPGoodsInfo;
import com.yeshi.fanli.dto.vipshop.VipShopOrderQueryModel;
import com.yeshi.fanli.dto.vipshop.VipShopQueryOrderResultDTO;
import com.yeshi.fanli.entity.vipshop.VipShopOrder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.*;
 
/**
 * 唯品会接口服务商接口
 * 权限文档: https://vop.vip.com/home#/console/app/detail/permission/2343e85c
 *
 * @author Administrator
 */
public class VipShopSCApiUtil {
 
    private final static String appKey = "2343e85c";
    private final static String appSecret = "16AC47F90859E29078FD59E5A951E335";
 
 
    /**
     * 文档:https://vop.vip.com/doccenter/viewdoc/33
     * 授权链接:https://auth.vip.com/oauth2/authorize?client_id=9870c528&response_type=code&redirect_uri=http://www.baidu.com#
     *
     * @param code
     * @return
     */
    private static String getAccessToken(String code) {
        //EFCD81A1A311349E2043F9D97B456B277165D3FE
        Map<String, String> params = new HashMap<>();
        params.put("client_id", appKey);
        params.put("client_secret", appSecret);
        params.put("grant_type", "authorization_code");
        params.put("redirect_uri", "http://www.baidu.com");
        params.put("request_client_ip", "192.168.3.122");
        params.put("code", code);
 
        List<String> list = new ArrayList<>();
        for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            list.add(key + "=" + URLEncoder.encode(params.get(key)));
        }
        String url = "https://auth.vip.com/oauth2/token?" + StringUtil.concat(list, "&");
        String result = HttpUtil.post(url);
        JSONObject data = JSONObject.fromObject(result);
        return data.optString("access_token");
    }
 
    /**
     * 获取签名
     *
     * @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 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);
        params.put("accessToken", "EFCD81A1A311349E2043F9D97B456B277165D3FE");
        return params;
    }
 
    /**
     * 基础请求
     *
     * @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;
    }
 
    @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;
    }
 
 
    /**
     * 商品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", "genByGoodsIdWithOauth",
                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) {
        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) {
        VipShopOrderQueryModel model = new VipShopOrderQueryModel();
        model.setPage(1);
        model.setPageSize(20);
        model.setOrderSnList(Arrays.asList(new String[]{"21081436395569", "21072601507337"}));
        VipShopQueryOrderResultDTO dto = getOrderList(model);
        System.out.println(dto);
    }
 
 
}