新增企业号工程并完成【部门管理】【成员管理】【标签管理】三个接口

This commit is contained in:
jy.hu
2014-11-19 21:26:11 +08:00
parent f4aec9dc99
commit 5b213cd87f
69 changed files with 2741 additions and 466 deletions
+28
View File
@@ -0,0 +1,28 @@
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*~
# eclipse ignore
*.settings/*
/.project
/.classpath
/.tomcatplugin
# maven ignore
target/*
# other ignore
*.log
*.tmp
Thumbs.db
/target/
.DS_Store
+56
View File
@@ -0,0 +1,56 @@
weixin4j-qy-api
===============
[微信企业号](http://qydev.weixin.qq.com/wiki/index.php)开发工具包
---------------------------------------------------------------
功能列表
-------
* DepartApi `部门管理API`
* UserApi `成员管理API`
* TagApi `标签管理API`
如何使用
--------
1.API工程可以单独打包到其他项目中使用,需新增或拷贝`weixin.properties`文件到项目的`classpath`
weixin.properties说明
| 属性名 | 说明 |
| :---------- | :-------------- |
| account | 微信企业号信息 `json格式` |
| token_path | 使用FileTokenHolder时token保存的物理路径 |
示例(properties中换行用右斜杆\\)
> account={"id":"corpid","secret":"corpsecret",
> "token":"开放者的token 非必须",
> "encodingAesKey":"AES加密密钥"}
> token_path=/tmp/weixin/token <br/>
2.实例化一个`WeixinProxy`对象,调用API
WeixinProxy weixinProxy = new WeixinProxy();
// weixinProxy = new WeixinProxy(corpid,corpsecret);
// weixinProxy = new WeixinProxy(weixinAccount);
weixinProxy.getUser(userid);
3.针对`token`存储有两种方案,`File存储`/`Redis存储`,当然也可自己实现`TokenHolder`(继承`AbstractTokenHolder`并重写`getToken`方法),默认使用文件(xml)的方式保存token,如果环境中支持`redis`,建议使用`RedisTokenHolder`.
WeixinProxy weixinProxy = new WeixinProxy(new RedisTokenHolder());
// weixinProxy = new WeixinProxy(new RedisTokenHolder(weixinAccount));
4.`mvn package`.
更新LOG
-------
* 2014-11-19
+ 新增`部门管理`接口
+ 新增`用户管理`接口
+ 新增`标签管理`接口
+24
View File
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.foxinmy.weixin4j</groupId>
<artifactId>weixin4j-qy</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>weixin4j-qy-api</artifactId>
<name>weixin4j-qy-api</name>
<url>https://github.com/foxinmy/weixin4j/tree/master/weixin4j-qy/weixin4j-qy-api</url>
<description>微信企业号API</description>
<build>
<finalName>weixin4j-qy-api</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,31 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>full</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>/**</include>
</includes>
<excludes>
<exclude>*.properties</exclude>
<exclude>*.xml</exclude>
</excludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<includes>
<include>com.foxinmy.weixin4j:weixin4j-base</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
@@ -0,0 +1,344 @@
package com.foxinmy.weixin4j.qy;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.model.WeixinQyAccount;
import com.foxinmy.weixin4j.qy.api.DepartApi;
import com.foxinmy.weixin4j.qy.api.TagApi;
import com.foxinmy.weixin4j.qy.api.UserApi;
import com.foxinmy.weixin4j.qy.model.Department;
import com.foxinmy.weixin4j.qy.model.User;
import com.foxinmy.weixin4j.qy.type.UserStatus;
import com.foxinmy.weixin4j.token.FileTokenHolder;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.type.AccountType;
/**
* 微信企业号接口实现
*
* @className WeixinProxy
* @author jy
* @date 2014年11月19日
* @since JDK 1.7
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php">api文档</a>
*/
public class WeixinProxy {
private final DepartApi departApi;
private final UserApi userApi;
private final TagApi tagApi;
/**
* 默认采用文件存放Token信息
*/
public WeixinProxy() {
this(new FileTokenHolder(AccountType.QY));
}
/**
* appid,appsecret
*
* @param appid
* @param appsecret
*/
public WeixinProxy(String corpid, String corpsecret) {
this(new WeixinQyAccount(corpid, corpsecret));
}
/**
* WeixinAccount对象
*
* @param weixinAccount
*/
public WeixinProxy(WeixinQyAccount weixinAccount) {
this(new FileTokenHolder(weixinAccount));
}
/**
* TokenHolder对象
*
* @param tokenHolder
*/
public WeixinProxy(TokenHolder tokenHolder) {
this.departApi = new DepartApi(tokenHolder);
this.userApi = new UserApi(tokenHolder);
this.tagApi = new TagApi(tokenHolder);
}
/**
* 创建部门(根部门的parentid为1)
*
* @param depart
* 部门对象
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E5.88.9B.E5.BB.BA.E9.83.A8.E9.97.A8">创建部门说明</a>
* @see com.foxinmy.weixin4j.qy.api.DepartApi
* @return 部门ID
* @throws WeixinException
*/
public int createDepart(Department depart) throws WeixinException {
return departApi.createDepart(depart);
}
/**
* 更新部门(如果非必须的字段未指定 则不更新该字段之前的设置值)
*
* @param depart
* 部门对象
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E6.9B.B4.E6.96.B0.E9.83.A8.E9.97.A8">更新部门说明</a>
* @see com.foxinmy.weixin4j.qy.api.DepartApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult updateDepart(Department depart) throws WeixinException {
return departApi.updateDepart(depart);
}
/**
* 查询部门列表(以部门的order字段从小到大排列)
*
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E5.88.97.E8.A1.A8">获取部门列表</a>
* @see com.foxinmy.weixin4j.qy.api.DepartApi
* @return 部门列表
* @throws WeixinException
*/
public List<Department> listDepart() throws WeixinException {
return departApi.listDepart();
}
/**
* 删除部门(不能删除根部门;不能删除含有子部门、成员的部门)
*
* @param departId
* 部门ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E5.88.A0.E9.99.A4.E9.83.A8.E9.97.A8">删除部门说明</a>
* @see com.foxinmy.weixin4j.qy.api.DepartApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult deleteDepart(int departId) throws WeixinException {
return departApi.deleteDepart(departId);
}
/**
* 创建成员
*
* @param user
* 成员对象
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E5.88.9B.E5.BB.BA.E6.88.90.E5.91.98">创建成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult createUser(User user) throws WeixinException {
return userApi.createUser(user);
}
/**
* 更新用户(如果非必须的字段未指定 则不更新该字段之前的设置值)
*
* @param user
* 成员对象
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E6.9B.B4.E6.96.B0.E6.88.90.E5.91.98">更新成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult updateUser(User user) throws WeixinException {
return userApi.updateUser(user);
}
/**
* 获取成员
*
* @param userid
* 成员唯一ID
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98">获取成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @return 成员对象
* @throws WeixinException
*/
public User getUser(String userid) throws WeixinException {
return userApi.getUser(userid);
}
/**
* 获取部门成员
*
* @param departId
* 部门ID 必须
* @param fetchChild
* 是否递归获取子部门下面的成员 非必须
* @param userStatus
* 成员状态 status可叠加 非必须
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98">获取部门成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @return 成员列表
* @throws WeixinException
*/
public List<User> listUser(int departId, boolean fetchChild,
UserStatus userStatus) throws WeixinException {
return userApi.listUser(departId, fetchChild, userStatus);
}
/**
* 获取部门下所有状态成员(不进行递归)
*
* @param departId
* 部门ID
* @see {@link com.foxinmy.weixin4j.qy.WeixinProxy#listUser(int, boolean,UserStatus)}
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @return 成员列表
* @throws WeixinException
*/
public List<User> listUser(int departId) throws WeixinException {
return userApi.listUser(departId);
}
/**
* 删除成员
*
* @param userid
* 成员ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E5.88.A0.E9.99.A4.E6.88.90.E5.91.98">删除成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.UserApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult deleteUser(String userid) throws WeixinException {
return userApi.deleteUser(userid);
}
/**
* 创建标签(创建的标签属于管理组;默认为未加锁状态)
*
* @param tagName
* 标签名称
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.9B.E5.BB.BA.E6.A0.87.E7.AD.BE">创建标签说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 标签ID
* @throws WeixinException
*/
public int createTag(String tagName) throws WeixinException {
return tagApi.createTag(tagName);
}
/**
* 更新标签(管理组必须是指定标签的创建者)
*
* @param tagId
* 标签ID
* @param tagName
* 标签名称
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E6.9B.B4.E6.96.B0.E6.A0.87.E7.AD.BE.E5.90.8D.E5.AD.97"
* >更新标签说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult updateTag(int tagId, String tagName)
throws WeixinException {
return tagApi.updateTag(tagId, tagName);
}
/**
* 删除标签(管理组必须是指定标签的创建者 并且标签的成员列表为空)
*
* @param tagId
* 标签ID
* @return 处理结果
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.A0.E9.99.A4.E6.A0.87.E7.AD.BE">删除标签说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @throws WeixinException
*/
public JsonResult deleteTag(int tagId) throws WeixinException {
return tagApi.deleteTag(tagId);
}
/**
* 获取标签列表
*
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E8.8E.B7.E5.8F.96.E6.A0.87.E7.AD.BE.E5.88.97.E8.A1.A8">获取标签列表说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 标签列表
* @throws WeixinException
*/
public JSONArray listTag() throws WeixinException {
return tagApi.listTag();
}
/**
* 获取标签成员(管理组须拥有“获取标签成员”的接口权限,标签须对管理组可见;返回列表仅包含管理组管辖范围的成员)
*
* @param tagId
* 标签ID
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E8.8E.B7.E5.8F.96.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">获取标签成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 成员列表
* @throws WeixinException
*/
public List<User> getTagUsers(int tagId) throws WeixinException {
return tagApi.getTagUsers(tagId);
}
/**
* 新增标签成员(标签对管理组可见且未加锁,成员属于管理组管辖范围)<br>
* <font color="red">若部分userid非法,则在text中体现</font>
*
* @param tagId
* 标签ID
* @param userIds
* 成员ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.A2.9E.E5.8A.A0.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">新增标签成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult addTagUsers(int tagId, List<String> userIds)
throws WeixinException {
return tagApi.addTagUsers(tagId, userIds);
}
/**
* 删除标签成员(标签对管理组可见且未加锁,成员属于管理组管辖范围)<br>
* <font color="red">若部分userid非法,则在text中体现</font>
*
* @param tagId
* 标签ID
* @param userIds
* 成员ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.A0.E9.99.A4.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">删除标签成员说明</a>
* @see com.foxinmy.weixin4j.qy.api.TagApi
* @return 处理结果
* @throws WeixinException
*/
public JsonResult deleteTagUsers(int tagId, List<String> userIds)
throws WeixinException {
return tagApi.deleteTagUsers(tagId, userIds);
}
}
@@ -0,0 +1,57 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.xml.Map2ObjectConverter;
import com.foxinmy.weixin4j.xml.XStream;
import com.thoughtworks.xstream.core.ClassLoaderReference;
import com.thoughtworks.xstream.mapper.DefaultMapper;
/**
*
* @className BaseApi
* @author jy.hu
* @date 2014年11月18日
* @since JDK 1.7
* @see <a href="http://qydev.weixin.qq.com/wiki/index.php">api文档</a>
*/
public class BaseApi {
protected final HttpRequest request = new HttpRequest();
protected final static XStream mapXstream = XStream.get();
private final static ResourceBundle weixinBundle;
static {
weixinBundle = ResourceBundle
.getBundle("com/foxinmy/weixin4j/qy/api/weixin");
mapXstream.alias("xml", Map.class);
mapXstream.registerConverter(new Map2ObjectConverter(new DefaultMapper(
new ClassLoaderReference(XStream.class.getClassLoader()))));
}
protected String map2xml(Map<?, ?> map) {
return mapXstream.toXML(map).replaceAll("__", "_");
}
@SuppressWarnings("unchecked")
protected Map<String, String> xml2map(String xml) {
return mapXstream.fromXML(xml, Map.class);
}
protected String getRequestUri(String key) {
String url = weixinBundle.getString(key);
Pattern p = Pattern.compile("(\\{[^\\}]*\\})");
Matcher m = p.matcher(url);
StringBuffer sb = new StringBuffer();
String sub = null;
while (m.find()) {
sub = m.group();
m.appendReplacement(sb,
getRequestUri(sub.substring(1, sub.length() - 1)));
}
m.appendTail(sb);
return sb.toString();
}
}
@@ -0,0 +1,110 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.qy.model.Department;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
* 部门API
*
* @className DepartApi
* @author jy
* @date 2014年11月18日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8">管理部门说明</a>
*/
public class DepartApi extends BaseApi {
private final TokenHolder tokenHolder;
public DepartApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
}
/**
* 创建部门(根部门的parentid为1)
*
* @param depart
* 部门对象
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E5.88.9B.E5.BB.BA.E9.83.A8.E9.97.A8">创建部门说明</a>
* @return 部门ID
* @throws WeixinException
*/
public int createDepart(Department depart) throws WeixinException {
String department_create_uri = getRequestUri("department_create_uri");
JSONObject obj = (JSONObject) JSON.toJSON(depart);
obj.remove("id");
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(department_create_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJson().getIntValue("id");
}
/**
* 更新部门(如果非必须的字段未指定 则不更新该字段之前的设置值)
*
* @param depart
* 部门对象
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E6.9B.B4.E6.96.B0.E9.83.A8.E9.97.A8">更新部门说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult updateDepart(Department depart) throws WeixinException {
String department_update_uri = getRequestUri("department_update_uri");
JSONObject obj = (JSONObject) JSON.toJSON(depart);
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(department_update_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJsonResult();
}
/**
* 查询部门列表(以部门的order字段从小到大排列)
*
* @see com.foxinmy.weixin4j.qy.model.Department
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E5.88.97.E8.A1.A8">获取部门列表</a>
* @return 部门列表
* @throws WeixinException
*/
public List<Department> listDepart() throws WeixinException {
String department_list_uri = getRequestUri("department_list_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(department_list_uri,
token.getAccessToken()));
return JSON.parseArray(response.getAsJson().getString("department"),
Department.class);
}
/**
* 删除部门(不能删除根部门;不能删除含有子部门、成员的部门)
*
* @param departId
* 部门ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8#.E5.88.A0.E9.99.A4.E9.83.A8.E9.97.A8">删除部门说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult deleteDepart(int departId) throws WeixinException {
String department_delete_uri = getRequestUri("department_delete_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(department_delete_uri,
token.getAccessToken(), departId));
return response.getAsJsonResult();
}
}
@@ -0,0 +1,179 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.qy.model.User;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
* 标签API
*
* @className TagApi
* @author jy
* @date 2014年11月19日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE">管理标签</a>
*/
public class TagApi extends BaseApi {
private final TokenHolder tokenHolder;
public TagApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
}
/**
* 创建标签(创建的标签属于管理组;默认为未加锁状态)
*
* @param tagName
* 标签名称
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.9B.E5.BB.BA.E6.A0.87.E7.AD.BE">创建标签说明</a>
* @return 标签ID
* @throws WeixinException
*/
public int createTag(String tagName) throws WeixinException {
String tag_create_uri = getRequestUri("tag_create_uri");
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(tag_create_uri, token.getAccessToken()),
String.format("{\"tagname\":\"%s\"}", tagName));
return response.getAsJson().getIntValue("tagid");
}
/**
* 更新标签(管理组必须是指定标签的创建者)
*
* @param tagId
* 标签ID
* @param tagName
* 标签名称
* @see <a href=
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E6.9B.B4.E6.96.B0.E6.A0.87.E7.AD.BE.E5.90.8D.E5.AD.97"
* >更新标签说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult updateTag(int tagId, String tagName)
throws WeixinException {
String tag_update_uri = getRequestUri("tag_update_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(tag_update_uri,
token.getAccessToken()), String.format(
"{\"tagid\":%d,\"tagname\":\"%s\"}", tagId, tagName));
return response.getAsJsonResult();
}
/**
* 删除标签(管理组必须是指定标签的创建者 并且标签的成员列表为空)
*
* @param tagId
* 标签ID
* @return 处理结果
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.A0.E9.99.A4.E6.A0.87.E7.AD.BE">删除标签说明</a>
* @throws WeixinException
*/
public JsonResult deleteTag(int tagId) throws WeixinException {
String tag_delete_uri = getRequestUri("tag_delete_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(tag_delete_uri,
token.getAccessToken(), tagId));
return response.getAsJsonResult();
}
/**
* 获取标签列表
*
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E8.8E.B7.E5.8F.96.E6.A0.87.E7.AD.BE.E5.88.97.E8.A1.A8">获取标签列表说明</a>
* @return 标签列表
* @throws WeixinException
*/
public JSONArray listTag() throws WeixinException {
String tag_list_uri = getRequestUri("tag_list_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(tag_list_uri,
token.getAccessToken()));
return response.getAsJson().getJSONArray("taglist");
}
/**
* 获取标签成员(管理组须拥有“获取标签成员”的接口权限,标签须对管理组可见;返回列表仅包含管理组管辖范围的成员)
*
* @param tagId
* 标签ID
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E8.8E.B7.E5.8F.96.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">获取标签成员说明</a>
* @return 成员列表
* @throws WeixinException
*/
public List<User> getTagUsers(int tagId) throws WeixinException {
String tag_get_user_uri = getRequestUri("tag_get_user_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(tag_get_user_uri,
token.getAccessToken(), tagId));
return JSON.parseArray(response.getAsJson().getString("userlist"),
User.class);
}
/**
* 新增标签成员(标签对管理组可见且未加锁,成员属于管理组管辖范围)<br>
* <font color="red">若部分userid非法,则在text中体现</font>
*
* @param tagId
* 标签ID
* @param userIds
* 成员ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.A2.9E.E5.8A.A0.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">新增标签成员说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult addTagUsers(int tagId, List<String> userIds)
throws WeixinException {
String tag_add_user_uri = getRequestUri("tag_add_user_uri");
return excuteUsers(tag_add_user_uri, tagId, userIds);
}
/**
* 删除标签成员(标签对管理组可见且未加锁,成员属于管理组管辖范围)<br>
* <font color="red">若部分userid非法,则在text中体现</font>
*
* @param tagId
* 标签ID
* @param userIds
* 成员ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%A0%87%E7%AD%BE#.E5.88.A0.E9.99.A4.E6.A0.87.E7.AD.BE.E6.88.90.E5.91.98">删除标签成员说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult deleteTagUsers(int tagId, List<String> userIds)
throws WeixinException {
String tag_delete_user_uri = getRequestUri("tag_delete_user_uri");
return excuteUsers(tag_delete_user_uri, tagId, userIds);
}
private JsonResult excuteUsers(String uri, int tagId, List<String> userIds)
throws WeixinException {
JSONObject obj = new JSONObject();
obj.put("tagid", tagId);
obj.put("userlist", userIds);
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(uri, token.getAccessToken()), obj.toJSONString());
obj = response.getAsJson();
JsonResult result = JSON.toJavaObject(obj, JsonResult.class);
result.setText(obj.getString("invalidlist"));
return result;
}
}
@@ -0,0 +1,159 @@
package com.foxinmy.weixin4j.qy.api;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.http.Response;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.qy.model.User;
import com.foxinmy.weixin4j.qy.type.UserStatus;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
* 成员API
*
* @className UserApi
* @author jy
* @date 2014年11月19日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98">管理成员说明</a>
*/
public class UserApi extends BaseApi {
private final TokenHolder tokenHolder;
public UserApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
}
/**
* 创建成员
*
* @param user
* 成员对象
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E5.88.9B.E5.BB.BA.E6.88.90.E5.91.98">创建成员说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult createUser(User user) throws WeixinException {
String user_create_uri = getRequestUri("user_create_uri");
return excute(user_create_uri, user);
}
/**
* 更新用户(如果非必须的字段未指定 则不更新该字段之前的设置值)
*
* @param user
* 成员对象
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E6.9B.B4.E6.96.B0.E6.88.90.E5.91.98">更新成员说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult updateUser(User user) throws WeixinException {
String user_update_uri = getRequestUri("user_update_uri");
return excute(user_update_uri, user);
}
private JsonResult excute(String uri, User user) throws WeixinException {
JSONObject obj = (JSONObject) JSON.toJSON(user);
Object extattr = obj.remove("extattr");
if (extattr != null) {
JSONObject attrs = new JSONObject();
attrs.put("attrs", extattr);
obj.put("extattr", attrs);
}
Token token = tokenHolder.getToken();
Response response = request.post(
String.format(uri, token.getAccessToken()), obj.toJSONString());
return response.getAsJsonResult();
}
/**
* 获取成员
*
* @param userid
* 成员唯一ID
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98">获取成员说明</a>
* @return 成员对象
* @throws WeixinException
*/
public User getUser(String userid) throws WeixinException {
String user_get_uri = getRequestUri("user_get_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(user_get_uri,
token.getAccessToken(), userid));
JSONObject obj = response.getAsJson();
Object attrs = obj.getJSONObject("extattr").remove("attrs");
if (attrs != null) {
obj.put("extattr", attrs);
}
return JSON.toJavaObject(obj, User.class);
}
/**
* 获取部门成员
*
* @param departId
* 部门ID 必须
* @param fetchChild
* 是否递归获取子部门下面的成员 非必须
* @param userStatus
* 成员状态 status可叠加 非必须
* @see com.foxinmy.weixin4j.qy.model.User
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E9.83.A8.E9.97.A8.E6.88.90.E5.91.98">获取部门成员说明</a>
* @return 成员列表
* @throws WeixinException
*/
public List<User> listUser(int departId, boolean fetchChild,
UserStatus userStatus) throws WeixinException {
String user_list_uri = getRequestUri("user_list_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(user_list_uri,
token.getAccessToken(), departId, fetchChild ? 1 : 0,
userStatus.getVal()));
return JSON.parseArray(response.getAsJson().getString("userlist"),
User.class);
}
/**
* 获取部门下所有状态成员(不进行递归)
*
* @param departId
* 部门ID
* @see {@link com.foxinmy.weixin4j.qy.api.UserApi#listUser(int, boolean,UserStatus)}
* @return 成员列表
* @throws WeixinException
*/
public List<User> listUser(int departId) throws WeixinException {
return listUser(departId, false, UserStatus.BOTH);
}
/**
* 删除成员
*
* @param userid
* 成员ID
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E5.88.A0.E9.99.A4.E6.88.90.E5.91.98">删除成员说明</a>
* @return 处理结果
* @throws WeixinException
*/
public JsonResult deleteUser(String userid) throws WeixinException {
String user_delete_uri = getRequestUri("user_delete_uri");
Token token = tokenHolder.getToken();
Response response = request.post(String.format(user_delete_uri,
token.getAccessToken(), userid));
return response.getAsJsonResult();
}
}
@@ -0,0 +1,39 @@
# ----------------------------------------------------------------------------
# api\u9996\u9875
# http://qydev.weixin.qq.com/wiki/index.php
# ----------------------------------------------------------------------------
api_base_url=https://qyapi.weixin.qq.com/cgi-bin
# \u521b\u5efa\u90e8\u95e8
department_create_uri={api_base_url}/department/create?access_token=%s
# \u66f4\u65b0\u90e8\u95e8
department_update_uri={api_base_url}/department/update?access_token=%s
# \u90e8\u95e8\u5217\u8868
department_list_uri={api_base_url}/department/list?access_token=%s
# \u5220\u9664\u90e8\u95e8
department_delete_uri={api_base_url}/department/delete?access_token=%s&id=%d
# \u521b\u5efa\u6210\u5458
user_create_uri={api_base_url}/user/create?access_token=%s
# \u66f4\u65b0\u6210\u5458
user_update_uri={api_base_url}/user/update?access_token=%s
# \u83b7\u53d6\u6210\u5458
user_get_uri={api_base_url}/user/get?access_token=%s&userid=%s
# \u83b7\u53d6\u90e8\u95e8\u6210\u5458
user_list_uri={api_base_url}/user/simplelist?access_token=%s&department_id=%d&fetch_child=%d&status=%d
# \u5220\u9664\u6210\u5458
user_delete_uri={api_base_url}/user/delete?access_token=%s&userid=%s
# \u521b\u5efa\u6807\u7b7e
tag_create_uri={api_base_url}/tag/create?access_token=%s
# \u66f4\u65b0\u6807\u7b7e
tag_update_uri={api_base_url}/tag/update?access_token=%s
# \u5220\u9664\u6807\u7b7e
tag_delete_uri={api_base_url}/tag/delete?access_token=%s&tagid=%d
# \u83b7\u53d6\u6807\u7b7e
tag_list_uri={api_base_url}/tag/list?access_token=%s
# \u83b7\u53d6\u6807\u7b7e\u6210\u5458
tag_get_user_uri={api_base_url}/tag/get?access_token=%s&tagid=%d
# \u6dfb\u52a0\u6807\u7b7e\u6210\u5458
tag_add_user_uri={api_base_url}/tag/addtagusers?access_token=%s
# \u5220\u9664\u6807\u7b7e\u6210\u5458
tag_delete_user_uri={api_base_url}/tag/deltagusers?access_token=%s
@@ -0,0 +1,74 @@
package com.foxinmy.weixin4j.qy.model;
import java.io.Serializable;
/**
* 部门对象
*
* @className Department
* @author jy
* @date 2014年11月18日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E9%83%A8%E9%97%A8">管理部门说明</a>
*/
public class Department implements Serializable {
private static final long serialVersionUID = -2567893218591084288L;
private int id; // 部门ID
private String name; // 部门名称。长度限制为1~64个字符
private int parentid;// 父亲部门id。根部门id为1
private int order;// 在父部门中的次序。从1开始,数字越大排序越靠后
public Department() {
}
public Department(String name) {
this(name, 1, 1);
}
public Department(String name, int parentid, int order) {
this.name = name;
this.parentid = parentid;
this.order = order;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParentid() {
return parentid;
}
public void setParentid(int parentid) {
this.parentid = parentid;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + ", parentid="
+ parentid + ", order=" + order + "]";
}
}
@@ -0,0 +1,40 @@
package com.foxinmy.weixin4j.qy.model;
import java.io.Serializable;
public class NameValue implements Serializable {
private static final long serialVersionUID = -348620146718819093L;
private String name;
private String value;
public NameValue() {
}
public NameValue(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
public void setName(String name) {
this.name = name;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "NameValue [name=" + name + ", value=" + value + "]";
}
}
@@ -0,0 +1,203 @@
package com.foxinmy.weixin4j.qy.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.model.Gender;
import com.foxinmy.weixin4j.qy.type.UserStatus;
/**
* 部门成员对象
*
* @className User
* @author jy
* @date 2014年11月19日
* @since JDK 1.7
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98">管理成员说明</a>
*/
public class User implements Serializable {
private static final long serialVersionUID = 4747301605060801611L;
private String userid;// 是 员工UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字符
private String name;// 是 成员名称。长度为1~64个字符
private List<Integer> department;// 否 成员所属部门id列表。注意,每个部门的直属员工上限为1000个
private String position;// 否 职位信息。长度为0~64个字符
private String mobile;// 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
private int gender;// 否 性别。gender=0表示男,=1表示女。默认gender=0
private String tel;// 否 办公电话。长度为0~64个字符
private String email;// 否 邮箱。长度为0~64个字符。企业内必须唯一
private String weixinid;// 否 微信号。企业内必须唯一
private String avatar;// 头像url。注:如果要获取小图将url最后的"/0"改成"/64"即可
private int status;// 关注状态: 1=已关注,2=已冻结,4=未关注
private List<NameValue> extattr;// 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值
public User() {
this.extattr = new ArrayList<NameValue>();
}
public User(String userid, String name) {
this(userid, name, null, null, null);
}
/**
* mobile/weixinid/email三者不能同时为空
*
* @param userid
* @param name
* @param tel
* @param email
* @param weixinid
*/
public User(String userid, String name, String tel, String email,
String weixinid) {
this.userid = userid;
this.name = name;
this.tel = tel;
this.email = email;
this.weixinid = weixinid;
this.extattr = new ArrayList<NameValue>();
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Integer> getDepartment() {
return department;
}
public void setDepartment(List<Integer> department) {
this.department = department;
}
public void setDepartment(Integer... department) {
this.department = Arrays.asList(department);
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public int getGender() {
return gender;
}
@JSONField(serialize = false)
public Gender getEnumGender() {
if (gender == 0) {
return Gender.male;
} else if (gender == 1) {
return Gender.female;
} else {
return Gender.unknown;
}
}
public void setGender(int gender) {
this.gender = gender;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWeixinid() {
return weixinid;
}
public void setWeixinid(String weixinid) {
this.weixinid = weixinid;
}
@JSONField(serialize = false)
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
@JSONField(serialize = false)
public UserStatus getStatus() {
for (UserStatus userStatus : UserStatus.values()) {
if (userStatus.getVal() == status) {
return userStatus;
}
}
return null;
}
public void setStatus(int status) {
this.status = status;
}
public List<NameValue> getExtattr() {
return extattr;
}
public void setExtattr(List<NameValue> extattr) {
this.extattr = extattr;
}
public void setExtattr(NameValue... extattr) {
this.extattr = Arrays.asList(extattr);
}
public void pushExattr(String name, String value) {
pushExattr(new NameValue(name, value));
}
public void pushExattr(NameValue nameValue) {
extattr.add(nameValue);
}
@Override
public String toString() {
return "User [userid=" + userid + ", name=" + name + ", department="
+ department + ", position=" + position + ", mobile=" + mobile
+ ", gender=" + gender + ", tel=" + tel + ", email=" + email
+ ", weixinid=" + weixinid + ", avatar=" + avatar + ", status="
+ status + ", extattr=" + extattr + "]";
}
}
@@ -0,0 +1,26 @@
package com.foxinmy.weixin4j.qy.type;
/**
* 成员状态
*
* @className UserStatus
* @author jy
* @date 2014年11月19日
* @since JDK 1.7
* @see
*/
public enum UserStatus {
BOTH(0), // 0=全部
FOLLOW(1), // 1=已关注
FROZEN(2), // 2=已冻结
UNFOLLOW(4);// 4=未关注
private int val;
UserStatus(int val) {
this.val = val;
}
public int getVal() {
return val;
}
}
@@ -0,0 +1,13 @@
# \u6d4b\u8bd5\u4e4b\u7528 \u6b63\u5f0f\u73af\u5883\u4e0bcopy\u4e00\u4efd\u5230classpath
# \u4f01\u4e1a\u53f7\u4fe1\u606f
account={"id":"wxd9d9fa581a07cefd","secret":"7tr6FXh2NORvqq1la9CVwxV0xZWGsRSgI6tfqd3-JFc9E2p7UukNNYeKoOTLe0Ru",\
"token":"gp2eGT5mIpngr",\
"encodingAesKey":"BRYfV4zPFUJb3v3MySNBg1ERKE3vyyMRoScu76vFySv"\
}
# \u4f7f\u7528FileTokenHolder\u65f6token\u7684\u5b58\u653e\u8def\u5f84
token_path=/tmp/weixin/token
# \u4e8c\u7ef4\u7801\u4fdd\u5b58\u8def\u5f84
qr_path=/tmp/weixin/qr
# \u5a92\u4f53\u6587\u4ef6\u4fdd\u5b58\u8def\u5f84
media_path=/tmp/weixin/media
@@ -0,0 +1,58 @@
package com.foxinmy.weixin4j.qy.test;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.qy.api.DepartApi;
import com.foxinmy.weixin4j.qy.model.Department;
/**
* 部门API测试
*
* @className DepartTest
* @author jy
* @date 2014年11月18日
* @since JDK 1.7
* @see
*/
public class DepartTest extends TokenTest {
public DepartApi departApi;
@Before
public void init() {
this.departApi = new DepartApi(tokenHolder);
}
@Test
public void create() throws WeixinException {
Department depart = new Department("苦逼组");
int id = departApi.createDepart(depart);
Assert.assertTrue(id > 0);
}
@Test
public void update() throws WeixinException {
Department depart = new Department("苦逼组111");
depart.setId(2);
JsonResult result = departApi.updateDepart(depart);
Assert.assertEquals("updated", result.getDesc());
}
@Test
public void list() throws WeixinException {
List<Department> list = departApi.listDepart();
Assert.assertFalse(list.isEmpty());
System.out.println(list);
}
@Test
public void delete() throws WeixinException {
JsonResult result = departApi.deleteDepart(2);
Assert.assertEquals("deleted", result.getDesc());
}
}
@@ -0,0 +1,77 @@
package com.foxinmy.weixin4j.qy.test;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.alibaba.fastjson.JSONArray;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.qy.api.TagApi;
import com.foxinmy.weixin4j.qy.model.User;
/**
* 部门API测试
*
* @className DepartTest
* @author jy
* @date 2014年11月18日
* @since JDK 1.7
* @see
*/
public class TagTest extends TokenTest {
public TagApi tagApi;
@Before
public void init() {
this.tagApi = new TagApi(tokenHolder);
}
@Test
public void create() throws WeixinException {
int tagId = tagApi.createTag("coder");
Assert.assertTrue(tagId > 0);
}
@Test
public void update() throws WeixinException {
JsonResult result = tagApi.updateTag(1, "coder456");
Assert.assertEquals("updated", result.getDesc());
}
@Test
public void getUsers() throws WeixinException {
List<User> listUser = tagApi.getTagUsers(1);
Assert.assertFalse(listUser.isEmpty());
System.out.println(listUser);
}
@Test
public void addUsers() throws WeixinException {
JsonResult result = tagApi.addTagUsers(1, Arrays.asList("jinyu"));
Assert.assertEquals("ok", result.getDesc());
}
@Test
public void deleteUsers() throws WeixinException {
JsonResult result = tagApi.deleteTagUsers(1, Arrays.asList("jinyu"));
Assert.assertEquals("ok", result.getDesc());
System.out.println(result);
}
@Test
public void list() throws WeixinException {
JSONArray tags = tagApi.listTag();
Assert.assertFalse(tags.isEmpty());
System.out.println(tags);
}
@Test
public void delete() throws WeixinException {
JsonResult result = tagApi.deleteTag(3);
Assert.assertEquals("deleted", result.getDesc());
}
}
@@ -0,0 +1,33 @@
package com.foxinmy.weixin4j.qy.test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.token.FileTokenHolder;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.type.AccountType;
/**
* token测试
*
* @className TokenTest
* @author jy.hu
* @date 2014年4月10日
* @since JDK 1.7
*/
public class TokenTest {
protected TokenHolder tokenHolder;
@Before
public void setUp() {
tokenHolder = new FileTokenHolder(AccountType.QY);
}
@Test
public void test() throws WeixinException {
Assert.assertNotNull(tokenHolder.getToken());
}
}
@@ -0,0 +1,70 @@
package com.foxinmy.weixin4j.qy.test;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.JsonResult;
import com.foxinmy.weixin4j.qy.api.UserApi;
import com.foxinmy.weixin4j.qy.model.User;
/**
* 部门API测试
*
* @className DepartTest
* @author jy
* @date 2014年11月18日
* @since JDK 1.7
* @see
*/
public class UserTest extends TokenTest {
public UserApi userApi;
@Before
public void init() {
this.userApi = new UserApi(tokenHolder);
}
@Test
public void create() throws WeixinException {
User user = new User("u001", "jack");
user.setMobile("13500000000");
user.setDepartment(1);
user.pushExattr("爱好", "code");
JsonResult result = userApi.createUser(user);
Assert.assertEquals("created", result.getDesc());
}
@Test
public void update() throws WeixinException {
User user = new User("u001", "ted");
user.setMobile("13500000000");
user.setDepartment(1);
user.pushExattr("爱好", "code");
JsonResult result = userApi.updateUser(user);
Assert.assertEquals("updated", result.getDesc());
}
@Test
public void get() throws WeixinException {
User user = userApi.getUser("u001");
Assert.assertTrue(user != null);
System.out.println(user);
}
@Test
public void list() throws WeixinException {
List<User> userList = userApi.listUser(1);
Assert.assertFalse(userList.isEmpty());
System.out.println(userList);
}
@Test
public void delete() throws WeixinException {
JsonResult result = userApi.deleteUser("u001");
Assert.assertEquals("deleted", result.getDesc());
}
}