1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.util.List;
4
5 import com.alibaba.fastjson.JSON;
6 import com.alibaba.fastjson.JSONObject;
7 import com.foxinmy.weixin4j.exception.WeixinException;
8 import com.foxinmy.weixin4j.http.weixin.ApiResult;
9 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
10 import com.foxinmy.weixin4j.model.Token;
11 import com.foxinmy.weixin4j.mp.model.Group;
12 import com.foxinmy.weixin4j.token.TokenManager;
13
14
15
16
17
18
19
20
21
22
23 @Deprecated
24 public class GroupApi extends MpApi {
25
26 private final TokenManager tokenManager;
27
28 public GroupApi(TokenManager tokenManager) {
29 this.tokenManager = tokenManager;
30 }
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public Group createGroup(String name) throws WeixinException {
45 String group_create_uri = getRequestUri("group_create_uri");
46 Token token = tokenManager.getCache();
47 Group group = new Group(name);
48 WeixinResponse response = weixinExecutor.post(
49 String.format(group_create_uri, token.getAccessToken()),
50 group.toCreateJson());
51 return JSON.parseObject(response.getAsJson().getString("group"),
52 Group.class);
53 }
54
55
56
57
58
59
60
61
62
63
64 public List<Group> getGroups() throws WeixinException {
65 String group_get_uri = getRequestUri("group_get_uri");
66 Token token = tokenManager.getCache();
67 WeixinResponse response = weixinExecutor.get(String.format(
68 group_get_uri, token.getAccessToken()));
69
70 return JSON.parseArray(response.getAsJson().getString("groups"),
71 Group.class);
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85 public int getGroupByOpenId(String openId) throws WeixinException {
86 String group_getid_uri = getRequestUri("group_getid_uri");
87 Token token = tokenManager.getCache();
88 WeixinResponse response = weixinExecutor.post(
89 String.format(group_getid_uri, token.getAccessToken()),
90 String.format("{\"openid\":\"%s\"}", openId));
91
92 return response.getAsJson().getIntValue("groupid");
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public ApiResult modifyGroup(int groupId, String name)
109 throws WeixinException {
110 String group_modify_uri = getRequestUri("group_modify_uri");
111 Token token = tokenManager.getCache();
112 Group group = new Group(groupId, name);
113
114 WeixinResponse response = weixinExecutor.post(
115 String.format(group_modify_uri, token.getAccessToken()),
116 group.toModifyJson());
117 return response.getAsResult();
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public ApiResult moveGroup(int groupId, String openId)
133 throws WeixinException {
134 String group_move_uri = getRequestUri("group_move_uri");
135 Token token = tokenManager.getCache();
136 WeixinResponse response = weixinExecutor.post(String.format(
137 group_move_uri, token.getAccessToken()), String.format(
138 "{\"openid\":\"%s\",\"to_groupid\":%d}", openId, groupId));
139
140 return response.getAsResult();
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155 public ApiResult moveGroup(int groupId, String... openIds)
156 throws WeixinException {
157 String group_batchmove_uri = getRequestUri("group_batchmove_uri");
158 Token token = tokenManager.getCache();
159 JSONObject obj = new JSONObject();
160 obj.put("to_groupid", groupId);
161 obj.put("openid_list", openIds);
162 WeixinResponse response = weixinExecutor.post(
163 String.format(group_batchmove_uri, token.getAccessToken()),
164 obj.toJSONString());
165
166 return response.getAsResult();
167 }
168
169
170
171
172
173
174
175
176
177
178
179 public ApiResult deleteGroup(int groupId) throws WeixinException {
180 String group_delete_uri = getRequestUri("group_delete_uri");
181 Token token = tokenManager.getCache();
182 WeixinResponse response = weixinExecutor.post(
183 String.format(group_delete_uri, token.getAccessToken()),
184 String.format("{\"group\":{\"id\":%d}}", groupId));
185
186 return response.getAsResult();
187 }
188 }