Skip to content

Commit 2f80dd3

Browse files
committed
添加部门管理接口
1 parent b1f5d07 commit 2f80dd3

File tree

6 files changed

+113
-174
lines changed

6 files changed

+113
-174
lines changed

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpService.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,46 +133,47 @@ public interface WxCpService {
133133

134134
/**
135135
* <pre>
136-
* 分组管理接口 - 创建分组
137-
* 最多支持创建500个分组
138-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口
136+
* 部门管理接口 - 创建部门
137+
* 最多支持创建500个部门
138+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
139139
* </pre>
140-
* @param name 分组名字(30个字符以内)
140+
* @param depart 部门
141+
* @return 部门id
141142
* @throws WxErrorException
142143
*/
143-
public WxCpDepart departmentCreate(String name) throws WxErrorException;
144+
public Integer departCreate(WxCpDepart depart) throws WxErrorException;
144145

145146
/**
146147
* <pre>
147-
* 分组管理接口 - 查询所有分组
148-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口
148+
* 部门管理接口 - 查询所有部门
149+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
149150
* </pre>
150151
* @return
151152
* @throws WxErrorException
152153
*/
153-
public List<WxCpDepart> departmentGet() throws WxErrorException;
154+
public List<WxCpDepart> departGet() throws WxErrorException;
154155

155156
/**
156157
* <pre>
157-
* 分组管理接口 - 修改分组名
158-
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口
158+
* 部门管理接口 - 修改部门名
159+
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=部门管理接口
159160
*
160-
* 如果id为0(未分组),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
161+
* 如果id为0(未部门),1(黑名单),2(星标组),或者不存在的id,微信会返回系统繁忙的错误
161162
* </pre>
162163
* @param group 要更新的group,group的id,name必须设置
163164
* @throws WxErrorException
164165
*/
165-
public void departmentUpdate(WxCpDepart group) throws WxErrorException;
166+
public void departUpdate(WxCpDepart group) throws WxErrorException;
166167

167168
/**
168169
* <pre>
169170
* 部门管理接口 - 删除部门
170171
*
171172
* </pre>
172-
* @param department
173+
* @param departId
173174
* @throws WxErrorException
174175
*/
175-
public void departmentDelete(WxCpDepart department) throws WxErrorException;
176+
public void departDelete(Integer departId) throws WxErrorException;
176177

177178
public void userCreate(WxUser user) throws WxErrorException;
178179

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/api/WxCpServiceImpl.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.concurrent.atomic.AtomicBoolean;
1010

1111
import me.chanjar.weixin.common.bean.WxAccessToken;
12+
import me.chanjar.weixin.common.util.GsonHelper;
1213
import me.chanjar.weixin.enterprise.bean.*;
1314
import me.chanjar.weixin.enterprise.util.http.SimpleGetRequestExecutor;
1415
import me.chanjar.weixin.enterprise.util.crypto.SHA1;
@@ -147,44 +148,39 @@ public File mediaDownload(String media_id) throws WxErrorException {
147148
}
148149

149150

150-
public WxCpDepart departmentCreate(String name) throws WxErrorException {
151-
// TODO
152-
String url = "https://api.weixin.qq.com/cgi-bin/groups/create";
153-
JsonObject json = new JsonObject();
154-
JsonObject groupJson = new JsonObject();
155-
json.add("group", groupJson);
156-
groupJson.addProperty("name", name);
157-
151+
public Integer departCreate(WxCpDepart depart) throws WxErrorException {
152+
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create";
158153
String responseContent = execute(
159154
new SimplePostRequestExecutor(),
160155
url,
161-
json.toString());
162-
return WxCpDepart.fromJson(responseContent);
156+
depart.toJson());
157+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
158+
return GsonHelper.getAsInteger(tmpJsonElement.getAsJsonObject().get("id"));
163159
}
164160

165-
public void departmentUpdate(WxCpDepart group) throws WxErrorException {
166-
// TODO
167-
String url = "https://api.weixin.qq.com/cgi-bin/groups/update";
161+
public void departUpdate(WxCpDepart group) throws WxErrorException {
162+
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/update";
168163
execute(new SimplePostRequestExecutor(), url, group.toJson());
169164
}
170165

171-
public List<WxCpDepart> departmentGet() throws WxErrorException {
172-
// TODO
173-
String url = "https://api.weixin.qq.com/cgi-bin/groups/get";
166+
public void departDelete(Integer departId) throws WxErrorException {
167+
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId;
168+
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
169+
}
170+
171+
public List<WxCpDepart> departGet() throws WxErrorException {
172+
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
174173
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
175174
/*
176175
* 操蛋的微信API,创建时返回的是 { group : { id : ..., name : ...} }
177176
* 查询时返回的是 { groups : [ { id : ..., name : ..., count : ... }, ... ] }
178177
*/
179178
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
180179
return WxCpGsonBuilder.INSTANCE.create()
181-
.fromJson(tmpJsonElement.getAsJsonObject().get("groups"), new TypeToken<List<WxCpDepart>>() {
182-
}.getType());
183-
}
184-
185-
public void departmentDelete(WxCpDepart department) throws WxErrorException {
186-
// TODO
187-
180+
.fromJson(
181+
tmpJsonElement.getAsJsonObject().get("department"),
182+
new TypeToken<List<WxCpDepart>>() { }.getType()
183+
);
188184
}
189185

190186
@Override

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/WxCpDepart.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,52 @@
44

55
/**
66
* 微信部门
7-
* @author Daniel Qian
87
*
8+
* @author Daniel Qian
99
*/
1010
public class WxCpDepart {
1111

12-
private long id = -1;
12+
private Integer id;
1313
private String name;
14-
private long parentId;
15-
private long order;
14+
private Integer parentId;
15+
private Integer order;
1616

17-
public long getId() {
17+
public Integer getId() {
1818
return id;
1919
}
20-
public void setId(long id) {
20+
21+
public void setId(Integer id) {
2122
this.id = id;
2223
}
24+
2325
public String getName() {
2426
return name;
2527
}
28+
2629
public void setName(String name) {
2730
this.name = name;
2831
}
2932

33+
public Integer getParentId() {
34+
return parentId;
35+
}
36+
37+
public void setParentId(Integer parentId) {
38+
this.parentId = parentId;
39+
}
40+
41+
public Integer getOrder() {
42+
return order;
43+
}
44+
45+
public void setOrder(Integer order) {
46+
this.order = order;
47+
}
48+
3049
public static WxCpDepart fromJson(String json) {
3150
return WxCpGsonBuilder.create().fromJson(json, WxCpDepart.class);
3251
}
33-
52+
3453
public String toJson() {
3554
return WxCpGsonBuilder.create().toJson(this);
3655
}

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/bean/result/WxError.java

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,93 +13,6 @@
1313
*/
1414
public class WxError {
1515

16-
protected static final Map<Integer, String> errMap = new HashMap<Integer, String>();
17-
18-
static {
19-
errMap.put(-1, "系统繁忙");
20-
errMap.put(0, "请求成功");
21-
errMap.put(40001, "获取access_token时AppSecret错误,或者access_token无效");
22-
errMap.put(40002, "不合法的凭证类型");
23-
errMap.put(40003, "不合法的OpenID");
24-
errMap.put(40004, "不合法的媒体文件类型");
25-
errMap.put(40005, "不合法的文件类型");
26-
errMap.put(40006, "不合法的文件大小");
27-
errMap.put(40007, "不合法的媒体文件id");
28-
errMap.put(40008, "不合法的消息类型");
29-
errMap.put(40009, "不合法的图片文件大小");
30-
errMap.put(40010, "不合法的语音文件大小");
31-
errMap.put(40011, "不合法的视频文件大小");
32-
errMap.put(40012, "不合法的缩略图文件大小");
33-
errMap.put(40013, "不合法的APPID");
34-
errMap.put(40014, "不合法的access_token");
35-
errMap.put(40015, "不合法的菜单类型");
36-
errMap.put(40016, "不合法的按钮个数");
37-
errMap.put(40017, "不合法的按钮个数");
38-
errMap.put(40018, "不合法的按钮名字长度");
39-
errMap.put(40019, "不合法的按钮KEY长度");
40-
errMap.put(40020, "不合法的按钮URL长度");
41-
errMap.put(40021, "不合法的菜单版本号");
42-
errMap.put(40022, "不合法的子菜单级数");
43-
errMap.put(40023, "不合法的子菜单按钮个数");
44-
errMap.put(40024, "不合法的子菜单按钮类型");
45-
errMap.put(40025, "不合法的子菜单按钮名字长度");
46-
errMap.put(40026, "不合法的子菜单按钮KEY长度");
47-
errMap.put(40027, "不合法的子菜单按钮URL长度");
48-
errMap.put(40028, "不合法的自定义菜单使用用户");
49-
errMap.put(40029, "不合法的oauth_code");
50-
errMap.put(40030, "不合法的refresh_token");
51-
errMap.put(40031, "不合法的openid列表");
52-
errMap.put(40032, "不合法的openid列表长度");
53-
errMap.put(40033, "不合法的请求字符,不能包含\\uxxxx格式的字符");
54-
errMap.put(40035, "不合法的参数");
55-
errMap.put(40038, "不合法的请求格式");
56-
errMap.put(40039, "不合法的URL长度");
57-
errMap.put(40050, "不合法的分组id");
58-
errMap.put(40051, "分组名字不合法");
59-
errMap.put(41001, "缺少access_token参数");
60-
errMap.put(41002, "缺少appid参数");
61-
errMap.put(41003, "缺少refresh_token参数");
62-
errMap.put(41004, "缺少secret参数");
63-
errMap.put(41005, "缺少多媒体文件数据");
64-
errMap.put(41006, "缺少media_id参数");
65-
errMap.put(41007, "缺少子菜单数据");
66-
errMap.put(41008, "缺少oauth code");
67-
errMap.put(41009, "缺少openid");
68-
errMap.put(42001, "access_token超时");
69-
errMap.put(42002, "refresh_token超时");
70-
errMap.put(42003, "oauth_code超时");
71-
errMap.put(43001, "需要GET请求");
72-
errMap.put(43002, "需要POST请求");
73-
errMap.put(43003, "需要HTTPS请求");
74-
errMap.put(43004, "需要接收者关注");
75-
errMap.put(43005, "需要好友关系");
76-
errMap.put(44001, "多媒体文件为空");
77-
errMap.put(44002, "POST的数据包为空");
78-
errMap.put(44003, "图文消息内容为空");
79-
errMap.put(44004, "文本消息内容为空");
80-
errMap.put(45001, "多媒体文件大小超过限制");
81-
errMap.put(45002, "消息内容超过限制");
82-
errMap.put(45003, "标题字段超过限制");
83-
errMap.put(45004, "描述字段超过限制");
84-
errMap.put(45005, "链接字段超过限制");
85-
errMap.put(45006, "图片链接字段超过限制");
86-
errMap.put(45007, "语音播放时间超过限制");
87-
errMap.put(45008, "图文消息超过限制");
88-
errMap.put(45009, "接口调用超过限制");
89-
errMap.put(45010, "创建菜单个数超过限制");
90-
errMap.put(45015, "回复时间超过限制");
91-
errMap.put(45016, "系统分组,不允许修改");
92-
errMap.put(45017, "分组名字过长");
93-
errMap.put(45018, "分组数量超过上限");
94-
errMap.put(46001, "不存在媒体数据");
95-
errMap.put(46002, "不存在的菜单版本");
96-
errMap.put(46003, "不存在的菜单数据");
97-
errMap.put(46004, "不存在的用户");
98-
errMap.put(47001, "解析JSON/XML内容错误");
99-
errMap.put(48001, "api功能未授权");
100-
errMap.put(50001, "用户未授权该api");
101-
}
102-
10316
protected int errorCode;
10417

10518
protected String errorMsg;
@@ -120,17 +33,13 @@ public void setErrorMsg(String errorMsg) {
12033
this.errorMsg = errorMsg;
12134
}
12235

123-
public String getDescription() {
124-
return errMap.get(errorCode);
125-
}
126-
12736
public static WxError fromJson(String json) {
12837
return WxCpGsonBuilder.create().fromJson(json, WxError.class);
12938
}
13039

13140
@Override
13241
public String toString() {
133-
return "微信错误 errcode=" + errorCode + ", errmsg=" + errorMsg + ", description=" + getDescription();
42+
return "微信错误 errcode=" + errorCode + ", errmsg=" + errorMsg;
13443
}
13544

13645
}

weixin-java-enterprise/src/main/java/me/chanjar/weixin/enterprise/util/json/WxCpDepartGsonAdapter.java

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,44 @@
2222
import com.google.gson.JsonSerializer;
2323

2424
/**
25-
*
2625
* @author Daniel Qian
27-
*
2826
*/
2927
public class WxCpDepartGsonAdapter implements JsonSerializer<WxCpDepart>, JsonDeserializer<WxCpDepart> {
3028

3129
public JsonElement serialize(WxCpDepart group, Type typeOfSrc, JsonSerializationContext context) {
3230
JsonObject json = new JsonObject();
33-
JsonObject groupJson = new JsonObject();
34-
groupJson.addProperty("name", group.getName());
35-
groupJson.addProperty("id", group.getId());
36-
groupJson.addProperty("count", group.getCount());
37-
json.add("group", groupJson);
31+
if (group.getId() != null) {
32+
json.addProperty("id", group.getId());
33+
}
34+
if (group.getName() != null) {
35+
json.addProperty("name", group.getName());
36+
}
37+
if (group.getParentId() != null) {
38+
json.addProperty("parentid", group.getParentId());
39+
}
40+
if (group.getOrder() != null) {
41+
json.addProperty("order", String.valueOf(group.getOrder()));
42+
}
3843
return json;
3944
}
4045

41-
public WxCpDepart deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
42-
WxCpDepart group = new WxCpDepart();
43-
JsonObject groupJson = json.getAsJsonObject();
44-
if (json.getAsJsonObject().get("group") != null) {
45-
groupJson = json.getAsJsonObject().get("group").getAsJsonObject();
46+
public WxCpDepart deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
47+
throws JsonParseException {
48+
WxCpDepart depart = new WxCpDepart();
49+
JsonObject departJson = json.getAsJsonObject();
50+
if (departJson.get("id") != null && !departJson.get("id").isJsonNull()) {
51+
depart.setId(GsonHelper.getAsInteger(departJson.get("id")));
4652
}
47-
if (groupJson.get("name") != null && !groupJson.get("name").isJsonNull()) {
48-
group.setName(GsonHelper.getAsString(groupJson.get("name")));
53+
if (departJson.get("name") != null && !departJson.get("name").isJsonNull()) {
54+
depart.setName(GsonHelper.getAsString(departJson.get("name")));
4955
}
50-
if (groupJson.get("id") != null && !groupJson.get("id").isJsonNull()) {
51-
group.setId(GsonHelper.getAsPrimitiveLong(groupJson.get("id")));
56+
if (departJson.get("order") != null && !departJson.get("order").isJsonNull()) {
57+
depart.setOrder(GsonHelper.getAsInteger(departJson.get("order")));
5258
}
53-
if (groupJson.get("count") != null && !groupJson.get("count").isJsonNull()) {
54-
group.setCount(GsonHelper.getAsPrimitiveLong(groupJson.get("count")));
59+
if (departJson.get("parentid") != null && !departJson.get("parentid").isJsonNull()) {
60+
depart.setParentId(GsonHelper.getAsInteger(departJson.get("parentid")));
5561
}
56-
return group;
62+
return depart;
5763
}
58-
64+
5965
}

0 commit comments

Comments
 (0)