8000 #783 企业微信模块增加群聊相关接口 · linlinjava/WxJava@9b68931 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b68931

Browse files
gaigeshenbinarywang
authored andcommitted
binarywang#783 企业微信模块增加群聊相关接口
* 实现消息推送的服务,添加发送应用消息的方法 * 添加创建群聊会话方法,去除不需要的类 * 完成群聊会话的修改和获取 * 重构群聊服务,提出到单独的服务类里面 * 完成群聊的测试
1 parent b7d3f83 commit 9b68931

File tree

9 files changed

+298
-6
lines changed

9 files changed

+298
-6
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.chanjar.weixin.cp.api;
2+
3+
import java.util.List;
4+
5+
import me.chanjar.weixin.common.error.WxErrorException;
6+
import me.chanjar.weixin.cp.bean.WxCpChat;
7+
8+
/**
9+
* 群聊服务
10+
*
11+
* @author gaigeshen
12+
*/
13+
public interface WxCpChatService {
14+
15+
/**
16+
* 创建群聊会话,注意:刚创建的群,如果没有下发消息,在企业微信不会出现该群。
17+
*
18+
* @param name 群聊名,最多50个utf8字符,超过将截断
19+
* @param owner 指定群主的id。如果不指定,系统会随机从userlist中选一人作为群主
20+
* @param users 群成员id列表。至少2人,至多500人
21+
* @param chatId 群聊的唯一标志,不能与已有的群重复;字符串类型,最长32个字符。只允许字符0-9及字母a-zA-Z。如果不填,系统会随机生成群id
22+
* @return 创建群聊会话的结果,群聊的唯一标志
23+
* @throws WxErrorException 发生异常
24+
*/
25+
String chatCreate(String name, String owner, List<String> users, String chatId) throws WxErrorException;
26+
27+
/**
28+
* 修改群聊会话
29+
*
30+
* @param chatId 群聊id
31+
* @param name 新的群聊名。若不需更新,请忽略此参数(null or empty)。最多50个utf8字符,超过将截断
32+
* @param owner 新群主的id。若不需更新,请忽略此参数(null or empty)
33+
* @param usersToAdd 添加成员的id列表,若不需要更新,则传递空对象或者空集合
34+
* @param usersToDelete 踢出成员的id列表,若不需要更新,则传递空对象或者空集合
35+
* @throws WxErrorException 发生异常
36+
*/
37+
void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException;
38+
39+
/**
40+
* 获取群聊会话
41+
*
42+
* @param chatId 群聊编号
43+
* @return 群聊会话
44+
* @throws WxErrorException 发生异常
45+
*/
46+
WxCpChat chatGet(String chatId) throws WxErrorException;
47+
48+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
88
import me.chanjar.weixin.common.util.http.RequestExecutor;
99
import me.chanjar.weixin.common.util.http.RequestHttp;
10-
import me.chanjar.weixin.cp.bean.*;
10+
import me.chanjar.weixin.cp.bean.WxCpMessage;
11+
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
1112
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1213

1314
/**
@@ -241,13 +242,20 @@ public interface WxCpService {
241242
* 获取用户相关接口的服务类对象
242243
*/
243244
WxCpUserService getUserService();
245+
246+
/**
247+
* 获取群聊服务
248+
*
249+
* @return 群聊服务
250+
*/
251+
WxCpChatService getChatService();
244252

245253
WxCpAgentService getAgentService();
246254

247255
/**
248256
* http请求对象
249257
*/
250-
RequestHttp getRequestHttp();
258+
RequestHttp<?, ?> getRequestHttp();
251259

252260
void setUserService(WxCpUserService userService);
253261

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3-
import com.google.gson.Gson;
3+
import java.util.List;
4+
45
import com.google.gson.JsonObject;
56
import com.google.gson.JsonParser;
67
import com.google.gson.reflect.TypeToken;
8+
79
import me.chanjar.weixin.common.error.WxError;
810
import me.chanjar.weixin.common.error.WxErrorException;
911
import me.chanjar.weixin.cp.api.WxCpAgentService;
1012
import me.chanjar.weixin.cp.api.WxCpService;
1113
import me.chanjar.weixin.cp.bean.WxCpAgent;
1214
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
1315

14-
import java.util.List;
15-
1616

1717
/**
1818
* <pre>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
import com.google.gson.JsonParser;
10+
11+
import me.chanjar.weixin.common.error.WxErrorException;
12+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
13+
import me.chanjar.weixin.cp.api.WxCpChatService;
14+
import me.chanjar.weixin.cp.api.WxCpService;
15+
import me.chanjar.weixin.cp.bean.WxCpChat;
16+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
17+
18+
/**
19+
* 群聊服务实现
20+
*
21+
* @author gaigeshen
22+
*/
23+
public class WxCpChatServiceImpl implements WxCpChatService {
24+
25+
private final WxCpService internalService;
26+
27+
/**
28+
* 创建群聊服务实现的实例
29+
*
30+
* @param internalService 企业微信的服务
31+
*/
32+
public WxCpChatServiceImpl(WxCpService internalService) {
33+
this.internalService = internalService;
34+
}
35+
36+
@Override
37+
public String chatCreate(String name, String owner, List<String> users, String chatId) throws WxErrorException {
38+
Map<String, Object> data = new HashMap<>(4);
39+
if (StringUtils.isNotBlank(name)) {
40+
data.put("name", name);
41+
}
42+
if (StringUtils.isNotBlank(owner)) {
43+
data.put("owner", owner);
44+
}
45+
if (users != null) {
46+
data.put("userlist", users);
47+
}
48+
if (StringUtils.isNotBlank(chatId)) {
49+
data.put("chatid", chatId);
50+
}
51+
String result = internalService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/create", WxGsonBuilder.create().toJson(data));
52+
return new JsonParser().parse(result).getAsJsonObject().get("chatid").getAsString();
53+
}
54+
55+
@Override
56+
public void chatUpdate(String chatId, String name, String owner, List<String> usersToAdd, List<String> usersToDelete) throws WxErrorException {
57+
Map<String, Object> data = new HashMap<>(5);
58+
if (StringUtils.isNotBlank(chatId)) {
59+
data.put("chatid", chatId);
60+
}
61+
if (StringUtils.isNotBlank(name)) {
62+
data.put("name", name);
63+
}
64+
if (StringUtils.isNotBlank(owner)) {
65+
data.put("owner", owner);
66+
}
67+
if (usersToAdd != null && !usersToAdd.isEmpty()) {
68+
data.put("add_user_list", usersToAdd);
69+
}
70+
if (usersToDelete != null && !usersToDelete.isEmpty()) {
71+
data.put("del_user_list", usersToDelete);
72+
}
73+
internalService.post("https://qyapi.weixin.qq.com/cgi-bin/appchat/update", WxGsonBuilder.create().toJson(data));
74+
}
75+
76+
@Override
77+
public WxCpChat chatGet(String chatId) throws WxErrorException {
78+
String result = internalService.get("https://qyapi.weixin.qq.com/cgi-bin/appchat/get?chatid=" + chatId, null);
79+
return WxCpGsonBuilder.create().fromJson(
80+
new JsonParser().parse(result).getAsJsonObject().getAsJsonObject("chat_info").toString(), WxCpChat.class);
81+
}
82+
83+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceAbstractImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.google.gson.JsonElement;
1111
import com.google.gson.JsonObject;
1212
import com.google.gson.JsonParser;
13+
1314
import me.chanjar.weixin.common.bean.WxJsapiSignature;
1415
import me.chanjar.weixin.common.error.WxError;
1516
import me.chanjar.weixin.common.error.WxErrorException;
@@ -24,6 +25,7 @@
2425
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
2526
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
2627
import me.chanjar.weixin.cp.api.WxCpAgentService;
28+
import me.chanjar.weixin.cp.api.WxCpChatService;
2729
import me.chanjar.weixin.cp.api.WxCpDepartmentService;
2830
import me.chanjar.weixin.cp.api.WxCpMediaService;
2931
import me.chanjar.weixin.cp.api.WxCpMenuService;
@@ -39,6 +41,7 @@ public abstract class WxCpServiceAbstractImpl<H, P> implements WxCpService, Requ
3941
protected final Logger log = LoggerFactory.getLogger(this.getClass());
4042

4143
private WxCpUserService userService = new WxCpUserServiceImpl(this);
44+
private WxCpChatService chatService = new WxCpChatServiceImpl(this);
4245
private WxCpDepartmentService departmentService = new WxCpDepartmentServiceImpl(this);
4346
private WxCpMediaService mediaService = new WxCpMediaServiceImpl(this);
4447
private WxCpMenuService menuService = new WxCpMenuServiceImpl(this);
@@ -343,7 +346,12 @@ public WxCpUserService getUserService() {
343346
}
344347

345348
@Override
346-
public RequestHttp getRequestHttp() {
349+
public WxCpChatService getChatService() {
350+
return chatService;
351+
}
352+
353+
@Override
354+
public RequestHttp<?, ?> getRequestHttp() {
347355
return this;
348356
}
349357

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package me.chanjar.weixin.cp.bean;
2< F438 /code>+
3+
import java.util.List;
4+
5+
import lombok.Data;
6+
7+
/**
8+
* 群聊
9+
*
10+
* @author gaigeshen
11+
*/
12+
@Data
13+
public class WxCpChat {
14+
15+
private String id;
16+
private String name;
17+
private String owner;
18+
private List<String> users;
19+
20+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
3+
*
4+
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
5+
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
6+
* arose from modification of the original source, or other redistribution of this source
7+
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
8+
*/
9+
package me.chanjar.weixin.cp.util.json;
10+
11+
import java.lang.reflect.Type;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import com.google.gson.JsonArray;
16+
import com.google.gson.JsonDeserializationContext;
17+
import com.google.gson.JsonDeserializer;
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonParseException;
21+
import com.google.gson.JsonSerializationContext;
22+
import com.google.gson.JsonSerializer;
23+
24+
import me.chanjar.weixin.common.util.json.GsonHelper;
25+
import me.chanjar.weixin.cp.bean.WxCpChat;
26+
27+
/**
28+
* 群聊适配器
29+
*
30+
* @author gaigeshen
31+
*/
32+
public class WxCpChatGsonAdapter implements JsonSerializer<WxCpChat>, JsonDeserializer<WxCpChat> {
33+
34+
@Override
35+
public JsonElement serialize(WxCpChat chat, Type typeOfSrc, JsonSerializationContext context) {
36+
JsonObject json = new JsonObject();
37+
if (chat.getId() != null) {
38+
json.addProperty("chatid", chat.getId());
39+
}
40+
if (chat.getName() != null) {
41+
json.addProperty("name", chat.getName());
42+
}
43+
if (chat.getOwner() != null) {
44+
json.addProperty("owner", chat.getOwner());
45+
}
46+
if (chat.getUsers() != null) {
47+
JsonArray users = new JsonArray();
48+
for (String user : chat.getUsers()) {
49+
users.add(user);
50+
}
51+
json.add("userlist", users);
52+
}
53+
return json;
54+
}
55+
56+
@Override
57+
public WxCpChat deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
58+
JsonObject chatJson = json.getAsJsonObject();
59+
60+
WxCpChat chat = new WxCpChat();
61+
chat.setId(GsonHelper.getAsString(chatJson.get("chatid")));
62+
chat.setName(GsonHelper.getAsString(chatJson.get("name")));
63+
chat.setOwner(GsonHelper.getAsString(chatJson.get("owner")));
64+
65+
JsonArray usersJson = chatJson.getAsJsonArray("userlist");
66+
if (usersJson != null) {
67+
List<String> users = new ArrayList<>(usersJson.size());
68+
chat.setUsers(users);
69+
for (JsonElement userJson : usersJson) {
70+
users.add(userJson.getAsString());
71+
}
72+
}
73+
74+
return chat;
75+
}
76+
77+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.chanjar.weixin.common.bean.menu.WxMenu;
66
import me.chanjar.weixin.common.error.WxError;
77
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
8+
import me.chanjar.weixin.cp.bean.WxCpChat;
89
import me.chanjar.weixin.cp.bean.WxCpDepart;
910
import me.chanjar.weixin.cp.bean.WxCpMessage;
1011
import me.chanjar.weixin.cp.bean.WxCpTag;
@@ -20,6 +21,7 @@ public class WxCpGsonBuilder {
2021
static {
2122
INSTANCE.disableHtmlEscaping();
2223
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
24+
INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter());
2325
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
2426
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
2527
INSTANCE.registerTypeAdapter(WxError.class, new WxErrorAdapter());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import java.util.Arrays;
4+
5+
import me.chanjar.weixin.cp.bean.WxCpChat;
6+
import org.testng.Assert;
7+
import org.testng.annotations.Guice;
8+
import org.testng.annotations.Test;
9+
10+
import com.google.inject.Inject;
11+
12+
import me.chanjar.weixin.cp.api.ApiTestModule;
13+
import me.chanjar.weixin.cp.api.WxCpService;
14+
15+
/**
16+
* 测试群聊服务
17+
*
18+
* @author gaigeshen
19+
*/
20+
@Guice(modules = ApiTestModule.class)
21+
public class WxCpChatServiceImplTest {
22+
23+
@Inject
24+
private WxCpService wxCpService;
25+
26+
@Test
27+
public void create() throws Exception {
28+
wxCpService.getChatService().chatCreate("测试群聊", "gaige_shen", Arrays.asList("gaige_shen", "ZhangXiaoMing"), "mychatid");
29+
}
30+
31+
@Test
32+
public void get() throws Exception {
33+
WxCpChat chat = wxCpService.getChatService().chatGet("mychatid");
34+
System.out.println(chat);
35+
Assert.assertEquals(chat.getName(), "测试群聊");
36+
}
37+
38+
@Test
39+
public void update() throws Exception {
40+
wxCpService.getChatService().chatUpdate("mychatid", "", "", Arrays.asList("ZhengWuYao"), null);
41+
WxCpChat chat = wxCpService.getChatService().chatGet("mychatid");
42+
System.out.println(chat);
43+
Assert.assertEquals(chat.getUsers().size(), 3);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)
0