admin
2020-06-11 d3a5dfe218fdee554decdd58a3c995e03bc1dec1
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package com.yeshi.fanli.util.aitaoker;
 
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
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.yeshi.utils.HttpUtil;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dto.aitaoker.QrcodeLoginDTO;
import com.yeshi.fanli.dto.aitaoker.RobotInfoDTO;
import com.yeshi.fanli.dto.aitaoker.WeiXinGroupDTO;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class AitaokerApiUtil {
    public static String APP_KEY = "1077080250";
    public static String SECRET_KEY = "7c6118bd-7aa5-65b8-c6d4-058728e9446f";
    // 请求连接
    private static String SERVER_URL = "http://router.itaoke.org/api";
    
    
    private static String baseRequest(String method, Map<String, String> param) {
        Map<String, String> baseMap = new HashMap<>();
        baseMap.put("app_key", APP_KEY);
        baseMap.put("v", "1.0");
        baseMap.put("format", "json");
        baseMap.put("sign_method", "md5");
        baseMap.put("timestamp", System.currentTimeMillis()+ "");
        baseMap.put("method", method);
        baseMap.put("domain", "hi.flqapp.com");
        baseMap.put("client", "113.249.194.232");
        baseMap.put("partner_id", "top-sdk-php-20190618");
        String url = combinedUrl(baseMap);
        
        if (param != null) {
            Iterator<String> its = param.keySet().iterator();
            while (its.hasNext()) {
                String key = its.next();
                baseMap.put(key, param.get(key));
            }
        }
        url+= "sign=" + getSign(baseMap);
        return HttpUtil.post(url, param);
    }
    
    /**
     * 拼接所有系统参数包括sign的url
     * @param params
     * @return
     */
    private static String combinedUrl (Map<String, String> params) {
        String url = SERVER_URL + "?";
        Iterator<String> its = params.keySet().iterator();
        while (its.hasNext()) {
            String key = its.next();
            try {
                url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return url;
    }
    
    /**
     * 获取签名
     * @param params
     * @return
     */
    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));
        }
        Collections.sort(list);
        
        String str = "";
        for (String st : list) {
            str += st;
        }
        return StringUtil.Md5(SECRET_KEY + str + SECRET_KEY).toUpperCase();
    }
    
    /**
     * 获取登录二维码
     * @param robotId
     * @return
     */
    public static QrcodeLoginDTO getQrcodeMaclogin(int robotId) {
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        String result = baseRequest("itaoke.robot.qrcode.maclogin", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        resultJson = resultJson.optJSONObject("data");
        if (resultJson != null) {
            Type type = new TypeToken<QrcodeLoginDTO>() {}.getType();
            return new Gson().fromJson(resultJson.toString(), type);
        }
        return null;
    }
    
    
    /**
     *   检查是否登陆(真正的登录接口)
     * @param robotId 机器人id
     * @param wId  获取二维码接口返回的微信实例id
     * @return
     */
    public static QrcodeLoginDTO getQrcodeMacloginCheck(int robotId, String wId) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("uuid", wId);
        // 请求结果
        String result = baseRequest("itaoke.robot.async.mlogin", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            resultJson = resultJson.optJSONObject("data");
            if (resultJson != null) {
                Type type = new TypeToken<QrcodeLoginDTO>() {}.getType();
                return new Gson().fromJson(resultJson.toString(), type);
            }
        }
        return null;
    }
    
    /**
     *   检查是否在线
     * @param robotId 机器人id
     * @return
     */
    public static boolean onlineCheck(int robotId) {
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        String result = baseRequest("itaoke.robot.check.online", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            resultJson = resultJson.optJSONObject("data");
            if (resultJson != null) {
                return resultJson.optBoolean("isOnline");
            }
        }
        return false;
    }
    
    
    /**
     * 4.强制下线
     * @param robotId 机器人id
     * @return
     */
    public static boolean macloginOffline(int robotId) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        // 请求结果
        String result = baseRequest("itaoke.robot.force.offline", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            return true;
        }
        return false;
    }
    
    
    /**
     * 创建机器人
     * @param month 月数
     * @param robotType 机器人类型 1 发单机器人 2转发机器人 3 返利机器人 4全能机器人 5小型机器人 6发圈机器人
     * @param wechatrobot  微信号
     * @param agentUid  代理id
     * @return
     */
    public static RobotInfoDTO robotCreate(int month , int robotType, String wechatrobot, String agentUid) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("month", month  +"");
        map.put("robot_type", robotType + "");
        map.put("wechatrobot", wechatrobot);
        if(!StringUtil.isNullOrEmpty(agentUid)) 
            map.put("agent_uid", agentUid);
        
        // 请求结果
        String result = baseRequest("itaoke.robot.create.get", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("code"))) {
            resultJson = resultJson.optJSONObject("data");
            Type type = new TypeToken<RobotInfoDTO>() {}.getType();
            return new Gson().fromJson(resultJson.toString(), type);
        }
        return null;
    }
    
    
    
    /**
     * 机器人更换微信号
     * @param robotId 机器人id
     * @param wxid 微信号
     * @return
     */
    public static RobotInfoDTO robotChangeWeiXin(int robotId, String wxid) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("wechatrobot", wxid);
        // 请求结果
        String result = baseRequest("itaoke.robot.change.get", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("code"))) {
            resultJson = resultJson.optJSONObject("data");
            Type type = new TypeToken<RobotInfoDTO>() {}.getType();
            return new Gson().fromJson(resultJson.toString(), type);
        }
        return null;
    }
    
    
    /**
     * 机器人续费
     * @param robotId 机器人id
     * @param wxid 微信号
     * @return
     */
    public static RobotInfoDTO robotRenewals(int robotId, int month) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("month", month +"");
        // 请求结果
        String result = baseRequest("itaoke.robot.change.get", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("code"))) {
            resultJson = resultJson.optJSONObject("data");
            Type type = new TypeToken<RobotInfoDTO>() {}.getType();
            return new Gson().fromJson(resultJson.toString(), type);
        }
        return null;
    }
    
    
    /**
     * 删除机器人
     * @param robotId
     * @return
     */
    public static boolean robotDelete(int robotId) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        // 请求结果
        String result = baseRequest("itaoke.robot.delete.get", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("code"))) {
            return true;
        }
        return false;
    }
    
    
    /**
     * 获取群列表
     * @param robotId
     * @return
     */
    public static List<WeiXinGroupDTO> getContract(int robotId) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        // 请求结果
        String result = baseRequest("itaoke.robot.room.list", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            JSONArray groupArray = resultJson.optJSONArray("data");
            if (groupArray != null && groupArray.size() > 0) {
                Type type = new TypeToken<ArrayList<WeiXinGroupDTO>>() {
                }.getType();
                List<WeiXinGroupDTO> goodsList = new Gson().fromJson(groupArray.toString(), type);
                return goodsList;
            }
        }
        return null;
    }
    
    
    /**
     * 发朋友圈 
     * @param robotId
     * @param content
     * @param picUrl 图片url,多个请用;分隔
     * @return
     */
    public static String macsendCircle(int robotId, String content, String picUrl) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("pic_url", picUrl);
        map.put("content", content);
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.circle", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            resultJson = resultJson.optJSONObject("data");
            if (resultJson != null) {
                resultJson = resultJson.optJSONObject("object");
                if (resultJson != null) {
                    return resultJson.optString("id");
                }
            }
        }
        return null;
    }
    
    
    /**
     * 朋友圈发送视频
     * @param robotId
     * @param videoPath 视频地址
     * @param thumbPath 封面url
     * @return
     */
    public static String macsendCircleVideo(int robotId, String videoPath, String thumbPath) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("video_path", videoPath);
        map.put("thumb_path", thumbPath);
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.videocircle", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("1000".equals(resultJson.optString("code"))) {
            resultJson = resultJson.optJSONObject("data");
            return resultJson.optString("id");
        }
        return null;
    }
    
    
    /**
     * 朋友圈发送视频
     * @param robotId
     * @param videoPath 视频地址
     * @param thumbPath 封面url
     * @return
     */
    public static boolean macsendCircleComment(int robotId, String wxId, String msgId, String content) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("wx_id", wxId);
        map.put("msg_id", msgId);
        map.put("content", content);
        map.put("comment_id", "0");
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.circlecomment", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            return true;
        }
        return false;
    }
    
    
    
    /**
     *  发文本消息
     * @param robotId
     * @param wxId 微信群ID
     * @param content 内容
     * @return
     */
    public static boolean macsendText(int robotId, String toWxId, String content) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("toWxId", toWxId );
        map.put("content", content);
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.text", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if ("0000".equals(resultJson.optString("status"))) {
            return true;
        }
        return false;
    }
    
    
 
    /**
     * 发base64图
     * @param robotId
     * @param wxId
     * @param imgBase64
     * @return
     */
    public static boolean macsendImgBase64(int robotId, String toWxId, String imgBase64) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("toWxId", toWxId );
        map.put("base64_data", imgBase64);
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.base64", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") == 1000) {
            return true;
        }
        return false;
    }
    
    /**
     *   发链接消息
     * @param robotId
     * @param wxId 微信群ID
     * @param title 标题 
     * @param url 链接
     * @param description 描述
     * @param thumbUrl 图片
     * @return
     */
    public static boolean macsendCard(int robotId, String wxId, String title, String url,
            String description ,String thumbUrl) {
        // 请求参数
        Map<String, String> map = new HashMap<>();
        map.put("robot_id", robotId +"");
        map.put("wx_id", wxId);
        map.put("title", title);
        map.put("url", url);
        map.put("description", description);
        map.put("thumbUrl", thumbUrl);
        // 请求结果
        String result = baseRequest("itaoke.robot.macsend.card", map);
        JSONObject resultJson = JSONObject.fromObject(result);
        if (resultJson.optInt("code") == 1000) {
            return true;
        }
        return false;
    }
    
    
    
}