diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpBaseChildService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpBaseChildService.java new file mode 100644 index 00000000..2176af57 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpBaseChildService.java @@ -0,0 +1,15 @@ +package me.chanjar.weixin.mp.api; + +/** + * + * @author Binary Wang + * + */ + +public interface WxMpBaseChildService { + /** + * 设置WxMpService对象 + * @return + */ + void setWxMpService(WxMpService wxMpService); +} 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 new file mode 100644 index 00000000..8220a673 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java @@ -0,0 +1,73 @@ +package me.chanjar.weixin.mp.api; + +import java.io.File; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.bean.customerservice.request.WxMpKfAccountRequest; +import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfList; +import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfOnlineList; + +/** + * 客服接口 , + * 命名采用kefu拼音的原因是: + * 其英文CustomerService如果再加上Service后缀显得有点啰嗦, + * 如果不加又显得表意不完整 + * @author Binary Wang + * + */ +public interface WxMpKefuService extends WxMpBaseChildService { + /** + *
+ * 获取客服基本信息 + * 详情请见:客服管理 + * https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=ACCESS_TOKEN + *+ */ + WxMpKfList kfList() throws WxErrorException; + + /** + *
+ * 获取在线客服接待信息 + * 详情请见:客服管理 + * https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=ACCESS_TOKEN + *+ */ + WxMpKfOnlineList kfOnlineList() throws WxErrorException; + + /** + *
+ * 添加客服账号 + * 详情请见:客服管理 + * https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN + *+ */ + boolean kfAccountAdd(WxMpKfAccountRequest request) throws WxErrorException; + + /** + *
+ * 设置客服信息(更新) + * 详情请见:客服管理 + * https://api.weixin.qq.com/customservice/kfaccount/update?access_token=ACCESS_TOKEN + *+ */ + boolean kfAccountUpdate(WxMpKfAccountRequest request) throws WxErrorException; + + /** + *
+ * 上传客服头像 + * 详情请见:客服管理 + * http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT + *+ */ + + boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException; + + /** + *
+ * 删除客服账号 + * 详情请见:客服管理 + * https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT + *+ */ + boolean kfAccountDel(String kfAccount) throws WxErrorException; +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index d822fc33..634f97d2 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -1,14 +1,5 @@ package me.chanjar.weixin.mp.api; -import me.chanjar.weixin.common.bean.WxCardApiSignature; -import me.chanjar.weixin.common.bean.WxMenu; -import me.chanjar.weixin.common.bean.WxJsapiSignature; -import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.common.util.http.RequestExecutor; -import me.chanjar.weixin.mp.bean.*; -import me.chanjar.weixin.mp.bean.result.*; - import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -17,6 +8,47 @@ import java.util.Date; import java.util.List; import java.util.Map; +import me.chanjar.weixin.common.bean.WxCardApiSignature; +import me.chanjar.weixin.common.bean.WxJsapiSignature; +import me.chanjar.weixin.common.bean.WxMenu; +import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.RequestExecutor; +import me.chanjar.weixin.mp.bean.WxMpCustomMessage; +import me.chanjar.weixin.mp.bean.WxMpGroup; +import me.chanjar.weixin.mp.bean.WxMpIndustry; +import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage; +import me.chanjar.weixin.mp.bean.WxMpMassNews; +import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage; +import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage; +import me.chanjar.weixin.mp.bean.WxMpMassVideo; +import me.chanjar.weixin.mp.bean.WxMpMaterial; +import me.chanjar.weixin.mp.bean.WxMpMaterialArticleUpdate; +import me.chanjar.weixin.mp.bean.WxMpMaterialNews; +import me.chanjar.weixin.mp.bean.WxMpSemanticQuery; +import me.chanjar.weixin.mp.bean.WxMpTemplateMessage; +import me.chanjar.weixin.mp.bean.result.WxMediaImgUploadResult; +import me.chanjar.weixin.mp.bean.result.WxMpCardResult; +import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult; +import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult; +import me.chanjar.weixin.mp.bean.result.WxMpMaterialCountResult; +import me.chanjar.weixin.mp.bean.result.WxMpMaterialFileBatchGetResult; +import me.chanjar.weixin.mp.bean.result.WxMpMaterialNewsBatchGetResult; +import me.chanjar.weixin.mp.bean.result.WxMpMaterialUploadResult; +import me.chanjar.weixin.mp.bean.result.WxMpMaterialVideoInfoResult; +import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; +import me.chanjar.weixin.mp.bean.result.WxMpPayCallback; +import me.chanjar.weixin.mp.bean.result.WxMpPayRefundResult; +import me.chanjar.weixin.mp.bean.result.WxMpPayResult; +import me.chanjar.weixin.mp.bean.result.WxMpPrepayIdResult; +import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket; +import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult; +import me.chanjar.weixin.mp.bean.result.WxMpUser; +import me.chanjar.weixin.mp.bean.result.WxMpUserCumulate; +import me.chanjar.weixin.mp.bean.result.WxMpUserList; +import me.chanjar.weixin.mp.bean.result.WxMpUserSummary; +import me.chanjar.weixin.mp.bean.result.WxRedpackResult; + /** * 微信API的Service */ @@ -1001,4 +1033,11 @@ public interface WxMpService { * @throws WxErrorException */ WxMpIndustry getIndustry() throws WxErrorException; + + /** + * 返回客服接口方法实现类,以方便调用个其各种接口 + * @return WxMpKefuService + */ + WxMpKefuService getKefuService(); + } diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java index 18fb3aef..e7242101 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java @@ -74,6 +74,8 @@ public class WxMpServiceImpl implements WxMpService { protected final Object globalCardApiTicketRefreshLock = new Object(); protected WxMpConfigStorage wxMpConfigStorage; + + protected WxMpKefuService kefuService; protected CloseableHttpClient httpClient; @@ -1369,4 +1371,14 @@ public class WxMpServiceImpl implements WxMpService { String responseContent = execute(new SimpleGetRequestExecutor(), url, null); return WxMpIndustry.fromJson(responseContent); } + + @Override + public WxMpKefuService getKefuService() { + return this.kefuService; + } + + public void setCustomerService(WxMpKefuService kefuService) { + this.kefuService = kefuService; + this.kefuService.setWxMpService(this); + } } 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 new file mode 100644 index 00000000..8efcfdff --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java @@ -0,0 +1,72 @@ +package me.chanjar.weixin.mp.api.impl; + +import java.io.File; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; +import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; +import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; +import me.chanjar.weixin.mp.api.WxMpKefuService; +import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.customerservice.request.WxMpKfAccountRequest; +import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfList; +import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfOnlineList; + +/** + * + * @author Binary Wang + * + */ +public class WxMpKefuServiceImpl implements WxMpKefuService { + private WxMpService wxMpService; + + @Override + public void setWxMpService(WxMpService wxMpService) { + this.wxMpService = wxMpService; + } + + @Override + public WxMpKfList kfList() throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist"; + String responseContent = this.wxMpService.execute( + new SimpleGetRequestExecutor(), url, null); + return WxMpKfList.fromJson(responseContent); + } + + @Override + public WxMpKfOnlineList kfOnlineList() throws WxErrorException { + String url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist"; + String responseContent = this.wxMpService.execute( + new SimpleGetRequestExecutor(), url, null); + return WxMpKfOnlineList.fromJson(responseContent); + } + + @Override + public boolean kfAccountAdd(WxMpKfAccountRequest request) throws WxErrorException { + String url = "https://api.weixin.qq.com/customservice/kfaccount/add"; + this.wxMpService.execute(new SimplePostRequestExecutor(), url, request.toJson()); + return true; + } + + @Override + public boolean kfAccountUpdate(WxMpKfAccountRequest request) throws WxErrorException { + String url = "https://api.weixin.qq.com/customservice/kfaccount/update"; + this.wxMpService.execute(new SimplePostRequestExecutor(), url, request.toJson()); + return true; + } + + @Override + public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException { + String url = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account=" + kfAccount ; + this.wxMpService.execute(new MediaUploadRequestExecutor(), url, imgFile); + return true; + } + + @Override + public boolean kfAccountDel(String kfAccount) throws WxErrorException { + String url = "https://api.weixin.qq.com/customservice/kfaccount/del?kf_account=" + kfAccount; + this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null); + return true; + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/request/WxMpKfAccountRequest.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/request/WxMpKfAccountRequest.java new file mode 100644 index 00000000..a5d301da --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/request/WxMpKfAccountRequest.java @@ -0,0 +1,133 @@ +package me.chanjar.weixin.mp.bean.customerservice.request; + +import java.io.Serializable; + +import org.apache.commons.codec.digest.Md5Crypt; +import org.apache.commons.lang3.builder.ToStringBuilder; + +import com.google.gson.annotations.SerializedName; + +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; + +public class WxMpKfAccountRequest implements Serializable { + private static final long serialVersionUID = -5451863610674856927L; + + /** + * kf_account 完整客服账号,格式为:账号前缀@公众号微信号 + */ + @SerializedName("kf_account") + private String kfAccount; + + /** + * nickname 客服昵称,最长6个汉字或12个英文字符 + */ + @SerializedName("nickname") + private String nickName; + + /** + * password 客服账号登录密码,格式为密码明文的32位加密MD5值 + */ + @SerializedName("password") + private String password; + + /** + * rawPassword 客服账号登录密码,明文密码,仅用于辅助操作 + */ + @SerializedName("rawPassword") + private String rawPassword; + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } + + public String toJson() { + return WxMpGsonBuilder.INSTANCE.create().toJson(this); + } + + public String getKfAccount() { + return this.kfAccount; + } + + public void setKfAccount(String kfAccount) { + this.kfAccount = kfAccount; + } + + public String getNickName() { + return this.nickName; + } + + public void setNickName(String nickName) { + this.nickName = nickName; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRawPassword() { + return this.rawPassword; + } + + public void setRawPassword(String rawPassword) { + this.rawPassword = rawPassword; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String kfAccount; + private String nickName; + private String password; + private String rawPassword; + + @SuppressWarnings("hiding") + public Builder kfAccount(String kfAccount) { + this.kfAccount = kfAccount; + return this; + } + + @SuppressWarnings("hiding") + public Builder nickName(String nickName) { + this.nickName = nickName; + return this; + } + + @SuppressWarnings("hiding") + public Builder password(String password) { + this.password = password; + return this; + } + + @SuppressWarnings("hiding") + public Builder rawPassword(String rawPassword) { + this.rawPassword = rawPassword; + this.password(Md5Crypt.md5Crypt(rawPassword.getBytes())); + return this; + } + + public Builder from(WxMpKfAccountRequest origin) { + this.kfAccount(origin.kfAccount); + this.nickName(origin.nickName); + this.password(origin.password); + this.rawPassword(origin.rawPassword); + return this; + } + + public WxMpKfAccountRequest build() { + WxMpKfAccountRequest m = new WxMpKfAccountRequest(); + m.kfAccount = this.kfAccount; + m.nickName = this.nickName; + m.password = this.password; + m.rawPassword = this.rawPassword; + return m; + } + } + +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfInfo.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfInfo.java new file mode 100644 index 00000000..95b3a96f --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfInfo.java @@ -0,0 +1,123 @@ +package me.chanjar.weixin.mp.bean.customerservice.result; + +import java.io.Serializable; + +import org.apache.commons.lang3.builder.ToStringBuilder; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +/** + * 客服基本信息以及客服在线状态信息 + * @author Binary Wang + * + */ +public class WxMpKfInfo implements Serializable { + private static final long serialVersionUID = -5877300750666022290L; + + /** + * kf_account 完整客服账号,格式为:账号前缀@公众号微信号 + */ + @SerializedName("kf_account") + private String account; + + /** + * kf_headimgurl 客服头像地址 + */ + @SerializedName("kf_headimgurl") + private String headImgUrl; + + /** + * kf_id 客服工号 + */ + @SerializedName("kf_id") + private String id; + + /** + * kf_nick 客服昵称 + */ + @SerializedName("kf_nick") + private String nick; + + /** + * status 客服在线状态 1:pc在线,2:手机在线。若pc和手机同时在线则为 1+2=3 + */ + @SerializedName("status") + private Integer status; + + /** + * auto_accept 客服设置的最大自动接入数 + */ + @Expose + @SerializedName("auto_accept") + private Integer autoAccept; + + /** + * accepted_case 客服当前正在接待的会话数 + * @return + */ + @Expose + @SerializedName("accepted_case") + private Integer acceptedCase; + + public Integer getStatus() { + return this.status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public Integer getAutoAccept() { + return this.autoAccept; + } + + public void setAutoAccept(Integer autoAccept) { + this.autoAccept = autoAccept; + } + + public Integer getAcceptedCase() { + return this.acceptedCase; + } + + public void setAcceptedCase(Integer acceptedCase) { + this.acceptedCase = acceptedCase; + } + + public String getAccount() { + return this.account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getHeadImgUrl() { + return this.headImgUrl; + } + + public void setHeadImgUrl(String headImgUrl) { + this.headImgUrl = headImgUrl; + } + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public String getNick() { + return this.nick; + } + + public void setNick(String nick) { + this.nick = nick; + } + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfList.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfList.java new file mode 100644 index 00000000..335204fb --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfList.java @@ -0,0 +1,35 @@ +package me.chanjar.weixin.mp.bean.customerservice.result; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; + +import com.google.gson.annotations.SerializedName; + +import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; +/** + * + * @author Binary Wang + * + */ +public class WxMpKfList { + @SerializedName("kf_list") + private List