mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 02:28:25 +08:00 
			
		
		
		
	抽取客服相关接口请求URL到其接口类中 #195
This commit is contained in:
		| @ -9,14 +9,27 @@ import java.io.File; | |||||||
| import java.util.Date; | import java.util.Date; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  |  * <pre> | ||||||
|  * 客服接口 , |  * 客服接口 , | ||||||
|  * 命名采用kefu拼音的原因是: |  * 注意:命名采用kefu拼音的原因是:其英文CustomerService如果再加上Service后缀显得有点啰嗦,如果不加又显得表意不完整。 | ||||||
|  * 其英文CustomerService如果再加上Service后缀显得有点啰嗦, |  * </pre> | ||||||
|  * 如果不加又显得表意不完整 |  | ||||||
|  * |  | ||||||
|  * @author Binary Wang |  * @author Binary Wang | ||||||
|  */ |  */ | ||||||
| public interface WxMpKefuService { | public interface WxMpKefuService { | ||||||
|  |   String MESSAGE_CUSTOM_SEND = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; | ||||||
|  |   String GET_KF_LIST = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist"; | ||||||
|  |   String GET_ONLINE_KF_LIST = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist"; | ||||||
|  |   String KFACCOUNT_ADD = "https://api.weixin.qq.com/customservice/kfaccount/add"; | ||||||
|  |   String KFACCOUNT_UPDATE = "https://api.weixin.qq.com/customservice/kfaccount/update"; | ||||||
|  |   String KFACCOUNT_INVITE_WORKER = "https://api.weixin.qq.com/customservice/kfaccount/inviteworker"; | ||||||
|  |   String KFACCOUNT_UPLOAD_HEAD_IMG = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account=%s"; | ||||||
|  |   String KFACCOUNT_DEL = "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account=%s"; | ||||||
|  |   String KFSESSION_CREATE = "https://api.weixin.qq.com/customservice/kfsession/create"; | ||||||
|  |   String KFSESSION_CLOSE = "https://api.weixin.qq.com/customservice/kfsession/close"; | ||||||
|  |   String KFSESSION_GET_SESSION = "https://api.weixin.qq.com/customservice/kfsession/getsession?openid=%s"; | ||||||
|  |   String KFSESSION_GET_SESSION_LIST = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?kf_account=%s"; | ||||||
|  |   String KFSESSION_GET_WAIT_CASE = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase"; | ||||||
|  |   String MSGRECORD_GET_MSG_LIST = "https://api.weixin.qq.com/customservice/msgrecord/getmsglist"; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|  | |||||||
| @ -18,15 +18,10 @@ import java.io.File; | |||||||
| import java.util.Date; | import java.util.Date; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * |  | ||||||
|  * @author Binary Wang |  * @author Binary Wang | ||||||
|  * |  | ||||||
|  */ |  */ | ||||||
| public class WxMpKefuServiceImpl implements WxMpKefuService { | public class WxMpKefuServiceImpl implements WxMpKefuService { | ||||||
|   protected final Logger log = LoggerFactory |   protected final Logger log = LoggerFactory.getLogger(this.getClass()); | ||||||
|       .getLogger(WxMpKefuServiceImpl.class); |  | ||||||
|   private static final String API_URL_PREFIX = "https://api.weixin.qq.com/customservice"; |  | ||||||
|   private static final String API_URL_PREFIX_WITH_CGI_BIN = "https://api.weixin.qq.com/cgi-bin/customservice"; |  | ||||||
|   private WxMpService wxMpService; |   private WxMpService wxMpService; | ||||||
|  |  | ||||||
|   public WxMpKefuServiceImpl(WxMpService wxMpService) { |   public WxMpKefuServiceImpl(WxMpService wxMpService) { | ||||||
| @ -34,127 +29,103 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean sendKefuMessage(WxMpKefuMessage message) |   public boolean sendKefuMessage(WxMpKefuMessage message) throws WxErrorException { | ||||||
|       throws WxErrorException { |     String responseContent = this.wxMpService.post(MESSAGE_CUSTOM_SEND, message.toJson()); | ||||||
|     String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send"; |  | ||||||
|     String responseContent = this.wxMpService.post(url, message.toJson()); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKfList kfList() throws WxErrorException { |   public WxMpKfList kfList() throws WxErrorException { | ||||||
|     String url = API_URL_PREFIX_WITH_CGI_BIN + "/getkflist"; |     String responseContent = this.wxMpService.get(GET_KF_LIST, null); | ||||||
|     String responseContent = this.wxMpService.get(url, null); |  | ||||||
|     return WxMpKfList.fromJson(responseContent); |     return WxMpKfList.fromJson(responseContent); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKfOnlineList kfOnlineList() throws WxErrorException { |   public WxMpKfOnlineList kfOnlineList() throws WxErrorException { | ||||||
|     String url = API_URL_PREFIX_WITH_CGI_BIN + "/getonlinekflist"; |     String responseContent = this.wxMpService.get(GET_ONLINE_KF_LIST, null); | ||||||
|     String responseContent = this.wxMpService.get(url, null); |  | ||||||
|     return WxMpKfOnlineList.fromJson(responseContent); |     return WxMpKfOnlineList.fromJson(responseContent); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfAccountAdd(WxMpKfAccountRequest request) |   public boolean kfAccountAdd(WxMpKfAccountRequest request) throws WxErrorException { | ||||||
|       throws WxErrorException { |     String responseContent = this.wxMpService.post(KFACCOUNT_ADD, request.toJson()); | ||||||
|     String url = API_URL_PREFIX + "/kfaccount/add"; |  | ||||||
|     String responseContent = this.wxMpService.post(url, request.toJson()); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfAccountUpdate(WxMpKfAccountRequest request) |   public boolean kfAccountUpdate(WxMpKfAccountRequest request) throws WxErrorException { | ||||||
|       throws WxErrorException { |     String responseContent = this.wxMpService.post(KFACCOUNT_UPDATE, request.toJson()); | ||||||
|     String url = API_URL_PREFIX + "/kfaccount/update"; |  | ||||||
|     String responseContent = this.wxMpService.post(url, request.toJson()); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException { |   public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException { | ||||||
|     String url = API_URL_PREFIX + "/kfaccount/inviteworker"; |     String responseContent = this.wxMpService.post(KFACCOUNT_INVITE_WORKER, request.toJson()); | ||||||
|     String responseContent = this.wxMpService.post(url, request.toJson()); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) |   public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException { | ||||||
|       throws WxErrorException { |  | ||||||
|     String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount; |  | ||||||
|     WxMediaUploadResult responseContent = this.wxMpService |     WxMediaUploadResult responseContent = this.wxMpService | ||||||
|         .execute(new MediaUploadRequestExecutor(), url, imgFile); |       .execute(new MediaUploadRequestExecutor(), String.format(KFACCOUNT_UPLOAD_HEAD_IMG, kfAccount), imgFile); | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfAccountDel(String kfAccount) throws WxErrorException { |   public boolean kfAccountDel(String kfAccount) throws WxErrorException { | ||||||
|     String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount; |     String responseContent = this.wxMpService.get(String.format(KFACCOUNT_DEL, kfAccount), null); | ||||||
|     String responseContent = this.wxMpService.get(url, null); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfSessionCreate(String openid, String kfAccount) |   public boolean kfSessionCreate(String openid, String kfAccount) throws WxErrorException { | ||||||
|       throws WxErrorException { |  | ||||||
|     WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); |     WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); | ||||||
|     String url = API_URL_PREFIX + "/kfsession/create"; |     String responseContent = this.wxMpService.post(KFSESSION_CREATE, request.toJson()); | ||||||
|     String responseContent = this.wxMpService.post(url, request.toJson()); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public boolean kfSessionClose(String openid, String kfAccount) |   public boolean kfSessionClose(String openid, String kfAccount) throws WxErrorException { | ||||||
|       throws WxErrorException { |  | ||||||
|     WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); |     WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid); | ||||||
|     String url = API_URL_PREFIX + "/kfsession/close"; |     String responseContent = this.wxMpService.post(KFSESSION_CLOSE, request.toJson()); | ||||||
|     String responseContent = this.wxMpService.post(url, request.toJson()); |  | ||||||
|     return responseContent != null; |     return responseContent != null; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKfSessionGetResult kfSessionGet(String openid) |   public WxMpKfSessionGetResult kfSessionGet(String openid) throws WxErrorException { | ||||||
|       throws WxErrorException { |     String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION, openid), null); | ||||||
|     String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid; |  | ||||||
|     String responseContent = this.wxMpService.get(url, null); |  | ||||||
|     return WxMpKfSessionGetResult.fromJson(responseContent); |     return WxMpKfSessionGetResult.fromJson(responseContent); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKfSessionList kfSessionList(String kfAccount) |   public WxMpKfSessionList kfSessionList(String kfAccount) throws WxErrorException { | ||||||
|       throws WxErrorException { |     String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION_LIST, kfAccount), null); | ||||||
|     String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount; |  | ||||||
|     String responseContent = this.wxMpService.get(url, null); |  | ||||||
|     return WxMpKfSessionList.fromJson(responseContent); |     return WxMpKfSessionList.fromJson(responseContent); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKfSessionWaitCaseList kfSessionGetWaitCase() |   public WxMpKfSessionWaitCaseList kfSessionGetWaitCase() throws WxErrorException { | ||||||
|       throws WxErrorException { |     String responseContent = this.wxMpService.get(KFSESSION_GET_WAIT_CASE, null); | ||||||
|     String url = API_URL_PREFIX + "/kfsession/getwaitcase"; |  | ||||||
|     String responseContent = this.wxMpService.get(url, null); |  | ||||||
|     return WxMpKfSessionWaitCaseList.fromJson(responseContent); |     return WxMpKfSessionWaitCaseList.fromJson(responseContent); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException { |   public WxMpKfMsgList kfMsgList(Date startTime, Date endTime, Long msgId, Integer number) throws WxErrorException { | ||||||
|     if(number > 10000){ |     if (number > 10000) { | ||||||
|       throw new WxErrorException(WxError.newBuilder().setErrorMsg("非法参数请求,每次最多查询10000条记录!").build()); |       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()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     String url = API_URL_PREFIX + "/msgrecord/getmsglist"; |  | ||||||
|  |  | ||||||
|     JsonObject param = new JsonObject(); |     JsonObject param = new JsonObject(); | ||||||
|     param.addProperty("starttime", startTime.getTime() / 1000); //starttime	起始时间,unix时间戳 |     param.addProperty("starttime", startTime.getTime() / 1000); //starttime	起始时间,unix时间戳 | ||||||
|     param.addProperty("endtime", endTime.getTime() / 1000); //endtime	结束时间,unix时间戳,每次查询时段不能超过24小时 |     param.addProperty("endtime", endTime.getTime() / 1000); //endtime	结束时间,unix时间戳,每次查询时段不能超过24小时 | ||||||
|     param.addProperty("msgid", msgId); //msgid	消息id顺序从小到大,从1开始 |     param.addProperty("msgid", msgId); //msgid	消息id顺序从小到大,从1开始 | ||||||
|     param.addProperty("number", number); //number	每次获取条数,最多10000条 |     param.addProperty("number", number); //number	每次获取条数,最多10000条 | ||||||
|  |  | ||||||
|     String responseContent = this.wxMpService.post(url, param.toString()); |     String responseContent = this.wxMpService.post(MSGRECORD_GET_MSG_LIST, param.toString()); | ||||||
|  |  | ||||||
|     return WxMpKfMsgList.fromJson(responseContent); |     return WxMpKfMsgList.fromJson(responseContent); | ||||||
|   } |   } | ||||||
| @ -162,16 +133,16 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||||||
|   @Override |   @Override | ||||||
|   public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException { |   public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException { | ||||||
|     int number = 10000; |     int number = 10000; | ||||||
|     WxMpKfMsgList result =  this.kfMsgList(startTime,endTime, 1L, number); |     WxMpKfMsgList result = this.kfMsgList(startTime, endTime, 1L, number); | ||||||
|  |  | ||||||
|     if(result != null && result.getNumber() == number){ |     if (result != null && result.getNumber() == number) { | ||||||
|       Long msgId = result.getMsgId(); |       Long msgId = result.getMsgId(); | ||||||
|       WxMpKfMsgList followingResult =  this.kfMsgList(startTime,endTime, msgId, number); |       WxMpKfMsgList followingResult = this.kfMsgList(startTime, endTime, msgId, number); | ||||||
|       while(followingResult != null  && followingResult.getRecords().size() > 0){ |       while (followingResult != null && followingResult.getRecords().size() > 0) { | ||||||
|         result.getRecords().addAll(followingResult.getRecords()); |         result.getRecords().addAll(followingResult.getRecords()); | ||||||
|         result.setNumber(result.getNumber() + followingResult.getNumber()); |         result.setNumber(result.getNumber() + followingResult.getNumber()); | ||||||
|         result.setMsgId(followingResult.getMsgId()); |         result.setMsgId(followingResult.getMsgId()); | ||||||
|         followingResult = this.kfMsgList(startTime,endTime, followingResult.getMsgId(), number); |         followingResult = this.kfMsgList(startTime, endTime, followingResult.getMsgId(), number); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang