1 package com.foxinmy.weixin4j.qy;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import com.alibaba.fastjson.JSON;
10 import com.foxinmy.weixin4j.cache.CacheStorager;
11 import com.foxinmy.weixin4j.cache.FileCacheStorager;
12 import com.foxinmy.weixin4j.exception.WeixinException;
13 import com.foxinmy.weixin4j.model.Token;
14 import com.foxinmy.weixin4j.model.WeixinAccount;
15 import com.foxinmy.weixin4j.qy.api.ProviderApi;
16 import com.foxinmy.weixin4j.qy.api.SuiteApi;
17 import com.foxinmy.weixin4j.qy.model.OUserInfo;
18 import com.foxinmy.weixin4j.qy.model.WeixinQyAccount;
19 import com.foxinmy.weixin4j.qy.token.WeixinProviderTokenCreator;
20 import com.foxinmy.weixin4j.qy.type.LoginTargetType;
21 import com.foxinmy.weixin4j.qy.type.URLConsts;
22 import com.foxinmy.weixin4j.token.TicketManager;
23 import com.foxinmy.weixin4j.token.TokenManager;
24 import com.foxinmy.weixin4j.util.Consts;
25 import com.foxinmy.weixin4j.util.StringUtil;
26 import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
27
28
29
30
31
32
33
34
35
36
37
38 public class WeixinSuiteProxy {
39
40
41
42
43 private Map<String, SuiteApi> suiteMap;
44
45
46
47 private ProviderApi providerApi;
48
49
50
51 private final WeixinQyAccount weixinQyAccount;
52
53
54
55
56
57 public WeixinSuiteProxy() {
58 this(new FileCacheStorager<Token>());
59 }
60
61
62
63
64
65
66
67 public WeixinSuiteProxy(CacheStorager<Token> cacheStorager) {
68 this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
69 WeixinQyAccount.class), cacheStorager);
70 }
71
72
73
74
75
76
77
78
79
80 public WeixinSuiteProxy(WeixinQyAccount weixinQyAccount,
81 CacheStorager<Token> cacheStorager) {
82 if (weixinQyAccount == null) {
83 throw new IllegalArgumentException(
84 "weixinQyAccount must not be empty");
85 }
86 if (cacheStorager == null) {
87 throw new IllegalArgumentException(
88 "cacheStorager must not be empty");
89 }
90 this.weixinQyAccount = weixinQyAccount;
91 List<WeixinAccount> suites = weixinQyAccount.getSuites();
92 if (suites != null && !suites.isEmpty()) {
93 this.suiteMap = new HashMap<String, SuiteApi>(suites.size());
94 for (WeixinAccount suite : suites) {
95 this.suiteMap.put(suite.getId(), new SuiteApi(
96 new TicketManager(suite.getId(), suite.getSecret(),
97 cacheStorager)));
98 }
99 this.suiteMap.put(null, suiteMap.get(suites.get(0).getId()));
100 }
101 if (StringUtil.isNotBlank(weixinQyAccount.getId())
102 && StringUtil.isNotBlank(weixinQyAccount.getProviderSecret())) {
103 this.providerApi = new ProviderApi(
104 new TokenManager(new WeixinProviderTokenCreator(
105 weixinQyAccount.getId(), weixinQyAccount
106 .getProviderSecret()), cacheStorager),
107 cacheStorager);
108 }
109 }
110
111
112
113
114
115
116 public WeixinQyAccount getWeixinQyAccount() {
117 return weixinQyAccount;
118 }
119
120
121
122
123
124
125
126 public SuiteApi suite() {
127 return this.suiteMap.get(null);
128 }
129
130
131
132
133
134
135
136
137
138 public SuiteApi suite(String suiteId) {
139 return this.suiteMap.get(suiteId);
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public String getPreSuiteTicket(String suiteId) throws WeixinException {
155 SuiteApi suite = suite(suiteId);
156 Token token = suite.getTicketManager().getTicket();
157 if (token == null || StringUtil.isBlank(token.getAccessToken())) {
158 throw new WeixinException("maybe oauth first?");
159 }
160 return suite.getPreCodeManager().getAccessToken();
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174 public void cacheSuiteTicket(String suiteId, String suiteTicket)
175 throws WeixinException {
176 suite(suiteId).getTicketManager().cachingTicket(suiteTicket);
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 public String getSuiteAuthorizationURL(String suiteId)
191 throws WeixinException {
192 String redirectUri = Weixin4jConfigUtil
193 .getValue("suite.oauth.redirect.uri");
194 return getSuiteAuthorizationURL(suiteId, redirectUri, "state");
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public String getSuiteAuthorizationURL(String suiteId, String redirectUri,
218 String state) throws WeixinException {
219 try {
220 return String.format(URLConsts.SUITE_OAUTH_URL, suiteId,
221 getPreSuiteTicket(suiteId),
222 URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
223 } catch (UnsupportedEncodingException e) {
224 ;
225 }
226 return "";
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 public OUserInfo getOUserInfoByCode(String authCode) throws WeixinException {
244 return providerApi.getOUserInfoByCode(authCode);
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 public String getLoginUrl(String corpId, LoginTargetType targetType,
264 int agentId) throws WeixinException {
265 return providerApi.getLoginUrl(corpId, targetType, agentId);
266 }
267
268
269
270
271
272
273
274
275
276
277
278 public WeixinProxy getWeixinProxy(String suiteId, String authCorpId) {
279 return new WeixinProxy(suite(suiteId).getPerTicketManager(authCorpId),
280 suite(suiteId).getTokenManager());
281 }
282
283 public final static String VERSION = Consts.VERSION;
284 }