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 f57c9630..3e74d041 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 @@ -9,14 +9,27 @@ import java.io.File; import java.util.Date; /** + *
* 客服接口 , - * 命名采用kefu拼音的原因是: - * 其英文CustomerService如果再加上Service后缀显得有点啰嗦, - * 如果不加又显得表意不完整 - * + * 注意:命名采用kefu拼音的原因是:其英文CustomerService如果再加上Service后缀显得有点啰嗦,如果不加又显得表意不完整。 + ** @author Binary Wang */ 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"; /** *
@@ -82,7 +95,7 @@ public interface WxMpKefuService {
*
*/
boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
- throws WxErrorException;
+ 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 4667f327..b5a6b395 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
@@ -18,15 +18,10 @@ import java.io.File;
import java.util.Date;
/**
- *
* @author Binary Wang
- *
*/
public class WxMpKefuServiceImpl implements WxMpKefuService {
- protected final Logger log = LoggerFactory
- .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";
+ protected final Logger log = LoggerFactory.getLogger(this.getClass());
private WxMpService wxMpService;
public WxMpKefuServiceImpl(WxMpService wxMpService) {
@@ -34,127 +29,103 @@ public class WxMpKefuServiceImpl implements WxMpKefuService {
}
@Override
- public boolean sendKefuMessage(WxMpKefuMessage message)
- throws WxErrorException {
- String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
- String responseContent = this.wxMpService.post(url, message.toJson());
+ public boolean sendKefuMessage(WxMpKefuMessage message) throws WxErrorException {
+ String responseContent = this.wxMpService.post(MESSAGE_CUSTOM_SEND, message.toJson());
return responseContent != null;
}
@Override
public WxMpKfList kfList() throws WxErrorException {
- String url = API_URL_PREFIX_WITH_CGI_BIN + "/getkflist";
- String responseContent = this.wxMpService.get(url, null);
+ String responseContent = this.wxMpService.get(GET_KF_LIST, null);
return WxMpKfList.fromJson(responseContent);
}
@Override
public WxMpKfOnlineList kfOnlineList() throws WxErrorException {
- String url = API_URL_PREFIX_WITH_CGI_BIN + "/getonlinekflist";
- String responseContent = this.wxMpService.get(url, null);
+ String responseContent = this.wxMpService.get(GET_ONLINE_KF_LIST, null);
return WxMpKfOnlineList.fromJson(responseContent);
}
@Override
- public boolean kfAccountAdd(WxMpKfAccountRequest request)
- throws WxErrorException {
- String url = API_URL_PREFIX + "/kfaccount/add";
- String responseContent = this.wxMpService.post(url, request.toJson());
+ public boolean kfAccountAdd(WxMpKfAccountRequest request) throws WxErrorException {
+ String responseContent = this.wxMpService.post(KFACCOUNT_ADD, request.toJson());
return responseContent != null;
}
@Override
- public boolean kfAccountUpdate(WxMpKfAccountRequest request)
- throws WxErrorException {
- String url = API_URL_PREFIX + "/kfaccount/update";
- String responseContent = this.wxMpService.post(url, request.toJson());
+ public boolean kfAccountUpdate(WxMpKfAccountRequest request) throws WxErrorException {
+ String responseContent = this.wxMpService.post(KFACCOUNT_UPDATE, request.toJson());
return responseContent != null;
}
@Override
public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErrorException {
- String url = API_URL_PREFIX + "/kfaccount/inviteworker";
- String responseContent = this.wxMpService.post(url, request.toJson());
+ String responseContent = this.wxMpService.post(KFACCOUNT_INVITE_WORKER, request.toJson());
return responseContent != null;
}
@Override
- public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile)
- throws WxErrorException {
- String url = API_URL_PREFIX + "/kfaccount/uploadheadimg?kf_account=" + kfAccount;
+ public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException {
WxMediaUploadResult responseContent = this.wxMpService
- .execute(new MediaUploadRequestExecutor(), url, imgFile);
+ .execute(new MediaUploadRequestExecutor(), String.format(KFACCOUNT_UPLOAD_HEAD_IMG, kfAccount), imgFile);
return responseContent != null;
}
@Override
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
- String url = API_URL_PREFIX + "/kfaccount/del?kf_account=" + kfAccount;
- String responseContent = this.wxMpService.get(url, null);
+ String responseContent = this.wxMpService.get(String.format(KFACCOUNT_DEL, kfAccount), null);
return responseContent != null;
}
@Override
- public boolean kfSessionCreate(String openid, String kfAccount)
- throws WxErrorException {
+ public boolean kfSessionCreate(String openid, String kfAccount) throws WxErrorException {
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
- String url = API_URL_PREFIX + "/kfsession/create";
- String responseContent = this.wxMpService.post(url, request.toJson());
+ String responseContent = this.wxMpService.post(KFSESSION_CREATE, request.toJson());
return responseContent != null;
}
@Override
- public boolean kfSessionClose(String openid, String kfAccount)
- throws WxErrorException {
+ public boolean kfSessionClose(String openid, String kfAccount) throws WxErrorException {
WxMpKfSessionRequest request = new WxMpKfSessionRequest(kfAccount, openid);
- String url = API_URL_PREFIX + "/kfsession/close";
- String responseContent = this.wxMpService.post(url, request.toJson());
+ String responseContent = this.wxMpService.post(KFSESSION_CLOSE, request.toJson());
return responseContent != null;
}
@Override
- public WxMpKfSessionGetResult kfSessionGet(String openid)
- throws WxErrorException {
- String url = API_URL_PREFIX + "/kfsession/getsession?openid=" + openid;
- String responseContent = this.wxMpService.get(url, null);
+ public WxMpKfSessionGetResult kfSessionGet(String openid) throws WxErrorException {
+ String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION, openid), null);
return WxMpKfSessionGetResult.fromJson(responseContent);
}
@Override
- public WxMpKfSessionList kfSessionList(String kfAccount)
- throws WxErrorException {
- String url = API_URL_PREFIX + "/kfsession/getsessionlist?kf_account=" + kfAccount;
- String responseContent = this.wxMpService.get(url, null);
+ public WxMpKfSessionList kfSessionList(String kfAccount) throws WxErrorException {
+ String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION_LIST, kfAccount), null);
return WxMpKfSessionList.fromJson(responseContent);
}
@Override
- public WxMpKfSessionWaitCaseList kfSessionGetWaitCase()
- throws WxErrorException {
- String url = API_URL_PREFIX + "/kfsession/getwaitcase";
- String responseContent = this.wxMpService.get(url, null);
+ public WxMpKfSessionWaitCaseList kfSessionGetWaitCase() throws WxErrorException {
+ String responseContent = this.wxMpService.get(KFSESSION_GET_WAIT_CASE, null);
return WxMpKfSessionWaitCaseList.fromJson(responseContent);
}
@Override
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());
}
- if(startTime.after(endTime)){
+ if (startTime.after(endTime)) {
throw new WxErrorException(WxError.newBuilder().setErrorMsg("起始时间不能晚于结束时间!").build());
}
- String url = API_URL_PREFIX + "/msgrecord/getmsglist";
-
JsonObject param = new JsonObject();
param.addProperty("starttime", startTime.getTime() / 1000); //starttime 起始时间,unix时间戳
param.addProperty("endtime", endTime.getTime() / 1000); //endtime 结束时间,unix时间戳,每次查询时段不能超过24小时
param.addProperty("msgid", msgId); //msgid 消息id顺序从小到大,从1开始
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);
}
@@ -162,16 +133,16 @@ public class WxMpKefuServiceImpl implements WxMpKefuService {
@Override
public WxMpKfMsgList kfMsgList(Date startTime, Date endTime) throws WxErrorException {
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();
- WxMpKfMsgList followingResult = this.kfMsgList(startTime,endTime, msgId, number);
- while(followingResult != null && followingResult.getRecords().size() > 0){
+ 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);
+ followingResult = this.kfMsgList(startTime, endTime, followingResult.getMsgId(), number);
}
}