diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java new file mode 100644 index 00000000..7ca1ade3 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpDepartmentService.java @@ -0,0 +1,57 @@ +package me.chanjar.weixin.cp.api; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.cp.bean.WxCpDepart; + +import java.util.List; + +/** + *
+ *  部门管理接口
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public interface WxCpDepartmentService { + + /** + *
+   * 部门管理接口 - 创建部门
+   * 最多支持创建500个部门
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
+   * 
+ * + * @param depart 部门 + * @return 部门id + */ + Integer create(WxCpDepart depart) throws WxErrorException; + + /** + *
+   * 部门管理接口 - 查询所有部门
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
+   * 
+ */ + List listAll() throws WxErrorException; + + /** + *
+   * 部门管理接口 - 修改部门名
+   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
+   * 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
+   * 
+ * + * @param group 要更新的group,group的id,name必须设置 + */ + void update(WxCpDepart group) throws WxErrorException; + + /** + *
+   * 部门管理接口 - 删除部门
+   * 
+ * + * @param departId 部门id + */ + void delete(Integer departId) throws WxErrorException; +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java new file mode 100644 index 00000000..ba0241a6 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java @@ -0,0 +1,11 @@ +package me.chanjar.weixin.cp.api; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public interface WxCpMediaService { +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java new file mode 100644 index 00000000..f6a45e43 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java @@ -0,0 +1,11 @@ +package me.chanjar.weixin.cp.api; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public interface WxCpMenuService { +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOauth2Service.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOauth2Service.java new file mode 100644 index 00000000..af86f1ed --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOauth2Service.java @@ -0,0 +1,11 @@ +package me.chanjar.weixin.cp.api; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public interface WxCpOauth2Service { +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java index 444fa98d..52692090 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java @@ -9,6 +9,7 @@ import me.chanjar.weixin.common.session.WxSessionManager; import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.cp.bean.*; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import java.io.File; import java.io.IOException; @@ -33,16 +34,6 @@ public interface WxCpService { */ boolean checkSignature(String msgSignature, String timestamp, String nonce, String data); - /** - *
-   *   用在二次验证的时候
-   *   企业在员工验证成功后,调用本方法告诉企业号平台该员工关注成功。
-   * 
- * - * @param userId 用户id - */ - void userAuthenticated(String userId) throws WxErrorException; - /** * 获取access_token, 不强制刷新access_token * @@ -218,109 +209,77 @@ public interface WxCpService { WxMenu menuGet(Integer agentId) throws WxErrorException; /** - *
-   * 部门管理接口 - 创建部门
-   * 最多支持创建500个部门
-   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
-   * 
- * - * @param depart 部门 - * @return 部门id + * @deprecated 请使用 {@link WxCpDepartmentService#create(WxCpDepart depart) } */ + @Deprecated Integer departCreate(WxCpDepart depart) throws WxErrorException; /** - *
-   * 部门管理接口 - 查询所有部门
-   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
-   * 
- */ - List departGet() throws WxErrorException; - - /** - *
-   * 部门管理接口 - 修改部门名
-   * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
-   * 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
-   * 
- * - * @param group 要更新的group,group的id,name必须设置 + * @deprecated 请使用 {@link WxCpDepartmentService#update(WxCpDepart group) } */ + @Deprecated void departUpdate(WxCpDepart group) throws WxErrorException; /** - *
-   * 部门管理接口 - 删除部门
-   * 
- * - * @param departId 部门id + * @deprecated 请使用 {@link WxCpDepartmentService#delete(Integer departId) } */ + @Deprecated void departDelete(Integer departId) throws WxErrorException; /** - *
-   * 获取部门成员(详情)
-   *
-   * http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98.28.E8.AF.A6.E6.83.85.29
-   * 
- * - * @param departId 必填。部门id - * @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员 - * @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 + * @deprecated 请使用 {@link WxCpDepartmentService#listAll() } */ - List userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; + @Deprecated + List departGet() throws WxErrorException; /** - *
-   * 获取部门成员
-   *
-   * http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98
-   * 
- * - * @param departId 必填。部门id - * @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员 - * @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 + * @deprecated 请使用 {@link WxCpUserService#authenticate(String userId) } */ - List departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; + @Deprecated + void userAuthenticated(String userId) throws WxErrorException; /** - * 新建用户 - * - * @param user 用户对象 + * @deprecated 请使用 {@link WxCpUserService#create(WxCpUser user) } */ + @Deprecated void userCreate(WxCpUser user) throws WxErrorException; /** - * 更新用户 - * - * @param user 用户对象 + * @deprecated 请使用 {@link WxCpUserService#update(WxCpUser user)} */ + @Deprecated void userUpdate(WxCpUser user) throws WxErrorException; /** - * 删除用户 - * - * @param userid 用户id + * @deprecated 请使用 {@link WxCpUserService#delete(String... userids) } */ + @Deprecated void userDelete(String userid) throws WxErrorException; /** - *
-   * 批量删除成员
-   * http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E6.89.B9.E9.87.8F.E5.88.A0.E9.99.A4.E6.88.90.E5.91.98
-   * 
- * - * @param userids 员工UserID列表。对应管理端的帐号 + * @deprecated 请使用 {@link WxCpUserService#delete(String[] userids) } */ + @Deprecated void userDelete(String[] userids) throws WxErrorException; /** - * 获取用户 - * - * @param userid 用户id + * @deprecated 请使用 {@link WxCpUserService#getById(String userid) } */ + @Deprecated WxCpUser userGet(String userid) throws WxErrorException; + /** + * @deprecated 请使用 {@link WxCpUserService#listByDepartment(Integer departId, Boolean fetchChild, Integer status) } + */ + @Deprecated + List userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; + + /** + * @deprecated 请使用 {@link WxCpUserService#listSimpleByDepartment(Integer departId, Boolean fetchChild, Integer status) } + */ + @Deprecated + List departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; + /** * 创建标签 * @@ -560,4 +519,34 @@ public interface WxCpService { * @param wxConfigProvider 配置对象 */ void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider); + + /** + * 获取部门相关接口的服务类对象 + */ + WxCpDepartmentService getDepartmentService(); + + /** + * 获取媒体相关接口的服务类对象 + */ + WxCpMediaService getMediaService(); + + /** + * 获取菜单相关接口的服务类对象 + */ + WxCpMenuService getMenuService(); + + /** + * 获取Oauth2相关接口的服务类对象 + */ + WxCpOauth2Service getOauth2Service(); + + /** + * 获取标签相关接口的服务类对象 + */ + WxCpTagService getTagService(); + + /** + * 获取用户相关接口的服务类对象 + */ + WxCpUserService getUserService(); } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java new file mode 100644 index 00000000..d84f4c95 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java @@ -0,0 +1,11 @@ +package me.chanjar.weixin.cp.api; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public interface WxCpTagService { +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java new file mode 100644 index 00000000..49ef9134 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpUserService.java @@ -0,0 +1,83 @@ +package me.chanjar.weixin.cp.api; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.cp.bean.WxCpUser; + +import java.util.List; + +/** + *
+ * 用户管理接口
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public interface WxCpUserService { + /** + *
+   *   用在二次验证的时候
+   *   企业在员工验证成功后,调用本方法告诉企业号平台该员工关注成功。
+   * 
+ * + * @param userId 用户id + */ + void authenticate(String userId) throws WxErrorException; + + /** + *
+   * 获取部门成员(详情)
+   *
+   * http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98.28.E8.AF.A6.E6.83.85.29
+   * 
+ * + * @param departId 必填。部门id + * @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员 + * @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 + */ + List listByDepartment(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; + + /** + *
+   * 获取部门成员
+   *
+   * http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98
+   * 
+ * + * @param departId 必填。部门id + * @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员 + * @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 + */ + List listSimpleByDepartment(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; + + /** + * 新建用户 + * + * @param user 用户对象 + */ + void create(WxCpUser user) throws WxErrorException; + + /** + * 更新用户 + * + * @param user 用户对象 + */ + void update(WxCpUser user) throws WxErrorException; + + /** + *
+   * 删除用户/批量删除成员
+   * http://qydev.weixin.qq.com/wiki/index.php?title=管理成员#.E6.89.B9.E9.87.8F.E5.88.A0.E9.99.A4.E6.88.90.E5.91.98
+   * 
+ * + * @param userIds 员工UserID列表。对应管理端的帐号 + */ + void delete(String... userIds) throws WxErrorException; + + /** + * 获取用户 + * + * @param userid 用户id + */ + WxCpUser getById(String userid) throws WxErrorException; +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java index 497832b3..ac6fe8b2 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java @@ -15,9 +15,9 @@ import me.chanjar.weixin.common.util.crypto.SHA1; import me.chanjar.weixin.common.util.fs.FileUtils; import me.chanjar.weixin.common.util.http.*; import me.chanjar.weixin.common.util.json.GsonHelper; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; -import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.api.*; import me.chanjar.weixin.cp.bean.*; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -30,9 +30,15 @@ import java.util.List; import java.util.UUID; public abstract class AbstractWxCpServiceImpl implements WxCpService, RequestHttp { - protected final Logger log = LoggerFactory.getLogger(this.getClass()); + private WxCpUserService userService = new WxCpUserServiceImpl(this); + private WxCpDepartmentService departmentService = new WxCpDepartmentServiceImpl(this); + private WxCpMediaService mediaService; + private WxCpMenuService menuService; + private WxCpOauth2Service oauth2Service; + private WxCpTagService tagService; + /** * 全局的是否正在刷新access token的锁 */ @@ -65,12 +71,6 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } } - @Override - public void userAuthenticated(String userId) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?userid=" + userId; - get(url, null); - } - @Override public String getAccessToken() throws WxErrorException { return getAccessToken(false); @@ -134,11 +134,13 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public void menuCreate(WxMenu menu) throws WxErrorException { menuCreate(this.configStorage.getAgentId(), menu); } @Override + @Deprecated public void menuCreate(Integer agentId, WxMenu menu) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" + agentId; @@ -146,22 +148,26 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public void menuDelete() throws WxErrorException { menuDelete(this.configStorage.getAgentId()); } @Override + @Deprecated public void menuDelete(Integer agentId) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=" + agentId; get(url, null); } @Override + @Deprecated public WxMenu menuGet() throws WxErrorException { return menuGet(this.configStorage.getAgentId()); } @Override + @Deprecated public WxMenu menuGet(Integer agentId) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + agentId; try { @@ -177,18 +183,21 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); } @Override + @Deprecated public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType; return execute(MediaUploadRequestExecutor.create(this), url, file); } @Override + @Deprecated public File mediaDownload(String mediaId) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"; return execute( @@ -197,130 +206,56 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ url, "media_id=" + mediaId); } - - @Override - public Integer departCreate(WxCpDepart depart) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create"; - String responseContent = execute( - SimplePostRequestExecutor.create(this), - url, - depart.toJson()); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - return GsonHelper.getAsInteger(tmpJsonElement.getAsJsonObject().get("id")); - } - - @Override - public void departUpdate(WxCpDepart group) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/department/update"; - post(url, group.toJson()); - } - @Override - public void departDelete(Integer departId) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId; - get(url, null); - } - - @Override - public List departGet() throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; - String responseContent = get(url, null); - /* - * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} } - * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] } - */ - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - return WxCpGsonBuilder.INSTANCE.create() - .fromJson( - tmpJsonElement.getAsJsonObject().get("department"), - new TypeToken>() { - }.getType() - ); + @Deprecated + public void userAuthenticated(String userId) throws WxErrorException { + this.getUserService().authenticate(userId); } @Override + @Deprecated public void userCreate(WxCpUser user) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/create"; - post(url, user.toJson()); + this.getUserService().create(user); } @Override + @Deprecated public void userUpdate(WxCpUser user) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/update"; - post(url, user.toJson()); + this.getUserService().update(user); } @Override + @Deprecated public void userDelete(String userid) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=" + userid; - get(url, null); + this.getUserService().delete(userid); } @Override + @Deprecated public void userDelete(String[] userids) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete"; - JsonObject jsonObject = new JsonObject(); - JsonArray jsonArray = new JsonArray(); - for (String userid : userids) { - jsonArray.add(new JsonPrimitive(userid)); - } - jsonObject.add("useridlist", jsonArray); - post(url, jsonObject.toString()); + this.getUserService().delete(userids); } @Override + @Deprecated public WxCpUser userGet(String userid) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=" + userid; - String responseContent = get(url, null); - return WxCpUser.fromJson(responseContent); + return this.getUserService().getById(userid); } @Override + @Deprecated public List userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=" + departId; - String params = ""; - if (fetchChild != null) { - params += "&fetch_child=" + (fetchChild ? "1" : "0"); - } - if (status != null) { - params += "&status=" + status; - } else { - params += "&status=0"; - } - - String responseContent = get(url, params); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - return WxCpGsonBuilder.INSTANCE.create() - .fromJson(tmpJsonElement.getAsJsonObject().get("userlist"), - new TypeToken>() { - }.getType() - ); + return this.getUserService().listByDepartment(departId, fetchChild, status); } @Override + @Deprecated public List departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { - String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId; - String params = ""; - if (fetchChild != null) { - params += "&fetch_child=" + (fetchChild ? "1" : "0"); - } - if (status != null) { - params += "&status=" + status; - } else { - params += "&status=0"; - } - - String responseContent = get(url, params); - JsonElement tmpJsonElement = new JsonParser().parse(responseContent); - return WxCpGsonBuilder.INSTANCE.create() - .fromJson( - tmpJsonElement.getAsJsonObject().get("userlist"), - new TypeToken>() { - }.getType() - ); + return this.getUserService().listSimpleByDepartment(departId, fetchChild, status); } @Override + @Deprecated public String tagCreate(String tagName) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"; JsonObject o = new JsonObject(); @@ -331,6 +266,7 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public void tagUpdate(String tagId, String tagName) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update"; JsonObject o = new JsonObject(); @@ -340,12 +276,14 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public void tagDelete(String tagId) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=" + tagId; get(url, null); } @Override + @Deprecated public List tagGet() throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"; String responseContent = get(url, null); @@ -359,6 +297,7 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public List tagGetUsers(String tagId) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId; String responseContent = get(url, null); @@ -372,6 +311,7 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public void tagAddUsers(String tagId, List userIds, List partyIds) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"; JsonObject jsonObject = new JsonObject(); @@ -394,6 +334,7 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public void tagRemoveUsers(String tagId, List userIds) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers"; JsonObject jsonObject = new JsonObject(); @@ -407,6 +348,7 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public String oauth2buildAuthorizationUrl(String state) { return this.oauth2buildAuthorizationUrl( this.configStorage.getOauth2redirectUri(), @@ -415,6 +357,7 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public String oauth2buildAuthorizationUrl(String redirectUri, String state) { String url = "https://open.weixin.qq.com/connect/oauth2/authorize?"; url += "appid=" + this.configStorage.getCorpId(); @@ -429,11 +372,13 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ } @Override + @Deprecated public String[] oauth2getUserInfo(String code) throws WxErrorException { return oauth2getUserInfo(this.configStorage.getAgentId(), code); } @Override + @Deprecated public String[] oauth2getUserInfo(Integer agentId, String code) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?" + "code=" + code @@ -588,7 +533,6 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ return this.sessionManager.getSession(id, create); } - @Override public void setSessionManager(WxSessionManager sessionManager) { this.sessionManager = sessionManager; @@ -624,4 +568,57 @@ public abstract class AbstractWxCpServiceImpl implements WxCpService, Requ this.tmpDirFile = tmpDirFile; } + @Override + public WxCpDepartmentService getDepartmentService() { + return departmentService; + } + + @Override + public WxCpMediaService getMediaService() { + return mediaService; + } + + @Override + public WxCpMenuService getMenuService() { + return menuService; + } + + @Override + public WxCpOauth2Service getOauth2Service() { + return oauth2Service; + } + + @Override + public WxCpTagService getTagService() { + return tagService; + } + + @Override + public WxCpUserService getUserService() { + return userService; + } + + @Override + @Deprecated + public Integer departCreate(WxCpDepart depart) throws WxErrorException { + return this.getDepartmentService().create(depart); + } + + @Override + @Deprecated + public void departUpdate(WxCpDepart depart) throws WxErrorException { + this.getDepartmentService().update(depart); + } + + @Override + @Deprecated + public void departDelete(Integer departId) throws WxErrorException { + this.getDepartmentService().delete(departId); + } + + @Override + @Deprecated + public List departGet() throws WxErrorException { + return this.getDepartmentService().listAll(); + } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java new file mode 100644 index 00000000..82cd8c34 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImpl.java @@ -0,0 +1,65 @@ +package me.chanjar.weixin.cp.api.impl; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.common.util.json.GsonHelper; +import me.chanjar.weixin.cp.api.WxCpDepartmentService; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.WxCpDepart; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + +import java.util.List; + +/** + *
+ *  部门管理接口
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public class WxCpDepartmentServiceImpl implements WxCpDepartmentService { + private WxCpService mainService; + + public WxCpDepartmentServiceImpl(WxCpService mainService) { + this.mainService = mainService; + } + + @Override + public Integer create(WxCpDepart depart) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create"; + String responseContent = this.mainService.post(url, depart.toJson()); + JsonElement tmpJsonElement = new JsonParser().parse(responseContent); + return GsonHelper.getAsInteger(tmpJsonElement.getAsJsonObject().get("id")); + } + + @Override + public void update(WxCpDepart group) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/department/update"; + this.mainService.post(url, group.toJson()); + } + + @Override + public void delete(Integer departId) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId; + this.mainService.get(url, null); + } + + @Override + public List listAll() throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list"; + String responseContent = this.mainService.get(url, null); + /* + * 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} } + * 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] } + */ + JsonElement tmpJsonElement = new JsonParser().parse(responseContent); + return WxCpGsonBuilder.INSTANCE.create() + .fromJson(tmpJsonElement.getAsJsonObject().get("department"), + new TypeToken>() { + }.getType() + ); + } +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java index fecb2646..9c87ccbd 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java @@ -7,8 +7,8 @@ import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; -import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl; +import me.chanjar.weixin.cp.bean.WxCpDepart; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; @@ -17,6 +17,7 @@ import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import java.io.IOException; +import java.util.List; public class WxCpServiceApacheHttpClientImpl extends AbstractWxCpServiceImpl { protected CloseableHttpClient httpClient; diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java index 5ceabd0c..3000916f 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceJoddHttpImpl.java @@ -5,8 +5,7 @@ import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.HttpType; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; -import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; public class WxCpServiceJoddHttpImpl extends AbstractWxCpServiceImpl { protected HttpConnectionProvider httpClient; diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java index 11411cda..4dd66d9d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceOkHttpImpl.java @@ -5,8 +5,7 @@ import me.chanjar.weixin.common.bean.result.WxError; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.common.util.http.HttpType; import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; -import me.chanjar.weixin.cp.api.impl.AbstractWxCpServiceImpl; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import okhttp3.*; import java.io.IOException; diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java new file mode 100644 index 00000000..c49c1ffd --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImpl.java @@ -0,0 +1,118 @@ +package me.chanjar.weixin.cp.api.impl; + +import com.google.gson.*; +import com.google.gson.reflect.TypeToken; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.api.WxCpUserService; +import me.chanjar.weixin.cp.bean.WxCpUser; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + +import java.util.List; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +public class WxCpUserServiceImpl implements WxCpUserService { + private WxCpService mainService; + + public WxCpUserServiceImpl(WxCpService mainService) { + this.mainService = mainService; + } + + @Override + public void authenticate(String userId) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/authsucc?userid=" + userId; + this.mainService.get(url, null); + } + + @Override + public void create(WxCpUser user) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/create"; + this.mainService.post(url, user.toJson()); + } + + @Override + public void update(WxCpUser user) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/update"; + this.mainService.post(url, user.toJson()); + } + + public void deleteOne(String userId) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=" + userId; + this.mainService.get(url, null); + } + + @Override + public void delete(String... userIds) throws WxErrorException { + if (userIds.length == 1) { + this.deleteOne(userIds[0]); + } + + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete"; + JsonObject jsonObject = new JsonObject(); + JsonArray jsonArray = new JsonArray(); + for (String userid : userIds) { + jsonArray.add(new JsonPrimitive(userid)); + } + jsonObject.add("useridlist", jsonArray); + this.mainService.post(url, jsonObject.toString()); + } + + @Override + public WxCpUser getById(String userid) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=" + userid; + String responseContent = this.mainService.get(url, null); + return WxCpUser.fromJson(responseContent); + } + + @Override + public List listByDepartment(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?department_id=" + departId; + String params = ""; + if (fetchChild != null) { + params += "&fetch_child=" + (fetchChild ? "1" : "0"); + } + if (status != null) { + params += "&status=" + status; + } else { + params += "&status=0"; + } + + String responseContent = this.mainService.get(url, params); + JsonElement tmpJsonElement = new JsonParser().parse(responseContent); + return WxCpGsonBuilder.INSTANCE.create() + .fromJson(tmpJsonElement.getAsJsonObject().get("userlist"), + new TypeToken>() { + }.getType() + ); + } + + @Override + public List listSimpleByDepartment(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { + String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId; + String params = ""; + if (fetchChild != null) { + params += "&fetch_child=" + (fetchChild ? "1" : "0"); + } + if (status != null) { + params += "&status=" + status; + } else { + params += "&status=0"; + } + + String responseContent = this.mainService.get(url, params); + JsonElement tmpJsonElement = new JsonParser().parse(responseContent); + return WxCpGsonBuilder.INSTANCE.create() + .fromJson( + tmpJsonElement.getAsJsonObject().get("userlist"), + new TypeToken>() { + }.getType() + ); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlMessage.java index 3c0f50b8..8b7ee5f1 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlMessage.java @@ -3,7 +3,7 @@ package me.chanjar.weixin.cp.bean; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamConverter; import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; import me.chanjar.weixin.cp.util.xml.XStreamTransformer; import org.apache.commons.io.IOUtils; diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlOutMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlOutMessage.java index 0d90a012..d6970350 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlOutMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpXmlOutMessage.java @@ -3,7 +3,7 @@ package me.chanjar.weixin.cp.bean; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamConverter; import me.chanjar.weixin.common.util.xml.XStreamCDataConverter; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.bean.outxmlbuilder.*; import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil; import me.chanjar.weixin.cp.util.xml.XStreamTransformer; diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java index 2b2dfe47..770ef827 100755 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java @@ -22,7 +22,7 @@ package me.chanjar.weixin.cp.util.crypto; import me.chanjar.weixin.common.util.crypto.WxCryptUtil; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import org.apache.commons.codec.binary.Base64; public class WxCpCryptUtil extends WxCryptUtil { diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java index 1c3fd5e7..ca4bc636 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java @@ -6,6 +6,8 @@ import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamAlias; import me.chanjar.weixin.common.util.xml.XStreamInitializer; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage; import java.io.IOException; import java.io.InputStream; @@ -25,10 +27,10 @@ public class ApiTestModule implements Module { .getSystemResourceAsStream("test-config.xml")) { WxXmlCpInMemoryConfigStorage config = fromXml( WxXmlCpInMemoryConfigStorage.class, is1); - WxCpServiceImpl wxService = new WxCpServiceImpl(); + WxCpService wxService = new WxCpServiceImpl(); wxService.setWxCpConfigStorage(config); - binder.bind(WxCpServiceImpl.class).toInstance(wxService); + binder.bind(WxCpService.class).toInstance(wxService); binder.bind(WxCpConfigStorage.class).toInstance(config); } catch (IOException e) { e.printStackTrace(); diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBaseAPITest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBaseAPITest.java index a778046a..b535634a 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBaseAPITest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBaseAPITest.java @@ -3,6 +3,7 @@ package me.chanjar.weixin.cp.api; import com.google.inject.Inject; import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import org.apache.commons.lang3.StringUtils; import org.testng.Assert; import org.testng.annotations.Guice; diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageRouterTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageRouterTest.java index 67889365..e52101f5 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageRouterTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageRouterTest.java @@ -3,6 +3,9 @@ package me.chanjar.weixin.cp.api; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.session.StandardSessionManager; import me.chanjar.weixin.common.session.WxSessionManager; +import me.chanjar.weixin.cp.message.WxCpMessageHandler; +import me.chanjar.weixin.cp.message.WxCpMessageMatcher; +import me.chanjar.weixin.cp.message.WxCpMessageRouter; import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; import org.testng.Assert; diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpTagAPITest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpTagAPITest.java index 761b1a91..cb743f3d 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpTagAPITest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpTagAPITest.java @@ -4,6 +4,7 @@ import com.google.inject.Inject; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpUser; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; import org.testng.Assert; import org.testng.annotations.Guice; import org.testng.annotations.Test; diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpUserAPITest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpUserAPITest.java deleted file mode 100644 index c93cb884..00000000 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpUserAPITest.java +++ /dev/null @@ -1,76 +0,0 @@ -package me.chanjar.weixin.cp.api; - -import com.google.inject.Inject; -import me.chanjar.weixin.common.exception.WxErrorException; -import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; -import me.chanjar.weixin.cp.bean.WxCpUser; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; -import org.testng.Assert; -import org.testng.annotations.Guice; -import org.testng.annotations.Test; - -import java.util.List; - -import static org.testng.Assert.assertNotEquals; - -/** - * 测试用户接口 - * - * @author Daniel Qian - */ -@Test(groups = "userAPI") -@Guice(modules = ApiTestModule.class) -public class WxCpUserAPITest { - - @Inject - protected WxCpServiceImpl wxCpService; - - public void testUserCreate() throws WxErrorException { - WxCpUser user = new WxCpUser(); - user.setUserId("some.woman"); - user.setName("Some Woman"); - user.setDepartIds(new Integer[]{9, 8}); - user.setEmail("none@none.com"); - user.setGender("女"); - user.setMobile("13560084979"); - user.setPosition("woman"); - user.setTelephone("3300393"); - user.addExtAttr("爱好", "table"); - this.wxCpService.userCreate(user); - } - - @Test(dependsOnMethods = "testUserCreate") - public void testUserUpdate() throws WxErrorException { - WxCpUser user = new WxCpUser(); - user.setUserId("some.woman"); - user.setName("Some Woman"); - user.addExtAttr("爱好", "table2"); - this.wxCpService.userUpdate(user); - } - - @Test(dependsOnMethods = "testUserUpdate") - public void testUserGet() throws WxErrorException { - WxCpUser user = this.wxCpService.userGet("some.woman"); - Assert.assertNotNull(user); - } - - @Test(dependsOnMethods = "testUserGet") - public void testDepartGetUsers() throws WxErrorException { - List users = this.wxCpService.departGetUsers(1, true, 0); - assertNotEquals(users.size(), 0); - } - - @Test(dependsOnMethods = "testDepartGetUsers") - public void testUserDelete() throws WxErrorException { - this.wxCpService.userDelete("some.woman"); - } - - public void testUserList() throws WxErrorException { - List users = this.wxCpService.userList(1, true, 0); - assertNotEquals(users.size(), 0); - for (WxCpUser user : users) { - System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); - } - } -} diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java new file mode 100644 index 00000000..685f3744 --- /dev/null +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpDepartmentServiceImplTest.java @@ -0,0 +1,66 @@ +package me.chanjar.weixin.cp.api.impl; + +import com.google.inject.Inject; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.cp.api.ApiTestModule; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.WxCpDepart; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import java.util.List; + +import static org.testng.Assert.*; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +@Guice(modules = ApiTestModule.class) +public class WxCpDepartmentServiceImplTest { + @Inject + private WxCpService wxCpService; + + private WxCpDepart depart; + + @Test + public void testCreate() throws Exception { + WxCpDepart cpDepart = new WxCpDepart(); + cpDepart.setName("子部门" + System.currentTimeMillis()); + cpDepart.setParentId(1); + cpDepart.setOrder(1); + Integer departId = this.wxCpService.getDepartmentService().create(cpDepart); + System.out.println(departId); + } + + @Test + public void testListAll() throws Exception { + System.out.println("=================获取部门"); + List departList = this.wxCpService.getDepartmentService().listAll(); + assertNotNull(departList); + assertTrue(departList.size() > 0); + for (WxCpDepart g : departList) { + this.depart = g; + System.out.println(this.depart.getId() + ":" + this.depart.getName()); + assertNotNull(g.getName()); + } + } + + @Test(dependsOnMethods = {"testListAll", "testCreate"}) + public void testUpdate() throws Exception { + System.out.println("=================更新部门"); + this.depart.setName("子部门改名" + System.currentTimeMillis()); + this.wxCpService.getDepartmentService().update(this.depart); + } + + @Test(dependsOnMethods = "testUpdate") + public void testDelete() throws Exception { + System.out.println("=================删除部门"); + System.out.println(this.depart.getId() + ":" + this.depart.getName()); + this.wxCpService.getDepartmentService().delete(this.depart.getId()); + } + +} diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java new file mode 100644 index 00000000..d576b866 --- /dev/null +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java @@ -0,0 +1,86 @@ +package me.chanjar.weixin.cp.api.impl; + +import com.google.inject.Inject; +import me.chanjar.weixin.cp.api.ApiTestModule; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.WxCpUser; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import org.testng.Assert; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import java.util.List; + +import static org.testng.Assert.*; + +/** + *
+ *  Created by BinaryWang on 2017/6/24.
+ * 
+ * + * @author Binary Wang + */ +@Guice(modules = ApiTestModule.class) +public class WxCpUserServiceImplTest { + @Inject + private WxCpService wxCpService; + + @Test + public void testAuthenticate() throws Exception { + } + + @Test + public void testCreate() throws Exception { + WxCpUser user = new WxCpUser(); + user.setUserId("some.woman"); + user.setName("Some Woman"); + user.setDepartIds(new Integer[]{9, 8}); + user.setEmail("none@none.com"); + user.setGender("女"); + user.setMobile("13560084979"); + user.setPosition("woman"); + user.setTelephone("3300393"); + user.addExtAttr("爱好", "table"); + this.wxCpService.getUserService().create(user); + } + + @Test(dependsOnMethods = "testCreate") + public void testUpdate() throws Exception { + WxCpUser user = new WxCpUser(); + user.setUserId("some.woman"); + user.setName("Some Woman"); + user.addExtAttr("爱好", "table2"); + this.wxCpService.getUserService().update(user); + } + + @Test + public void testDelete() throws Exception { + this.wxCpService.getUserService().delete("some.woman"); + } + + @Test(dependsOnMethods = "testUpdate") + public void testGetById() throws Exception { + WxCpUser user = this.wxCpService.getUserService().getById("some.woman"); + assertNotNull(user); + } + + @Test + public void testListByDepartment() throws Exception { + List users = this.wxCpService.getUserService().listByDepartment(1, true, 0); + assertNotEquals(users.size(), 0); + for (WxCpUser user : users) { + System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); + } + } + + @Test + public void testListSimpleByDepartment() throws Exception { + List users = this.wxCpService.getUserService().listSimpleByDepartment(1, true, 0); + assertNotEquals(users.size(), 0); + for (WxCpUser user : users) { + System.out.println(ToStringBuilder.reflectionToString(user, ToStringStyle.MULTI_LINE_STYLE)); + } + } + +} diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoInMemoryConfigStorage.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoInMemoryConfigStorage.java index 4171119b..93ff1bbc 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoInMemoryConfigStorage.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoInMemoryConfigStorage.java @@ -3,7 +3,7 @@ package me.chanjar.weixin.cp.demo; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamAlias; import me.chanjar.weixin.common.util.xml.XStreamInitializer; -import me.chanjar.weixin.cp.api.WxCpInMemoryConfigStorage; +import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage; import java.io.InputStream; diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java index 9c9b50c1..e9d78768 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java @@ -1,9 +1,9 @@ package me.chanjar.weixin.cp.demo; import me.chanjar.weixin.common.session.WxSessionManager; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; -import me.chanjar.weixin.cp.api.WxCpMessageHandler; -import me.chanjar.weixin.cp.api.WxCpMessageRouter; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; +import me.chanjar.weixin.cp.message.WxCpMessageHandler; +import me.chanjar.weixin.cp.message.WxCpMessageRouter; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.bean.WxCpXmlMessage; diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java index dec6faee..291cef40 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpEndpointServlet.java @@ -1,7 +1,7 @@ package me.chanjar.weixin.cp.demo; -import me.chanjar.weixin.cp.api.WxCpConfigStorage; -import me.chanjar.weixin.cp.api.WxCpMessageRouter; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; +import me.chanjar.weixin.cp.message.WxCpMessageRouter; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.bean.WxCpXmlMessage; import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;