Skip to content

Commit e00320d

Browse files
committed
🎨 优化部分代码
1 parent 7261f23 commit e00320d

12 files changed

Lines changed: 197 additions & 225 deletions

File tree

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTpService.java

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ public interface WxCpTpService {
2727
* @param timestamp 时间戳
2828
* @param nonce 随机数
2929
* @param data 微信传输过来的数据,有可能是echoStr,有可能是xml消息
30+
* @return the boolean
3031
*/
3132
boolean checkSignature(String msgSignature, String timestamp, String nonce, String data);
3233

3334
/**
3435
* 获取suite_access_token, 不强制刷新suite_access_token
3536
*
36-
* @see #getSuiteAccessToken(boolean)
37+
* @return the suite access token
38+
* @throws WxErrorException the wx error exception
39+
* @see #getSuiteAccessToken(boolean) #getSuiteAccessToken(boolean)
3740
*/
3841
String getSuiteAccessToken() throws WxErrorException;
3942

@@ -47,13 +50,17 @@ public interface WxCpTpService {
4750
* </pre>
4851
*
4952
* @param forceRefresh 强制刷新
53+
* @return the suite access token
54+
* @throws WxErrorException the wx error exception
5055
*/
5156
String getSuiteAccessToken(boolean forceRefresh) throws WxErrorException;
5257

5358
/**
5459
* 获得suite_ticket,不强制刷新suite_ticket
5560
*
56-
* @see #getSuiteTicket(boolean)
61+
* @return the suite ticket
62+
* @throws WxErrorException the wx error exception
63+
* @see #getSuiteTicket(boolean) #getSuiteTicket(boolean)
5764
*/
5865
String getSuiteTicket() throws WxErrorException;
5966

@@ -66,13 +73,17 @@ public interface WxCpTpService {
6673
* </pre>
6774
*
6875
* @param forceRefresh 强制刷新
76+
* @return the suite ticket
77+
* @throws WxErrorException the wx error exception
6978
*/
7079
String getSuiteTicket(boolean forceRefresh) throws WxErrorException;
7180

7281
/**
7382
* 小程序登录凭证校验
7483
*
7584
* @param jsCode 登录时获取的 code
85+
* @return the wx cp ma js code 2 session result
86+
* @throws WxErrorException the wx error exception
7687
*/
7788
WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException;
7889

@@ -81,14 +92,17 @@ public interface WxCpTpService {
8192
*
8293
* @param authCorpid 授权方corpid
8394
* @param permanentCode 永久授权码,通过get_permanent_code获取
95+
* @return the corp token
96+
* @throws WxErrorException the wx error exception
8497
*/
8598
WxAccessToken getCorpToken(String authCorpid, String permanentCode) throws WxErrorException;
8699

87100
/**
88101
* 获取企业永久授权码 .
89102
*
90103
* @param authCode .
91-
* @return .
104+
* @return . permanent code
105+
* @throws WxErrorException the wx error exception
92106
*/
93107
@Deprecated
94108
WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException;
@@ -99,42 +113,43 @@ public interface WxCpTpService {
99113
* 原来的方法实现不全
100114
* </pre>
101115
*
102-
* @param authCode
103-
* @return
104-
*
116+
* @param authCode the auth code
117+
* @return permanent code info
118+
* @throws WxErrorException the wx error exception
105119
* @author yuan
106-
* @since 2020-03-18
107-
*
108-
* @throws WxErrorException
120+
* @since 2020 -03-18
109121
*/
110122
WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException;
111123

112124
/**
113125
* <pre>
114126
* 获取预授权链接
115127
* </pre>
128+
*
116129
* @param redirectUri 授权完成后的回调网址
117-
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
118-
* @return
119-
* @throws WxErrorException
130+
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
131+
* @return pre auth url
132+
* @throws WxErrorException the wx error exception
120133
*/
121-
String getPreAuthUrl(String redirectUri,String state) throws WxErrorException;
134+
String getPreAuthUrl(String redirectUri, String state) throws WxErrorException;
122135

123136
/**
124137
* 获取企业的授权信息
125138
*
126-
* @param authCorpId 授权企业的corpId
139+
* @param authCorpId 授权企业的corpId
127140
* @param permanentCode 授权企业的永久授权码
128-
* @return
129-
* @throws WxErrorException
141+
* @return auth info
142+
* @throws WxErrorException the wx error exception
130143
*/
131-
WxCpTpAuthInfo getAuthInfo(String authCorpId,String permanentCode) throws WxErrorException;
144+
WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException;
132145

133146
/**
134147
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
135148
*
136149
* @param url 接口地址
137150
* @param queryParam 请求参数
151+
* @return the string
152+
* @throws WxErrorException the wx error exception
138153
*/
139154
String get(String url, String queryParam) throws WxErrorException;
140155

@@ -143,6 +158,8 @@ public interface WxCpTpService {
143158
*
144159
* @param url 接口地址
145160
* @param postData 请求body字符串
161+
* @return the string
162+
* @throws WxErrorException the wx error exception
146163
*/
147164
String post(String url, String postData) throws WxErrorException;
148165

@@ -153,11 +170,13 @@ public interface WxCpTpService {
153170
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
154171
* </pre>
155172
*
173+
* @param <T> 请求值类型
174+
* @param <E> 返回值类型
156175
* @param executor 执行器
157176
* @param uri 请求地址
158177
* @param data 参数
159-
* @param <T> 请求值类型
160-
* @param <E> 返回值类型
178+
* @return the t
179+
* @throws WxErrorException the wx error exception
161180
*/
162181
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
163182

@@ -189,7 +208,7 @@ public interface WxCpTpService {
189208
/**
190209
* 获取WxMpConfigStorage 对象.
191210
*
192-
* @return WxMpConfigStorage
211+
* @return WxMpConfigStorage wx cp tp config storage
193212
*/
194213
WxCpTpConfigStorage getWxCpTpConfigStorage();
195214

@@ -202,6 +221,8 @@ public interface WxCpTpService {
202221

203222
/**
204223
* http请求对象.
224+
*
225+
* @return the request http
205226
*/
206227
RequestHttp<?, ?> getRequestHttp();
207228

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpTpServiceImpl.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.google.gson.JsonObject;
55
import lombok.SneakyThrows;
66
import lombok.extern.slf4j.Slf4j;
7-
import me.chanjar.weixin.common.enums.WxType;
87
import me.chanjar.weixin.common.bean.WxAccessToken;
8+
import me.chanjar.weixin.common.enums.WxType;
99
import me.chanjar.weixin.common.error.WxCpErrorMsgEnum;
1010
import me.chanjar.weixin.common.error.WxError;
1111
import me.chanjar.weixin.common.error.WxErrorException;
@@ -127,7 +127,7 @@ public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
127127
}
128128

129129
@Override
130-
public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException{
130+
public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException {
131131
JsonObject jsonObject = new JsonObject();
132132
jsonObject.addProperty("auth_code", authCode);
133133
String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
@@ -136,18 +136,19 @@ public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxEr
136136

137137
@Override
138138
@SneakyThrows
139-
public String getPreAuthUrl(String redirectUri,String state) throws WxErrorException{
140-
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE),null);
141-
WxCpTpPreauthCode preauthCode = WxCpTpPreauthCode.fromJson(result);
142-
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id="+configStorage.getSuiteId()+
143-
"&pre_auth_code="+preauthCode.getPreAuthCode()+"&redirect_uri="+ URLEncoder.encode(redirectUri,"utf-8");
144-
if(StringUtils.isNotBlank(state))
145-
preAuthUrl += "&state="+state;
139+
public String getPreAuthUrl(String redirectUri, String state) throws WxErrorException {
140+
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE), null);
141+
WxCpTpPreauthCode preAuthCode = WxCpTpPreauthCode.fromJson(result);
142+
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id=" + configStorage.getSuiteId() +
143+
"&pre_auth_code=" + preAuthCode.getPreAuthCode() + "&redirect_uri=" + URLEncoder.encode(redirectUri, "utf-8");
144+
if (StringUtils.isNotBlank(state)) {
145+
preAuthUrl += "&state=" + state;
146+
}
146147
return preAuthUrl;
147148
}
148149

149150
@Override
150-
public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException{
151+
public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException {
151152
JsonObject jsonObject = new JsonObject();
152153
jsonObject.addProperty("auth_corpid", authCorpId);
153154
jsonObject.addProperty("permanent_code", permanentCode);

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageHandler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
public interface WxCpMessageHandler {
1717

1818
/**
19-
* @param wxMessage
19+
* Handle wx cp xml out message.
20+
*
21+
* @param wxMessage the wx message
2022
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
21-
* @param wxCpService
22-
* @param sessionManager
23-
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null
23+
* @param wxCpService the wx cp service
24+
* @param sessionManager the session manager
25+
* @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null
26+
* @throws WxErrorException the wx error exception
2427
*/
2528
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
2629
Map<String, Object> context,

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageInterceptor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ public interface WxCpMessageInterceptor {
1717
/**
1818
* 拦截微信消息
1919
*
20-
* @param wxMessage
20+
* @param wxMessage the wx message
2121
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
22-
* @param wxCpService
23-
* @param sessionManager
24-
* @return true代表OK,false代表不OK
22+
* @param wxCpService the wx cp service
23+
* @param sessionManager the session manager
24+
* @return true代表OK ,false代表不OK
25+
* @throws WxErrorException the wx error exception
2526
*/
2627
boolean intercept(WxCpXmlMessage wxMessage,
2728
Map<String, Object> context,

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageMatcher.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
/**
66
* 消息匹配器,用在消息路由的时候
7+
*
8+
* @author Daniel Qian
79
*/
810
public interface WxCpMessageMatcher {
911

1012
/**
1113
* 消息是否匹配某种模式
14+
*
15+
* @param message the message
16+
* @return the boolean
1217
*/
1318
boolean match(WxCpXmlMessage message);
1419

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouter.java

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
package me.chanjar.weixin.cp.message;
22

3-
import java.util.*;
4-
import java.util.concurrent.ExecutionException;
5-
import java.util.concurrent.ExecutorService;
6-
import java.util.concurrent.Executors;
7-
import java.util.concurrent.Future;
8-
9-
import org.apache.commons.lang3.StringUtils;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
12-
3+
import lombok.extern.slf4j.Slf4j;
134
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
145
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
156
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
@@ -20,6 +11,16 @@
2011
import me.chanjar.weixin.cp.api.WxCpService;
2112
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
2213
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
14+
import org.apache.commons.lang3.StringUtils;
15+
16+
import java.util.ArrayList;
17+
import java.util.HashMap;
18+
import java.util.List;
19+
import java.util.Map;
20+
import java.util.concurrent.ExecutionException;
21+
import java.util.concurrent.ExecutorService;
22+
import java.util.concurrent.Executors;
23+
import java.util.concurrent.Future;
2324

2425
/**
2526
* <pre>
@@ -49,9 +50,9 @@
4950
*
5051
* @author Daniel Qian
5152
*/
53+
@Slf4j
5254
public class WxCpMessageRouter {
5355
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
54-
private final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
5556
private final List<WxCpMessageRouterRule> rules = new ArrayList<>();
5657

5758
private final WxCpService wxCpService;
@@ -156,37 +157,31 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
156157
// 返回最后一个非异步的rule的执行结果
157158
if (rule.isAsync()) {
158159
futures.add(
159-
this.executorService.submit(new Runnable() {
160-
@Override
161-
public void run() {
162-
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler);
163-
}
160+
this.executorService.submit(() -> {
161+
rule.service(wxMessage, context, WxCpMessageRouter.this.wxCpService, WxCpMessageRouter.this.sessionManager, WxCpMessageRouter.this.exceptionHandler);
164162
})
165163
);
166164
} else {
167165
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
168166
// 在同步操作结束,session访问结束
169-
this.log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
167+
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
170168
sessionEndAccess(wxMessage);
171169
}
172170
}
173171

174172
if (futures.size() > 0) {
175-
this.executorService.submit(new Runnable() {
176-
@Override
177-
public void run() {
178-
for (Future future : futures) {
179-
try {
180-
future.get();
181-
WxCpMessageRouter.this.log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
182-
// 异步操作结束,session访问结束
183-
sessionEndAccess(wxMessage);
184-
} catch (InterruptedException e) {
185-
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
186-
Thread.currentThread().interrupt();
187-
} catch (ExecutionException e) {
188-
WxCpMessageRouter.this.log.error("Error happened when wait task finish", e);
189-
}
173+
this.executorService.submit(() -> {
174+
for (Future future : futures) {
175+
try {
176+
future.get();
177+
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
178+
// 异步操作结束,session访问结束
179+
sessionEndAccess(wxMessage);
180+
} catch (InterruptedException e) {
181+
log.error("Error happened when wait task finish", e);
182+
Thread.currentThread().interrupt();
183+
} catch (ExecutionException e) {
184+
log.error("Error happened when wait task finish", e);
190185
}
191186
}
192187
});
@@ -198,7 +193,7 @@ public void run() {
198193
* 处理微信消息.
199194
*/
200195
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
201-
return this.route(wxMessage, new HashMap<String, Object>(2));
196+
return this.route(wxMessage, new HashMap<>(2));
202197
}
203198

204199
private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {

0 commit comments

Comments
 (0)