Skip to content

Commit e00e7bb

Browse files
committed
格式化开放平台模块代码
1 parent 9939997 commit e00e7bb

20 files changed

+152
-86
lines changed

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenComponentService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface WxOpenComponentService {
3434
* 刷新oauth2的access token
3535
*/
3636
String OAUTH2_REFRESH_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s&component_appid==%s";
37+
3738
WxMpService getWxMpServiceByAppid(String appid);
3839

3940
WxOpenConfigStorage getWxOpenConfigStorage();

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenConfigStorage.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,39 @@
99
*/
1010
public interface WxOpenConfigStorage {
1111

12+
String getComponentAppId();
13+
1214
void setComponentAppId(String componentAppId);
1315

16+
String getComponentAppSecret();
17+
1418
void setComponentAppSecret(String componentAppSecret);
1519

20+
String getComponentToken();
21+
1622
void setComponentToken(String componentToken);
1723

24+
String getComponentAesKey();
25+
1826
void setComponentAesKey(String componentAesKey);
1927

20-
String getComponentAppId();
21-
String getComponentAppSecret();
22-
String getComponentToken();
23-
String getComponentAesKey();
2428
String getComponentVerifyTicket();
29+
2530
void setComponentVerifyTicket(String componentVerifyTicket);
31+
2632
String getComponentAccessToken();
33+
2734
boolean isComponentAccessTokenExpired();
35+
2836
void updateComponentAccessTokent(WxOpenComponentAccessToken componentAccessToken);
37+
2938
WxMpConfigStorage getWxMpConfigStorage(String appId);
39+
3040
/**
3141
* 应该是线程安全的
3242
*
33-
* @param componentAccessToken 新的accessToken值
34-
* @param expiresInSeconds 过期时间,以秒为单位
43+
* @param componentAccessToken 新的accessToken值
44+
* @param expiresInSeconds 过期时间,以秒为单位
3545
*/
3646
void updateComponentAccessTokent(String componentAccessToken, int expiresInSeconds);
3747

@@ -42,7 +52,9 @@ public interface WxOpenConfigStorage {
4252

4353

4454
String getAuthorizerRefreshToken(String appId);
55+
4556
void setAuthorizerRefreshToken(String appId, String authorizerRefreshToken);
57+
4658
String getAuthorizerAccessToken(String appId);
4759

4860

@@ -63,8 +75,8 @@ public interface WxOpenConfigStorage {
6375
/**
6476
* 应该是线程安全的
6577
*
66-
* @param authorizerAccessToken 新的accessToken值
67-
* @param expiresInSeconds 过期时间,以秒为单位
78+
* @param authorizerAccessToken 新的accessToken值
79+
* @param expiresInSeconds 过期时间,以秒为单位
6880
*/
6981
void updateAuthorizerAccessToken(String appId, String authorizerAccessToken, int expiresInSeconds);
7082

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/WxOpenService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
*/
88
public interface WxOpenService {
99
WxOpenComponentService getWxOpenComponentService();
10+
1011
WxOpenConfigStorage getWxOpenConfigStorage();
12+
1113
void setWxOpenConfigStorage(WxOpenConfigStorage wxOpenConfigStorage);
14+
1215
/**
1316
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
1417
*/

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenComponentServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
* @author <a href="https://github.com/007gzs">007</a>
3131
*/
3232
public class WxOpenComponentServiceImpl implements WxOpenComponentService {
33+
private static final Map<String, WxMpService> wxOpenMpServiceMap = new Hashtable<>();
3334
protected final Logger log = LoggerFactory.getLogger(this.getClass());
3435
private WxOpenService wxOpenService;
35-
private static final Map<String, WxMpService> wxOpenMpServiceMap = new Hashtable<>();
3636

3737
public WxOpenComponentServiceImpl(WxOpenService wxOpenService) {
3838
this.wxOpenService = wxOpenService;
@@ -62,6 +62,7 @@ public WxOpenService getWxOpenService() {
6262
public WxOpenConfigStorage getWxOpenConfigStorage() {
6363
return wxOpenService.getWxOpenConfigStorage();
6464
}
65+
6566
@Override
6667
public boolean checkSignature(String timestamp, String nonce, String signature) {
6768
try {
@@ -72,6 +73,7 @@ public boolean checkSignature(String timestamp, String nonce, String signature)
7273
return false;
7374
}
7475
}
76+
7577
@Override
7678
public String getComponentAccessToken(boolean forceRefresh) throws WxErrorException {
7779

@@ -137,6 +139,7 @@ public String route(final WxOpenXmlMessage wxMessage) throws WxErrorException {
137139
}
138140
return null;
139141
}
142+
140143
@Override
141144
public WxOpenQueryAuthResult getQueryAuth(String authorizationCode) throws WxErrorException {
142145
JsonObject jsonObject = new JsonObject();
@@ -145,6 +148,7 @@ public WxOpenQueryAuthResult getQueryAuth(String authorizationCode) throws WxErr
145148
String responseContent = post(API_QUERY_AUTH_URL, jsonObject.toString());
146149
return WxOpenGsonBuilder.create().fromJson(responseContent, WxOpenQueryAuthResult.class);
147150
}
151+
148152
@Override
149153
public WxOpenAuthorizerInfoResult getAuthorizerInfo(String authorizerAppid) throws WxErrorException {
150154
JsonObject jsonObject = new JsonObject();
@@ -163,6 +167,7 @@ public WxOpenAuthorizerOptionResult getAuthorizerOption(String authorizerAppid,
163167
String responseContent = post(API_GET_AUTHORIZER_OPTION_URL, jsonObject.toString());
164168
return WxOpenGsonBuilder.create().fromJson(responseContent, WxOpenAuthorizerOptionResult.class);
165169
}
170+
166171
@Override
167172
public WxError setAuthorizerOption(String authorizerAppid, String optionName, String optionValue) throws WxErrorException {
168173
JsonObject jsonObject = new JsonObject();

weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenInMemoryConfigStorage.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,45 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage {
3535
private Map<String, Token> cardApiTickets = new Hashtable<>();
3636

3737
@Override
38-
public void setComponentAppId(String componentAppId) {
39-
this.componentAppId = componentAppId;
40-
}
41-
@Override
42-
public void setComponentAppSecret(String componentAppSecret) {
43-
this.componentAppSecret = componentAppSecret;
44-
}
45-
@Override
46-
public void setComponentToken(String componentToken) {
47-
this.componentToken = componentToken;
48-
}
49-
@Override
50-
public void setComponentAesKey(String componentAesKey) {
51-
this.componentAesKey = componentAesKey;
38+
public String getComponentAppId() {
39+
return componentAppId;
5240
}
5341

5442
@Override
55-
public String getComponentAppId() {
56-
return componentAppId;
43+
public void setComponentAppId(String componentAppId) {
44+
this.componentAppId = componentAppId;
5745
}
5846

5947
@Override
6048
public String getComponentAppSecret() {
6149
return componentAppSecret;
6250
}
6351

52+
@Override
53+
public void setComponentAppSecret(String componentAppSecret) {
54+
this.componentAppSecret = componentAppSecret;
55+
}
56+
6457
@Override
6558
public String getComponentToken() {
6659
return componentToken;
6760
}
6861

62+
@Override
63+
public void setComponentToken(String componentToken) {
64+
this.componentToken = componentToken;
65+
}
66+
6967
@Override
7068
public String getComponentAesKey() {
7169
return componentAesKey;
7270
}
7371

72+
@Override
73+
public void setComponentAesKey(String componentAesKey) {
74+
this.componentAesKey = componentAesKey;
75+
}
76+
7477
@Override
7578
public String getComponentVerifyTicket() {
7679
return componentVerifyTicket;
@@ -111,30 +114,34 @@ public void updateComponentAccessTokent(String componentAccessToken, int expires
111114
public boolean autoRefreshToken() {
112115
return true;
113116
}
114-
private String getTokenString(Map<String, Token> map, String key){
117+
118+
private String getTokenString(Map<String, Token> map, String key) {
115119
Token token = map.get(key);
116-
if(token == null || (token.expiresTime != null && System.currentTimeMillis() > token.expiresTime)){
120+
if (token == null || (token.expiresTime != null && System.currentTimeMillis() > token.expiresTime)) {
117121
return null;
118122
}
119123
return token.token;
120124
}
121-
private void expireToken(Map<String, Token> map, String key){
125+
126+
private void expireToken(Map<String, Token> map, String key) {
122127
Token token = map.get(key);
123-
if(token != null){
128+
if (token != null) {
124129
token.expiresTime = 0L;
125130
}
126131
}
127-
private void updateToken(Map<String, Token> map, String key, String tokenString, Integer expiresInSeconds){
132+
133+
private void updateToken(Map<String, Token> map, String key, String tokenString, Integer expiresInSeconds) {
128134
Token token = map.get(key);
129-
if(token == null){
135+
if (token == null) {
130136
token = new Token();
131137
map.put(key, token);
132138
}
133139
token.token = tokenString;
134-
if(expiresInSeconds != null) {
140+
if (expiresInSeconds != null) {
135141
token.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
136142
}
137143
}
144+
138145
@Override
139146
public String getAuthorizerRefreshToken(String appId) {
140147
return getTokenString(authorizerRefreshTokens, appId);
@@ -211,22 +218,21 @@ public void updateCardApiTicket(String appId, String cardApiTicket, int expiresI
211218
updateToken(cardApiTickets, appId, cardApiTicket, expiresInSeconds);
212219
}
213220

214-
private static class Token{
221+
private static class Token {
215222
private String token;
216223
private Long expiresTime;
217224
}
218-
private static class WxOpenMpConfigStorage implements WxMpConfigStorage{
225+
226+
private static class WxOpenMpConfigStorage implements WxMpConfigStorage {
219227
private WxOpenConfigStorage wxOpenConfigStorage;
220228
private String appId;
221-
private WxOpenMpConfigStorage(WxOpenConfigStorage wxOpenConfigStorage, String appId){
222-
this.wxOpenConfigStorage = wxOpenConfigStorage;
223-
this.appId = appId;
224-
}
225-
226229
private Lock accessTokenLock = new ReentrantLock();
227230
private Lock jsapiTicketLock = new ReentrantLock();
228231
private Lock cardApiTicketLock = new ReentrantLock();
229-
232+
private WxOpenMpConfigStorage(WxOpenConfigStorage wxOpenConfigStorage, String appId) {
233+
this.wxOpenConfigStorage = wxOpenConfigStorage;
234+
this.appId = appId;
235+
}
230236

231237
@Override
232238
public String getAccessToken() {

0 commit comments

Comments
 (0)