添加标签管理支持

This commit is contained in:
Daniel Qian 2014-10-22 10:19:04 +08:00
parent 3db7c1789d
commit 67795a092d
13 changed files with 394 additions and 62 deletions

View File

@ -10,10 +10,12 @@ import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;
*/
public class WxError {
protected int errorCode;
private int errorCode;
protected String errorMsg;
private String errorMsg;
private String json;
public int getErrorCode() {
return errorCode;
}
@ -31,12 +33,14 @@ public class WxError {
}
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
public String toString() {
return "微信错误 errcode=" + errorCode + ", errmsg=" + errorMsg;
return "微信错误 errcode=" + errorCode + ", errmsg=" + errorMsg + "\njson:" + json;
}
}

View File

@ -18,7 +18,7 @@ public class WxCpConsts {
public static final String XML_MSG_EVENT = "event";
///////////////////////
// 客服消息的消息类型
// 消息的消息类型
///////////////////////
public static final String CUSTOM_MSG_TEXT = "text";
public static final String CUSTOM_MSG_IMAGE = "image";

View File

@ -15,16 +15,17 @@ import me.chanjar.weixin.enterprise.exception.WxErrorException;
* 微信API的Service
*/
public interface WxCpService {
/**
* <pre>
* 验证推送过来的消息的正确性
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性
* </pre>
*
* @param msgSignature
* @param timestamp
* @param nonce
* @param data 微信传输过来的数据有可能是echoStr有可能是xml消息
* @param data 微信传输过来的数据有可能是echoStr有可能是xml消息
* @return
*/
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data);
@ -34,6 +35,7 @@ public interface WxCpService {
* 用在二次验证的时候
* 企业在员工验证成功后调用本方法告诉企业号平台该员工关注成功
* </pre>
*
* @param userId
*/
public void userAuthenticated(String userId) throws WxErrorException;
@ -42,90 +44,93 @@ public interface WxCpService {
* <pre>
* 获取access_token本方法线程安全
* 且在多线程同时刷新时只刷新一次避免超出2000次/日的调用次数上限
*
* 本service的所有方法都会在access_token过期是调用此方法
*
* 程序员在非必要情况下尽量不要主动调用此方法
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=获取access_token
* </pre>
*
* @throws me.chanjar.weixin.enterprise.exception.WxErrorException
*/
public void accessTokenRefresh() throws WxErrorException;
/**
* <pre>
* 上传多媒体文件
*
* 上传的多媒体文件有格式和大小限制如下
* 图片image: 1M支持JPG格式
* 语音voice2M播放长度不超过60s支持AMR\MP3格式
* 视频video10MB支持MP4格式
* 缩略图thumb64KB支持JPG格式
*
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
* @param mediaType 媒体类型, 请看{@link WxCpConsts}
* @param fileType 文件类型请看{@link WxCpConsts}
* @param inputStream 输入流
*
* @param mediaType 媒体类型, 请看{@link WxCpConsts}
* @param fileType 文件类型请看{@link WxCpConsts}
* @param inputStream 输入流
* @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 file
* @throws WxErrorException
* @see #mediaUpload(String, String, InputStream)
*/
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException;
/**
* <pre>
* 下载多媒体文件
* 根据微信文档视频文件下载不了会返回null
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
* </pre>
* @params media_id
*
* @return 保存到本地的临时文件
* @throws WxErrorException
* @params media_id
*/
public File mediaDownload(String media_id) throws WxErrorException;
/**
* <pre>
* 发送客服消息
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=发送客服消息
* 发送消息
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=发送消息
* </pre>
*
* @param message
* @throws WxErrorException
*/
public void messageSend(WxCpMessage message) throws WxErrorException;
/**
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
* </pre>
*
* @param menu
* @throws WxErrorException
*/
public void menuCreate(WxCpMenu menu) throws WxErrorException;
/**
* <pre>
* 自定义菜单删除接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口
* </pre>
*
* @throws WxErrorException
*/
public void menuDelete() throws WxErrorException;
/**
* <pre>
* 自定义菜单查询接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口
* </pre>
*
* @return
* @throws WxErrorException
*/
@ -137,30 +142,32 @@ public interface WxCpService {
* 最多支持创建500个部门
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
* </pre>
*
* @param depart 部门
* @return 部门id
* @throws WxErrorException
*/
public Integer departCreate(WxCpDepart depart) throws WxErrorException;
/**
* <pre>
* 部门管理接口 - 查询所有部门
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
* </pre>
*
* @return
* @throws WxErrorException
*/
public List<WxCpDepart> departGet() throws WxErrorException;
/**
* <pre>
* 部门管理接口 - 修改部门名
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
*
* 如果id为0(未部门),1(黑名单),2(星标组)或者不存在的id微信会返回系统繁忙的错误
* </pre>
* @param group 要更新的groupgroup的id,name必须设置
*
* @param group 要更新的groupgroup的id,name必须设置
* @throws WxErrorException
*/
public void departUpdate(WxCpDepart group) throws WxErrorException;
@ -168,33 +175,114 @@ public interface WxCpService {
/**
* <pre>
* 部门管理接口 - 删除部门
*
* </pre>
*
* @param 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
* @param departId 必填部门id
* @param fetchChild 非必填1/0是否递归获取子部门下面的成员
* @param status 非必填0获取全部员工1获取已关注成员列表2获取禁用成员列表4获取未关注成员列表status可叠加
*
* @param departId 必填部门id
* @param fetchChild 非必填1/0是否递归获取子部门下面的成员
* @param status 非必填0获取全部员工1获取已关注成员列表2获取禁用成员列表4获取未关注成员列表status可叠加
* @return
* @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} 的实现
*
* @param wxConfigProvider
*/
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider);

View File

@ -8,6 +8,9 @@ import java.util.List;
import java.util.UUID;
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.util.GsonHelper;
import me.chanjar.weixin.enterprise.bean.*;
@ -208,7 +211,7 @@ public class WxCpServiceImpl implements WxCpService {
}
@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 params = "";
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过期时才去刷新然后重新执行请求而不是全局定时请求
*

View File

@ -7,7 +7,7 @@ import me.chanjar.weixin.enterprise.bean.messagebuilder.*;
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;
/**
* 客服消息
* 消息
* @author Daniel Qian
*
*/

View File

@ -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);
}
}

View File

@ -96,14 +96,6 @@ public class WxCpUser {
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) {
this.extAttrs.add(new Attr(name, value));
}
@ -112,6 +104,14 @@ public class WxCpUser {
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 {
private String name;

View File

@ -1,6 +1,5 @@
package me.chanjar.weixin.enterprise.util.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.chanjar.weixin.common.bean.result.WxAccessToken;
@ -11,7 +10,7 @@ import me.chanjar.weixin.enterprise.bean.result.*;
public class WxCpGsonBuilder {
public static final GsonBuilder INSTANCE = new GsonBuilder();
static {
INSTANCE.disableHtmlEscaping();
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
@ -21,10 +20,11 @@ public class WxCpGsonBuilder {
INSTANCE.registerTypeAdapter(WxAccessToken.class, new WxCpAccessTokenAdapter());
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
INSTANCE.registerTypeAdapter(WxMediaUploadResult.class, new WxCpMediaUploadResultAdapter());
INSTANCE.registerTypeAdapter(WxCpTag.class, new WxCpTagGsonAdapter());
}
public static Gson create() {
return INSTANCE.create();
}
}

View File

@ -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"));
}
}

View File

@ -9,7 +9,7 @@ import me.chanjar.weixin.enterprise.exception.WxErrorException;
import com.google.inject.Inject;
/***
* 测试发送客服消息
* 测试发送消息
* @author Daniel Qian
*
*/

View File

@ -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);
}
}

View File

@ -54,12 +54,12 @@ public class WxCpUserAPITest {
}
@Test(dependsOnMethods = "testUserGet")
public void testUserGetByDepart() throws WxErrorException {
List<WxCpUser> users = wxService.userGetByDepart(1, true, 0);
public void testDepartGetUsers() throws WxErrorException {
List<WxCpUser> users = wxService.departGetUsers(1, true, 0);
Assert.assertNotEquals(users.size(), 0);
}
@Test(dependsOnMethods = "testUserGetByDepart")
@Test(dependsOnMethods = "testDepartGetUsers")
public void testUserDelete() throws WxErrorException {
wxService.userDelete("xiaohe.yang");
}

View File

@ -9,7 +9,9 @@
<class name="me.chanjar.weixin.enterprise.api.WxCpDepartAPITest" />
<class name="me.chanjar.weixin.enterprise.api.WxCpMediaAPITest" />
<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 name="Bean_Test">