Skip to content

Commit 21dcf17

Browse files
committed
增加获取客服聊天记录的重载方法,方便一次性获取一定时间间隔内的所有聊天记录
1 parent c040ba3 commit 21dcf17

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
136136
//*******************获取聊天记录的接口***********************//
137137
/**
138138
* <pre>
139-
* 获取聊天记录
139+
* 获取聊天记录(原始接口)
140140
* 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
141141
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN">获取聊天记录</a>
142142
* 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
@@ -149,6 +149,21 @@ boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
149149
* @return 聊天记录对象
150150
* @throws WxErrorException
151151
*/
152-
WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Integer number) throws WxErrorException;
152+
WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException;
153+
154+
/**
155+
* <pre>
156+
* 获取聊天记录(优化接口,返回指定时间段内所有的聊天记录)
157+
* 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
158+
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN">获取聊天记录</a>
159+
* 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
160+
* </pre>
161+
*
162+
* @param startTime 起始时间
163+
* @param endTime 结束时间
164+
* @return 聊天记录对象
165+
* @throws WxErrorException
166+
*/
167+
WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException;
153168

154169
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ public WxMpKfSessionWaitCaseList kfSessionGetWaitCase()
133133
}
134134

135135
@Override
136-
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Integer number) throws WxErrorException {
136+
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException {
137+
if(number > 10000){
138+
throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法参数请求,每次最多查询10000条记录!").build());
139+
}
140+
137141
if(startTime.after(endTime)){
138142
throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build());
139143
}
@@ -150,4 +154,23 @@ public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Integer msgId, Inte
150154
return WxMpKfMsgList.fromJson(responseContent);
151155
}
152156

157+
@Override
158+
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException {
159+
int number = 10000;
160+
WxMpKfMsgList result = this.kfMsgList(startTime,endTime, 1L, number);
161+
Long msgId = result.getMsgId();
162+
163+
if(result != null && result.getNumber() >= number){
164+
WxMpKfMsgList followingResult = this.kfMsgList(startTime,endTime, msgId, number);
165+
while(followingResult != null && followingResult.getRecords().size() > 0){
166+
result.getRecords().addAll(followingResult.getRecords());
167+
result.setNumber(result.getNumber() + followingResult.getNumber());
168+
result.setMsgId(followingResult.getMsgId());
169+
followingResult = this.kfMsgList(startTime,endTime, followingResult.getMsgId(), number);
170+
}
171+
}
172+
173+
return result;
174+
}
175+
153176
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,19 @@ public void testKfSessionGetWaitCase() throws WxErrorException {
147147

148148
@Test
149149
public void testKfMsgList() throws WxErrorException, JsonProcessingException {
150-
BasicConfigurator.configureDefaultContext();
151150
Date startTime = DateTime.now().minusDays(1).toDate();
152-
Date endTime = DateTime.now().toDate();
153-
WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime, 0, 20);
151+
Date endTime = DateTime.now().minusDays(0).toDate();
152+
WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime, 1L, 50);
154153
Assert.assertNotNull(result);
155154
System.err.println(new ObjectMapper().writeValueAsString(result));
156155
}
157156

157+
@Test
158+
public void testKfMsgListAll() throws WxErrorException, JsonProcessingException {
159+
Date startTime = DateTime.now().minusDays(1).toDate();
160+
Date endTime = DateTime.now().minusDays(0).toDate();
161+
WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime);
162+
Assert.assertNotNull(result);
163+
System.err.println(new ObjectMapper().writeValueAsString(result));
164+
}
158165
}

0 commit comments

Comments
 (0)