1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.io.InputStream;
4 import java.util.ArrayList;
5 import java.util.Date;
6 import java.util.List;
7
8 import com.alibaba.fastjson.JSON;
9 import com.alibaba.fastjson.JSONObject;
10 import com.alibaba.fastjson.TypeReference;
11 import com.foxinmy.weixin4j.exception.WeixinException;
12 import com.foxinmy.weixin4j.http.MimeType;
13 import com.foxinmy.weixin4j.http.apache.content.InputStreamBody;
14 import com.foxinmy.weixin4j.http.apache.mime.FormBodyPart;
15 import com.foxinmy.weixin4j.http.weixin.ApiResult;
16 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
17 import com.foxinmy.weixin4j.model.Token;
18 import com.foxinmy.weixin4j.mp.model.KfAccount;
19 import com.foxinmy.weixin4j.mp.model.KfChatRecord;
20 import com.foxinmy.weixin4j.mp.model.KfOnlineAccount;
21 import com.foxinmy.weixin4j.mp.model.KfSession;
22 import com.foxinmy.weixin4j.mp.model.KfSession.KfSessionCounter;
23 import com.foxinmy.weixin4j.token.TokenManager;
24 import com.foxinmy.weixin4j.util.FileUtil;
25 import com.foxinmy.weixin4j.util.ObjectId;
26 import com.foxinmy.weixin4j.util.StringUtil;
27
28
29
30
31
32
33
34
35
36
37 public class CustomApi extends MpApi {
38
39 private final TokenManager tokenManager;
40
41 public CustomApi(TokenManager tokenManager) {
42 this.tokenManager = tokenManager;
43 }
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public List<KfChatRecord> getKfChatRecord(Date startTime, Date endTime,
62 int number) throws WeixinException {
63 List<KfChatRecord> records = new ArrayList<KfChatRecord>();
64 String kf_chatrecord_uri = getRequestUri("kf_chatrecord_uri");
65 Token token = tokenManager.getCache();
66 JSONObject obj = new JSONObject();
67 obj.put("starttime", startTime.getTime() / 1000);
68 obj.put("endtime", endTime.getTime() / 1000);
69 obj.put("msgid", "1");
70 obj.put("number", Math.min(10000, number));
71 JSONObject result = null;
72 do {
73 WeixinResponse response = weixinExecutor.post(
74 String.format(kf_chatrecord_uri, token.getAccessToken()),
75 obj.toJSONString());
76 result = response.getAsJson();
77 String text = result.getString("recordlist");
78 if (StringUtil.isBlank(text) || "[]".equals(text)) {
79 break;
80 }
81 records.addAll(JSON.parseArray(text, KfChatRecord.class));
82 obj.put("msgid", result.getString("msgid"));
83 } while (obj.getIntValue("number") == result.getIntValue("number"));
84 return records;
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public List<KfAccount> listKfAccount() throws WeixinException {
98 Token token = tokenManager.getCache();
99 String kf_list_uri = getRequestUri("kf_list_uri");
100 WeixinResponse response = weixinExecutor.get(String.format(kf_list_uri,
101 token.getAccessToken()));
102 String text = response.getAsJson().getString("kf_list");
103 return JSON.parseArray(text, KfAccount.class);
104 }
105
106
107
108
109
110
111
112
113
114
115
116 public List<KfOnlineAccount> listOnlineKfAccount() throws WeixinException {
117 Token token = tokenManager.getCache();
118 String kf_onlinelist_uri = getRequestUri("kf_onlinelist_uri");
119 WeixinResponse response = weixinExecutor.get(String.format(
120 kf_onlinelist_uri, token.getAccessToken()));
121 String text = response.getAsJson().getString("kf_online_list");
122 return JSON.parseArray(text, KfOnlineAccount.class);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 public ApiResult createKfAccount(String id, String name)
140 throws WeixinException {
141 JSONObject obj = new JSONObject();
142 obj.put("kf_account", id);
143 obj.put("nickname", name);
144 String kf_create_uri = getRequestUri("kf_create_uri");
145 Token token = tokenManager.getCache();
146 WeixinResponse response = weixinExecutor.post(
147 String.format(kf_create_uri, token.getAccessToken()),
148 obj.toJSONString());
149 return response.getAsResult();
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public ApiResult updateKfAccount(String id, String name)
167 throws WeixinException {
168 JSONObject obj = new JSONObject();
169 obj.put("kf_account", id);
170 obj.put("nickname", name);
171 String kf_update_uri = getRequestUri("kf_update_uri");
172 Token token = tokenManager.getCache();
173 WeixinResponse response = weixinExecutor.post(
174 String.format(kf_update_uri, token.getAccessToken()),
175 obj.toJSONString());
176 return response.getAsResult();
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public ApiResult inviteKfAccount(String kfAccount, String inviteAccount)
195 throws WeixinException {
196 JSONObject obj = new JSONObject();
197 obj.put("kf_account", kfAccount);
198 obj.put("invite_wx", inviteAccount);
199 String kf_invite_uri = getRequestUri("kf_invite_uri");
200 Token token = tokenManager.getCache();
201 WeixinResponse response = weixinExecutor.post(
202 String.format(kf_invite_uri, token.getAccessToken()),
203 obj.toJSONString());
204 return response.getAsResult();
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public ApiResult uploadKfAvatar(String accountId, InputStream is,
223 String fileName) throws WeixinException {
224 if (StringUtil.isBlank(fileName)) {
225 fileName = ObjectId.get().toHexString();
226 }
227 if (StringUtil.isBlank(FileUtil.getFileExtension(fileName))) {
228 fileName = String.format("%s.jpg", fileName);
229 }
230 MimeType mimeType = new MimeType("image",
231 FileUtil.getFileExtension(fileName));
232 Token token = tokenManager.getCache();
233 String kf_avatar_uri = getRequestUri("kf_avatar_uri");
234 WeixinResponse response = weixinExecutor
235 .post(String.format(kf_avatar_uri, token.getAccessToken(),
236 accountId), new FormBodyPart("media",
237 new InputStreamBody(is, mimeType.toString(), fileName)));
238
239 return response.getAsResult();
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253 public ApiResult deleteKfAccount(String id) throws WeixinException {
254 Token token = tokenManager.getCache();
255 String kf_delete_uri = getRequestUri("kf_delete_uri");
256 WeixinResponse response = weixinExecutor.get(String.format(
257 kf_delete_uri, token.getAccessToken(), id));
258
259 return response.getAsResult();
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 public ApiResult createKfSession(String userOpenId, String kfAccount,
282 String text) throws WeixinException {
283 Token token = tokenManager.getCache();
284 String kfsession_create_uri = getRequestUri("kfsession_create_uri");
285 JSONObject obj = new JSONObject();
286 obj.put("openid", userOpenId);
287 obj.put("kf_account", kfAccount);
288 obj.put("text", text);
289 WeixinResponse response = weixinExecutor.post(
290 String.format(kfsession_create_uri, token.getAccessToken()),
291 obj.toJSONString());
292
293 return response.getAsResult();
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 public ApiResult closeKfSession(String userOpenId, String kfAccount,
312 String text) throws WeixinException {
313 Token token = tokenManager.getCache();
314 String kfsession_close_uri = getRequestUri("kfsession_close_uri");
315 JSONObject obj = new JSONObject();
316 obj.put("openid", userOpenId);
317 obj.put("kf_account", kfAccount);
318 obj.put("text", text);
319 WeixinResponse response = weixinExecutor.post(
320 String.format(kfsession_close_uri, token.getAccessToken()),
321 obj.toJSONString());
322
323 return response.getAsResult();
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337
338 public KfSession getKfSession(String userOpenId) throws WeixinException {
339 Token token = tokenManager.getCache();
340 String kfsession_get_uri = getRequestUri("kfsession_get_uri");
341 WeixinResponse response = weixinExecutor.get(String.format(
342 kfsession_get_uri, token.getAccessToken(), userOpenId));
343
344 KfSession session = response
345 .getAsObject(new TypeReference<KfSession>() {
346 });
347 session.setUserOpenId(userOpenId);
348 return session;
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363 public List<KfSession> listKfSession(String kfAccount)
364 throws WeixinException {
365 Token token = tokenManager.getCache();
366 String kfsession_list_uri = getRequestUri("kfsession_list_uri");
367 WeixinResponse response = weixinExecutor.get(String.format(
368 kfsession_list_uri, token.getAccessToken(), kfAccount));
369
370 List<KfSession> sessionList = JSON.parseArray(response.getAsJson()
371 .getString("sessionlist"), KfSession.class);
372 return sessionList;
373 }
374
375
376
377
378
379
380
381
382
383
384
385
386
387 public KfSessionCounter listKfWaitSession() throws WeixinException {
388 Token token = tokenManager.getCache();
389 String kfsession_wait_uri = getRequestUri("kfsession_wait_uri");
390 WeixinResponse response = weixinExecutor.get(String.format(
391 kfsession_wait_uri, token.getAccessToken()));
392
393 return response.getAsObject(new TypeReference<KfSessionCounter>() {
394 });
395 }
396 }