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.alibaba.fastjson.TypeReference;
9 import com.foxinmy.weixin4j.exception.WeixinException;
10 import com.foxinmy.weixin4j.http.weixin.ApiResult;
11 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
12 import com.foxinmy.weixin4j.model.Token;
13 import com.foxinmy.weixin4j.mp.model.Following;
14 import com.foxinmy.weixin4j.mp.model.User;
15 import com.foxinmy.weixin4j.mp.model.ChangeOpenidResult;
16 import com.foxinmy.weixin4j.mp.type.Lang;
17 import com.foxinmy.weixin4j.token.TokenManager;
18
19
20
21
22
23
24
25
26
27
28 public class UserApi extends MpApi {
29
30 private final TokenManager tokenManager;
31
32 public UserApi(TokenManager tokenManager) {
33 this.tokenManager = tokenManager;
34 }
35
36
37
38
39
40
41
42
43
44
45 public User getUser(String openId) throws WeixinException {
46 return getUser(openId, Lang.zh_CN);
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public User getUser(String openId, Lang lang) throws WeixinException {
69 String user_info_uri = getRequestUri("api_user_info_uri");
70 Token token = tokenManager.getCache();
71 WeixinResponse response = weixinExecutor
72 .get(String.format(user_info_uri, token.getAccessToken(), openId, lang.name()));
73
74 return response.getAsObject(new TypeReference<User>() {
75 });
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public List<User> getUsers(String... openIds) throws WeixinException {
92 return getUsers(Lang.zh_CN, openIds);
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public List<User> getUsers(Lang lang, String... openIds) throws WeixinException {
111 String api_users_info_uri = getRequestUri("api_users_info_uri");
112 StringBuilder parameter = new StringBuilder();
113 parameter.append("{\"user_list\": [");
114 for (String openId : openIds) {
115 parameter.append("{\"openid\": \"").append(openId).append("\"");
116 parameter.append(",\"lang\": \"").append(lang.name()).append("\"").append("},");
117 }
118 parameter.delete(parameter.length() - 1, parameter.length());
119 parameter.append("]}");
120 Token token = tokenManager.getCache();
121 WeixinResponse response = weixinExecutor.post(String.format(api_users_info_uri, token.getAccessToken()),
122 parameter.toString());
123
124 return JSON.parseArray(response.getAsJson().getString("user_info_list"), User.class);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 public Following getFollowing(String nextOpenId) throws WeixinException {
144 Following following = getFollowingOpenIds(nextOpenId);
145 if (following.getCount() > 0) {
146 List<User> users = new ArrayList<User>(following.getCount());
147 for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
148 users.addAll(getUsers(following.getOpenIds()
149 .subList((i - 1) * 100, Math.min(i * 100, following.getCount())).toArray(new String[] {})));
150 }
151 following.setUserList(users);
152 }
153 return following;
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168 public Following getFollowingOpenIds(String nextOpenId) throws WeixinException {
169 String following_uri = getRequestUri("following_uri");
170 Token token = tokenManager.getCache();
171 WeixinResponse response = weixinExecutor
172 .get(String.format(following_uri, token.getAccessToken(), nextOpenId == null ? "" : nextOpenId));
173
174 JSONObject result = response.getAsJson();
175 Following following = JSON.toJavaObject(result, Following.class);
176
177 if (following.getCount() > 0) {
178 following.setOpenIds(JSON.parseArray(result.getJSONObject("data").getString("openid"), String.class));
179 }
180 return following;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public List<User> getAllFollowing() throws WeixinException {
203 List<User> userList = new ArrayList<User>();
204 String nextOpenId = null;
205 Following f = null;
206 for (;;) {
207 f = getFollowing(nextOpenId);
208 if (f.hasContent()) {
209 userList.addAll(f.getUserList());
210 nextOpenId = f.getNextOpenId();
211 continue;
212 }
213 break;
214 }
215 return userList;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 public List<String> getAllFollowingOpenIds() throws WeixinException {
233 List<String> openIds = new ArrayList<String>();
234 String nextOpenId = null;
235 Following f = null;
236 for (;;) {
237 f = getFollowingOpenIds(nextOpenId);
238 if (f.hasContent()) {
239 openIds.addAll(f.getOpenIds());
240 nextOpenId = f.getNextOpenId();
241 continue;
242 }
243 break;
244 }
245 return openIds;
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260 public ApiResult remarkUserName(String openId, String remark) throws WeixinException {
261 String username_remark_uri = getRequestUri("username_remark_uri");
262 Token token = tokenManager.getCache();
263 JSONObject obj = new JSONObject();
264 obj.put("openid", openId);
265 obj.put("remark", remark);
266 WeixinResponse response = weixinExecutor.post(String.format(username_remark_uri, token.getAccessToken()),
267 obj.toJSONString());
268
269 return response.getAsResult();
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283 public List<ChangeOpenidResult> batchChangeOpenid(String fromAppid, List<String> openIds) throws WeixinException {
284 String change_openid_uri = getRequestUri("change_openid_uri");
285 StringBuilder parameter = new StringBuilder();
286 parameter.append("{\"from_appid\":\"").append(fromAppid).append("\"");
287 parameter.append(",\"openid_list\": [");
288 for (String openId : openIds) {
289 parameter.append("\"").append(openId).append("\",");
290 }
291 parameter.delete(parameter.length() - 1, parameter.length());
292 parameter.append("]}");
293 Token token = tokenManager.getCache();
294 WeixinResponse response = weixinExecutor.post(String.format(change_openid_uri, token.getAccessToken()),
295 parameter.toString());
296
297 return JSON.parseArray(response.getAsJson().getString("result_list"), ChangeOpenidResult.class);
298 }
299
300
301
302
303
304
305
306
307
308
309 public List<ChangeOpenidResult> changeAllOpenid(String fromAppid) throws WeixinException {
310 List<String> openIds = null;
311 String nextOpenId = null;
312 Following following = null;
313 int batchSize = 100;
314 List<ChangeOpenidResult> results = new ArrayList<ChangeOpenidResult>();
315 for (;;) {
316 following = getFollowingOpenIds(nextOpenId);
317 if (following.hasContent()) {
318 openIds = following.getOpenIds();
319 int split = (int) Math.ceil(1.0 * openIds.size() / batchSize);
320 for (int i = 0; i < split; i++) {
321 List<String> batch = openIds.subList((i * batchSize), Math.min(openIds.size(), (i + 1) * batchSize));
322 results.addAll(batchChangeOpenid(fromAppid,batch));
323 }
324 nextOpenId = following.getNextOpenId();
325 continue;
326 }
327 break;
328 }
329 return results;
330 }
331 }