1 package com.foxinmy.weixin4j.mp.api;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import com.alibaba.fastjson.JSON;
9 import com.alibaba.fastjson.JSONArray;
10 import com.alibaba.fastjson.JSONObject;
11 import com.foxinmy.weixin4j.exception.WeixinException;
12 import com.foxinmy.weixin4j.http.weixin.ApiResult;
13 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
14 import com.foxinmy.weixin4j.mp.component.WeixinComponentPreCodeCreator;
15 import com.foxinmy.weixin4j.mp.component.WeixinComponentTokenCreator;
16 import com.foxinmy.weixin4j.mp.component.WeixinTokenComponentCreator;
17 import com.foxinmy.weixin4j.mp.model.AuthorizerOption;
18 import com.foxinmy.weixin4j.mp.model.AuthorizerOption.AuthorizerOptionName;
19 import com.foxinmy.weixin4j.mp.model.ComponentAuthorizer;
20 import com.foxinmy.weixin4j.mp.model.ComponentAuthorizerToken;
21 import com.foxinmy.weixin4j.mp.model.OauthToken;
22 import com.foxinmy.weixin4j.token.PerTicketManager;
23 import com.foxinmy.weixin4j.token.TicketManager;
24 import com.foxinmy.weixin4j.token.TokenCreator;
25 import com.foxinmy.weixin4j.token.TokenManager;
26 import com.foxinmy.weixin4j.util.Consts;
27 import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class ComponentApi extends MpApi {
41
42
43
44 private final TokenManager tokenManager;
45
46
47
48 private final TicketManager ticketManager;
49
50
51
52 private final TokenManager preCodeManager;
53
54
55
56
57
58
59 public ComponentApi(TicketManager ticketManager) {
60 this.ticketManager = ticketManager;
61 this.tokenManager = new TokenManager(new WeixinComponentTokenCreator(ticketManager),
62 ticketManager.getCacheStorager());
63 this.preCodeManager = new TokenManager(
64 new WeixinComponentPreCodeCreator(tokenManager, ticketManager.getThirdId()),
65 ticketManager.getCacheStorager());
66 }
67
68
69
70
71
72
73 public TokenManager getTokenManager() {
74 return this.tokenManager;
75 }
76
77
78
79
80
81
82 public TicketManager getTicketManager() {
83 return this.ticketManager;
84 }
85
86
87
88
89
90
91 public TokenManager getPreCodeManager() {
92 return this.preCodeManager;
93 }
94
95
96
97
98
99
100
101
102
103 public PerTicketManager getRefreshTokenManager(String authAppId) {
104 return new PerTicketManager(authAppId, ticketManager.getThirdId(), ticketManager.getThirdSecret(),
105 ticketManager.getCacheStorager());
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public String getUserAuthorizationURL(String authAppId) {
122 String redirectUri = Weixin4jConfigUtil.getValue("component.user.oauth.redirect.uri");
123 return getUserAuthorizationURL(authAppId, redirectUri, "snsapi_base", "state");
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 public String getUserAuthorizationURL(String authAppId, String redirectUri, String scope, String state) {
143 String sns_component_user_auth_uri = getRequestUri("sns_component_user_auth_uri");
144 try {
145 return String.format(sns_component_user_auth_uri, authAppId,
146 URLEncoder.encode(redirectUri, Consts.UTF_8.name()), scope, state, this.ticketManager.getThirdId());
147 } catch (UnsupportedEncodingException e) {
148 ;
149 }
150 return "";
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public OauthToken getAuthorizationToken(String authAppId, String code) throws WeixinException {
166 String sns_component_user_token_uri = getRequestUri("sns_component_user_token_uri");
167 String accessToken = tokenManager.getAccessToken();
168 WeixinResponse response = weixinExecutor.get(
169 String.format(sns_component_user_token_uri, authAppId, code, ticketManager.getThirdId(), accessToken));
170 JSONObject result = response.getAsJson();
171 OauthToken token = new OauthToken(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
172 token.setOpenId(result.getString("openid"));
173 token.setScope(result.getString("scope"));
174 token.setRefreshToken(result.getString("refresh_token"));
175 return token;
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 public OauthToken refreshAuthorizationToken(String authAppId, String refreshToken) throws WeixinException {
192 String sns_component_token_refresh_uri = getRequestUri("sns_component_token_refresh_uri");
193 String accessToken = tokenManager.getAccessToken();
194 WeixinResponse response = weixinExecutor.get(String.format(sns_component_token_refresh_uri, authAppId,
195 ticketManager.getThirdId(), accessToken, refreshToken));
196 JSONObject result = response.getAsJson();
197 OauthToken token = new OauthToken(result.getString("access_token"), result.getLongValue("expires_in") * 1000l);
198 token.setOpenId(result.getString("openid"));
199 token.setScope(result.getString("scope"));
200 token.setRefreshToken(result.getString("refresh_token"));
201 return token;
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 public ComponentAuthorizerToken exchangeAuthorizerToken(String authCode) throws WeixinException {
219 String component_exchange_authorizer_uri = getRequestUri("component_query_authorization_uri");
220 JSONObject obj = new JSONObject();
221 obj.put("component_appid", ticketManager.getThirdId());
222 obj.put("authorization_code", authCode);
223 WeixinResponse response = weixinExecutor.post(
224 String.format(component_exchange_authorizer_uri, tokenManager.getAccessToken()), obj.toJSONString());
225 JSONObject authObj = response.getAsJson().getJSONObject("authorization_info");
226 JSONArray privilegesObj = authObj.getJSONArray("func_info");
227 List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
228 for (int i = 0; i < privilegesObj.size(); i++) {
229 privileges.add(privilegesObj.getJSONObject(i).getJSONObject("funcscope_category").getInteger("id"));
230 }
231 ComponentAuthorizerToken token = new ComponentAuthorizerToken(authObj.getString("authorizer_access_token"),
232 authObj.getLongValue("expires_in") * 1000l);
233 token.setRefreshToken(authObj.getString("authorizer_refresh_token"));
234 token.setPrivileges(privileges);
235 token.setAppId(authObj.getString("authorizer_appid"));
236
237 PerTicketManager perTicketManager = getRefreshTokenManager(token.getAppId());
238
239 TokenCreator tokenCreator = new WeixinTokenComponentCreator(perTicketManager, tokenManager);
240 ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
241
242 perTicketManager.cachingTicket(token.getRefreshToken());
243 return token;
244 }
245
246
247
248
249
250
251
252
253
254
255
256
257 public ComponentAuthorizer getAuthorizerInfo(String authAppId) throws WeixinException {
258 String component_get_authorizer_uri = getRequestUri("component_get_authorizer_uri");
259 JSONObject obj = new JSONObject();
260 obj.put("component_appid", ticketManager.getThirdId());
261 obj.put("authorizer_appid", authAppId);
262 WeixinResponse response = weixinExecutor
263 .post(String.format(component_get_authorizer_uri, tokenManager.getAccessToken()), obj.toJSONString());
264 obj = response.getAsJson();
265 JSONObject auth = obj.getJSONObject("authorizer_info");
266 ComponentAuthorizer authorizer = JSON.toJavaObject(auth, ComponentAuthorizer.class);
267 authorizer.setServiceType(auth.getJSONObject("service_type_info").getIntValue("id"));
268 authorizer.setVerifyType(auth.getJSONObject("verify_type_info").getIntValue("id"));
269 auth = obj.getJSONObject("authorization_info");
270 JSONArray privilegesObj = auth.getJSONArray("func_info");
271 List<Integer> privileges = new ArrayList<Integer>(privilegesObj.size());
272 for (int i = 0; i < privilegesObj.size(); i++) {
273 privileges.add(privilegesObj.getJSONObject(i).getJSONObject("funcscope_category").getInteger("id"));
274 }
275 authorizer.setPrivileges(privileges);
276 authorizer.setAppId(auth.getString("appid"));
277 return authorizer;
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291 public AuthorizerOption getAuthorizerOption(String authAppId, AuthorizerOptionName optionName)
292 throws WeixinException {
293 String component_get_authorizer_option_uri = getRequestUri("component_get_authorizer_option_uri");
294 JSONObject obj = new JSONObject();
295 obj.put("component_appid", ticketManager.getThirdId());
296 obj.put("authorizer_appid", authAppId);
297 obj.put("option_name", optionName.name());
298 WeixinResponse response = weixinExecutor.post(
299 String.format(component_get_authorizer_option_uri, tokenManager.getAccessToken()), obj.toJSONString());
300 int optionValue = response.getAsJson().getIntValue("option_value");
301 return AuthorizerOption.parse(optionName, optionValue);
302 }
303
304
305
306
307
308
309
310
311
312
313 public ApiResult setAuthorizerOption(String authAppId, AuthorizerOption option) throws WeixinException {
314 String component_set_authorizer_option_uri = getRequestUri("component_set_authorizer_option_uri");
315 JSONObject obj = new JSONObject();
316 obj.put("component_appid", ticketManager.getThirdId());
317 obj.put("authorizer_appid", authAppId);
318 obj.put("option_name", option.getName());
319 obj.put("option_value", option.getValue());
320 WeixinResponse response = weixinExecutor.post(
321 String.format(component_set_authorizer_option_uri, tokenManager.getAccessToken()), obj.toJSONString());
322 return response.getAsResult();
323 }
324 }