1 package com.foxinmy.weixin4j.qy.api;
2
3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONObject;
5 import com.foxinmy.weixin4j.exception.WeixinException;
6 import com.foxinmy.weixin4j.http.weixin.ApiResult;
7 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
8 import com.foxinmy.weixin4j.model.Token;
9 import com.foxinmy.weixin4j.qy.model.AgentInfo;
10 import com.foxinmy.weixin4j.qy.model.AgentSetter;
11 import com.foxinmy.weixin4j.qy.model.OUserInfo;
12 import com.foxinmy.weixin4j.qy.model.User;
13 import com.foxinmy.weixin4j.qy.suite.WeixinSuitePreCodeCreator;
14 import com.foxinmy.weixin4j.qy.suite.WeixinSuiteTokenCreator;
15 import com.foxinmy.weixin4j.qy.suite.WeixinTokenSuiteCreator;
16 import com.foxinmy.weixin4j.token.PerTicketManager;
17 import com.foxinmy.weixin4j.token.TicketManager;
18 import com.foxinmy.weixin4j.token.TokenCreator;
19 import com.foxinmy.weixin4j.token.TokenManager;
20
21
22
23
24
25
26
27
28
29
30 public class SuiteApi extends QyApi {
31
32
33
34 private final TokenManager tokenManager;
35
36
37
38 private final TicketManager ticketManager;
39
40
41
42 private final TokenManager preCodeManager;
43
44
45
46
47
48
49 public SuiteApi(TicketManager ticketManager) {
50 this.ticketManager = ticketManager;
51 this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator(
52 ticketManager), ticketManager.getCacheStorager());
53 this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator(
54 tokenManager, ticketManager.getThirdId()),
55 ticketManager.getCacheStorager());
56 }
57
58
59
60
61
62
63 public TokenManager getTokenManager() {
64 return this.tokenManager;
65 }
66
67
68
69
70
71
72 public TicketManager getTicketManager() {
73 return this.ticketManager;
74 }
75
76
77
78
79
80
81 public TokenManager getPreCodeManager() {
82 return this.preCodeManager;
83 }
84
85
86
87
88
89
90
91
92 public PerTicketManager getPerTicketManager(String authCorpId) {
93 return new PerTicketManager(authCorpId, ticketManager.getThirdId(),
94 ticketManager.getThirdSecret(),
95 ticketManager.getCacheStorager());
96 }
97
98
99
100
101
102
103
104
105 public TokenManager getPerTokenManager(String authCorpId) {
106 return new TokenManager(new WeixinTokenSuiteCreator(
107 getPerTicketManager(authCorpId), tokenManager),
108 ticketManager.getCacheStorager());
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 public ApiResult setSuiteSession(int... appids) throws WeixinException {
123 String suite_set_session_uri = getRequestUri("suite_set_session_uri");
124 JSONObject para = new JSONObject();
125 para.put("pre_auth_code", preCodeManager.getAccessToken());
126 JSONObject appid = new JSONObject();
127 appid.put("appid", appids);
128 para.put("session_info", appid);
129 WeixinResponse response = weixinExecutor.post(
130 String.format(suite_set_session_uri,
131 tokenManager.getAccessToken()), para.toJSONString());
132 return response.getAsResult();
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146 public OUserInfo exchangeAuthInfo(String authCode) throws WeixinException {
147 String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri");
148 JSONObject obj = new JSONObject();
149 obj.put("suite_id", ticketManager.getThirdId());
150 obj.put("auth_code", authCode);
151 WeixinResponse response = weixinExecutor.post(
152 String.format(suite_get_permanent_uri,
153 tokenManager.getAccessToken()), obj.toJSONString());
154 obj = response.getAsJson();
155 obj.put("corp_info", obj.remove("auth_corp_info"));
156 obj.put("user_info", obj.remove("auth_user_info"));
157 OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class);
158
159 PerTicketManager perTicketManager = getPerTicketManager(oInfo
160 .getCorpInfo().getCorpId());
161
162 TokenCreator tokenCreator = new WeixinTokenSuiteCreator(
163 perTicketManager, tokenManager);
164 Token token = new Token(obj.getString("access_token"),
165 obj.getLongValue("expires_in") * 1000l);
166 ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
167
168 perTicketManager.cachingTicket(obj.getString("permanent_code"));
169 return oInfo;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183 public OUserInfo getAuthInfo(String authCorpId) throws WeixinException {
184 String suite_get_authinfo_uri = getRequestUri("suite_get_authinfo_uri");
185 JSONObject obj = new JSONObject();
186 obj.put("suite_id", ticketManager.getThirdId());
187 obj.put("auth_corpid", authCorpId);
188 obj.put("permanent_code", getPerTicketManager(authCorpId)
189 .getAccessTicket());
190 WeixinResponse response = weixinExecutor.post(
191 String.format(suite_get_authinfo_uri,
192 tokenManager.getAccessToken()), obj.toJSONString());
193 obj = response.getAsJson();
194 obj.put("corp_info", obj.remove("auth_corp_info"));
195 obj.put("user_info", obj.remove("auth_user_info"));
196 return JSON.toJavaObject(obj, OUserInfo.class);
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public AgentInfo getAgent(String authCorpId, int agentid)
213 throws WeixinException {
214 String suite_get_agent_uri = getRequestUri("suite_get_agent_uri");
215 JSONObject obj = new JSONObject();
216 obj.put("suite_id", ticketManager.getThirdId());
217 obj.put("auth_corpid", authCorpId);
218 obj.put("permanent_code", getPerTicketManager(authCorpId)
219 .getAccessTicket());
220 obj.put("agentid", agentid);
221 WeixinResponse response = weixinExecutor.post(
222 String.format(suite_get_agent_uri,
223 tokenManager.getAccessToken()), obj.toJSONString());
224 JSONObject jsonObj = response.getAsJson();
225 AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class);
226 agent.setAllowUsers(JSON.parseArray(
227 jsonObj.getJSONObject("allow_userinfos").getString("user"),
228 User.class));
229 agent.setAllowPartys(JSON.parseArray(
230 jsonObj.getJSONObject("allow_partys").getString("partyid"),
231 Integer.class));
232 agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags")
233 .getString("tagid"), Integer.class));
234 return agent;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 public ApiResult setAgent(String authCorpId, AgentSetter agentSet)
251 throws WeixinException {
252 String suite_set_agent_uri = getRequestUri("suite_set_agent_uri");
253 JSONObject obj = new JSONObject();
254 obj.put("suite_id", ticketManager.getThirdId());
255 obj.put("auth_corpid", authCorpId);
256 obj.put("permanent_code", getPerTicketManager(authCorpId)
257 .getAccessTicket());
258 obj.put("agent", agentSet);
259 WeixinResponse response = weixinExecutor.post(
260 String.format(suite_set_agent_uri,
261 tokenManager.getAccessToken()),
262 JSON.toJSONString(obj, AgentApi.typeFilter));
263 return response.getAsResult();
264 }
265 }