Skip to content

Commit 3db7c17

Browse files
committed
用户管理支持
1 parent 2f80dd3 commit 3db7c17

26 files changed

+373
-363
lines changed

weixin-java-enterprise/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ wxService.setWxConfigStorage(config);
5050

5151
// 用户的openid在下面地址获得
5252
// https://mp.weixin.qq.com/debug/cgi-bin/apiinfo?t=index&type=用户管理&form=获取关注者列表接口%20/user/get
53-
String openId = ...;
54-
WxCustomMessage message = WxCustomMessage.TEXT().toUser(openId).content("Hello World").build();
53+
String userId = ...;
54+
WxCustomMessage message = WxCustomMessage.TEXT().toUser(userId).content("Hello World").build();
5555
wxService.customMessageSend(message);
5656
```

weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/WxAccessToken.java renamed to weixin-java-enterprise/src/main/java/me/chanjar/weixin/common/bean/result/WxAccessToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.chanjar.weixin.common.bean;
1+
package me.chanjar.weixin.common.bean.result;
22

33
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;
44

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
package me.chanjar.weixin.enterprise.bean.result;
2-
3-
import java.util.HashMap;
4-
import java.util.Map;
1+
package me.chanjar.weixin.common.bean.result;
52

63
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;
74

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package me.chanjar.weixin.enterprise.api;
22

3-
import me.chanjar.weixin.common.bean.WxAccessToken;
3+
import me.chanjar.weixin.common.bean.result.WxAccessToken;
44

55
/**
66
* 微信客户端配置存储

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import me.chanjar.weixin.enterprise.bean.*;
99
import me.chanjar.weixin.enterprise.bean.WxCpDepart;
1010
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
11-
import me.chanjar.weixin.enterprise.bean.result.WxUser;
11+
import me.chanjar.weixin.enterprise.bean.WxCpUser;
1212
import me.chanjar.weixin.enterprise.exception.WxErrorException;
1313

1414
/**
@@ -175,15 +175,23 @@ public interface WxCpService {
175175
*/
176176
public void departDelete(Integer departId) throws WxErrorException;
177177

178-
public void userCreate(WxUser user) throws WxErrorException;
178+
public void userCreate(WxCpUser user) throws WxErrorException;
179179

180-
public void userUpdate(WxUser user) throws WxErrorException;
180+
public void userUpdate(WxCpUser user) throws WxErrorException;
181181

182182
public void userDelete(String userid) throws WxErrorException;
183183

184-
public WxUser userGet(String userid) throws WxErrorException;
184+
public WxCpUser userGet(String userid) throws WxErrorException;
185185

186-
public List<WxUser> userGetByDepartment(String departmentId) throws WxErrorException;
186+
/**
187+
* 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
188+
* @param departId 必填。部门id
189+
* @param fetchChild 非必填。1/0:是否递归获取子部门下面的成员
190+
* @param status 非必填。0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加
191+
* @return
192+
* @throws WxErrorException
193+
*/
194+
public List<WxCpUser> userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException;
187195

188196
/**
189197
* 注入 {@link WxCpConfigStorage} 的实现

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.UUID;
99
import java.util.concurrent.atomic.AtomicBoolean;
1010

11-
import me.chanjar.weixin.common.bean.WxAccessToken;
11+
import me.chanjar.weixin.common.bean.result.WxAccessToken;
1212
import me.chanjar.weixin.common.util.GsonHelper;
1313
import me.chanjar.weixin.enterprise.bean.*;
1414
import me.chanjar.weixin.enterprise.util.http.SimpleGetRequestExecutor;
@@ -23,9 +23,9 @@
2323
import org.apache.http.impl.client.HttpClients;
2424

2525
import me.chanjar.weixin.enterprise.bean.WxCpDepart;
26-
import me.chanjar.weixin.enterprise.bean.result.WxError;
26+
import me.chanjar.weixin.common.bean.result.WxError;
2727
import me.chanjar.weixin.enterprise.bean.result.WxMediaUploadResult;
28-
import me.chanjar.weixin.enterprise.bean.result.WxUser;
28+
import me.chanjar.weixin.enterprise.bean.WxCpUser;
2929
import me.chanjar.weixin.enterprise.exception.WxErrorException;
3030
import me.chanjar.weixin.common.util.FileUtils;
3131
import me.chanjar.weixin.enterprise.util.http.MediaDownloadRequestExecutor;
@@ -34,7 +34,6 @@
3434
import me.chanjar.weixin.enterprise.util.http.SimplePostRequestExecutor;
3535

3636
import com.google.gson.JsonElement;
37-
import com.google.gson.JsonObject;
3837
import com.google.gson.internal.Streams;
3938
import com.google.gson.reflect.TypeToken;
4039
import com.google.gson.stream.JsonReader;
@@ -165,7 +164,7 @@ public void departUpdate(WxCpDepart group) throws WxErrorException {
165164

166165
public void departDelete(Integer departId) throws WxErrorException {
167166
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?id=" + departId;
168-
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
167+
execute(new SimpleGetRequestExecutor(), url, null);
169168
}
170169

171170
public List<WxCpDepart> departGet() throws WxErrorException {
@@ -184,28 +183,50 @@ public List<WxCpDepart> departGet() throws WxErrorException {
184183
}
185184

186185
@Override
187-
public void userCreate(WxUser user) throws WxErrorException {
188-
// TODO
186+
public void userCreate(WxCpUser user) throws WxErrorException {
187+
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/create";
188+
execute(new SimplePostRequestExecutor(), url, user.toJson());
189189
}
190190

191191
@Override
192-
public void userUpdate(WxUser user) throws WxErrorException {
193-
// TODO
192+
public void userUpdate(WxCpUser user) throws WxErrorException {
193+
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/update";
194+
execute(new SimplePostRequestExecutor(), url, user.toJson());
194195
}
195196

196197
@Override
197198
public void userDelete(String userid) throws WxErrorException {
198-
// TODO
199+
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?userid=" + userid;
200+
execute(new SimpleGetRequestExecutor(), url, null);
199201
}
200202

201203
@Override
202-
public WxUser userGet(String userid) throws WxErrorException {
203-
return null;
204+
public WxCpUser userGet(String userid) throws WxErrorException {
205+
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?userid=" + userid;
206+
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
207+
return WxCpUser.fromJson(responseContent);
204208
}
205209

206210
@Override
207-
public List<WxUser> userGetByDepartment(String departmentId) throws WxErrorException {
208-
return null;
211+
public List<WxCpUser> userGetByDepart(Integer departId, Boolean fetchChild, Integer status) throws WxErrorException {
212+
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?department_id=" + departId;
213+
String params = "";
214+
if (fetchChild != null) {
215+
params += "&fetch_child=" + (fetchChild ? "1" : "0");
216+
}
217+
if (status != null) {
218+
params += "&status=" + status;
219+
} else {
220+
params += "&status=0";
221+
}
222+
223+
String responseContent = execute(new SimpleGetRequestExecutor(), url, params);
224+
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
225+
return WxCpGsonBuilder.INSTANCE.create()
226+
.fromJson(
227+
tmpJsonElement.getAsJsonObject().get("userlist"),
228+
new TypeToken<List<WxCpUser>>() { }.getType()
229+
);
209230
}
210231

211232
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package me.chanjar.weixin.enterprise.api;
22

3-
import me.chanjar.weixin.common.bean.WxAccessToken;
3+
import me.chanjar.weixin.common.bean.result.WxAccessToken;
44

55
/**
66
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package me.chanjar.weixin.enterprise.bean;
2+
3+
import me.chanjar.weixin.enterprise.util.json.WxCpGsonBuilder;
4+
5+
import java.sql.PseudoColumnUsage;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* 微信用户信息
11+
*
12+
* @author Daniel Qian
13+
*/
14+
public class WxCpUser {
15+
16+
private String userId;
17+
private String name;
18+
private Integer[] departIds;
19+
private String position;
20+
private String mobile;
21+
private String gender;
22+
private String tel;
23+
private String email;
24+
private String weiXinId;
25+
private final List<Attr> extAttrs = new ArrayList<Attr>();
26+
27+
public String getUserId() {
28+
return userId;
29+
}
30+
31+
public void setUserId(String userId) {
32+
this.userId = userId;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public Integer[] getDepartIds() {
44+
return departIds;
45+
}
46+
47+
public void setDepartIds(Integer[] departIds) {
48+
this.departIds = departIds;
49+
}
50+
51+
public String getGender() {
52+
return gender;
53+
}
54+
55+
public void setGender(String gender) {
56+
this.gender = gender;
57+
}
58+
59+
public String getPosition() {
60+
return position;
61+
}
62+
63+
public void setPosition(String position) {
64+
this.position = position;
65+
}
66+
67+
public String getMobile() {
68+
return mobile;
69+
}
70+
71+
public void setMobile(String mobile) {
72+
this.mobile = mobile;
73+
}
74+
75+
public String getTel() {
76+
return tel;
77+
}
78+
79+
public void setTel(String tel) {
80+
this.tel = tel;
81+
}
82+
83+
public String getEmail() {
84+
return email;
85+
}
86+
87+
public void setEmail(String email) {
88+
this.email = email;
89+
}
90+
91+
public String getWeiXinId() {
92+
return weiXinId;
93+
}
94+
95+
public void setWeiXinId(String weiXinId) {
96+
this.weiXinId = weiXinId;
97+
}
98+
99+
public String toJson() {
100+
return WxCpGsonBuilder.INSTANCE.create().toJson(this);
101+
}
102+
103+
public static WxCpUser fromJson(String json) {
104+
return WxCpGsonBuilder.INSTANCE.create().fromJson(json, WxCpUser.class);
105+
}
106+
107+
public void addExtAttr(String name, String value) {
108+
this.extAttrs.add(new Attr(name, value));
109+
}
110+
111+
public List<Attr> getExtAttrs() {
112+
return extAttrs;
113+
}
114+
115+
public static class Attr {
116+
117+
private String name;
118+
private String value;
119+
120+
public Attr(String name, String value) {
121+
this.name = name;
122+
this.value = value;
123+
}
124+
125+
public String getName() {
126+
return name;
127+
}
128+
129+
public void setName(String name) {
130+
this.name = name;
131+
}
132+
133+
public String getValue() {
134+
return value;
135+
}
136+
137+
public void setValue(String value) {
138+
this.value = value;
139+
}
140+
141+
}
142+
143+
}

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)