1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import com.alibaba.fastjson.JSON;
7 import com.alibaba.fastjson.JSONObject;
8 import com.foxinmy.weixin4j.exception.WeixinException;
9 import com.foxinmy.weixin4j.http.weixin.ApiResult;
10 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
11 import com.foxinmy.weixin4j.model.Token;
12 import com.foxinmy.weixin4j.qy.model.Contacts;
13 import com.foxinmy.weixin4j.qy.model.IdParameter;
14 import com.foxinmy.weixin4j.qy.model.Tag;
15 import com.foxinmy.weixin4j.qy.model.User;
16 import com.foxinmy.weixin4j.token.TokenManager;
17
18
19
20
21
22
23
24
25
26
27 public class TagApi extends QyApi {
28 private final TokenManager tokenManager;
29
30 public TagApi(TokenManager tokenManager) {
31 this.tokenManager = tokenManager;
32 }
33
34
35
36
37
38
39
40
41
42
43
44 public int createTag(Tag tag) throws WeixinException {
45 String tag_create_uri = getRequestUri("tag_create_uri");
46 Token token = tokenManager.getCache();
47 JSONObject obj = (JSONObject) JSON.toJSON(tag);
48 if (obj.getIntValue("tagid") <= 0) {
49 obj.remove("tagid");
50 }
51 WeixinResponse response = weixinExecutor.post(
52 String.format(tag_create_uri, token.getAccessToken()),
53 obj.toJSONString());
54 return response.getAsJson().getIntValue("tagid");
55 }
56
57
58
59
60
61
62
63
64
65
66
67 public ApiResult updateTag(Tag tag) throws WeixinException {
68 String tag_update_uri = getRequestUri("tag_update_uri");
69 Token token = tokenManager.getCache();
70 WeixinResponse response = weixinExecutor.post(
71 String.format(tag_update_uri, token.getAccessToken()),
72 JSON.toJSONString(tag));
73 return response.getAsResult();
74 }
75
76
77
78
79
80
81
82
83
84
85 public ApiResult deleteTag(int tagId) throws WeixinException {
86 String tag_delete_uri = getRequestUri("tag_delete_uri");
87 Token token = tokenManager.getCache();
88 WeixinResponse response = weixinExecutor.get(String.format(
89 tag_delete_uri, token.getAccessToken(), tagId));
90 return response.getAsResult();
91 }
92
93
94
95
96
97
98
99
100
101 public List<Tag> listTag() throws WeixinException {
102 String tag_list_uri = getRequestUri("tag_list_uri");
103 Token token = tokenManager.getCache();
104 WeixinResponse response = weixinExecutor.get(String.format(
105 tag_list_uri, token.getAccessToken()));
106 return JSON.parseArray(response.getAsJson().getString("taglist"),
107 Tag.class);
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public Contacts getTagUsers(int tagId) throws WeixinException {
123 String tag_get_user_uri = getRequestUri("tag_get_user_uri");
124 Token token = tokenManager.getCache();
125 WeixinResponse response = weixinExecutor.get(String.format(
126 tag_get_user_uri, token.getAccessToken(), tagId));
127 JSONObject obj = response.getAsJson();
128 Contacts contacts = new Contacts();
129 contacts.setUsers(JSON.parseArray(obj.getString("userlist"), User.class));
130 contacts.setPartyIds(JSON.parseArray(obj.getString("partylist"),
131 Integer.class));
132 contacts.putTagIds(tagId);
133 return contacts;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public IdParameter addTagUsers(int tagId, List<String> userIds,
151 List<Integer> partyIds) throws WeixinException {
152 String tag_add_user_uri = getRequestUri("tag_add_user_uri");
153 return excuteUsers(tag_add_user_uri, tagId, userIds, partyIds);
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public IdParameter deleteTagUsers(int tagId, List<String> userIds,
171 List<Integer> partyIds) throws WeixinException {
172 String tag_delete_user_uri = getRequestUri("tag_delete_user_uri");
173 return excuteUsers(tag_delete_user_uri, tagId, userIds, partyIds);
174 }
175
176 private IdParameter excuteUsers(String uri, int tagId,
177 List<String> userIds, List<Integer> partyIds)
178 throws WeixinException {
179 JSONObject obj = new JSONObject();
180 obj.put("tagid", tagId);
181 obj.put("userlist", userIds);
182 obj.put("partylist", partyIds);
183 Token token = tokenManager.getCache();
184 WeixinResponse response = weixinExecutor.post(
185 String.format(uri, token.getAccessToken()), obj.toJSONString());
186 obj = response.getAsJson();
187 IdParameter idParameter = IdParameter.get();
188 if (obj.containsKey("invalidlist")) {
189 idParameter.setUserIds(Arrays.asList(obj.getString("invalidlist")
190 .split(IdParameter.SEPARATORS)));
191 }
192 if (obj.containsKey("partylist")) {
193 idParameter.setPartyIds(JSON.parseArray(obj.getString("partylist"),
194 Integer.class));
195 }
196 return idParameter;
197 }
198 }