|
| 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 | +} |
0 commit comments