mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-23 23:58:44 +08:00
增加获取客服聊天记录的重载方法,方便一次性获取一定时间间隔内的所有聊天记录
This commit is contained in:
parent
c040ba3929
commit
21dcf17a36
@ -136,7 +136,7 @@ public interface WxMpKefuService {
|
||||
//*******************获取聊天记录的接口***********************//
|
||||
/**
|
||||
* <pre>
|
||||
* 获取聊天记录
|
||||
* 获取聊天记录(原始接口)
|
||||
* 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
|
||||
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN">获取聊天记录</a>
|
||||
* 接口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;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 获取聊天记录(优化接口,返回指定时间段内所有的聊天记录)
|
||||
* 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video]
|
||||
* 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN">获取聊天记录</a>
|
||||
* 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @param startTime 起始时间
|
||||
* @param endTime 结束时间
|
||||
* @return 聊天记录对象
|
||||
* @throws WxErrorException
|
||||
*/
|
||||
WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException;
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user