1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.io.InputStream;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import com.alibaba.fastjson.JSON;
8 import com.alibaba.fastjson.JSONArray;
9 import com.alibaba.fastjson.JSONObject;
10 import com.foxinmy.weixin4j.exception.WeixinException;
11 import com.foxinmy.weixin4j.http.weixin.ApiResult;
12 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
13 import com.foxinmy.weixin4j.model.Token;
14 import com.foxinmy.weixin4j.qy.model.OUserInfo;
15 import com.foxinmy.weixin4j.qy.model.Party;
16 import com.foxinmy.weixin4j.qy.model.User;
17 import com.foxinmy.weixin4j.qy.type.InviteType;
18 import com.foxinmy.weixin4j.qy.type.UserStatus;
19 import com.foxinmy.weixin4j.token.TokenManager;
20 import com.foxinmy.weixin4j.util.NameValue;
21 import com.foxinmy.weixin4j.util.StringUtil;
22
23
24
25
26
27
28
29
30
31
32
33 public class UserApi extends QyApi {
34 private final MediaApi mediaApi;
35 private final PartyApi partyApi;
36 private final TokenManager tokenManager;
37
38 public UserApi(TokenManager tokenManager) {
39 this.tokenManager = tokenManager;
40 this.mediaApi = new MediaApi(tokenManager);
41 this.partyApi = new PartyApi(tokenManager);
42 }
43
44
45
46
47
48
49
50
51
52
53
54 public ApiResult createUser(User user) throws WeixinException {
55 String user_create_uri = getRequestUri("user_create_uri");
56 return excute(user_create_uri, user, null);
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public ApiResult createUser(User user, InputStream avatar)
72 throws WeixinException {
73 String user_create_uri = getRequestUri("user_create_uri");
74 return excute(user_create_uri, user, avatar);
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public ApiResult updateUser(User user) throws WeixinException {
88 String user_update_uri = getRequestUri("user_update_uri");
89 return excute(user_update_uri, user, null);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public ApiResult updateUser(User user, InputStream avatar)
105 throws WeixinException {
106 String user_update_uri = getRequestUri("user_update_uri");
107 return excute(user_update_uri, user, avatar);
108 }
109
110 private ApiResult excute(String uri, User user, InputStream avatar)
111 throws WeixinException {
112 JSONObject obj = (JSONObject) JSON.toJSON(user);
113 Object val = obj.remove("extattr");
114 if (val != null) {
115 JSONObject attrs = new JSONObject();
116 attrs.put("attrs", val);
117 obj.put("extattr", attrs);
118 }
119 val = obj.remove("status");
120 if (val != null) {
121 obj.put("enable", val);
122 }
123 if (avatar != null) {
124 obj.put("avatar_mediaid", mediaApi.uploadMedia(0, avatar, null));
125 } else {
126 obj.put("avatar_mediaid", obj.remove("avatar"));
127 }
128 Token token = tokenManager.getCache();
129 WeixinResponse response = weixinExecutor.post(
130 String.format(uri, token.getAccessToken()), obj.toJSONString());
131 return response.getAsResult();
132 }
133
134
135
136
137
138
139
140
141
142
143
144 public User getUser(String userid) throws WeixinException {
145 String user_get_uri = getRequestUri("user_get_uri");
146 Token token = tokenManager.getCache();
147 WeixinResponse response = weixinExecutor.get(String.format(
148 user_get_uri, token.getAccessToken(), userid));
149 JSONObject obj = response.getAsJson();
150 Object attrs = obj.remove("extattr");
151 User user = JSON.toJavaObject(obj, User.class);
152 if (attrs != null) {
153 user.setExtattr(JSON.parseArray(
154 ((JSONObject) attrs).getString("attrs"), NameValue.class));
155 }
156 return user;
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public User getUserByCode(String code) throws WeixinException {
173 JSONObject result = getUserIdByCode(code);
174 if (result.containsKey("user_ticket")) {
175 String user_ticket_detail_uri = getRequestUri("user_ticket_detail_uri");
176 Token token = tokenManager.getCache();
177 WeixinResponse response = weixinExecutor.post(
178 String.format(user_ticket_detail_uri,
179 token.getAccessToken()),
180 String.format("{\"user_ticket\":\"%s\"}",
181 result.getString("user_ticket")));
182 JSONObject obj = response.getAsJson();
183 Object attrs = obj.remove("extattr");
184 User user = JSON.toJavaObject(obj, User.class);
185 if (attrs != null) {
186 user.setExtattr(JSON.parseArray(
187 ((JSONObject) attrs).getString("attrs"),
188 NameValue.class));
189 }
190 return user;
191 } else {
192 String userId = result.getString("UserId");
193 if (StringUtil.isBlank(userId)) {
194 userId = openid2userid(result.getString("OpenId"));
195 }
196 return getUser(userId);
197 }
198 }
199
200
201
202
203
204
205
206
207
208
209
210 public JSONObject getUserIdByCode(String code) throws WeixinException {
211 String user_getid_uri = getRequestUri("user_getid_uri");
212 Token token = tokenManager.getCache();
213 WeixinResponse response = weixinExecutor.get(String.format(
214 user_getid_uri, token.getAccessToken(), code));
215 return response.getAsJson();
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public OUserInfo getOUserInfoByCode(String authCode) throws WeixinException {
231 Token token = tokenManager.getCache();
232 String oauth_logininfo_uri = getRequestUri("oauth_logininfo_uri");
233 WeixinResponse response = weixinExecutor.post(
234 String.format(oauth_logininfo_uri, token.getAccessToken()),
235 String.format("{\"auth_code\":\"%s\"}", authCode));
236 return JSON.parseObject(response.getAsString(), OUserInfo.class);
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public List<User> listUser(int partyId, boolean fetchChild,
256 UserStatus userStatus, boolean findDetail) throws WeixinException {
257 String user_list_uri = findDetail ? getRequestUri("user_list_uri")
258 : getRequestUri("user_slist_uri");
259 Token token = tokenManager.getCache();
260 if (userStatus == null) {
261 userStatus = UserStatus.UNFOLLOW;
262 }
263 WeixinResponse response = weixinExecutor.get(String.format(
264 user_list_uri, token.getAccessToken(), partyId, fetchChild ? 1
265 : 0, userStatus.getVal()));
266 List<User> list = null;
267 if (findDetail) {
268 JSONArray arrays = response.getAsJson().getJSONArray("userlist");
269 list = new ArrayList<User>(arrays.size());
270 for (int i = 0; i < arrays.size(); i++) {
271 JSONObject obj = arrays.getJSONObject(i);
272 Object attrs = obj.remove("extattr");
273 User user = JSON.toJavaObject(obj, User.class);
274 if (attrs != null) {
275 user.setExtattr(JSON.parseArray(
276 ((JSONObject) attrs).getString("attrs"),
277 NameValue.class));
278 }
279 list.add(user);
280 }
281 } else {
282 list = JSON.parseArray(response.getAsJson().getString("userlist"),
283 User.class);
284 }
285 return list;
286 }
287
288
289
290
291
292
293
294
295
296
297 public List<User> listUser(int partyId) throws WeixinException {
298 return listUser(partyId, false, UserStatus.BOTH, false);
299 }
300
301
302
303
304
305
306
307
308
309
310
311 public List<User> listAllUser(UserStatus userStatus) throws WeixinException {
312 List<User> users = null;
313 List<Party> parties = partyApi.listParty(0);
314 if (!parties.isEmpty()) {
315 if (userStatus == null) {
316 userStatus = UserStatus.BOTH;
317 }
318 users = new ArrayList<User>();
319 for (Party party : parties) {
320 users.addAll(listUser(party.getId(), true, userStatus, true));
321 }
322 }
323 return users;
324 }
325
326
327
328
329
330
331
332
333
334
335 public ApiResult deleteUser(String userid) throws WeixinException {
336 String user_delete_uri = getRequestUri("user_delete_uri");
337 Token token = tokenManager.getCache();
338 WeixinResponse response = weixinExecutor.get(String.format(
339 user_delete_uri, token.getAccessToken(), userid));
340 return response.getAsResult();
341 }
342
343
344
345
346
347
348
349
350
351
352 public ApiResult batchDeleteUser(List<String> userIds)
353 throws WeixinException {
354 JSONObject obj = new JSONObject();
355 obj.put("useridlist", userIds);
356 String user_delete_uri = getRequestUri("user_batchdelete_uri");
357 Token token = tokenManager.getCache();
358 WeixinResponse response = weixinExecutor.post(
359 String.format(user_delete_uri, token.getAccessToken()),
360 obj.toJSONString());
361 return response.getAsResult();
362 }
363
364
365
366
367
368
369
370
371
372
373 public ApiResult authsucc(String userId) throws WeixinException {
374 String user_authsucc_uri = getRequestUri("user_authsucc_uri");
375 Token token = tokenManager.getCache();
376 WeixinResponse response = weixinExecutor.get(String.format(
377 user_authsucc_uri, token.getAccessToken(), userId));
378 return response.getAsResult();
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 public InviteType inviteUser(String userId, String tips)
395 throws WeixinException {
396 JSONObject obj = new JSONObject();
397 obj.put("userid", userId);
398 obj.put("invite_tips", tips);
399 String invite_user_uri = getRequestUri("invite_user_uri");
400 Token token = tokenManager.getCache();
401 WeixinResponse response = weixinExecutor.post(
402 String.format(invite_user_uri, token.getAccessToken()),
403 obj.toJSONString());
404 int type = response.getAsJson().getIntValue("type");
405 if (type == 1) {
406 return InviteType.WEIXIN;
407 } else if (type == 2) {
408 return InviteType.EMAIL;
409 } else {
410 return null;
411 }
412 }
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 public String[] userid2openid(String userid, int agentid)
428 throws WeixinException {
429 JSONObject obj = new JSONObject();
430 obj.put("userid", userid);
431 if (agentid > 0) {
432 obj.put("agentid", agentid);
433 }
434 String userid2openid_uri = getRequestUri("userid2openid_uri");
435 WeixinResponse response = weixinExecutor
436 .post(String.format(userid2openid_uri,
437 tokenManager.getAccessToken()), obj.toJSONString());
438 obj = response.getAsJson();
439 return new String[] { obj.getString("openid"), obj.getString("appid") };
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453 public String openid2userid(String openid) throws WeixinException {
454 String openid2userid_uri = getRequestUri("openid2userid_uri");
455 WeixinResponse response = weixinExecutor
456 .post(String.format(openid2userid_uri,
457 tokenManager.getAccessToken()),
458 String.format("{\"openid\": \"%s\"}", openid));
459 return response.getAsJson().getString("userid");
460 }
461 }