Sfoglia il codice sorgente

优化重构企业号相关代码,修复了升级企业微信后出现的菜单问题和用户管理的问题

master
Binary Wang 7 anni fa
parent
commit
9f4a7a7796
29 ha cambiato i file con 1022 aggiunte e 513 eliminazioni
  1. +6
    -1
      weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMenuGsonAdapter.java
  2. +46
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java
  3. +80
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java
  4. +67
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java
  5. +0
    -11
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOauth2Service.java
  6. +81
    -172
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java
  7. +55
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java
  8. +43
    -124
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java
  9. +50
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java
  10. +64
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImpl.java
  11. +66
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java
  12. +1
    -2
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java
  13. +116
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java
  14. +85
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTagAddOrRemoveUsersResult.java
  15. +35
    -4
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpUser.java
  16. +2
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java
  17. +24
    -0
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMenuGsonAdapter.java
  18. +2
    -2
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpUserGsonAdapter.java
  19. +1
    -1
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java
  20. +0
    -77
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMediaAPITest.java
  21. +0
    -68
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpTagAPITest.java
  22. +78
    -0
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImplTest.java
  23. +30
    -30
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImplTest.java
  24. +75
    -0
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java
  25. +10
    -10
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java
  26. +4
    -5
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java
  27. +1
    -1
      weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpOAuth2Servlet.java
  28. BIN
      weixin-java-cp/src/test/resources/mm.amr
  29. +0
    -5
      weixin-java-cp/src/test/resources/testng.xml

+ 6
- 1
weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/WxMenuGsonAdapter.java Vedi File

@@ -90,9 +90,14 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ
* 操蛋的微信
* 创建菜单时是 { button : ... }
* 查询菜单时是 { menu : { button : ... } }
* 现在企业号升级为企业微信后,没有此问题,因此需要单独处理
*/
WxMenu menu = new WxMenu();
JsonArray buttonsJson = json.getAsJsonObject().get("menu").getAsJsonObject().get("button").getAsJsonArray();
return this.buildMenuFromJson(buttonsJson);
}

protected WxMenu buildMenuFromJson(JsonArray buttonsJson) {
WxMenu menu = new WxMenu();
for (int i = 0; i < buttonsJson.size(); i++) {
JsonObject buttonJson = buttonsJson.get(i).getAsJsonObject();
WxMenuButton button = convertFromJson(buttonJson);


+ 46
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMediaService.java Vedi File

@@ -1,11 +1,57 @@
package me.chanjar.weixin.cp.api;

import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* <pre>
* 媒体管理接口
* Created by BinaryWang on 2017/6/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxCpMediaService {

/**
* <pre>
* 上传多媒体文件
* 上传的多媒体文件有格式和大小限制,如下:
* 图片(image): 1M,支持JPG格式
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
* 视频(video):10MB,支持MP4格式
* 缩略图(thumb):64KB,支持JPG格式
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
*
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param inputStream 输入流
*/
WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputStream)
throws WxErrorException, IOException;

/**
* @param mediaType 媒体类型
* @param file 文件对象
* @see #upload(String, String, InputStream)
*/
WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException;

/**
* <pre>
* 下载多媒体文件
* 根据微信文档,视频文件下载不了,会返回null
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
*
* @param mediaId 媒体id
* @return 保存到本地的临时文件
*/
File download(String mediaId) throws WxErrorException;

}

+ 80
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMenuService.java Vedi File

@@ -1,11 +1,91 @@
package me.chanjar.weixin.cp.api;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.exception.WxErrorException;

/**
* <pre>
* 菜单管理相关接口
* Created by BinaryWang on 2017/6/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxCpMenuService {
/**
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @param menu 菜单对象
* @see #create(Integer, WxMenu)
*/
void create(WxMenu menu) throws WxErrorException;

/**
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @param menu 菜单对象
* @see #create(me.chanjar.weixin.common.bean.menu.WxMenu)
*/
void create(Integer agentId, WxMenu menu) throws WxErrorException;

/**
* <pre>
* 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @see #delete(Integer)
*/
void delete() throws WxErrorException;

/**
* <pre>
* 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @see #delete()
*/
void delete(Integer agentId) throws WxErrorException;

/**
* <pre>
* 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @see #get(Integer)
*/
WxMenu get() throws WxErrorException;

/**
* <pre>
* 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @see #get()
*/
WxMenu get(Integer agentId) throws WxErrorException;
}

+ 67
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java Vedi File

@@ -0,0 +1,67 @@
package me.chanjar.weixin.cp.api;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
* <pre>
* OAuth2相关管理接口
* Created by BinaryWang on 2017/6/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxCpOAuth2Service {
/**
* <pre>
* 构造oauth2授权的url连接
* </pre>
*
* @param state 状态码
* @return url
*/
String buildAuthorizationUrl(String state);

/**
* <pre>
* 构造oauth2授权的url连接
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=企业获取code
* </pre>
*
* @param redirectUri 跳转链接地址
* @param state 状态码
* @return url
*/
String buildAuthorizationUrl(String redirectUri, String state);

/**
* <pre>
* 用oauth2获取用户信息
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名,所以无法测试,因此这个方法很可能是坏的。
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @param code 微信oauth授权返回的代码
* @return [userid, deviceid]
* @see #getUserInfo(Integer, String)
*/
String[] getUserInfo(String code) throws WxErrorException;

/**
* <pre>
* 用oauth2获取用户信息
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名,所以无法测试,因此这个方法很可能是坏的。
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @param code 微信oauth授权返回的代码
* @return [userid, deviceid]
* @see #getUserInfo(String)
*/
String[] getUserInfo(Integer agentId, String code) throws WxErrorException;

}

+ 0
- 11
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOauth2Service.java Vedi File

@@ -1,11 +0,0 @@
package me.chanjar.weixin.cp.api;

/**
* <pre>
* Created by BinaryWang on 2017/6/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxCpOauth2Service {
}

+ 81
- 172
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java Vedi File

@@ -8,6 +8,7 @@ import me.chanjar.weixin.common.session.WxSession;
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.common.util.http.RequestHttp;
import me.chanjar.weixin.cp.bean.*;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;

@@ -85,305 +86,208 @@ public interface WxCpService {
WxJsapiSignature createJsapiSignature(String url) throws WxErrorException;

/**
* <pre>
* 上传多媒体文件
* 上传的多媒体文件有格式和大小限制,如下:
* 图片(image): 1M,支持JPG格式
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
* 视频(video):10MB,支持MP4格式
* 缩略图(thumb):64KB,支持JPG格式
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
*
* @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param fileType 文件类型,请看{@link me.chanjar.weixin.common.api.WxConsts}
* @param inputStream 输入流
* @deprecated 请使用 {@link WxCpMenuService#create(WxMenu)}
*/
WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream)
throws WxErrorException, IOException;
@Deprecated
void menuCreate(WxMenu menu) throws WxErrorException;

/**
* @param mediaType 媒体类型
* @param file 文件对象
* @see #mediaUpload(String, String, InputStream)
* @deprecated 请使用 {@link WxCpMenuService#create(Integer, WxMenu)}
*/
WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
@Deprecated
void menuCreate(Integer agentId, WxMenu menu) throws WxErrorException;

/**
* <pre>
* 下载多媒体文件
* 根据微信文档,视频文件下载不了,会返回null
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
*
* @param mediaId 媒体id
* @return 保存到本地的临时文件
* @deprecated 请使用 {@link WxCpMenuService#delete()} }
*/
File mediaDownload(String mediaId) throws WxErrorException;
@Deprecated
void menuDelete() throws WxErrorException;

/**
* <pre>
* 发送消息
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
* </pre>
*
* @param message 要发送的消息对象
* @deprecated 请使用 {@link WxCpMenuService#delete(Integer)}
*/
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;
@Deprecated
void menuDelete(Integer agentId) throws WxErrorException;

/**
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @param menu 菜单对象
* @see #menuCreate(Integer, WxMenu)
* @deprecated 请使用 {@link WxCpMenuService#get() }
*/
void menuCreate(WxMenu menu) throws WxErrorException;
@Deprecated
WxMenu menuGet() throws WxErrorException;

/**
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @param menu 菜单对象
* @see #menuCreate(me.chanjar.weixin.common.bean.menu.WxMenu)
* @deprecated 请使用 {@link WxCpMenuService#get(Integer)}
*/
void menuCreate(Integer agentId, WxMenu menu) throws WxErrorException;
@Deprecated
WxMenu menuGet(Integer agentId) throws WxErrorException;

/**
* <pre>
* 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* 发送消息
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
* </pre>
*
* @see #menuDelete(Integer)
* @param message 要发送的消息对象
*/
void menuDelete() throws WxErrorException;
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;

/**
* <pre>
* 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @see #menuDelete()
* @deprecated 请使用 {@link WxCpDepartmentService#create(WxCpDepart)}
*/
void menuDelete(Integer agentId) throws WxErrorException;
@Deprecated
Integer departCreate(WxCpDepart depart) throws WxErrorException;

/**
* <pre>
* 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @see #menuGet(Integer)
* @deprecated 请使用 {@link WxCpDepartmentService#update(WxCpDepart)}
*/
WxMenu menuGet() throws WxErrorException;
@Deprecated
void departUpdate(WxCpDepart group) throws WxErrorException;

/**
* <pre>
* 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @see #menuGet()
* @deprecated 请使用 {@link WxCpDepartmentService#delete(Integer)}
*/
WxMenu menuGet(Integer agentId) throws WxErrorException;
@Deprecated
void departDelete(Integer departId) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpDepartmentService#create(WxCpDepart depart) }
* @deprecated 请使用 {@link WxCpDepartmentService#listAll() }
*/
@Deprecated
Integer departCreate(WxCpDepart depart) throws WxErrorException;
List<WxCpDepart> departGet() throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpDepartmentService#update(WxCpDepart group) }
* @deprecated 请使用 {@link WxCpMediaService#upload(String, String, InputStream)}
*/
@Deprecated
void departUpdate(WxCpDepart group) throws WxErrorException;
WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream)
throws WxErrorException, IOException;

/**
* @deprecated 请使用 {@link WxCpDepartmentService#delete(Integer departId) }
* @deprecated 请使用 {@link WxCpMediaService#upload(String, File)}
*/
@Deprecated
void departDelete(Integer departId) throws WxErrorException;
WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpDepartmentService#listAll() }
* @deprecated 请使用 {@link WxCpMediaService#download(String)}
*/
@Deprecated
List<WxCpDepart> departGet() throws WxErrorException;
File mediaDownload(String mediaId) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#authenticate(String userId) }
* @deprecated 请使用 {@link WxCpUserService#authenticate(String)}
*/
@Deprecated
void userAuthenticated(String userId) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#create(WxCpUser user) }
* @deprecated 请使用 {@link WxCpUserService#create(WxCpUser)}
*/
@Deprecated
void userCreate(WxCpUser user) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#update(WxCpUser user)}
* @deprecated 请使用 {@link WxCpUserService#update(WxCpUser)}
*/
@Deprecated
void userUpdate(WxCpUser user) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#delete(String... userids) }
* @deprecated 请使用 {@link WxCpUserService#delete(String...)}
*/
@Deprecated
void userDelete(String userid) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#delete(String[] userids) }
* @deprecated 请使用 {@link WxCpUserService#delete(String...)}
*/
@Deprecated
void userDelete(String[] userids) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#getById(String userid) }
* @deprecated 请使用 {@link WxCpUserService#getById(String)}
*/
@Deprecated
WxCpUser userGet(String userid) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#listByDepartment(Integer departId, Boolean fetchChild, Integer status) }
* @deprecated 请使用 {@link WxCpUserService#listByDepartment(Integer, Boolean, Integer)}
*/
@Deprecated
List<WxCpUser> userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpUserService#listSimpleByDepartment(Integer departId, Boolean fetchChild, Integer status) }
* @deprecated 请使用 {@link WxCpUserService#listSimpleByDepartment(Integer, Boolean, Integer)}
*/
@Deprecated
List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException;

/**
* 创建标签
*
* @param tagName 标签名
* @deprecated 请使用 {@link WxCpTagService#create(String)}
*/
@Deprecated
String tagCreate(String tagName) throws WxErrorException;

/**
* 更新标签
*
* @param tagId 标签id
* @param tagName 标签名
* @deprecated 请使用 {@link WxCpTagService#update(String, String)}
*/
@Deprecated
void tagUpdate(String tagId, String tagName) throws WxErrorException;

/**
* 删除标签
*
* @param tagId 标签id
* @deprecated 请使用 {@link WxCpTagService#delete(String)}
*/
@Deprecated
void tagDelete(String tagId) throws WxErrorException;

/**
* 获得标签列表
* @deprecated 请使用 {@link WxCpTagService#listAll()}
*/
@Deprecated
List<WxCpTag> tagGet() throws WxErrorException;

/**
* 获取标签成员
*
* @param tagId 标签ID
* @deprecated 请使用 {@link WxCpTagService#listUsersByTagId(String)}
*/
@Deprecated
List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException;

/**
* 增加标签成员
*
* @param tagId 标签id
* @param userIds 用户ID 列表
* @deprecated 请使用 {@link WxCpTagService#addUsers2Tag(String, List, List)}
*/
@Deprecated
void tagAddUsers(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException;

/**
* <pre>
* 构造oauth2授权的url连接
* </pre>
*
* @param state 状态码
* @return url
* @deprecated 请使用 {@link WxCpTagService#removeUsersFromTag(String, List)}
*/
@Deprecated
void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorException;

/**
* @deprecated 请使用 {@link WxCpOAuth2Service#buildAuthorizationUrl(String)}
*/
@Deprecated
String oauth2buildAuthorizationUrl(String state);

/**
* <pre>
* 构造oauth2授权的url连接
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=企业获取code
* </pre>
*
* @param redirectUri 跳转链接地址
* @param state 状态码
* @return url
* @deprecated 请使用 {@link WxCpOAuth2Service#buildAuthorizationUrl(String, String)}
*/
@Deprecated
String oauth2buildAuthorizationUrl(String redirectUri, String state);

/**
* <pre>
* 用oauth2获取用户信息
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名,所以无法测试,因此这个方法很可能是坏的。
*
* 注意: 这个方法使用WxCpConfigStorage里的agentId
* </pre>
*
* @param code 微信oauth授权返回的代码
* @return [userid, deviceid]
* @see #oauth2getUserInfo(Integer, String)
* @deprecated 请使用 {@link WxCpOAuth2Service#getUserInfo(String)}
*/
@Deprecated
String[] oauth2getUserInfo(String code) throws WxErrorException;

/**
* <pre>
* 用oauth2获取用户信息
* http://qydev.weixin.qq.com/wiki/index.php?title=根据code获取成员信息
* 因为企业号oauth2.0必须在应用设置里设置通过ICP备案的可信域名,所以无法测试,因此这个方法很可能是坏的。
*
* 注意: 这个方法不使用WxCpConfigStorage里的agentId,需要开发人员自己给出
* </pre>
*
* @param agentId 企业号应用的id
* @param code 微信oauth授权返回的代码
* @return [userid, deviceid]
* @see #oauth2getUserInfo(String)
* @deprecated 请使用 {@link WxCpOAuth2Service#getUserInfo(Integer, String)}
*/
@Deprecated
String[] oauth2getUserInfo(Integer agentId, String code) throws WxErrorException;


/**
* 移除标签成员
*
* @param tagId 标签id
* @param userIds 用户id列表
*/
void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorException;

/**
* <pre>
* 邀请成员关注
@@ -538,7 +442,7 @@ public interface WxCpService {
/**
* 获取Oauth2相关接口的服务类对象
*/
WxCpOauth2Service getOauth2Service();
WxCpOAuth2Service getOauth2Service();

/**
* 获取标签相关接口的服务类对象
@@ -549,4 +453,9 @@ public interface WxCpService {
* 获取用户相关接口的服务类对象
*/
WxCpUserService getUserService();

/**
* http请求对象
*/
RequestHttp getRequestHttp();
}

+ 55
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java Vedi File

@@ -1,11 +1,66 @@
package me.chanjar.weixin.cp.api;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult;
import me.chanjar.weixin.cp.bean.WxCpUser;

import java.util.List;

/**
* <pre>
* 标签管理接口
* Created by BinaryWang on 2017/6/24.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
public interface WxCpTagService {
/**
* 创建标签
*
* @param tagName 标签名
*/
String create(String tagName) throws WxErrorException;

/**
* 更新标签
*
* @param tagId 标签id
* @param tagName 标签名
*/
void update(String tagId, String tagName) throws WxErrorException;

/**
* 删除标签
*
* @param tagId 标签id
*/
void delete(String tagId) throws WxErrorException;

/**
* 获得标签列表
*/
List<WxCpTag> listAll() throws WxErrorException;

/**
* 获取标签成员
*
* @param tagId 标签ID
*/
List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException;

/**
* 增加标签成员
* @param tagId 标签id
* @param userIds 用户ID 列表
*/
WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException;

/**
* 移除标签成员
* @param tagId 标签id
* @param userIds 用户id列表
*/
WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds) throws WxErrorException;
}

+ 43
- 124
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/AbstractWxCpServiceImpl.java Vedi File

@@ -1,7 +1,9 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.result.WxError;
@@ -12,13 +14,13 @@ import me.chanjar.weixin.common.session.WxSession;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.RandomUtils;
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.common.util.http.RequestExecutor;
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
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;
import org.slf4j.LoggerFactory;
@@ -27,17 +29,16 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, RequestHttp<H, P> {
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;
private WxCpMediaService mediaService = new WxCpMediaServiceImpl(this);
private WxCpMenuService menuService = new WxCpMenuServiceImpl(this);
private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this);
private WxCpTagService tagService = new WxCpTagServiceImpl(this);

/**
* 全局的是否正在刷新access token的锁
@@ -136,74 +137,56 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ
@Override
@Deprecated
public void menuCreate(WxMenu menu) throws WxErrorException {
menuCreate(this.configStorage.getAgentId(), menu);
this.getMenuService().create(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;
post(url, menu.toJson());
this.getMenuService().create(agentId, menu);
}

@Override
@Deprecated
public void menuDelete() throws WxErrorException {
menuDelete(this.configStorage.getAgentId());
this.getMenuService().delete();
}

@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);
this.getMenuService().delete(agentId);
}

@Override
@Deprecated
public WxMenu menuGet() throws WxErrorException {
return menuGet(this.configStorage.getAgentId());
return this.getMenuService().get();
}

@Override
@Deprecated
public WxMenu menuGet(Integer agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + agentId;
try {
String resultContent = get(url, null);
return WxMenu.fromJson(resultContent);
} catch (WxErrorException e) {
// 46003 不存在的菜单数据
if (e.getError().getErrorCode() == 46003) {
return null;
}
throw e;
}
return this.getMenuService().get(agentId);
}

@Override
@Deprecated
public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream)
throws WxErrorException, IOException {
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
return this.getMediaService().upload(mediaType, fileType, inputStream);
}

@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);
return this.getMediaService().upload(mediaType, file);
}

@Override
@Deprecated
public File mediaDownload(String mediaId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
return execute(
MediaDownloadRequestExecutor.create(this,
this.configStorage.getTmpDirFile()),
url, "media_id=" + mediaId);
return this.getMediaService().download(mediaId);
}

@Override
@@ -245,148 +228,79 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ
@Override
@Deprecated
public List<WxCpUser> userList(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException {
return this.getUserService().listByDepartment(departId, fetchChild, status);
return this.getUserService().listByDepartment(departId, fetchChild, status);
}

@Override
@Deprecated
public List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException {
return this.getUserService().listSimpleByDepartment(departId, fetchChild, status);
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();
o.addProperty("tagname", tagName);
String responseContent = post(url, o.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
return this.getTagService().create(tagName);
}

@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();
o.addProperty("tagid", tagId);
o.addProperty("tagname", tagName);
post(url, o.toString());
this.getTagService().update(tagId, tagName);
}

@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);
this.getTagService().delete(tagId);
}

@Override
@Deprecated
public List<WxCpTag> tagGet() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
String responseContent = get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("taglist"),
new TypeToken<List<WxCpTag>>() {
}.getType()
);
return this.getTagService().listAll();
}

@Override
@Deprecated
public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId;
String responseContent = get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() {
}.getType()
);
return this.getTagService().listUsersByTagId(tagId);
}

@Override
@Deprecated
public void tagAddUsers(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
if (userIds != null) {
JsonArray jsonArray = new JsonArray();
for (String userId : userIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("userlist", jsonArray);
}
if (partyIds != null) {
JsonArray jsonArray = new JsonArray();
for (String userId : partyIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("partylist", jsonArray);
}
post(url, jsonObject.toString());
this.getTagService().addUsers2Tag(tagId, userIds, partyIds);
}

@Override
@Deprecated
public void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers";
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
JsonArray jsonArray = new JsonArray();
for (String userId : userIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("userlist", jsonArray);
post(url, jsonObject.toString());
this.getTagService().removeUsersFromTag(tagId, userIds);
}

@Override
@Deprecated
public String oauth2buildAuthorizationUrl(String state) {
return this.oauth2buildAuthorizationUrl(
this.configStorage.getOauth2redirectUri(),
state
);
return this.getOauth2Service().buildAuthorizationUrl(state);
}

@Override
@Deprecated
public String oauth2buildAuthorizationUrl(String redirectUri, String state) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
url += "appid=" + this.configStorage.getCorpId();
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectUri);
url += "&response_type=code";
url += "&scope=snsapi_base";
if (state != null) {
url += "&state=" + state;
}
url += "#wechat_redirect";
return url;
return this.getOauth2Service().buildAuthorizationUrl(redirectUri, state);
}

@Override
@Deprecated
public String[] oauth2getUserInfo(String code) throws WxErrorException {
return oauth2getUserInfo(this.configStorage.getAgentId(), code);
return this.getOauth2Service().getUserInfo(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
+ "&agentid=" + agentId;
String responseText = get(url, null);
JsonElement je = new JsonParser().parse(responseText);
JsonObject jo = je.getAsJsonObject();
return new String[]{GsonHelper.getString(jo, "UserId"), GsonHelper.getString(jo, "DeviceId"), GsonHelper.getString(jo, "OpenId")};
return this.getOauth2Service().getUserInfo(agentId, code);
}

@Override
@@ -473,7 +387,7 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ

try {
T result = executor.execute(uriWithAccessToken, data);
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, result);
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, data, result);
return result;
} catch (WxErrorException e) {
WxError error = e.getError();
@@ -490,12 +404,12 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ
}

if (error.getErrorCode() != 0) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, error);
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, data, error);
throw new WxErrorException(error);
}
return null;
} catch (IOException e) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uriWithAccessToken, data, e.getMessage());
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, data, e.getMessage());
throw new RuntimeException(e);
}
}
@@ -584,7 +498,7 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ
}

@Override
public WxCpOauth2Service getOauth2Service() {
public WxCpOAuth2Service getOauth2Service() {
return oauth2Service;
}

@@ -621,4 +535,9 @@ public abstract class AbstractWxCpServiceImpl<H, P> implements WxCpService, Requ
public List<WxCpDepart> departGet() throws WxErrorException {
return this.getDepartmentService().listAll();
}

@Override
public RequestHttp getRequestHttp() {
return this;
}
}

+ 50
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImpl.java Vedi File

@@ -0,0 +1,50 @@
package me.chanjar.weixin.cp.api.impl;

import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.fs.FileUtils;
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import me.chanjar.weixin.cp.api.WxCpMediaService;
import me.chanjar.weixin.cp.api.WxCpService;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
* <pre>
* 媒体管理接口
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
public class WxCpMediaServiceImpl implements WxCpMediaService {
private WxCpService mainService;

public WxCpMediaServiceImpl(WxCpService mainService) {
this.mainService = mainService;
}

@Override
public WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputStream)
throws WxErrorException, IOException {
return this.upload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
}

@Override
public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType;
return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), url, file);
}

@Override
public File download(String mediaId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
return this.mainService.execute(
MediaDownloadRequestExecutor.create(this.mainService.getRequestHttp(),
this.mainService.getWxCpConfigStorage().getTmpDirFile()),
url, "media_id=" + mediaId);
}
}

+ 64
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImpl.java Vedi File

@@ -0,0 +1,64 @@
package me.chanjar.weixin.cp.api.impl;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMenuService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

/**
* <pre>
* 菜单管理相关接口
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
public class WxCpMenuServiceImpl implements WxCpMenuService {
private WxCpService mainService;

public WxCpMenuServiceImpl(WxCpService mainService) {
this.mainService = mainService;
}

@Override
public void create(WxMenu menu) throws WxErrorException {
this.create(this.mainService.getWxCpConfigStorage().getAgentId(), menu);
}

@Override
public void create(Integer agentId, WxMenu menu) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid=" + agentId;
this.mainService.post(url, menu.toJson());
}

@Override
public void delete() throws WxErrorException {
this.delete(this.mainService.getWxCpConfigStorage().getAgentId());
}

@Override
public void delete(Integer agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete?agentid=" + agentId;
this.mainService.get(url, null);
}

@Override
public WxMenu get() throws WxErrorException {
return this.get(this.mainService.getWxCpConfigStorage().getAgentId());
}

@Override
public WxMenu get(Integer agentId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get?agentid=" + agentId;
try {
String resultContent = this.mainService.get(url, null);
return WxCpGsonBuilder.create().fromJson(resultContent, WxMenu.class);
} catch (WxErrorException e) {
// 46003 不存在的菜单数据
if (e.getError().getErrorCode() == 46003) {
return null;
}
throw e;
}
}
}

+ 66
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java Vedi File

@@ -0,0 +1,66 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
import me.chanjar.weixin.cp.api.WxCpService;

/**
* <pre>
*
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
public class WxCpOAuth2ServiceImpl implements WxCpOAuth2Service {
private WxCpService mainService;

public WxCpOAuth2ServiceImpl(WxCpService mainService) {
this.mainService = mainService;
}

@Override
public String buildAuthorizationUrl(String state) {
return this.buildAuthorizationUrl(
this.mainService.getWxCpConfigStorage().getOauth2redirectUri(),
state
);
}

@Override
public String buildAuthorizationUrl(String redirectUri, String state) {
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
url += "appid=" + this.mainService.getWxCpConfigStorage().getCorpId();
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectUri);
url += "&response_type=code";
url += "&scope=snsapi_base";
if (state != null) {
url += "&state=" + state;
}
url += "#wechat_redirect";
return url;
}

@Override
public String[] getUserInfo(String code) throws WxErrorException {
return getUserInfo(this.mainService.getWxCpConfigStorage().getAgentId(), code);
}

@Override
public String[] getUserInfo(Integer agentId, String code) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
+ "code=" + code
+ "&agentid=" + agentId;
String responseText = this.mainService.get(url, null);
JsonElement je = new JsonParser().parse(responseText);
JsonObject jo = je.getAsJsonObject();
return new String[]{GsonHelper.getString(jo, "UserId"),
GsonHelper.getString(jo, "DeviceId"),
GsonHelper.getString(jo, "OpenId")};
}

}

+ 1
- 2
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceApacheHttpClientImpl.java Vedi File

@@ -7,7 +7,6 @@ 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.bean.WxCpDepart;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
@@ -17,7 +16,6 @@ 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<CloseableHttpClient, HttpHost> {
protected CloseableHttpClient httpClient;
@@ -103,4 +101,5 @@ public class WxCpServiceApacheHttpClientImpl extends AbstractWxCpServiceImpl<Clo
public WxCpConfigStorage getWxCpConfigStorage() {
return this.configStorage;
}

}

+ 116
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java Vedi File

@@ -0,0 +1,116 @@
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.WxCpTagService;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.util.List;

/**
* <pre>
* 标签管理接口
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
public class WxCpTagServiceImpl implements WxCpTagService {
private WxCpService mainService;

public WxCpTagServiceImpl(WxCpService mainService) {
this.mainService = mainService;
}

@Override
public String create(String tagName) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create";
JsonObject o = new JsonObject();
o.addProperty("tagname", tagName);
String responseContent = this.mainService.post(url, o.toString());
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return tmpJsonElement.getAsJsonObject().get("tagid").getAsString();
}

@Override
public void update(String tagId, String tagName) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/update";
JsonObject o = new JsonObject();
o.addProperty("tagid", tagId);
o.addProperty("tagname", tagName);
this.mainService.post(url, o.toString());
}

@Override
public void delete(String tagId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=" + tagId;
this.mainService.get(url, null);
}

@Override
public List<WxCpTag> listAll() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("taglist"),
new TypeToken<List<WxCpTag>>() {
}.getType()
);
}

@Override
public List<WxCpUser> listUsersByTagId(String tagId) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId;
String responseContent = this.mainService.get(url, null);
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
return WxCpGsonBuilder.INSTANCE.create()
.fromJson(
tmpJsonElement.getAsJsonObject().get("userlist"),
new TypeToken<List<WxCpUser>>() {
}.getType()
);
}

@Override
public WxCpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers";
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
if (userIds != null) {
JsonArray jsonArray = new JsonArray();
for (String userId : userIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("userlist", jsonArray);
}
if (partyIds != null) {
JsonArray jsonArray = new JsonArray();
for (String userId : partyIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("partylist", jsonArray);
}

return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString()));
}

@Override
public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds) throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers";
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tagid", tagId);
JsonArray jsonArray = new JsonArray();
for (String userId : userIds) {
jsonArray.add(new JsonPrimitive(userId));
}
jsonObject.add("userlist", jsonArray);

return WxCpTagAddOrRemoveUsersResult.fromJson(this.mainService.post(url, jsonObject.toString()));
}
}

+ 85
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTagAddOrRemoveUsersResult.java Vedi File

@@ -0,0 +1,85 @@
package me.chanjar.weixin.cp.bean;

import com.google.common.base.Splitter;
import com.google.gson.annotations.SerializedName;
import me.chanjar.weixin.common.util.ToStringUtils;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

/**
* <pre>
* 为标签添加或移除用户结果对象类
* Created by Binary Wang on 2017-6-22.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
public class WxCpTagAddOrRemoveUsersResult {
@Override
public String toString() {
return ToStringUtils.toSimpleString(this);
}

public static WxCpTagAddOrRemoveUsersResult fromJson(String json) {
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpTagAddOrRemoveUsersResult.class);
}

@SerializedName("errcode")
private Integer errCode;

@SerializedName("errmsg")
private String errMsg;

@SerializedName("invalidlist")
private String invalidUsers;

@SerializedName("invalidparty")
private String[] invalidParty;

public Integer getErrCode() {
return this.errCode;
}

public void setErrCode(Integer errCode) {
this.errCode = errCode;
}

public String getErrMsg() {
return this.errMsg;
}

public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}

public String getInvalidUser() {
return this.invalidUsers;
}

public void setInvalidUser(String invalidUser) {
this.invalidUsers = invalidUser;
}

public String[] getInvalidParty() {
return this.invalidParty;
}

public void setInvalidParty(String[] invalidParty) {
this.invalidParty = invalidParty;
}

public List<String> getInvalidUserList() {
return this.content2List(this.invalidUsers);
}

private List<String> content2List(String content) {
if(StringUtils.isBlank(content)){
return Collections.emptyList();
}

return Splitter.on("|").splitToList(content);
}

}

+ 35
- 4
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpUser.java Vedi File

@@ -13,13 +13,45 @@ import java.util.List;
*/
public class WxCpUser implements Serializable {

public enum Gender {
MALE("男", "1"),
FEMAIL("女", "2");

private String genderName;
private String code;

Gender(String genderName, String code) {
this.genderName = genderName;
this.code = code;
}

public String getGenderName() {
return this.genderName;
}

public String getCode() {
return this.code;
}

public static Gender fromCode(String code) {
if ("1".equals(code)) {
return Gender.MALE;
}
if ("2".equals(code)) {
return Gender.FEMAIL;
}

return null;
}
}

private static final long serialVersionUID = -5696099236344075582L;
private String userId;
private String name;
private Integer[] departIds;
private String position;
private String mobile;
private String gender;
private Gender gender;
private String email;
private String avatar;
private Integer status;
@@ -58,11 +90,11 @@ public class WxCpUser implements Serializable {
this.departIds = departIds;
}

public String getGender() {
public Gender getGender() {
return this.gender;
}

public void setGender(String gender) {
public void setGender(Gender gender) {
this.gender = gender;
}

@@ -159,7 +191,6 @@ public class WxCpUser implements Serializable {
}

public static class Attr {

private String name;
private String value;



+ 2
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java Vedi File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.cp.util.json;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
import me.chanjar.weixin.cp.bean.WxCpDepart;
@@ -19,6 +20,7 @@ public class WxCpGsonBuilder {
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
INSTANCE.registerTypeAdapter(WxMenu.class, new WxCpMenuGsonAdapter());
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter());
}



+ 24
- 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMenuGsonAdapter.java Vedi File

@@ -0,0 +1,24 @@
package me.chanjar.weixin.cp.util.json;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.util.json.WxMenuGsonAdapter;

import java.lang.reflect.Type;

/**
* <pre>
* 企业号菜单json转换适配器
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
public class WxCpMenuGsonAdapter extends WxMenuGsonAdapter {

@Override
public WxMenu deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return this.buildMenuFromJson(json.getAsJsonObject().get("button").getAsJsonArray());
}
}

+ 2
- 2
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpUserGsonAdapter.java Vedi File

@@ -39,7 +39,7 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
user.setName(GsonHelper.getString(o, "name"));
user.setPosition(GsonHelper.getString(o, "position"));
user.setMobile(GsonHelper.getString(o, "mobile"));
user.setGender(GsonHelper.getString(o, "gender"));
user.setGender(WxCpUser.Gender.fromCode(GsonHelper.getString(o, "gender")));
user.setEmail(GsonHelper.getString(o, "email"));
user.setAvatar(GsonHelper.getString(o, "avatar"));
user.setStatus(GsonHelper.getInteger(o, "status"));
@@ -85,7 +85,7 @@ public class WxCpUserGsonAdapter implements JsonDeserializer<WxCpUser>, JsonSeri
o.addProperty("mobile", user.getMobile());
}
if (user.getGender() != null) {
o.addProperty("gender", user.getGender());
o.addProperty("gender", user.getGender().getCode());
}
if (user.getEmail() != null) {
o.addProperty("email", user.getEmail());


+ 1
- 1
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/ApiTestModule.java Vedi File

@@ -31,7 +31,7 @@ public class ApiTestModule implements Module {
wxService.setWxCpConfigStorage(config);

binder.bind(WxCpService.class).toInstance(wxService);
binder.bind(WxCpConfigStorage.class).toInstance(config);
binder.bind(WxXmlCpInMemoryConfigStorage.class).toInstance(config);
} catch (IOException e) {
e.printStackTrace();
}


+ 0
- 77
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMediaAPITest.java Vedi File

@@ -1,77 +0,0 @@
package me.chanjar.weixin.cp.api;

import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
* 测试多媒体文件上传下载
*
* @author Daniel Qian
*/
//@Test(groups="mediaAPI", dependsOnGroups="baseAPI")
@Test
@Guice(modules = ApiTestModule.class)
public class WxCpMediaAPITest {

@Inject
protected WxCpServiceImpl wxService;

private List<String> media_ids = new ArrayList<>();

@Test(dataProvider = "uploadMedia")
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
try (InputStream inputStream = ClassLoader
.getSystemResourceAsStream(fileName);) {
WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType,
inputStream);
Assert.assertNotNull(res.getType());
Assert.assertNotNull(res.getCreatedAt());
Assert.assertTrue(
res.getMediaId() != null || res.getThumbMediaId() != null);

if (res.getMediaId() != null) {
this.media_ids.add(res.getMediaId());
}
if (res.getThumbMediaId() != null) {
this.media_ids.add(res.getThumbMediaId());
}
}
}

@DataProvider
public Object[][] uploadMedia() {
return new Object[][]{
new Object[]{WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_MP3, "mm.mp3"},
new Object[]{WxConsts.MEDIA_VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MEDIA_FILE, TestConstants.FILE_JPG, "mm.jpeg"}
};
}

@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
public void testDownloadMedia(String media_id) throws WxErrorException {
this.wxService.mediaDownload(media_id);
}

@DataProvider
public Object[][] downloadMedia() {
Object[][] params = new Object[this.media_ids.size()][];
for (int i = 0; i < this.media_ids.size(); i++) {
params[i] = new Object[]{this.media_ids.get(i)};
}
return params;
}

}

+ 0
- 68
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpTagAPITest.java Vedi File

@@ -1,68 +0,0 @@
package me.chanjar.weixin.cp.api;

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;

import java.util.ArrayList;
import java.util.List;

@Test(groups = "departAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)
public class WxCpTagAPITest {

@Inject
protected WxCpServiceImpl wxService;

@Inject
protected WxCpConfigStorage configStorage;

protected String tagId;

public void testTagCreate() throws Exception {
this.tagId = this.wxService.tagCreate("测试标签4");
System.out.println(this.tagId);
}

@Test(dependsOnMethods = "testTagCreate")
public void testTagUpdate() throws Exception {
this.wxService.tagUpdate(this.tagId, "测试标签-改名");
}

@Test(dependsOnMethods = "testTagUpdate")
public void testTagGet() throws Exception {
List<WxCpTag> tags = this.wxService.tagGet();
Assert.assertNotEquals(tags.size(), 0);
}

@Test(dependsOnMethods = "testTagGet")
public void testTagAddUsers() throws Exception {
List<String> userIds = new ArrayList<>();
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage) this.configStorage).getUserId());
this.wxService.tagAddUsers(this.tagId, userIds, null);
}

@Test(dependsOnMethods = "testTagAddUsers")
public void testTagGetUsers() throws Exception {
List<WxCpUser> users = this.wxService.tagGetUsers(this.tagId);
Assert.assertNotEquals(users.size(), 0);
}

@Test(dependsOnMethods = "testTagGetUsers")
public void testTagRemoveUsers() throws Exception {
List<String> userIds = new ArrayList<>();
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage) this.configStorage).getUserId());
this.wxService.tagRemoveUsers(this.tagId, userIds);
}

@Test(dependsOnMethods = "testTagRemoveUsers")
public void testTagDelete() throws Exception {
this.wxService.tagDelete(this.tagId);
}

}

+ 78
- 0
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMediaServiceImplTest.java Vedi File

@@ -0,0 +1,78 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.TestConstants;
import me.chanjar.weixin.cp.api.WxCpService;
import org.testng.annotations.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.testng.Assert.*;

/**
* <pre>
*
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
@Guice(modules = ApiTestModule.class)
public class WxCpMediaServiceImplTest {
@Inject
private WxCpService wxService;

private List<String> mediaIds = new ArrayList<>();

@DataProvider
public Object[][] mediaData() {
return new Object[][]{
new Object[]{WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_MP3, "mm.mp3"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_AMR, "mm.amr"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
new Object[]{WxConsts.MEDIA_VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MEDIA_FILE, TestConstants.FILE_JPG, "mm.jpeg"}
};
}

@Test(dataProvider = "mediaData")
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) {
WxMediaUploadResult res = this.wxService.getMediaService().upload(mediaType, fileType, inputStream);
assertNotNull(res.getType());
assertNotNull(res.getCreatedAt());
assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);

if (res.getMediaId() != null) {
this.mediaIds.add(res.getMediaId());
}
if (res.getThumbMediaId() != null) {
this.mediaIds.add(res.getThumbMediaId());
}
}
}

@DataProvider
public Object[][] downloadMedia() {
Object[][] params = new Object[this.mediaIds.size()][];
for (int i = 0; i < this.mediaIds.size(); i++) {
params[i] = new Object[]{this.mediaIds.get(i)};
}
return params;
}

@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
public void testDownloadMedia(String media_id) throws WxErrorException {
File file = this.wxService.getMediaService().download(media_id);
assertNotNull(file);
System.out.println(file);
}

}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxMenuAPITest.java → weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpMenuServiceImplTest.java Vedi File

@@ -1,45 +1,29 @@
package me.chanjar.weixin.cp.api;
package me.chanjar.weixin.cp.api.impl;

import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import org.testng.annotations.*;

import static org.testng.Assert.*;

/**
* 测试菜单
* <pre>
*
* @author Daniel Qian
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
@Test(groups = "menuAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)
public class WxMenuAPITest {

public class WxCpMenuServiceImplTest {
@Inject
protected WxCpServiceImpl wxService;

@Test(dataProvider = "menu")
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
this.wxService.menuCreate(wxMenu);
}
protected WxCpService wxService;

@Test(dependsOnMethods = {"testCreateMenu"})
public void testGetMenu() throws WxErrorException {
Assert.assertNotNull(this.wxService.menuGet());
}

@Test(dependsOnMethods = {"testGetMenu"})
public void testDeleteMenu() throws WxErrorException {
this.wxService.menuDelete();
}

@DataProvider(name = "menu")
public Object[][] getMenu() {
@DataProvider
public Object[][] menuData() {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxConsts.BUTTON_CLICK);
@@ -85,5 +69,21 @@ public class WxMenuAPITest {

}

@Test(dataProvider = "menuData")
public void testCreate(WxMenu wxMenu) throws Exception {
this.wxService.getMenuService().create(wxMenu);
}

@Test(dependsOnMethods = "testCreate")
public void testGet() throws Exception {
WxMenu menu = this.wxService.getMenuService().get();
assertNotNull(menu);
System.out.println(menu.toJson());
}

@Test(dependsOnMethods = {"testGet", "testCreate"})
public void testDelete() throws Exception {
this.wxService.getMenuService().delete();
}

}

+ 75
- 0
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java Vedi File

@@ -0,0 +1,75 @@
package me.chanjar.weixin.cp.api.impl;

import com.google.common.base.Splitter;
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.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.testng.annotations.*;

import java.util.List;

import static org.testng.Assert.*;

/**
* <pre>
*
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
@Guice(modules = ApiTestModule.class)
public class WxCpTagServiceImplTest {
@Inject
protected WxCpService wxService;

@Inject
protected ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage;

protected String tagId;

@Test
public void testCreate() throws Exception {
this.tagId = this.wxService.getTagService().create("测试标签" + System.currentTimeMillis());
System.out.println(this.tagId);
}

@Test(dependsOnMethods = "testCreate")
public void testUpdate() throws Exception {
this.wxService.getTagService().update(this.tagId, "测试标签-改名" + System.currentTimeMillis());
}

@Test(dependsOnMethods = {"testUpdate", "testCreate"})
public void testListAll() throws Exception {
List<WxCpTag> tags = this.wxService.getTagService().listAll();
assertNotEquals(tags.size(), 0);
}

@Test(dependsOnMethods = {"testListAll", "testUpdate", "testCreate"})
public void testAddUsers2Tag() throws Exception {
List<String> userIds = Splitter.on("|").splitToList(this.configStorage.getUserId());
WxCpTagAddOrRemoveUsersResult result = this.wxService.getTagService().addUsers2Tag(this.tagId, userIds, null);
assertEquals(result.getErrCode(), Integer.valueOf(0));
}

@Test(dependsOnMethods = {"testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"})
public void testListUsersByTagId() throws Exception {
List<WxCpUser> users = this.wxService.getTagService().listUsersByTagId(this.tagId);
assertNotEquals(users.size(), 0);
}

@Test(dependsOnMethods = {"testListUsersByTagId", "testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"})
public void testRemoveUsersFromTag() throws Exception {
List<String> userIds = Splitter.on("|").splitToList(this.configStorage.getUserId());
WxCpTagAddOrRemoveUsersResult result = this.wxService.getTagService().removeUsersFromTag(this.tagId, userIds);
assertEquals(result.getErrCode(), Integer.valueOf(0));
}

@Test(dependsOnMethods = {"testRemoveUsersFromTag", "testListUsersByTagId", "testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"})
public void testDelete() throws Exception {
this.wxService.getTagService().delete(this.tagId);
}

}

+ 10
- 10
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java Vedi File

@@ -6,9 +6,7 @@ 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 org.testng.annotations.*;

import java.util.List;

@@ -25,19 +23,21 @@ import static org.testng.Assert.*;
public class WxCpUserServiceImplTest {
@Inject
private WxCpService wxCpService;
private String userId = "someone" + System.currentTimeMillis();

@Test
public void testAuthenticate() throws Exception {
this.wxCpService.getUserService().authenticate("abc");
}

@Test
public void testCreate() throws Exception {
WxCpUser user = new WxCpUser();
user.setUserId("some.woman");
user.setUserId(userId);
user.setName("Some Woman");
user.setDepartIds(new Integer[]{9, 8});
user.setDepartIds(new Integer[]{2});
user.setEmail("none@none.com");
user.setGender("女");
user.setGender(WxCpUser.Gender.FEMAIL);
user.setMobile("13560084979");
user.setPosition("woman");
user.setTelephone("3300393");
@@ -48,20 +48,20 @@ public class WxCpUserServiceImplTest {
@Test(dependsOnMethods = "testCreate")
public void testUpdate() throws Exception {
WxCpUser user = new WxCpUser();
user.setUserId("some.woman");
user.setUserId(userId);
user.setName("Some Woman");
user.addExtAttr("爱好", "table2");
this.wxCpService.getUserService().update(user);
}

@Test
@Test(dependsOnMethods = {"testCreate", "testUpdate"})
public void testDelete() throws Exception {
this.wxCpService.getUserService().delete("some.woman");
this.wxCpService.getUserService().delete(userId);
}

@Test(dependsOnMethods = "testUpdate")
public void testGetById() throws Exception {
WxCpUser user = this.wxCpService.getUserService().getById("some.woman");
WxCpUser user = this.wxCpService.getUserService().getById(userId);
assertNotNull(user);
}



+ 4
- 5
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpDemoServer.java Vedi File

@@ -1,14 +1,14 @@
package me.chanjar.weixin.cp.demo;

import me.chanjar.weixin.common.session.WxSessionManager;
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;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@@ -69,8 +69,7 @@ public class WxCpDemoServer {
Map<String, Object> context, WxCpService wxService,
WxSessionManager sessionManager) {
String href = "<a href=\""
+ wxService.oauth2buildAuthorizationUrl(
wxCpConfigStorage.getOauth2redirectUri(), null)
+ wxService.getOauth2Service().buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null)
+ "\">测试oauth2</a>";
return WxCpXmlOutMessage.TEXT().content(href)
.fromUser(wxMessage.getToUserName())


+ 1
- 1
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/demo/WxCpOAuth2Servlet.java Vedi File

@@ -30,7 +30,7 @@ public class WxCpOAuth2Servlet extends HttpServlet {
response.getWriter().println("<h1>code</h1>");
response.getWriter().println(code);

String[] res = this.wxCpService.oauth2getUserInfo(code);
String[] res = this.wxCpService.getOauth2Service().getUserInfo(code);
response.getWriter().println("<h1>result</h1>");
response.getWriter().println(Arrays.toString(res));
} catch (WxErrorException e) {


BIN
weixin-java-cp/src/test/resources/mm.amr Vedi File


+ 0
- 5
weixin-java-cp/src/test/resources/testng.xml Vedi File

@@ -6,12 +6,7 @@
<class name="me.chanjar.weixin.cp.api.WxCpBusyRetryTest"/>
<class name="me.chanjar.weixin.cp.api.WxCpBaseAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpMessageAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxMenuAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpDepartAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpMediaAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpMessageRouterTest"/>
<class name="me.chanjar.weixin.cp.api.WxCpTagAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpUserAPITest"/>
</classes>
</test>



Caricamento…
Annulla
Salva