1 package com.foxinmy.weixin4j.mp;
2
3 import java.io.UnsupportedEncodingException;
4 import java.net.URLEncoder;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import com.alibaba.fastjson.JSON;
9 import com.foxinmy.weixin4j.cache.CacheStorager;
10 import com.foxinmy.weixin4j.cache.FileCacheStorager;
11 import com.foxinmy.weixin4j.exception.WeixinException;
12 import com.foxinmy.weixin4j.model.Token;
13 import com.foxinmy.weixin4j.model.WeixinAccount;
14 import com.foxinmy.weixin4j.mp.api.ComponentApi;
15 import com.foxinmy.weixin4j.mp.model.WeixinMpAccount;
16 import com.foxinmy.weixin4j.mp.type.URLConsts;
17 import com.foxinmy.weixin4j.token.TicketManager;
18 import com.foxinmy.weixin4j.util.Consts;
19 import com.foxinmy.weixin4j.util.StringUtil;
20 import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class WeixinComponentProxy {
35
36
37
38
39 private Map<String, ComponentApi> componentMap;
40
41
42
43 private final WeixinMpAccount weixinMpAccount;
44
45
46
47
48
49 public WeixinComponentProxy() {
50 this(new FileCacheStorager<Token>());
51 }
52
53
54
55
56
57
58
59 public WeixinComponentProxy(CacheStorager<Token> cacheStorager) {
60 this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"), WeixinMpAccount.class), cacheStorager);
61 }
62
63
64
65
66
67
68
69
70
71 public WeixinComponentProxy(WeixinMpAccount weixinMpAccount, CacheStorager<Token> cacheStorager) {
72 if (weixinMpAccount == null) {
73 throw new IllegalArgumentException("weixinMpAccount must not be empty");
74 }
75 if (cacheStorager == null) {
76 throw new IllegalArgumentException("cacheStorager must not be empty");
77 }
78 this.weixinMpAccount = weixinMpAccount;
79 this.componentMap = new HashMap<String, ComponentApi>(weixinMpAccount.getComponents().size());
80 for (WeixinAccount component : weixinMpAccount.getComponents()) {
81 this.componentMap.put(component.getId(),
82 new ComponentApi(new TicketManager(component.getId(), component.getSecret(), cacheStorager)));
83 }
84 this.componentMap.put(null, componentMap.get(weixinMpAccount.getComponents().get(0).getId()));
85 }
86
87
88
89
90
91
92 public WeixinMpAccount getWeixinMpAccount() {
93 return this.weixinMpAccount;
94 }
95
96
97
98
99
100
101
102 public ComponentApi component() {
103 return this.componentMap.get(null);
104 }
105
106
107
108
109
110
111
112
113
114 public ComponentApi component(String componentId) {
115 return this.componentMap.get(componentId);
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public String getPreComponentTicket(String componentId) throws WeixinException {
131 ComponentApi component = component(componentId);
132 Token token = component.getTicketManager().getTicket();
133 if (token == null || StringUtil.isBlank(token.getAccessToken())) {
134 throw new WeixinException("maybe oauth first?");
135 }
136 return component.getPreCodeManager().getAccessToken();
137 }
138
139
140
141
142
143
144
145
146
147
148 public void cacheComponentTicket(String componentId, String componentTicket) throws WeixinException {
149 component(componentId).getTicketManager().cachingTicket(componentTicket);
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163 public String getComponentAuthorizationURL(String componentId) throws WeixinException {
164 String redirectUri = Weixin4jConfigUtil.getValue("component.oauth.redirect.uri");
165 return getComponentAuthorizationURL(componentId, redirectUri, "state");
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189 public String getComponentAuthorizationURL(String componentId, String redirectUri, String state)
190 throws WeixinException {
191 try {
192 return String.format(URLConsts.COMPONENT_OAUTH_URL, componentId, getPreComponentTicket(componentId),
193 URLEncoder.encode(redirectUri, Consts.UTF_8.name()), state);
194 } catch (UnsupportedEncodingException e) {
195 ;
196 }
197 return "";
198 }
199
200
201
202
203
204
205
206
207
208
209
210 public WeixinProxy getWeixinProxy(String componentId, String authAppId) {
211 return new WeixinProxy(component(componentId).getRefreshTokenManager(authAppId),
212 component(componentId).getTokenManager());
213 }
214
215 public final static String VERSION = Consts.VERSION;
216 }