From e169bcc2e972424e8b82e165fee4e2a78fd884f3 Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Mon, 4 Jul 2016 17:31:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=9A=E5=AE=A2=E6=9C=8D?= =?UTF-8?q?=E4=B8=AD=E5=AE=A2=E6=9C=8D=E7=AE=A1=E7=90=86=E7=9A=846?= =?UTF-8?q?=E4=B8=AA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/mp/api/WxMpBaseChildService.java | 15 ++ .../weixin/mp/api/WxMpKefuService.java | 73 ++++++++++ .../me/chanjar/weixin/mp/api/WxMpService.java | 57 ++++++-- .../weixin/mp/api/WxMpServiceImpl.java | 12 ++ .../mp/api/impl/WxMpKefuServiceImpl.java | 72 ++++++++++ .../request/WxMpKfAccountRequest.java | 133 ++++++++++++++++++ .../customerservice/result/WxMpKfInfo.java | 123 ++++++++++++++++ .../customerservice/result/WxMpKfList.java | 35 +++++ .../result/WxMpKfOnlineList.java | 35 +++++ .../chanjar/weixin/mp/api/ApiTestModule.java | 44 ++++-- .../weixin/mp/api/impl/WxMpKefuImplTest.java | 70 +++++++++ .../mp/bean/result/kefu/WxMpKfListTest.java | 41 ++++++ .../result/kefu/WxMpKfOnlineListTest.java | 39 +++++ 13 files changed, 725 insertions(+), 24 deletions(-) create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpBaseChildService.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpKefuService.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImpl.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/request/WxMpKfAccountRequest.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfInfo.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfList.java create mode 100644 weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfOnlineList.java create mode 100644 weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuImplTest.java create mode 100644 weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfListTest.java create mode 100644 weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfOnlineListTest.java 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 kfList; + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } + + public List getKfList() { + return this.kfList; + } + + public void setKfList(List kfList) { + this.kfList = kfList; + } + + public static WxMpKfList fromJson(String json) { + return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpKfList.class); + } +} diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfOnlineList.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfOnlineList.java new file mode 100644 index 00000000..949d8849 --- /dev/null +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/customerservice/result/WxMpKfOnlineList.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 WxMpKfOnlineList { + @SerializedName("kf_online_list") + private List kfOnlineList; + + @Override + public String toString() { + return ToStringBuilder.reflectionToString(this); + } + + public List getKfOnlineList() { + return this.kfOnlineList; + } + + public void setKfOnlineList(List kfOnlineList) { + this.kfOnlineList = kfOnlineList; + } + + public static WxMpKfOnlineList fromJson(String json) { + return WxMpGsonBuilder.INSTANCE.create().fromJson(json, WxMpKfOnlineList.class); + } +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/ApiTestModule.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/ApiTestModule.java index e6df8e34..7c3db8e8 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/ApiTestModule.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/ApiTestModule.java @@ -1,24 +1,36 @@ package me.chanjar.weixin.mp.api; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.lang3.builder.ToStringBuilder; + import com.google.inject.Binder; import com.google.inject.Module; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamAlias; -import me.chanjar.weixin.common.util.xml.XStreamInitializer; -import java.io.InputStream; +import me.chanjar.weixin.common.util.xml.XStreamInitializer; +import me.chanjar.weixin.mp.api.impl.WxMpKefuServiceImpl; public class ApiTestModule implements Module { @Override public void configure(Binder binder) { - InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml"); - WxXmlMpInMemoryConfigStorage config = fromXml(WxXmlMpInMemoryConfigStorage.class, is1); - WxMpServiceImpl wxService = new WxMpServiceImpl(); - wxService.setWxMpConfigStorage(config); + try (InputStream is1 = ClassLoader + .getSystemResourceAsStream("test-config.xml")) { + WxXmlMpInMemoryConfigStorage config = fromXml( + WxXmlMpInMemoryConfigStorage.class, is1); + WxMpServiceImpl wxService = new WxMpServiceImpl(); + wxService.setWxMpConfigStorage(config); + WxMpKefuService customerService = new WxMpKefuServiceImpl(); + wxService.setCustomerService(customerService); - binder.bind(WxMpServiceImpl.class).toInstance(wxService); - binder.bind(WxMpConfigStorage.class).toInstance(config); + binder.bind(WxMpServiceImpl.class).toInstance(wxService); + binder.bind(WxMpConfigStorage.class).toInstance(config); + } catch (IOException e) { + e.printStackTrace(); + } } public static T fromXml(Class clazz, InputStream is) { @@ -29,22 +41,24 @@ public class ApiTestModule implements Module { } @XStreamAlias("xml") - public static class WxXmlMpInMemoryConfigStorage extends WxMpInMemoryConfigStorage { - + public static class WxXmlMpInMemoryConfigStorage + extends WxMpInMemoryConfigStorage { + protected String openId; public String getOpenId() { - return openId; + return this.openId; } + public void setOpenId(String openId) { this.openId = openId; } + @Override public String toString() { - return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken - + ", expiresTime=" + expiresTime + ", token=" + token + ", openId=" + openId + "]"; + return ToStringBuilder.reflectionToString(this); } - + } - + } diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuImplTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuImplTest.java new file mode 100644 index 00000000..f5d4ab04 --- /dev/null +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuImplTest.java @@ -0,0 +1,70 @@ +package me.chanjar.weixin.mp.api.impl; + +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import com.google.inject.Inject; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.mp.api.ApiTestModule; +import me.chanjar.weixin.mp.api.WxMpServiceImpl; +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; + +import java.io.File; + +import org.testng.Assert; + +/** + * 测试客服相关接口 + * @author Binary Wang + * + */ +@Test +@Guice(modules = ApiTestModule.class) +public class WxMpKefuImplTest { + private static final String KF_ACCOUNT = "kf2009@youxintest"; + + @Inject + protected WxMpServiceImpl wxService; + + public void testKfList() throws WxErrorException { + WxMpKfList kfList = this.wxService.getKefuService().kfList(); + Assert.assertNotNull(kfList); + System.err.println(kfList); + } + + public void testKfOnlineList() throws WxErrorException { + WxMpKfOnlineList kfOnlineList = this.wxService.getKefuService().kfOnlineList(); + Assert.assertNotNull(kfOnlineList); + System.err.println(kfOnlineList); + } + + public void testKfAccountAdd() throws WxErrorException { + WxMpKfAccountRequest request = WxMpKfAccountRequest.builder() + .kfAccount(KF_ACCOUNT).nickName("我晕").rawPassword("123").build(); + Assert.assertTrue(this.wxService.getKefuService().kfAccountAdd(request)); + } + + @Test(dependsOnMethods = { "testKfAccountAdd" }) + public void testKfAccountUpdate() throws WxErrorException { + WxMpKfAccountRequest request = WxMpKfAccountRequest.builder() + .kfAccount(KF_ACCOUNT).nickName("我晕").rawPassword("123").build(); + Assert.assertTrue(this.wxService.getKefuService().kfAccountUpdate(request)); + } + + @Test(dependsOnMethods = { "testKfAccountUpdate" }) + public void testKfAccountUploadHeadImg() throws WxErrorException { + File imgFile = new File("src\\test\\resources\\mm.jpeg"); + boolean result = this.wxService.getKefuService().kfAccountUploadHeadImg(KF_ACCOUNT, imgFile); + Assert.assertTrue(result); + } + + @Test(dependsOnMethods = { "testKfAccountUploadHeadImg" }) + public void testKfAccountDel() throws WxErrorException { + boolean result = this.wxService.getKefuService().kfAccountDel(KF_ACCOUNT); + Assert.assertTrue(result); + } + +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfListTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfListTest.java new file mode 100644 index 00000000..0d3841ef --- /dev/null +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfListTest.java @@ -0,0 +1,41 @@ +package me.chanjar.weixin.mp.bean.result.kefu; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.testng.Assert; +import org.testng.annotations.Test; + +import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfList; + +@Test +public class WxMpKfListTest { + + public void testFromJson() { + String json=" {\r\n" + + " \"kf_list\" : [\r\n" + + " {\r\n" + + " \"kf_account\" : \"test1@test\",\r\n" + + " \"kf_headimgurl\" : \"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjfUS8Ym0GSaLic0FD3vN0V8PILcibEGb2fPfEOmw/0\",\r\n" + + " \"kf_id\" : \"1001\",\r\n" + + " \"kf_nick\" : \"ntest1\"\r\n" + + " },\r\n" + + " {\r\n" + + " \"kf_account\" : \"test2@test\",\r\n" + + " \"kf_headimgurl\" : \"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjfUS8Ym0GSaLic0FD3vN0V8PILcibEGb2fPfEOmw/0\",\r\n" + + " \"kf_id\" : \"1002\",\r\n" + + " \"kf_nick\" : \"ntest2\"\r\n" + + " },\r\n" + + " {\r\n" + + " \"kf_account\" : \"test3@test\",\r\n" + + " \"kf_headimgurl\" : \"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjfUS8Ym0GSaLic0FD3vN0V8PILcibEGb2fPfEOmw/0\",\r\n" + + " \"kf_id\" : \"1003\",\r\n" + + " \"kf_nick\" : \"ntest3\"\r\n" + + " }\r\n" + + " ]\r\n" + + " }"; + + WxMpKfList wxMpKfList = WxMpKfList.fromJson(json); + Assert.assertNotNull(wxMpKfList); + System.err.println(ToStringBuilder.reflectionToString(wxMpKfList)); + } + +} diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfOnlineListTest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfOnlineListTest.java new file mode 100644 index 00000000..052f8e25 --- /dev/null +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/result/kefu/WxMpKfOnlineListTest.java @@ -0,0 +1,39 @@ +package me.chanjar.weixin.mp.bean.result.kefu; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.testng.Assert; +import org.testng.annotations.Test; + +import me.chanjar.weixin.mp.bean.customerservice.result.WxMpKfOnlineList; + +@Test +public class WxMpKfOnlineListTest { + + @Test + public void testFromJson() { + String json = "{\r\n" + + " \"kf_online_list\": [\r\n" + + " {\r\n" + + " \"kf_account\": \"test1@test\", \r\n" + + " \"status\": 1, \r\n" + + " \"kf_id\": \"1001\", \r\n" + + " \"auto_accept\": 0, \r\n" + + " \"accepted_case\": 1\r\n" + + " },\r\n" + + " {\r\n" + + " \"kf_account\": \"test2@test\", \r\n" + + " \"status\": 1, \r\n" + + " \"kf_id\": \"1002\", \r\n" + + " \"auto_accept\": 0, \r\n" + + " \"accepted_case\": 2\r\n" + + " }\r\n" + + " ]\r\n" + + "}"; + + WxMpKfOnlineList wxMpKfOnlineList = WxMpKfOnlineList.fromJson(json); + Assert.assertNotNull(wxMpKfOnlineList); + System.err.println(ToStringBuilder.reflectionToString(wxMpKfOnlineList)); + + } + +}