@@ -1,9 +1,11 @@ | |||
package me.chanjar.weixin.mp.api; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import me.chanjar.weixin.mp.bean.comment.WxMpCommentListVo; | |||
/** | |||
* 评论数据管理. | |||
* 图文消息留言管理接口. | |||
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1494572718_WzHIY | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2019-06-16 | |||
@@ -12,11 +14,34 @@ public interface WxMpCommentService { | |||
/** | |||
* 打开已群发文章评论. | |||
* https://api.weixin.qq.com/cgi-bin/comment/open?access_token=ACCESS_TOKEN | |||
* 参数 是否必须 类型 说明 | |||
* | |||
* @param msgDataId 群发返回的msg_data_id | |||
* @param index 多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文 | |||
* @throws WxErrorException 异常 | |||
*/ | |||
void open(Integer msgDataId, Integer index) throws WxErrorException; | |||
/** | |||
* 关闭已群发文章评论. | |||
* https://api.weixin.qq.com/cgi-bin/comment/close?access_token=ACCESS_TOKEN | |||
* | |||
* @param msgDataId 群发返回的msg_data_id | |||
* @param index 多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文 | |||
* @throws WxErrorException 异常 | |||
*/ | |||
void close(Integer msgDataId, Integer index) throws WxErrorException; | |||
/** | |||
* 查看指定文章的评论数据. | |||
* https://api.weixin.qq.com/cgi-bin/comment/list?access_token=ACCESS_TOKEN | |||
* | |||
* @param msgDataId 群发返回的msg_data_id | |||
* @param index 多图文时,用来指定第几篇图文,从0开始,不带默认操作该msg_data_id的第一篇图文 | |||
* @param begin 起始位置 | |||
* @param count 获取数目(>=50会被拒绝) | |||
* @param type type=0 普通评论&精选评论 type=1 普通评论 type=2 精选评论 | |||
* @return 评论列表数据 | |||
* @throws WxErrorException 异常 | |||
*/ | |||
WxMpCommentListVo list(Integer msgDataId, Integer index, int begin, int count, int type) throws WxErrorException; | |||
} |
@@ -5,7 +5,9 @@ import lombok.RequiredArgsConstructor; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import me.chanjar.weixin.mp.api.WxMpCommentService; | |||
import me.chanjar.weixin.mp.api.WxMpService; | |||
import me.chanjar.weixin.mp.enums.WxMpApiUrl; | |||
import me.chanjar.weixin.mp.bean.comment.WxMpCommentListVo; | |||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Comment.*; | |||
/** | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
@@ -22,6 +24,33 @@ public class WxMpCommentServiceImpl implements WxMpCommentService { | |||
if (index != null) { | |||
json.addProperty("index", index); | |||
} | |||
this.wxMpService.post(WxMpApiUrl.Comment.OPEN, json.toString()); | |||
this.wxMpService.post(OPEN, json.toString()); | |||
} | |||
@Override | |||
public void close(Integer msgDataId, Integer index) throws WxErrorException { | |||
JsonObject json = new JsonObject(); | |||
json.addProperty("msg_data_id", msgDataId); | |||
if (index != null) { | |||
json.addProperty("index", index); | |||
} | |||
this.wxMpService.post(CLOSE, json.toString()); | |||
} | |||
@Override | |||
public WxMpCommentListVo list(Integer msgDataId, Integer index, int begin, int count, int type) throws WxErrorException { | |||
JsonObject json = new JsonObject(); | |||
json.addProperty("msg_data_id", msgDataId); | |||
json.addProperty("begin", begin); | |||
json.addProperty("count", count); | |||
json.addProperty("type", type); | |||
if (index != null) { | |||
json.addProperty("index", index); | |||
} | |||
return WxMpCommentListVo.fromJson(this.wxMpService.post(LIST, json.toString())); | |||
} | |||
} |
@@ -0,0 +1,89 @@ | |||
package me.chanjar.weixin.mp.bean.comment; | |||
import com.google.gson.annotations.SerializedName; | |||
import lombok.Data; | |||
import me.chanjar.weixin.common.util.xml.IntegerArrayConverter; | |||
import me.chanjar.weixin.mp.bean.device.WxDeviceQrCodeResult; | |||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | |||
import java.io.Serializable; | |||
import java.util.List; | |||
/** | |||
* 群发图文评论数据. | |||
* | |||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
* @date 2019-08-30 | |||
*/ | |||
@Data | |||
public class WxMpCommentListVo implements Serializable { | |||
private static final long serialVersionUID = 7604754799359751247L; | |||
/** | |||
* 总数,非comment的size. | |||
*/ | |||
private Integer total; | |||
/** | |||
* 评论列表. | |||
*/ | |||
private List<WxMpComment> comment; | |||
@Data | |||
public static class Reply implements Serializable { | |||
private static final long serialVersionUID = 9174739515408520429L; | |||
/** | |||
* 作者回复时间 . | |||
*/ | |||
@SerializedName("create_time") | |||
private String createTime; | |||
/** | |||
* 作者回复内容. | |||
*/ | |||
private String content; | |||
} | |||
@Data | |||
public static class WxMpComment implements Serializable { | |||
private static final long serialVersionUID = 5401188720891942634L; | |||
/** | |||
* 用户评论id . | |||
*/ | |||
@SerializedName("user_comment_id") | |||
private Integer userCommentId; | |||
/** | |||
* 用户openid. | |||
*/ | |||
private String openid; | |||
/** | |||
* 评论时间. | |||
*/ | |||
@SerializedName("create_time") | |||
private String createTime; | |||
/** | |||
* 评论内容. | |||
*/ | |||
private String content; | |||
/** | |||
* 是否精选评论,0为即非精选,1为true,即精选. | |||
*/ | |||
@SerializedName("comment_type") | |||
private Integer commentType; | |||
/** | |||
* 作者回复. | |||
*/ | |||
private Reply reply; | |||
} | |||
public static WxMpCommentListVo fromJson(String json) { | |||
return WxMpGsonBuilder.create().fromJson(json, WxMpCommentListVo.class); | |||
} | |||
} |
@@ -875,7 +875,17 @@ public interface WxMpApiUrl { | |||
/** | |||
* 打开已群发文章评论. | |||
*/ | |||
OPEN(API_DEFAULT_HOST_URL, "/cgi-bin/comment/open"); | |||
OPEN(API_DEFAULT_HOST_URL, "/cgi-bin/comment/open"), | |||
/** | |||
* 关闭已群发文章评论. | |||
*/ | |||
CLOSE(API_DEFAULT_HOST_URL, "/cgi-bin/comment/close"), | |||
/** | |||
* 查看指定文章的评论数据. | |||
*/ | |||
LIST(API_DEFAULT_HOST_URL, "/cgi-bin/comment/list"); | |||
private String prefix; | |||
private String path; | |||
@@ -2,11 +2,20 @@ package me.chanjar.weixin.mp.api.impl; | |||
import com.google.inject.Inject; | |||
import me.chanjar.weixin.common.error.WxErrorException; | |||
import me.chanjar.weixin.mp.api.WxMpCommentService; | |||
import me.chanjar.weixin.mp.api.WxMpService; | |||
import me.chanjar.weixin.mp.api.test.ApiTestModule; | |||
import me.chanjar.weixin.mp.bean.comment.WxMpCommentListVo; | |||
import org.testng.annotations.Guice; | |||
import org.testng.annotations.Test; | |||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Comment.LIST; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Matchers.any; | |||
import static org.mockito.Matchers.anyString; | |||
import static org.mockito.Mockito.doReturn; | |||
import static org.mockito.Mockito.spy; | |||
/** | |||
* 测试类. | |||
* | |||
@@ -25,4 +34,44 @@ public class WxMpCommentServiceImplTest { | |||
this.wxService.getCommentService().open(1, null); | |||
this.wxService.getCommentService().open(1, 0); | |||
} | |||
@Test | |||
public void testClose() throws WxErrorException { | |||
this.wxService.getCommentService().close(1000000001, null); | |||
this.wxService.getCommentService().close(1, 0); | |||
} | |||
@Test | |||
public void testList() throws WxErrorException { | |||
String expectedResponse = "{\n" + | |||
" \"errcode\": 0,\n" + | |||
" \"errmsg\": \"ok\",\n" + | |||
" \"total\": 1,\n" + | |||
" \"comment\": [\n" + | |||
" {\n" + | |||
" \"user_comment_id\": 1,\n" + | |||
" \"openid\": \"OPENID\",\n" + | |||
" \"create_time\": \"CREATE_TIME\",\n" + | |||
" \"content\": \"CONTENT\",\n" + | |||
" \"comment_type\": 1,\n" + | |||
" \"reply\": {\n" + | |||
" \"content\": \"CONTENT\",\n" + | |||
" \"create_time\": \"CREATE_TIME\"\n" + | |||
" }\n" + | |||
" }\n" + | |||
" ]\n" + | |||
"}"; | |||
wxService = spy(wxService); | |||
WxMpCommentService commentService = new WxMpCommentServiceImpl(wxService); | |||
doReturn(expectedResponse).when(wxService).post(anyString(), anyString()); | |||
final WxMpCommentListVo commentListVo = commentService.list(1, 1, 1, 1, 1); | |||
assertThat(commentListVo).isNotNull(); | |||
System.out.println(commentListVo); | |||
assertThat(commentListVo.getTotal()).isEqualTo(1); | |||
assertThat(commentListVo.getComment()).isNotEmpty(); | |||
assertThat(commentListVo.getComment().get(0).getReply()).isNotNull(); | |||
} | |||
} |