Skip to content

Commit 21f1497

Browse files
committed
修复代理没有设置时会存在的问题
1 parent b26a017 commit 21f1497

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

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

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class WxCpServiceImpl implements WxCpService {
6565
*/
6666
protected final Object globalJsapiTicketRefreshLock = new Object();
6767

68-
protected WxCpConfigStorage wxCpConfigStorage;
68+
protected WxCpConfigStorage configStorage;
6969

7070
protected CloseableHttpClient httpClient;
7171

@@ -81,7 +81,7 @@ public class WxCpServiceImpl implements WxCpService {
8181
@Override
8282
public boolean checkSignature(String msgSignature, String timestamp, String nonce, String data) {
8383
try {
84-
return SHA1.gen(this.wxCpConfigStorage.getToken(), timestamp, nonce, data)
84+
return SHA1.gen(this.configStorage.getToken(), timestamp, nonce, data)
8585
.equals(msgSignature);
8686
} catch (Exception e) {
8787
return false;
@@ -102,14 +102,14 @@ public String getAccessToken() throws WxErrorException {
102102
@Override
103103
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
104104
if (forceRefresh) {
105-
this.wxCpConfigStorage.expireAccessToken();
105+
this.configStorage.expireAccessToken();
106106
}
107-
if (this.wxCpConfigStorage.isAccessTokenExpired()) {
107+
if (this.configStorage.isAccessTokenExpired()) {
108108
synchronized (this.globalAccessTokenRefreshLock) {
109-
if (this.wxCpConfigStorage.isAccessTokenExpired()) {
109+
if (this.configStorage.isAccessTokenExpired()) {
110110
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
111-
+ "&corpid=" + this.wxCpConfigStorage.getCorpId() + "&corpsecret="
112-
+ this.wxCpConfigStorage.getCorpSecret();
111+
+ "&corpid=" + this.configStorage.getCorpId()
112+
+ "&corpsecret=" + this.configStorage.getCorpSecret();
113113
try {
114114
HttpGet httpGet = new HttpGet(url);
115115
if (this.httpProxy != null) {
@@ -129,7 +129,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
129129
throw new WxErrorException(error);
130130
}
131131
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
132-
this.wxCpConfigStorage.updateAccessToken(
132+
this.configStorage.updateAccessToken(
133133
accessToken.getAccessToken(), accessToken.getExpiresIn());
134134
} catch (ClientProtocolException e) {
135135
throw new RuntimeException(e);
@@ -139,7 +139,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
139139
}
140140
}
141141
}
142-
return this.wxCpConfigStorage.getAccessToken();
142+
return this.configStorage.getAccessToken();
143143
}
144144

145145
@Override
@@ -150,23 +150,23 @@ public String getJsapiTicket() throws WxErrorException {
150150
@Override
151151
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
152152
if (forceRefresh) {
153-
this.wxCpConfigStorage.expireJsapiTicket();
153+
this.configStorage.expireJsapiTicket();
154154
}
155-
if (this.wxCpConfigStorage.isJsapiTicketExpired()) {
155+
if (this.configStorage.isJsapiTicketExpired()) {
156156
synchronized (this.globalJsapiTicketRefreshLock) {
157-
if (this.wxCpConfigStorage.isJsapiTicketExpired()) {
157+
if (this.configStorage.isJsapiTicketExpired()) {
158158
String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket";
159159
String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
160160
JsonElement tmpJsonElement = new JsonParser().parse(responseContent);
161161
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
162162
String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
163163
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
164-
this.wxCpConfigStorage.updateJsapiTicket(jsapiTicket,
164+
this.configStorage.updateJsapiTicket(jsapiTicket,
165165
expiresInSeconds);
166166
}
167167
}
168168
}
169-
return this.wxCpConfigStorage.getJsapiTicket();
169+
return this.configStorage.getJsapiTicket();
170170
}
171171

172172
@Override
@@ -187,7 +187,7 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
187187
jsapiSignature.setSignature(signature);
188188

189189
// Fixed bug
190-
jsapiSignature.setAppid(this.wxCpConfigStorage.getCorpId());
190+
jsapiSignature.setAppid(this.configStorage.getCorpId());
191191

192192
return jsapiSignature;
193193
}
@@ -200,19 +200,19 @@ public void messageSend(WxCpMessage message) throws WxErrorException {
200200

201201
@Override
202202
public void menuCreate(WxMenu menu) throws WxErrorException {
203-
menuCreate(this.wxCpConfigStorage.getAgentId(), menu);
203+
menuCreate(this.configStorage.getAgentId(), menu);
204204
}
205205

206206
@Override
207207
public void menuCreate(String agentId, WxMenu menu) throws WxErrorException {
208208
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid="
209-
+ this.wxCpConfigStorage.getAgentId();
209+
+ this.configStorage.getAgentId();
210210
post(url, menu.toJson());
211211
}
212212

213213
@Override
214214
public void menuDelete() throws WxErrorException {
215-
menuDelete(this.wxCpConfigStorage.getAgentId());
215+
menuDelete(this.configStorage.getAgentId());
216216
}
217217

218218
@Override
@@ -223,7 +223,7 @@ public void menuDelete(String agentId) throws WxErrorException {
223223

224224
@Override
225225
public WxMenu menuGet() throws WxErrorException {
226-
return menuGet(this.wxCpConfigStorage.getAgentId());
226+
return menuGet(this.configStorage.getAgentId());
227227
}
228228

229229
@Override
@@ -258,7 +258,7 @@ public File mediaDownload(String media_id) throws WxErrorException {
258258
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
259259
return execute(
260260
new MediaDownloadRequestExecutor(
261-
this.wxCpConfigStorage.getTmpDirFile()),
261+
this.configStorage.getTmpDirFile()),
262262
url, "media_id=" + media_id);
263263
}
264264

@@ -475,15 +475,15 @@ public void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorExc
475475
@Override
476476
public String oauth2buildAuthorizationUrl(String state) {
477477
return this.oauth2buildAuthorizationUrl(
478-
this.wxCpConfigStorage.getOauth2redirectUri(),
478+
this.configStorage.getOauth2redirectUri(),
479479
state
480480
);
481481
}
482482

483483
@Override
484484
public String oauth2buildAuthorizationUrl(String redirectUri, String state) {
485485
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?";
486-
url += "appid=" + this.wxCpConfigStorage.getCorpId();
486+
url += "appid=" + this.configStorage.getCorpId();
487487
url += "&redirect_uri=" + URIUtil.encodeURIComponent(redirectUri);
488488
url += "&response_type=code";
489489
url += "&scope=snsapi_base";
@@ -496,7 +496,7 @@ public String oauth2buildAuthorizationUrl(String redirectUri, String state) {
496496

497497
@Override
498498
public String[] oauth2getUserInfo(String code) throws WxErrorException {
499-
return oauth2getUserInfo(this.wxCpConfigStorage.getAgentId(), code);
499+
return oauth2getUserInfo(this.configStorage.getAgentId(), code);
500500
}
501501

502502
@Override
@@ -599,7 +599,7 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
599599
*/
600600
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
601601
// 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
602-
this.wxCpConfigStorage.expireAccessToken();
602+
this.configStorage.expireAccessToken();
603603
return execute(executor, uri, data);
604604
}
605605
if (error.getErrorCode() != 0) {
@@ -619,20 +619,22 @@ protected CloseableHttpClient getHttpclient() {
619619

620620
@Override
621621
public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
622-
this.wxCpConfigStorage = wxConfigProvider;
623-
ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxCpConfigStorage
622+
this.configStorage = wxConfigProvider;
623+
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage
624624
.getApacheHttpClientBuilder();
625625
if (null == apacheHttpClientBuilder) {
626626
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
627627
}
628-
apacheHttpClientBuilder
629-
.httpProxyHost(this.wxCpConfigStorage.getHttpProxyHost())
630-
.httpProxyPort(this.wxCpConfigStorage.getHttpProxyPort())
631-
.httpProxyUsername(this.wxCpConfigStorage.getHttpProxyUsername())
632-
.httpProxyPassword(this.wxCpConfigStorage.getHttpProxyPassword());
633-
634-
this.httpProxy = new HttpHost(this.wxCpConfigStorage.getHttpProxyHost(),
635-
this.wxCpConfigStorage.getHttpProxyPort());
628+
629+
apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
630+
.httpProxyPort(this.configStorage.getHttpProxyPort())
631+
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
632+
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
633+
634+
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
635+
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());
636+
}
637+
636638
this.httpClient = apacheHttpClientBuilder.build();
637639
}
638640

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBaseAPITest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package me.chanjar.weixin.cp.api;
22

3-
import com.google.inject.Inject;
4-
import me.chanjar.weixin.common.exception.WxErrorException;
5-
import me.chanjar.weixin.common.util.StringUtils;
63
import org.testng.Assert;
74
import org.testng.annotations.Guice;
85
import org.testng.annotations.Test;
96

7+
import com.google.inject.Inject;
8+
9+
import me.chanjar.weixin.common.exception.WxErrorException;
10+
import me.chanjar.weixin.common.util.StringUtils;
11+
1012
/**
1113
* 基础API测试
1214
*
@@ -20,7 +22,7 @@ public class WxCpBaseAPITest {
2022
protected WxCpServiceImpl wxService;
2123

2224
public void testRefreshAccessToken() throws WxErrorException {
23-
WxCpConfigStorage configStorage = this.wxService.wxCpConfigStorage;
25+
WxCpConfigStorage configStorage = this.wxService.configStorage;
2426
String before = configStorage.getAccessToken();
2527
this.wxService.getAccessToken(false);
2628

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageAPITest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import org.testng.annotations.Guice;
4+
import org.testng.annotations.Test;
5+
36
import com.google.inject.Inject;
7+
48
import me.chanjar.weixin.common.api.WxConsts;
59
import me.chanjar.weixin.common.exception.WxErrorException;
610
import me.chanjar.weixin.cp.bean.WxCpMessage;
7-
import org.testng.annotations.Guice;
8-
import org.testng.annotations.Test;
911

1012
/***
1113
* 测试发送消息
@@ -20,7 +22,7 @@ public class WxCpMessageAPITest {
2022
protected WxCpServiceImpl wxService;
2123

2224
public void testSendCustomMessage() throws WxErrorException {
23-
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.wxCpConfigStorage;
25+
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.configStorage;
2426
WxCpMessage message1 = new WxCpMessage();
2527
message1.setAgentId(configStorage.getAgentId());
2628
message1.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
@@ -32,7 +34,7 @@ public void testSendCustomMessage() throws WxErrorException {
3234
.TEXT()
3335
.agentId(configStorage.getAgentId())
3436
.toUser(configStorage.getUserId())
35-
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
37+
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
3638
.build();
3739
this.wxService.messageSend(message2);
3840

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,10 @@ private void initHttpClient() {
515515
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
516516
}
517517

518-
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(),
519-
this.configStorage.getHttpProxyPort());
518+
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
519+
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());
520+
}
521+
520522
this.httpClient = apacheHttpClientBuilder.build();
521523
}
522524

0 commit comments

Comments
 (0)