From 21dcf17a360ff8d390dd14fb712c64fd3f7ad4a1 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Mon, 18 Jul 2016 14:36:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=AE=A2?= =?UTF-8?q?=E6=9C=8D=E8=81=8A=E5=A4=A9=E8=AE=B0=E5=BD=95=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=E6=96=B9=E6=B3=95=EF=BC=8C=E6=96=B9=E4=BE=BF=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=80=A7=E8=8E=B7=E5=8F=96=E4=B8=80=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E9=97=B4=E9=9A=94=E5=86=85=E7=9A=84=E6=89=80=E6=9C=89?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/mp/api/WxMpKefuService.java | 19 ++++++++++++-- .../mp/api/impl/WxMpKefuServiceImpl.java | 25 ++++++++++++++++++- .../mp/api/impl/WxMpKefuServiceImplTest.java | 13 +++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java index 38eaa8a0..7e14cb8f 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java @@ -136,7 +136,7 @@ public interface WxMpKefuService { //*******************获取聊天记录的接口***********************// /** *
-   * 获取聊天记录
+   * 获取聊天记录(原始接口)
    * 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
    * 详情请见:获取聊天记录
    * 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
@@ -149,6 +149,21 @@ public interface WxMpKefuService {
    * @return 聊天记录对象
    * @throws WxErrorException
    */
-  WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Integer number) throws WxErrorException;
+  WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException;
+
+  /**
+   * 
+   * 获取聊天记录(优化接口,返回指定时间段内所有的聊天记录)
+   * 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
+   * 详情请见:获取聊天记录
+   * 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
+   * 
+ * + * @param startTime 起始时间 + * @param endTime 结束时间 + * @return 聊天记录对象 + * @throws WxErrorException + */ + WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException; } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java index d3857700..f3df9af0 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java @@ -133,7 +133,11 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { } @Override - public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Integer number) throws WxErrorException { + public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException { + if(number > 10000){ + throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法参数请求,每次最多查询10000条记录!").build()); + } + if(startTime.after(endTime)){ throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build()); } @@ -150,4 +154,23 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { return WxMpKfMsgList.fromJson(responseContent); } + @Override + public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException { + int number = 10000; + WxMpKfMsgList result = this.kfMsgList(startTime,endTime, 1L, number); + Long msgId = result.getMsgId(); + + if(result != null && result.getNumber() >= number){ + WxMpKfMsgList followingResult = this.kfMsgList(startTime,endTime, msgId, number); + while(followingResult != null && followingResult.getRecords().size() > 0){ + result.getRecords().addAll(followingResult.getRecords()); + result.setNumber(result.getNumber() + followingResult.getNumber()); + result.setMsgId(followingResult.getMsgId()); + followingResult = this.kfMsgList(startTime,endTime, followingResult.getMsgId(), number); + } + } + + return result; + } + } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java index 3f3e32dd..edfdc0b6 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java @@ -147,12 +147,19 @@ public class WxMpKefuServiceImplTest { @Test public void testKfMsgList() throws WxErrorException, JsonProcessingException { - BasicConfigurator.configureDefaultContext(); Date startTime = DateTime.now().minusDays(1).toDate(); - Date endTime = DateTime.now().toDate(); - WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime, 0, 20); + Date endTime = DateTime.now().minusDays(0).toDate(); + WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime, 1L, 50); Assert.assertNotNull(result); System.err.println(new ObjectMapper().writeValueAsString(result)); } + @Test + public void testKfMsgListAll() throws WxErrorException, JsonProcessingException { + Date startTime = DateTime.now().minusDays(1).toDate(); + Date endTime = DateTime.now().minusDays(0).toDate(); + WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime); + Assert.assertNotNull(result); + System.err.println(new ObjectMapper().writeValueAsString(result)); + } }