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.model.paging.Pageable;
13 import com.foxinmy.weixin4j.model.paging.Pagedata;
14 import com.foxinmy.weixin4j.mp.model.ArticleComment;
15 import com.foxinmy.weixin4j.mp.model.ArticleComment.ArticleCommentType;
16 import com.foxinmy.weixin4j.token.TokenManager;
17
18
19
20
21
22
23
24
25
26
27
28 public class CommentApi extends MpApi {
29 private final TokenManager tokenManager;
30
31 public CommentApi(TokenManager tokenManager) {
32 this.tokenManager = tokenManager;
33 }
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public ApiResult openComment(boolean open, String msgid, int index) throws WeixinException {
50 String news_comment = open ? getRequestUri("news_comment_open") : getRequestUri("news_comment_close");
51 Token token = tokenManager.getCache();
52 JSONObject obj = new JSONObject();
53 obj.put("msg_data_id", msgid);
54 obj.put("index", index);
55 WeixinResponse response = weixinExecutor.post(String.format(news_comment, token.getAccessToken()),
56 obj.toJSONString());
57
58 return response.getAsResult();
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public Pagedata<ArticleComment> listArticleComments(Pageable page, ArticleCommentType commentType, String msgid,
80 int index) throws WeixinException {
81 String news_comment_list = getRequestUri("news_comment_list");
82 Token token = tokenManager.getCache();
83 JSONObject obj = new JSONObject();
84 obj.put("msg_data_id", "msgid");
85 obj.put("index", index);
86 obj.put("begin", page.getOffset());
87 obj.put("count", Math.max(50, page.getPageSize()));
88 if (commentType != null) {
89 obj.put("type", commentType.ordinal() + 1);
90 } else {
91 obj.put("type", 0);
92 }
93 WeixinResponse response = weixinExecutor.post(String.format(news_comment_list, token.getAccessToken()),
94 obj.toJSONString());
95
96 JSONObject result = response.getAsJson();
97 int total = result.getIntValue("total");
98 List<ArticleComment> content = JSON.parseArray(result.getString("comment"), ArticleComment.class);
99 return new Pagedata<ArticleComment>(page, total, content);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 public List<ArticleComment> listAllArticleComments(ArticleCommentType commentType, String msgid, int index)
116 throws WeixinException {
117 List<ArticleComment> comments = new ArrayList<ArticleComment>();
118 Pagedata<ArticleComment> page = null;
119 Pageable pageable = null;
120 for (pageable = new Pageable(1, 50);; pageable = pageable.next()) {
121 page = listArticleComments(pageable, commentType, msgid, index);
122 if (!page.hasContent()) {
123 break;
124 }
125 comments.addAll(page.getContent());
126 }
127 return comments;
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 public ApiResult markelectComment(boolean markelect, String msgid, int index, String commentId)
146 throws WeixinException {
147 String news_comment = markelect ? getRequestUri("news_comment_markelect")
148 : getRequestUri("news_comment_unmarkelect");
149 Token token = tokenManager.getCache();
150 JSONObject obj = new JSONObject();
151 obj.put("msg_data_id", "msgid");
152 obj.put("index", index);
153 obj.put("user_comment_id", commentId);
154 WeixinResponse response = weixinExecutor.post(String.format(news_comment, token.getAccessToken()),
155 obj.toJSONString());
156
157 return response.getAsResult();
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public ApiResult deleteComment(String msgid, int index, String commentId) throws WeixinException {
174 String news_comment_delete = getRequestUri("news_comment_delete");
175 Token token = tokenManager.getCache();
176 JSONObject obj = new JSONObject();
177 obj.put("msg_data_id", "msgid");
178 obj.put("index", index);
179 obj.put("user_comment_id", commentId);
180 WeixinResponse response = weixinExecutor.post(String.format(news_comment_delete, token.getAccessToken()),
181 obj.toJSONString());
182
183 return response.getAsResult();
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 public ApiResult replyComment(String msgid, int index, String commentId, String content) throws WeixinException {
202 String news_comment_reply = getRequestUri("news_comment_reply_add");
203 Token token = tokenManager.getCache();
204 JSONObject obj = new JSONObject();
205 obj.put("msg_data_id", "msgid");
206 obj.put("index", index);
207 obj.put("user_comment_id", commentId);
208 obj.put("content", content);
209 WeixinResponse response = weixinExecutor.post(String.format(news_comment_reply, token.getAccessToken()),
210 obj.toJSONString());
211
212 return response.getAsResult();
213 }
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 public ApiResult deleteCommentReply(String msgid, int index, String commentId) throws WeixinException {
229 String news_comment_reply = getRequestUri("news_comment_reply_delete");
230 Token token = tokenManager.getCache();
231 JSONObject obj = new JSONObject();
232 obj.put("msg_data_id", "msgid");
233 obj.put("index", index);
234 obj.put("user_comment_id", commentId);
235 WeixinResponse response = weixinExecutor.post(String.format(news_comment_reply, token.getAccessToken()),
236 obj.toJSONString());
237
238 return response.getAsResult();
239 }
240 }