View Javadoc
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   * 文章评论API
20   *
21   * @className CommentApi
22   * @author jinyu
23   * @date May 19, 2017
24   * @since JDK 1.6
25   * @see <a href=
26   *      "https://mp.weixin.qq.com/wiki?action=doc&id=mp1494572718_WzHIY&t=0.6758084213658122">图文消息留言管理接口</a>
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       * @param open
39       *            true为打开,false为关闭
40       * @param msgid
41       *            群发返回的msg_data_id
42       * @param index
43       *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
44       * @return 操作结果
45       * @see {@link MassApi#massByTagId(com.foxinmy.weixin4j.tuple.MassTuple, int)}
46       * @see {@link MassApi#massByOpenIds(com.foxinmy.weixin4j.tuple.MassTuple, String...)}
47       * @throws WeixinException
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       * @param page
65       *            分页信息
66       * @param commentType
67       *            评论类型 为空获取全部类型
68       * @param msgid
69       *            群发返回的msg_data_id
70       * @param index
71       *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
72       * @return 分页数据
73       * @see ArticleComment
74       * @see ArticleCommentType
75       * @see {@link MassApi#massByTagId(com.foxinmy.weixin4j.tuple.MassTuple, int)}
76       * @see {@link MassApi#massByOpenIds(com.foxinmy.weixin4j.tuple.MassTuple, String...)}
77       * @throws WeixinException
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())); // 获取数目(>=50会被拒绝)
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      * @param commentType
106      *            评论类型 为空获取全部类型
107      * @param msgid
108      *            群发返回的msg_data_id
109      * @param index
110      *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
111      * @return 分页数据
112      * @see #listArticleComments(Pageable, ArticleCommentType, String, int)
113      * @throws WeixinException
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      * @param markelect
134      *            true为标记,false为取消
135      * @param msgid
136      *            群发返回的msg_data_id
137      * @param index
138      *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
139      * @param commentId
140      *            用户评论ID
141      * @return 操作结果
142      * @see #listArticleComments(Pageable, ArticleCommentType, String, int)
143      * @throws WeixinException
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      * @param msgid
164      *            群发返回的msg_data_id
165      * @param index
166      *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
167      * @param commentId
168      *            用户评论ID
169      * @return 操作结果
170      * @see #listArticleComments(Pageable, ArticleCommentType, String, int)
171      * @throws WeixinException
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      * @param msgid
190      *            群发返回的msg_data_id
191      * @param index
192      *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
193      * @param commentId
194      *            用户评论ID
195      * @param content
196      *            回复内容
197      * @return 操作结果
198      * @see #listArticleComments(Pageable, ArticleCommentType, String, int)
199      * @throws WeixinException
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      * @param msgid
219      *            群发返回的msg_data_id
220      * @param index
221      *            多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文
222      * @param commentId
223      *            用户评论ID
224      * @return 操作结果
225      * @see #listArticleComments(Pageable, ArticleCommentType, String, int)
226      * @throws WeixinException
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 }