mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	添加标签管理支持
This commit is contained in:
		| @ -10,9 +10,11 @@ import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | |||||||
|  */ |  */ | ||||||
| public class WxError { | public class WxError { | ||||||
|  |  | ||||||
|   protected int errorCode; |   private int errorCode; | ||||||
|    |    | ||||||
|   protected String errorMsg; |   private String errorMsg; | ||||||
|  |  | ||||||
|  |   private String json; | ||||||
|    |    | ||||||
|   public int getErrorCode() { |   public int getErrorCode() { | ||||||
|     return errorCode; |     return errorCode; | ||||||
| @ -31,12 +33,14 @@ public class WxError { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static WxError fromJson(String json) { |   public static WxError fromJson(String json) { | ||||||
|     return WxCpGsonBuilder.create().fromJson(json, WxError.class); |     WxError error = WxCpGsonBuilder.create().fromJson(json, WxError.class); | ||||||
|  |     error.json = json; | ||||||
|  |     return error; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public String toString() { |   public String toString() { | ||||||
|     return "微信错误 errcode=" + errorCode + ", errmsg=" + errorMsg; |     return "微信错误 errcode=" + errorCode + ", errmsg=" + errorMsg + "\njson:" + json; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ public class WxCpConsts { | |||||||
|   public static final String XML_MSG_EVENT = "event"; |   public static final String XML_MSG_EVENT = "event"; | ||||||
|  |  | ||||||
|   /////////////////////// |   /////////////////////// | ||||||
|   // 客服消息的消息类型 |   // 消息的消息类型 | ||||||
|   /////////////////////// |   /////////////////////// | ||||||
|   public static final String CUSTOM_MSG_TEXT = "text"; |   public static final String CUSTOM_MSG_TEXT = "text"; | ||||||
|   public static final String CUSTOM_MSG_IMAGE = "image"; |   public static final String CUSTOM_MSG_IMAGE = "image"; | ||||||
|  | |||||||
| @ -21,10 +21,11 @@ public interface WxCpService { | |||||||
|    * 验证推送过来的消息的正确性 |    * 验证推送过来的消息的正确性 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param msgSignature |    * @param msgSignature | ||||||
|    * @param timestamp |    * @param timestamp | ||||||
|    * @param nonce |    * @param nonce | ||||||
|    * @param data       微信传输过来的数据,有可能是echoStr,有可能是xml消息 |    * @param data         微信传输过来的数据,有可能是echoStr,有可能是xml消息 | ||||||
|    * @return |    * @return | ||||||
|    */ |    */ | ||||||
|   public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data); |   public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data); | ||||||
| @ -34,6 +35,7 @@ public interface WxCpService { | |||||||
|    *   用在二次验证的时候 |    *   用在二次验证的时候 | ||||||
|    *   企业在员工验证成功后,调用本方法告诉企业号平台该员工关注成功。 |    *   企业在员工验证成功后,调用本方法告诉企业号平台该员工关注成功。 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param userId |    * @param userId | ||||||
|    */ |    */ | ||||||
|   public void userAuthenticated(String userId) throws WxErrorException; |   public void userAuthenticated(String userId) throws WxErrorException; | ||||||
| @ -42,13 +44,11 @@ public interface WxCpService { | |||||||
|    * <pre> |    * <pre> | ||||||
|    * 获取access_token,本方法线程安全 |    * 获取access_token,本方法线程安全 | ||||||
|    * 且在多线程同时刷新时只刷新一次,避免超出2000次/日的调用次数上限 |    * 且在多线程同时刷新时只刷新一次,避免超出2000次/日的调用次数上限 | ||||||
|    *  |  | ||||||
|    * 另:本service的所有方法都会在access_token过期是调用此方法 |    * 另:本service的所有方法都会在access_token过期是调用此方法 | ||||||
|    *  |  | ||||||
|    * 程序员在非必要情况下尽量不要主动调用此方法 |    * 程序员在非必要情况下尽量不要主动调用此方法 | ||||||
|  |  | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @throws me.chanjar.weixin.enterprise.exception.WxErrorException |    * @throws me.chanjar.weixin.enterprise.exception.WxErrorException | ||||||
|    */ |    */ | ||||||
|   public void accessTokenRefresh() throws WxErrorException; |   public void accessTokenRefresh() throws WxErrorException; | ||||||
| @ -56,27 +56,27 @@ public interface WxCpService { | |||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 上传多媒体文件 |    * 上传多媒体文件 | ||||||
|    *  |  | ||||||
|    * 上传的多媒体文件有格式和大小限制,如下: |    * 上传的多媒体文件有格式和大小限制,如下: | ||||||
|    *   图片(image): 1M,支持JPG格式 |    *   图片(image): 1M,支持JPG格式 | ||||||
|    *   语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 |    *   语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 | ||||||
|    *   视频(video):10MB,支持MP4格式 |    *   视频(video):10MB,支持MP4格式 | ||||||
|    *   缩略图(thumb):64KB,支持JPG格式 |    *   缩略图(thumb):64KB,支持JPG格式 | ||||||
|    *     |  | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 | ||||||
|    * </pre> |    * </pre> | ||||||
|    * @param mediaType         媒体类型, 请看{@link WxCpConsts} |    * | ||||||
|    * @param fileType          文件类型,请看{@link WxCpConsts} |    * @param mediaType   媒体类型, 请看{@link WxCpConsts} | ||||||
|    * @param inputStream       输入流 |    * @param fileType    文件类型,请看{@link WxCpConsts} | ||||||
|  |    * @param inputStream 输入流 | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException; |   public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) | ||||||
|  |       throws WxErrorException, IOException; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * @see #mediaUpload(String, String, InputStream) |  | ||||||
|    * @param mediaType |    * @param mediaType | ||||||
|    * @param file |    * @param file | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|  |    * @see #mediaUpload(String, String, InputStream) | ||||||
|    */ |    */ | ||||||
|   public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException; |   public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException; | ||||||
|  |  | ||||||
| @ -86,17 +86,19 @@ public interface WxCpService { | |||||||
|    * 根据微信文档,视频文件下载不了,会返回null |    * 根据微信文档,视频文件下载不了,会返回null | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件 | ||||||
|    * </pre> |    * </pre> | ||||||
|    * @params media_id |    * | ||||||
|    * @return 保存到本地的临时文件 |    * @return 保存到本地的临时文件 | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|  |    * @params media_id | ||||||
|    */ |    */ | ||||||
|   public File mediaDownload(String media_id) throws WxErrorException; |   public File mediaDownload(String media_id) throws WxErrorException; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 发送客服消息 |    * 发送消息 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=发送客服消息 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=发送消息 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param message |    * @param message | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
| @ -107,6 +109,7 @@ public interface WxCpService { | |||||||
|    * 自定义菜单创建接口 |    * 自定义菜单创建接口 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param menu |    * @param menu | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
| @ -117,6 +120,7 @@ public interface WxCpService { | |||||||
|    * 自定义菜单删除接口 |    * 自定义菜单删除接口 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   public void menuDelete() throws WxErrorException; |   public void menuDelete() throws WxErrorException; | ||||||
| @ -126,6 +130,7 @@ public interface WxCpService { | |||||||
|    * 自定义菜单查询接口 |    * 自定义菜单查询接口 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @return |    * @return | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
| @ -137,6 +142,7 @@ public interface WxCpService { | |||||||
|    * 最多支持创建500个部门 |    * 最多支持创建500个部门 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param depart 部门 |    * @param depart 部门 | ||||||
|    * @return 部门id |    * @return 部门id | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
| @ -148,6 +154,7 @@ public interface WxCpService { | |||||||
|    * 部门管理接口 - 查询所有部门 |    * 部门管理接口 - 查询所有部门 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @return |    * @return | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
| @ -157,9 +164,9 @@ public interface WxCpService { | |||||||
|    * <pre> |    * <pre> | ||||||
|    * 部门管理接口 - 修改部门名 |    * 部门管理接口 - 修改部门名 | ||||||
|    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 |    * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口 | ||||||
|    *  |  | ||||||
|    * 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误 |    * 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误 | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param group 要更新的group,group的id,name必须设置 |    * @param group 要更新的group,group的id,name必须设置 | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
| @ -168,33 +175,114 @@ public interface WxCpService { | |||||||
|   /** |   /** | ||||||
|    * <pre> |    * <pre> | ||||||
|    * 部门管理接口 - 删除部门 |    * 部门管理接口 - 删除部门 | ||||||
|    * |  | ||||||
|    * </pre> |    * </pre> | ||||||
|  |    * | ||||||
|    * @param departId |    * @param departId | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   public void departDelete(Integer departId) throws WxErrorException; |   public void departDelete(Integer departId) throws WxErrorException; | ||||||
|  |  | ||||||
|   public void userCreate(WxCpUser user) throws WxErrorException; |  | ||||||
|  |  | ||||||
|   public void userUpdate(WxCpUser user) throws WxErrorException; |  | ||||||
|  |  | ||||||
|   public void userDelete(String userid) throws WxErrorException; |  | ||||||
|  |  | ||||||
|   public WxCpUser userGet(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 |    * 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 departId   必填。部门id | ||||||
|    * @param status      非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 |    * @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员 | ||||||
|  |    * @param status     非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 | ||||||
|    * @return |    * @return | ||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   public List<WxCpUser> userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; |   public List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 新建用户 | ||||||
|  |    * | ||||||
|  |    * @param user | ||||||
|  |    * @throws WxErrorException | ||||||
|  |    */ | ||||||
|  |   public void userCreate(WxCpUser user) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 更新用户 | ||||||
|  |    * | ||||||
|  |    * @param user | ||||||
|  |    * @throws WxErrorException | ||||||
|  |    */ | ||||||
|  |   public void userUpdate(WxCpUser user) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 删除用户 | ||||||
|  |    * | ||||||
|  |    * @param userid | ||||||
|  |    * @throws WxErrorException | ||||||
|  |    */ | ||||||
|  |   public void userDelete(String userid) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 获取用户 | ||||||
|  |    * | ||||||
|  |    * @param userid | ||||||
|  |    * @return | ||||||
|  |    * @throws WxErrorException | ||||||
|  |    */ | ||||||
|  |   public WxCpUser userGet(String userid) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 创建标签 | ||||||
|  |    * | ||||||
|  |    * @param tagName | ||||||
|  |    * @return | ||||||
|  |    */ | ||||||
|  |   public String tagCreate(String tagName) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 更新标签 | ||||||
|  |    * | ||||||
|  |    * @param tagId | ||||||
|  |    * @param tagName | ||||||
|  |    */ | ||||||
|  |   public void tagUpdate(String tagId, String tagName) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 删除标签 | ||||||
|  |    * | ||||||
|  |    * @param tagId | ||||||
|  |    */ | ||||||
|  |   public void tagDelete(String tagId) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 获得标签列表 | ||||||
|  |    * | ||||||
|  |    * @return | ||||||
|  |    */ | ||||||
|  |   public List<WxCpTag> tagGet() throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 获取标签成员 | ||||||
|  |    * | ||||||
|  |    * @param tagId | ||||||
|  |    * @return | ||||||
|  |    */ | ||||||
|  |   public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 增加标签成员 | ||||||
|  |    * | ||||||
|  |    * @param tagId | ||||||
|  |    * @param userIds | ||||||
|  |    */ | ||||||
|  |   public void tagAddUsers(String tagId, List<String> userIds) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 移除标签成员 | ||||||
|  |    * | ||||||
|  |    * @param tagId | ||||||
|  |    * @param userIds | ||||||
|  |    */ | ||||||
|  |   public void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorException; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 注入 {@link WxCpConfigStorage} 的实现 |    * 注入 {@link WxCpConfigStorage} 的实现 | ||||||
|  |    * | ||||||
|    * @param wxConfigProvider |    * @param wxConfigProvider | ||||||
|    */ |    */ | ||||||
|   public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider); |   public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider); | ||||||
|  | |||||||
| @ -8,6 +8,9 @@ import java.util.List; | |||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| import java.util.concurrent.atomic.AtomicBoolean; | import java.util.concurrent.atomic.AtomicBoolean; | ||||||
|  |  | ||||||
|  | import com.google.gson.JsonArray; | ||||||
|  | import com.google.gson.JsonObject; | ||||||
|  | import com.google.gson.JsonPrimitive; | ||||||
| import me.chanjar.weixin.common.bean.result.WxAccessToken; | import me.chanjar.weixin.common.bean.result.WxAccessToken; | ||||||
| import me.chanjar.weixin.common.util.GsonHelper; | import me.chanjar.weixin.common.util.GsonHelper; | ||||||
| import me.chanjar.weixin.enterprise.bean.*; | import me.chanjar.weixin.enterprise.bean.*; | ||||||
| @ -208,7 +211,7 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Override |   @Override | ||||||
|   public List<WxCpUser> userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { |   public List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException { | ||||||
|     String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId; |     String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId; | ||||||
|     String params = ""; |     String params = ""; | ||||||
|     if (fetchChild != null) { |     if (fetchChild != null) { | ||||||
| @ -229,6 +232,81 @@ public class WxCpServiceImpl implements WxCpService { | |||||||
|         ); |         ); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   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 = execute(new SimplePostRequestExecutor(), url, o.toString()); | ||||||
|  |     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||||
|  |     return tmpJsonElement.getAsJsonObject().get("tagid").getAsString(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   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); | ||||||
|  |     execute(new SimplePostRequestExecutor(), url, o.toString()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public void tagDelete(String tagId) throws WxErrorException { | ||||||
|  |     String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?tagid=" + tagId; | ||||||
|  |     execute(new SimpleGetRequestExecutor(), url, null); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public List<WxCpTag> tagGet() throws WxErrorException { | ||||||
|  |     String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list"; | ||||||
|  |     String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | ||||||
|  |     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||||
|  |     return WxCpGsonBuilder.INSTANCE.create() | ||||||
|  |         .fromJson( | ||||||
|  |             tmpJsonElement.getAsJsonObject().get("taglist"), | ||||||
|  |             new TypeToken<List<WxCpTag>>() { }.getType() | ||||||
|  |         ); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException { | ||||||
|  |     String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagid=" + tagId; | ||||||
|  |     String responseContent = execute(new SimpleGetRequestExecutor(), url, null); | ||||||
|  |     JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); | ||||||
|  |     return WxCpGsonBuilder.INSTANCE.create() | ||||||
|  |         .fromJson( | ||||||
|  |             tmpJsonElement.getAsJsonObject().get("userlist"), | ||||||
|  |             new TypeToken<List<WxCpUser>>() { }.getType() | ||||||
|  |         ); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public void tagAddUsers(String tagId, List<String> userIds) throws WxErrorException { | ||||||
|  |     String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers"; | ||||||
|  |     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); | ||||||
|  |     execute(new SimplePostRequestExecutor(), url, jsonObject.toString()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   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); | ||||||
|  |     execute(new SimplePostRequestExecutor(), url, jsonObject.toString()); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 |    * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求 | ||||||
|    * |    * | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ import me.chanjar.weixin.enterprise.bean.messagebuilder.*; | |||||||
| import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 客服消息 |  * 消息 | ||||||
|  * @author Daniel Qian |  * @author Daniel Qian | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
|  | |||||||
| @ -0,0 +1,55 @@ | |||||||
|  | package me.chanjar.weixin.enterprise.bean; | ||||||
|  |  | ||||||
|  | import com.google.gson.JsonElement; | ||||||
|  | import com.google.gson.JsonObject; | ||||||
|  | import com.google.gson.internal.Streams; | ||||||
|  | import com.google.gson.stream.JsonReader; | ||||||
|  | import me.chanjar.weixin.common.util.GsonHelper; | ||||||
|  | import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder; | ||||||
|  |  | ||||||
|  | import java.io.StringReader; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * Created by Daniel Qian | ||||||
|  |  */ | ||||||
|  | public class WxCpTag { | ||||||
|  |  | ||||||
|  |   private String id; | ||||||
|  |  | ||||||
|  |   private String name; | ||||||
|  |  | ||||||
|  |   public WxCpTag() { | ||||||
|  |     super(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public WxCpTag(String id, String name) { | ||||||
|  |     super(); | ||||||
|  |     this.id = id; | ||||||
|  |     this.name = name; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String getName() { | ||||||
|  |     return name; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public void setName(String name) { | ||||||
|  |     this.name = name; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String getId() { | ||||||
|  |     return id; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public void setId(String id) { | ||||||
|  |     this.id = id; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public static WxCpTag fromJson(String json) { | ||||||
|  |     return WxCpGsonBuilder.create().fromJson(json, WxCpTag.class); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String toJson() { | ||||||
|  |     return WxCpGsonBuilder.create().toJson(this); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @ -96,14 +96,6 @@ public class WxCpUser { | |||||||
|     this.weiXinId = weiXinId; |     this.weiXinId = weiXinId; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public String toJson() { |  | ||||||
|     return WxCpGsonBuilder.INSTANCE.create().toJson(this); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   public static WxCpUser fromJson(String json) { |  | ||||||
|     return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   public void addExtAttr(String name, String value) { |   public void addExtAttr(String name, String value) { | ||||||
|     this.extAttrs.add(new Attr(name, value)); |     this.extAttrs.add(new Attr(name, value)); | ||||||
|   } |   } | ||||||
| @ -112,6 +104,14 @@ public class WxCpUser { | |||||||
|     return extAttrs; |     return extAttrs; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   public String toJson() { | ||||||
|  |     return WxCpGsonBuilder.INSTANCE.create().toJson(this); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public static WxCpUser fromJson(String json) { | ||||||
|  |     return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   public static class Attr { |   public static class Attr { | ||||||
|  |  | ||||||
|     private String name; |     private String name; | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package me.chanjar.weixin.enterprise.util.json; | package me.chanjar.weixin.enterprise.util.json; | ||||||
|  |  | ||||||
|  |  | ||||||
| import com.google.gson.Gson; | import com.google.gson.Gson; | ||||||
| import com.google.gson.GsonBuilder; | import com.google.gson.GsonBuilder; | ||||||
| import me.chanjar.weixin.common.bean.result.WxAccessToken; | import me.chanjar.weixin.common.bean.result.WxAccessToken; | ||||||
| @ -21,6 +20,7 @@ public class WxCpGsonBuilder { | |||||||
|     INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxCpAccessTokenAdapter()); |     INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxCpAccessTokenAdapter()); | ||||||
|     INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); |     INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter()); | ||||||
|     INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxCpMediaUploadResultAdapter()); |     INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxCpMediaUploadResultAdapter()); | ||||||
|  |     INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static Gson create() { |   public static Gson create() { | ||||||
|  | |||||||
| @ -0,0 +1,39 @@ | |||||||
|  | /* | ||||||
|  |  * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. | ||||||
|  |  * | ||||||
|  |  * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended | ||||||
|  |  * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction | ||||||
|  |  * arose from modification of the original source, or other redistribution of this source | ||||||
|  |  * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. | ||||||
|  |  */ | ||||||
|  | package me.chanjar.weixin.enterprise.util.json; | ||||||
|  |  | ||||||
|  | import com.google.gson.*; | ||||||
|  | import com.google.gson.internal.Streams; | ||||||
|  | import com.google.gson.stream.JsonReader; | ||||||
|  | import me.chanjar.weixin.common.util.GsonHelper; | ||||||
|  | import me.chanjar.weixin.enterprise.bean.WxCpDepart; | ||||||
|  | import me.chanjar.weixin.enterprise.bean.WxCpTag; | ||||||
|  |  | ||||||
|  | import java.io.StringReader; | ||||||
|  | import java.lang.reflect.Type; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author Daniel Qian | ||||||
|  |  */ | ||||||
|  | public class WxCpTagGsonAdapter implements JsonSerializer<WxCpTag>, JsonDeserializer<WxCpTag> { | ||||||
|  |  | ||||||
|  |   public JsonElement serialize(WxCpTag group, Type typeOfSrc, JsonSerializationContext context) { | ||||||
|  |     JsonObject o = new JsonObject(); | ||||||
|  |     o.addProperty("tagid", group.getId()); | ||||||
|  |     o.addProperty("tagname", group.getName()); | ||||||
|  |     return o; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public WxCpTag deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||||||
|  |       throws JsonParseException { | ||||||
|  |     JsonObject jsonObject = json.getAsJsonObject(); | ||||||
|  |     return new WxCpTag(GsonHelper.getString(jsonObject, "tagid"), GsonHelper.getString(jsonObject, "name")); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @ -9,7 +9,7 @@ import me.chanjar.weixin.enterprise.exception.WxErrorException; | |||||||
| import com.google.inject.Inject; | import com.google.inject.Inject; | ||||||
|  |  | ||||||
| /*** | /*** | ||||||
|  * 测试发送客服消息 |  * 测试发送消息 | ||||||
|  * @author Daniel Qian |  * @author Daniel Qian | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
|  | |||||||
| @ -0,0 +1,66 @@ | |||||||
|  | package me.chanjar.weixin.enterprise.api; | ||||||
|  |  | ||||||
|  | import com.google.inject.Inject; | ||||||
|  | import me.chanjar.weixin.enterprise.bean.WxCpTag; | ||||||
|  | import me.chanjar.weixin.enterprise.bean.WxCpUser; | ||||||
|  | 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 { | ||||||
|  |     tagId = wxService.tagCreate("测试标签4"); | ||||||
|  |     System.out.println(tagId); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = "testTagCreate") | ||||||
|  |   public void testTagUpdate() throws Exception { | ||||||
|  |     wxService.tagUpdate(tagId, "测试标签-改名"); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = "testTagUpdate") | ||||||
|  |   public void testTagGet() throws Exception { | ||||||
|  |     List<WxCpTag> tags = wxService.tagGet(); | ||||||
|  |     Assert.assertNotEquals(tags.size(), 0); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = "testTagGet") | ||||||
|  |   public void testTagAddUsers() throws Exception { | ||||||
|  |     List<String> userIds = new ArrayList<String>(); | ||||||
|  |     userIds.add(((ApiTestModule.WxXmlCpConfigStorage)configStorage).getUserId()); | ||||||
|  |     wxService.tagAddUsers(tagId, userIds); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = "testTagAddUsers") | ||||||
|  |   public void testTagGetUsers() throws Exception { | ||||||
|  |     List<WxCpUser> users = wxService.tagGetUsers(tagId); | ||||||
|  |     Assert.assertNotEquals(users.size(), 0); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = "testTagGetUsers") | ||||||
|  |   public void testTagRemoveUsers() throws Exception { | ||||||
|  |     List<String> userIds = new ArrayList<String>(); | ||||||
|  |     userIds.add(((ApiTestModule.WxXmlCpConfigStorage)configStorage).getUserId()); | ||||||
|  |     wxService.tagRemoveUsers(tagId, userIds); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   @Test(dependsOnMethods = "testTagRemoveUsers") | ||||||
|  |   public void testTagDelete() throws Exception { | ||||||
|  |     wxService.tagDelete(tagId); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | } | ||||||
| @ -54,12 +54,12 @@ public class WxCpUserAPITest { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test(dependsOnMethods = "testUserGet") |   @Test(dependsOnMethods = "testUserGet") | ||||||
|   public void testUserGetByDepart() throws WxErrorException { |   public void testDepartGetUsers() throws WxErrorException { | ||||||
|     List<WxCpUser> users = wxService.userGetByDepart(1, true, 0); |     List<WxCpUser> users = wxService.departGetUsers(1, true, 0); | ||||||
|     Assert.assertNotEquals(users.size(), 0); |     Assert.assertNotEquals(users.size(), 0); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test(dependsOnMethods = "testUserGetByDepart") |   @Test(dependsOnMethods = "testDepartGetUsers") | ||||||
|   public void testUserDelete() throws WxErrorException { |   public void testUserDelete() throws WxErrorException { | ||||||
|     wxService.userDelete("xiaohe.yang"); |     wxService.userDelete("xiaohe.yang"); | ||||||
|   } |   } | ||||||
|  | |||||||
| @ -9,7 +9,9 @@ | |||||||
| 			<class name="me.chanjar.weixin.enterprise.api.WxCpDepartAPITest" /> | 			<class name="me.chanjar.weixin.enterprise.api.WxCpDepartAPITest" /> | ||||||
| 			<class name="me.chanjar.weixin.enterprise.api.WxCpMediaAPITest" /> | 			<class name="me.chanjar.weixin.enterprise.api.WxCpMediaAPITest" /> | ||||||
| 			<class name="me.chanjar.weixin.enterprise.api.WxCpMessageRouterTest" /> | 			<class name="me.chanjar.weixin.enterprise.api.WxCpMessageRouterTest" /> | ||||||
| 		</classes> |             <class name="me.chanjar.weixin.enterprise.api.WxCpTagAPITest" /> | ||||||
|  |             <class name="me.chanjar.weixin.enterprise.api.WxCpUserAPITest" /> | ||||||
|  |         </classes> | ||||||
| 	</test> | 	</test> | ||||||
|  |  | ||||||
| 	<test name="Bean_Test"> | 	<test name="Bean_Test"> | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Daniel Qian
					Daniel Qian