1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import com.alibaba.fastjson.JSON;
8 import com.alibaba.fastjson.JSONObject;
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.token.TokenManager;
14 import com.foxinmy.weixin4j.tuple.MassTuple;
15 import com.foxinmy.weixin4j.tuple.MpArticle;
16 import com.foxinmy.weixin4j.tuple.MpNews;
17 import com.foxinmy.weixin4j.tuple.Tuple;
18 import com.foxinmy.weixin4j.util.StringUtil;
19
20
21
22
23
24
25
26
27
28 public class MassApi extends MpApi {
29
30 private final TokenManager tokenManager;
31
32 public MassApi(TokenManager tokenManager) {
33 this.tokenManager = tokenManager;
34 }
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public String uploadArticle(List<MpArticle> articles) throws WeixinException {
50 String article_upload_uri = getRequestUri("article_upload_uri");
51 Token token = tokenManager.getCache();
52 JSONObject obj = new JSONObject();
53 obj.put("articles", articles);
54 WeixinResponse response = weixinExecutor.post(String.format(article_upload_uri, token.getAccessToken()),
55 obj.toJSONString());
56
57 return response.getAsJson().getString("media_id");
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 @Deprecated
89 public String[] massByGroupId(MassTuple tuple, boolean isToAll, int groupId) throws WeixinException {
90 if (tuple instanceof MpNews) {
91 MpNews _news = (MpNews) tuple;
92 List<MpArticle> _articles = _news.getArticles();
93 if (StringUtil.isBlank(_news.getMediaId())) {
94 if (_articles.isEmpty()) {
95 throw new WeixinException("mass fail:mediaId or articles is required");
96 }
97 tuple = new MpNews(uploadArticle(_articles));
98 }
99 }
100 String msgtype = tuple.getMessageType();
101 JSONObject obj = new JSONObject();
102 JSONObject item = new JSONObject();
103 item.put("is_to_all", isToAll);
104 if (!isToAll) {
105 item.put("group_id", groupId);
106 }
107 obj.put("filter", item);
108 obj.put(msgtype, JSON.toJSON(tuple));
109 obj.put("msgtype", msgtype);
110 String mass_group_uri = getRequestUri("mass_group_uri");
111 Token token = tokenManager.getCache();
112 WeixinResponse response = weixinExecutor.post(String.format(mass_group_uri, token.getAccessToken()),
113 obj.toJSONString());
114
115 obj = response.getAsJson();
116 return new String[] { obj.getString("msg_id"), obj.getString("msg_data_id") };
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 @Deprecated
134 public String[] massArticleByGroupId(List<MpArticle> articles, int groupId) throws WeixinException {
135 String mediaId = uploadArticle(articles);
136 return massByGroupId(new MpNews(mediaId), false, groupId);
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 private String[] mass(MassTuple tuple, JSONObject filter) throws WeixinException {
162 if (tuple instanceof MpNews) {
163 MpNews _news = (MpNews) tuple;
164 List<MpArticle> _articles = _news.getArticles();
165 if (StringUtil.isBlank(_news.getMediaId())) {
166 if (_articles.isEmpty()) {
167 throw new WeixinException("mass fail:mediaId or articles is required");
168 }
169 tuple = new MpNews(uploadArticle(_articles));
170 }
171 if (!filter.containsKey("send_ignore_reprint")) {
172 filter.put("send_ignore_reprint", 0);
173 }
174 }
175 String msgtype = tuple.getMessageType();
176 JSONObject obj = new JSONObject();
177 obj.putAll(filter);
178 obj.put(msgtype, JSON.toJSON(tuple));
179 obj.put("msgtype", msgtype);
180 String mass_uri = filter.containsKey("touser") ? getRequestUri("mass_openid_uri") : getRequestUri("mass_group_uri");
181 Token token = tokenManager.getCache();
182 WeixinResponse response = weixinExecutor.post(String.format(mass_uri, token.getAccessToken()),
183 obj.toJSONString());
184
185 obj = response.getAsJson();
186 return new String[] { obj.getString("msg_id"), obj.getString("msg_data_id") };
187 }
188
189
190
191
192
193
194
195
196
197
198
199 public String[] massToAll(MassTuple tuple) throws WeixinException {
200 String filter = String.format("{\"filter\":{\"is_to_all\":true}}");
201 return mass(tuple, JSON.parseObject(filter));
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public String[] massByTagId(MassTuple tuple, int tagId) throws WeixinException {
220 String filter = String.format("{\"filter\":{\"is_to_all\":false,\"tag_id\":%d}}", tagId);
221 return mass(tuple, JSON.parseObject(filter));
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 public String[] massArticleByTagId(List<MpArticle> articles, int tagId, boolean ignoreReprint)
241 throws WeixinException {
242 String mediaId = uploadArticle(articles);
243 String text = String.format("{\"filter\":{\"is_to_all\":false,\"tag_id\":%d}}", tagId);
244 JSONObject filter = JSON.parseObject(text);
245 filter.put("send_ignore_reprint", ignoreReprint ? 1 : 0);
246 return mass(new MpNews(mediaId), filter);
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 public String[] massByOpenIds(MassTuple tuple, String... openIds) throws WeixinException {
264 JSONObject filter = new JSONObject();
265 filter.put("touser", openIds);
266 return mass(tuple, filter);
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285 public String[] massArticleByOpenIds(List<MpArticle> articles, boolean ignoreReprint, String... openIds)
286 throws WeixinException {
287 String mediaId = uploadArticle(articles);
288 JSONObject filter = new JSONObject();
289 filter.put("touser", openIds);
290 filter.put("send_ignore_reprint", ignoreReprint ? 1 : 0);
291 return mass(new MpNews(mediaId), filter);
292 }
293
294
295
296
297
298
299
300
301
302
303
304 public ApiResult deleteMassNews(String msgid) throws WeixinException {
305 return deleteMassNews(msgid, 0);
306 }
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325 public ApiResult deleteMassNews(String msgid, int articleIndex) throws WeixinException {
326 JSONObject obj = new JSONObject();
327 obj.put("msgid", msgid);
328 if (articleIndex > 0)
329 obj.put("article_idx", articleIndex);
330 String mass_delete_uri = getRequestUri("mass_delete_uri");
331 Token token = tokenManager.getCache();
332 WeixinResponse response = weixinExecutor.post(String.format(mass_delete_uri, token.getAccessToken()),
333 obj.toJSONString());
334
335 return response.getAsResult();
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 public ApiResult previewMassNews(String toUser, String toWxName, MassTuple tuple) throws WeixinException {
355 String msgtype = tuple.getMessageType();
356 JSONObject obj = new JSONObject();
357 if(toUser != null){
358 obj.put("touser", toUser);
359 }
360 if(toWxName != null){
361 obj.put("towxname", toWxName);
362 }
363 obj.put(msgtype, JSON.toJSON(tuple));
364 obj.put("msgtype", msgtype);
365 String mass_preview_uri = getRequestUri("mass_preview_uri");
366 Token token = tokenManager.getCache();
367 WeixinResponse response = weixinExecutor.post(String.format(mass_preview_uri, token.getAccessToken()),
368 obj.toJSONString());
369
370 return response.getAsResult();
371 }
372
373
374
375
376
377
378
379
380
381
382
383 public String getMassNewStatus(String msgId) throws WeixinException {
384 JSONObject obj = new JSONObject();
385 obj.put("msg_id", msgId);
386 String mass_get_uri = getRequestUri("mass_get_uri");
387 Token token = tokenManager.getCache();
388 WeixinResponse response = weixinExecutor.post(String.format(mass_get_uri, token.getAccessToken()),
389 obj.toJSONString());
390
391 String status = response.getAsJson().getString("msg_status");
392 String desc = massStatusMap.get(status);
393 return String.format("%s:%s", status, desc);
394 }
395
396 private final static Map<String, String> massStatusMap;
397 static {
398 massStatusMap = new HashMap<String, String>();
399 massStatusMap.put("sendsuccess", "发送成功");
400 massStatusMap.put("send_success", "发送成功");
401 massStatusMap.put("success", "发送成功");
402 massStatusMap.put("send success", "发送成功");
403 massStatusMap.put("sendfail", "发送失败");
404 massStatusMap.put("send_fail", "发送失败");
405 massStatusMap.put("fail", "发送失败");
406 massStatusMap.put("send fail", "发送失败");
407 massStatusMap.put("err(10001)", "涉嫌广告");
408 massStatusMap.put("err(20001)", "涉嫌政治");
409 massStatusMap.put("err(20004)", "涉嫌社会");
410 massStatusMap.put("err(20006)", "涉嫌违法犯罪");
411 massStatusMap.put("err(20008)", "涉嫌欺诈");
412 massStatusMap.put("err(20013)", "涉嫌版权");
413 massStatusMap.put("err(22000)", "涉嫌互推(互相宣传)");
414 massStatusMap.put("err(21000)", "涉嫌其他");
415 }
416 }