1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.alibaba.fastjson.JSON;
7 import com.alibaba.fastjson.JSONObject;
8 import com.foxinmy.weixin4j.exception.WeixinException;
9 import com.foxinmy.weixin4j.http.weixin.ApiResult;
10 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
11 import com.foxinmy.weixin4j.model.Token;
12 import com.foxinmy.weixin4j.mp.model.Following;
13 import com.foxinmy.weixin4j.mp.model.Tag;
14 import com.foxinmy.weixin4j.mp.model.User;
15 import com.foxinmy.weixin4j.token.TokenManager;
16
17
18
19
20
21
22
23
24
25
26 public class TagApi extends MpApi {
27 private final TokenManager tokenManager;
28 private final UserApi userApi;
29
30 public TagApi(TokenManager tokenManager) {
31 this.tokenManager = tokenManager;
32 this.userApi = new UserApi(tokenManager);
33 }
34
35
36
37
38
39
40
41
42
43
44
45
46 public Tag createTag(String name) throws WeixinException {
47 String tag_create_uri = getRequestUri("tag_create_uri");
48 WeixinResponse response = weixinExecutor.post(
49 String.format(tag_create_uri, tokenManager.getAccessToken()),
50 String.format("{\"tag\":{\"name\":\"%s\"}}", name));
51 JSONObject obj = response.getAsJson().getJSONObject("tag");
52 return new Tag(obj.getIntValue("id"), obj.getString("name"));
53 }
54
55
56
57
58
59
60
61
62
63
64 public List<Tag> listTags() throws WeixinException {
65 String tag_get_uri = getRequestUri("tag_get_uri");
66 WeixinResponse response = weixinExecutor.get(String.format(tag_get_uri,
67 tokenManager.getAccessToken()));
68
69 return JSON.parseArray(response.getAsJson().getString("tags"),
70 Tag.class);
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84 public ApiResult updateTag(Tag tag) throws WeixinException {
85 String tag_update_uri = getRequestUri("tag_update_uri");
86 JSONObject obj = new JSONObject();
87 obj.put("tag", tag);
88 WeixinResponse response = weixinExecutor.post(
89 String.format(tag_update_uri, tokenManager.getAccessToken()),
90 obj.toJSONString());
91 return response.getAsResult();
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public ApiResult deleteTag(int tagId) throws WeixinException {
105 String tag_delete_uri = getRequestUri("tag_delete_uri");
106 WeixinResponse response = weixinExecutor.post(
107 String.format(tag_delete_uri, tokenManager.getAccessToken()),
108 String.format("{\"tag\":{\"id\":%d}}", tagId));
109 return response.getAsResult();
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public ApiResult taggingUsers(int tagId, String... openIds)
125 throws WeixinException {
126 return batchUsers("tag_tagging_uri", tagId, openIds);
127 }
128
129 private ApiResult batchUsers(String batchType, int tagId, String... openIds)
130 throws WeixinException {
131 String tag_batch_uri = getRequestUri(batchType);
132 JSONObject obj = new JSONObject();
133 obj.put("openid_list", openIds);
134 obj.put("tagid", tagId);
135 WeixinResponse response = weixinExecutor.post(
136 String.format(tag_batch_uri, tokenManager.getAccessToken()),
137 obj.toJSONString());
138 return response.getAsResult();
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public ApiResult untaggingUsers(int tagId, String... openIds)
154 throws WeixinException {
155 return batchUsers("tag_untagging_uri", tagId, openIds);
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public Following getTagFollowingOpenIds(int tagId, String nextOpenId)
171 throws WeixinException {
172 String tag_user_uri = getRequestUri("tag_user_uri");
173 JSONObject obj = new JSONObject();
174 obj.put("tagid", tagId);
175 obj.put("next_openid", nextOpenId);
176 WeixinResponse response = weixinExecutor.post(
177 String.format(tag_user_uri, tokenManager.getAccessToken()),
178 obj.toJSONString());
179
180 JSONObject result = response.getAsJson();
181 Following following = JSON.toJavaObject(result, Following.class);
182
183 if (following.getCount() > 0) {
184 following.setOpenIds(JSON.parseArray(result.getJSONObject("data")
185 .getString("openid"), String.class));
186 }
187 return following;
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public Following getTagFollowing(int tagId, String nextOpenId)
203 throws WeixinException {
204 Following following = getTagFollowingOpenIds(tagId, nextOpenId);
205 if (following.getCount() > 0) {
206 List<User> users = new ArrayList<User>(following.getCount());
207 for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
208 users.addAll(userApi.getUsers(following
209 .getOpenIds()
210 .subList((i - 1) * 100,
211 Math.min(i * 100, following.getCount()))
212 .toArray(new String[] {})));
213 }
214 following.setUserList(users);
215 }
216 return following;
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230 public List<String> getAllTagFollowingOpenIds(int tagId)
231 throws WeixinException {
232 List<String> openIds = new ArrayList<String>();
233 String nextOpenId = null;
234 Following f = null;
235 for (;;) {
236 f = getTagFollowingOpenIds(tagId, nextOpenId);
237 if (f.hasContent()) {
238 openIds.addAll(f.getOpenIds());
239 nextOpenId = f.getNextOpenId();
240 continue;
241 }
242 break;
243 }
244 return openIds;
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258 public List<User> getAllTagFollowing(int tagId) throws WeixinException {
259 List<User> userList = new ArrayList<User>();
260 String nextOpenId = null;
261 Following f = null;
262 for (;;) {
263 f = getTagFollowing(tagId, nextOpenId);
264 if (f.hasContent()) {
265 userList.addAll(f.getUserList());
266 nextOpenId = f.getNextOpenId();
267 continue;
268 }
269 break;
270 }
271 return userList;
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285 public Integer[] getUserTags(String openId) throws WeixinException {
286 String tag_userids_uri = getRequestUri("tag_userids_uri");
287 WeixinResponse response = weixinExecutor.post(
288 String.format(tag_userids_uri, tokenManager.getAccessToken()),
289 String.format("{\"openid\":\"%s\"}", openId));
290 return response.getAsJson().getJSONArray("tagid_list")
291 .toArray(new Integer[] {});
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305
306 public Following getBalcklistOpenIds(String nextOpenId)
307 throws WeixinException {
308 JSONObject obj = new JSONObject();
309 obj.put("begin_openid", nextOpenId == null ? "" : nextOpenId);
310 String getblacklist_uri = getRequestUri("getblacklist_uri");
311 Token token = tokenManager.getCache();
312 WeixinResponse response = weixinExecutor.post(String.format(
313 getblacklist_uri, token.getAccessToken(), obj.toJSONString()));
314 JSONObject result = response.getAsJson();
315 Following following = JSON.toJavaObject(result, Following.class);
316 if (following.getCount() > 0) {
317 following.setOpenIds(JSON.parseArray(result.getJSONObject("data")
318 .getString("openid"), String.class));
319 }
320 return following;
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337 public List<String> getAllBalcklistOpenIds() throws WeixinException {
338 List<String> openIds = new ArrayList<String>();
339 String nextOpenId = null;
340 Following f = null;
341 for (;;) {
342 f = getBalcklistOpenIds(nextOpenId);
343 if (f.hasContent()) {
344 openIds.addAll(f.getOpenIds());
345 nextOpenId = f.getNextOpenId();
346 continue;
347 }
348 break;
349 }
350 return openIds;
351 }
352
353
354
355
356
357
358
359
360
361
362
363
364
365 public ApiResult batchBlacklist(boolean blacklist, String... openIds)
366 throws WeixinException {
367 JSONObject obj = new JSONObject();
368 obj.put("openid_list", openIds);
369 String blacklist_url = blacklist ? getRequestUri("batchblacklist_uri")
370 : getRequestUri("batchunblacklist_uri");
371 WeixinResponse response = weixinExecutor.post(
372 String.format(blacklist_url, tokenManager.getAccessToken()),
373 obj.toJSONString());
374 return response.getAsResult();
375 }
376 }