mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	增加获取客服聊天记录的重载方法,方便一次性获取一定时间间隔内的所有聊天记录
This commit is contained in:
		| @ -136,7 +136,7 @@ public interface WxMpKefuService { | |||||||
|   //*******************获取聊天记录的接口***********************// |   //*******************获取聊天记录的接口***********************// | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 获取聊天记录 |    * 获取聊天记录(原始接口) | ||||||
|    * 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video] |    * 此接口返回的聊天记录中,对于图片、语音、视频,分别展示成文本格式的[image]、[voice]、[video] | ||||||
|    * 详情请见:<a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN">获取聊天记录</a> |    * 详情请见:<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 |    * 接口url格式: https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN | ||||||
| @ -149,6 +149,21 @@ public interface WxMpKefuService { | |||||||
|    * @return 聊天记录对象 |    * @return 聊天记录对象 | ||||||
|    * @throws WxErrorException |    * @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 |   @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)){ |     if(startTime.after(endTime)){ | ||||||
|       throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build()); |       throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build()); | ||||||
|     } |     } | ||||||
| @ -150,4 +154,23 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||||||
|     return WxMpKfMsgList.fromJson(responseContent); |     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 |   @Test | ||||||
|   public void testKfMsgList() throws WxErrorException, JsonProcessingException { |   public void testKfMsgList() throws WxErrorException, JsonProcessingException { | ||||||
|     BasicConfigurator.configureDefaultContext(); |  | ||||||
|     Date startTime = DateTime.now().minusDays(1).toDate(); |     Date startTime = DateTime.now().minusDays(1).toDate(); | ||||||
|     Date endTime = DateTime.now().toDate(); |     Date endTime = DateTime.now().minusDays(0).toDate(); | ||||||
|     WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime, 0, 20); |     WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime,endTime, 1L, 50); | ||||||
|     Assert.assertNotNull(result); |     Assert.assertNotNull(result); | ||||||
|     System.err.println(new ObjectMapper().writeValueAsString(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)); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 BinaryWang
					BinaryWang