|
| 1 | +package me.chanjar.weixin.cp.tp.service.impl; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonPrimitive; |
| 6 | +import com.google.gson.reflect.TypeToken; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 9 | +import me.chanjar.weixin.common.util.json.GsonParser; |
| 10 | +import me.chanjar.weixin.cp.bean.WxCpTpTag; |
| 11 | +import me.chanjar.weixin.cp.bean.WxCpTpTagAddOrRemoveUsersResult; |
| 12 | +import me.chanjar.weixin.cp.bean.WxCpTpTagGetResult; |
| 13 | +import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; |
| 14 | +import me.chanjar.weixin.cp.tp.service.WxCpTpService; |
| 15 | +import me.chanjar.weixin.cp.tp.service.WxCpTpTagService; |
| 16 | +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tag.*; |
| 21 | + |
| 22 | +/** |
| 23 | + * <pre> |
| 24 | + * 企业微信第三方开发-标签相关接口,部分照搬了WxCpTagServiceImpl |
| 25 | + * </pre> |
| 26 | + * |
| 27 | + * @author zhangq <zhangq002@gmail.com> |
| 28 | + * @since 2021-02-14 16:02 |
| 29 | + */ |
| 30 | +@RequiredArgsConstructor |
| 31 | +public class WxCpTpTagServiceImpl implements WxCpTpTagService { |
| 32 | + private final WxCpTpService mainService; |
| 33 | + |
| 34 | + @Override |
| 35 | + public String create(String name, Integer id) throws WxErrorException { |
| 36 | + JsonObject o = new JsonObject(); |
| 37 | + o.addProperty("tagname", name); |
| 38 | + |
| 39 | + if (id != null) { |
| 40 | + o.addProperty("tagid", id); |
| 41 | + } |
| 42 | + return this.create(o); |
| 43 | + } |
| 44 | + |
| 45 | + private String create(JsonObject param) throws WxErrorException { |
| 46 | + String url = getWxCpTpConfigStorage().getApiUrl(TAG_CREATE); |
| 47 | + String responseContent = this.mainService.post(url, param.toString()); |
| 48 | + JsonObject jsonObject = GsonParser.parse(responseContent); |
| 49 | + return jsonObject.get("tagid").getAsString(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void update(String tagId, String tagName) throws WxErrorException { |
| 54 | + String url = getWxCpTpConfigStorage().getApiUrl(TAG_UPDATE); |
| 55 | + JsonObject o = new JsonObject(); |
| 56 | + o.addProperty("tagid", tagId); |
| 57 | + o.addProperty("tagname", tagName); |
| 58 | + this.mainService.post(url, o.toString()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void delete(String tagId) throws WxErrorException { |
| 63 | + String url = String.format(getWxCpTpConfigStorage().getApiUrl(TAG_DELETE), tagId); |
| 64 | + this.mainService.get(url, null); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public List<WxCpTpTag> listAll() throws WxErrorException { |
| 69 | + String url = getWxCpTpConfigStorage().getApiUrl(TAG_LIST); |
| 70 | + String responseContent = this.mainService.get(url, null); |
| 71 | + JsonObject tmpJson = GsonParser.parse(responseContent); |
| 72 | + return WxCpGsonBuilder.create().fromJson(tmpJson.get("taglist"), new TypeToken<List<WxCpTpTag>>() { |
| 73 | + // do nothing |
| 74 | + }.getType()); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public WxCpTpTagGetResult get(String tagId) throws WxErrorException { |
| 79 | + if (tagId == null) { |
| 80 | + throw new IllegalArgumentException("缺少tagId参数"); |
| 81 | + } |
| 82 | + |
| 83 | + String url = String.format(getWxCpTpConfigStorage().getApiUrl(TAG_GET), tagId); |
| 84 | + String responseContent = this.mainService.get(url, null); |
| 85 | + return WxCpTpTagGetResult.deserialize(responseContent); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public WxCpTpTagAddOrRemoveUsersResult addUsers2Tag(String tagId, List<String> userIds, List<String> partyIds) |
| 90 | + throws WxErrorException { |
| 91 | + String url = getWxCpTpConfigStorage().getApiUrl(TAG_ADD_TAG_USERS); |
| 92 | + JsonObject jsonObject = new JsonObject(); |
| 93 | + jsonObject.addProperty("tagid", tagId); |
| 94 | + this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject); |
| 95 | + |
| 96 | + return WxCpTpTagAddOrRemoveUsersResult.deserialize(this.mainService.post(url, jsonObject.toString())); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public WxCpTpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List<String> userIds, List<String> partyIds) |
| 101 | + throws WxErrorException { |
| 102 | + String url = getWxCpTpConfigStorage().getApiUrl(TAG_DEL_TAG_USERS); |
| 103 | + JsonObject jsonObject = new JsonObject(); |
| 104 | + jsonObject.addProperty("tagid", tagId); |
| 105 | + this.addUserIdsAndPartyIdsToJson(userIds, partyIds, jsonObject); |
| 106 | + |
| 107 | + return WxCpTpTagAddOrRemoveUsersResult.deserialize(this.mainService.post(url, jsonObject.toString())); |
| 108 | + } |
| 109 | + |
| 110 | + private void addUserIdsAndPartyIdsToJson(List<String> userIds, List<String> partyIds, JsonObject jsonObject) { |
| 111 | + if (userIds != null) { |
| 112 | + JsonArray jsonArray = new JsonArray(); |
| 113 | + for (String userId : userIds) { |
| 114 | + jsonArray.add(new JsonPrimitive(userId)); |
| 115 | + } |
| 116 | + jsonObject.add("userlist", jsonArray); |
| 117 | + } |
| 118 | + |
| 119 | + if (partyIds != null) { |
| 120 | + JsonArray jsonArray = new JsonArray(); |
| 121 | + for (String userId : partyIds) { |
| 122 | + jsonArray.add(new JsonPrimitive(userId)); |
| 123 | + } |
| 124 | + jsonObject.add("partylist", jsonArray); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @SuppressWarnings("deprecation") |
| 129 | + private WxCpTpConfigStorage getWxCpTpConfigStorage() { |
| 130 | + return this.mainService.getWxCpTpConfigStorage(); |
| 131 | + } |
| 132 | +} |
0 commit comments