Skip to content

Commit af9b3a4

Browse files
committed
binarywang#1039 微信公众号接口地址域名部分进行可配置化 改造
1 parent 084ffcf commit af9b3a4

16 files changed

Lines changed: 317 additions & 241 deletions

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package me.chanjar.weixin.mp.api;
22

3-
import java.io.File;
4-
import java.util.concurrent.locks.Lock;
5-
63
import me.chanjar.weixin.common.bean.WxAccessToken;
74
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
5+
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
86
import me.chanjar.weixin.mp.enums.TicketType;
97

8+
import java.io.File;
9+
import java.util.concurrent.locks.Lock;
10+
1011
/**
1112
* 微信客户端配置存储.
1213
*
@@ -96,4 +97,8 @@ public interface WxMpConfigStorage {
9697
*/
9798
boolean autoRefreshToken();
9899

100+
/**
101+
* 得到微信接口地址域名部分的自定义设置信息.
102+
*/
103+
WxMpHostConfig getHostConfig();
99104
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.Data;
99
import me.chanjar.weixin.common.bean.WxAccessToken;
1010
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
11+
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
1112
import me.chanjar.weixin.mp.enums.TicketType;
1213
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
1314

@@ -179,4 +180,9 @@ public boolean autoRefreshToken() {
179180
return true;
180181
}
181182

183+
@Override
184+
public WxMpHostConfig getHostConfig() {
185+
return null;
186+
}
187+
182188
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ public WxMpSemanticQueryResult semanticQuery(WxMpSemanticQuery semanticQuery) th
157157

158158
@Override
159159
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
160-
return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(),
160+
return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(this.getWxMpConfigStorage()),
161161
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
162162
}
163163

164164
@Override
165165
public String buildQrConnectUrl(String redirectURI, String scope, String state) {
166-
return String.format(QRCONNECT_URL.getUrl(), this.getWxMpConfigStorage().getAppId(),
166+
return String.format(QRCONNECT_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(),
167167
URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
168168
}
169169

@@ -179,14 +179,14 @@ private WxMpOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorExc
179179

180180
@Override
181181
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
182-
String url = String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(), this.getWxMpConfigStorage().getAppId(),
182+
String url = String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(),
183183
this.getWxMpConfigStorage().getSecret(), code);
184184
return this.getOAuth2AccessToken(url);
185185
}
186186

187187
@Override
188188
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
189-
String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(), this.getWxMpConfigStorage().getAppId(), refreshToken);
189+
String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(), refreshToken);
190190
return this.getOAuth2AccessToken(url);
191191
}
192192

@@ -196,7 +196,7 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) thro
196196
lang = "zh_CN";
197197
}
198198

199-
String url = String.format(OAUTH2_USERINFO_URL.getUrl(), token.getAccessToken(), token.getOpenId(), lang);
199+
String url = String.format(OAUTH2_USERINFO_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId(), lang);
200200

201201
try {
202202
RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
@@ -209,7 +209,7 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken token, String lang) thro
209209

210210
@Override
211211
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken token) {
212-
String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(), token.getAccessToken(), token.getOpenId());
212+
String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId());
213213

214214
try {
215215
SimpleGetRequestExecutor.create(this).execute(url, null);
@@ -252,7 +252,7 @@ public String get(String url, String queryParam) throws WxErrorException {
252252

253253
@Override
254254
public String get(WxMpApiUrl url, String queryParam) throws WxErrorException {
255-
return this.get(url.getUrl(), queryParam);
255+
return this.get(url.getUrl(this.getWxMpConfigStorage()), queryParam);
256256
}
257257

258258
@Override
@@ -262,12 +262,12 @@ public String post(String url, String postData) throws WxErrorException {
262262

263263
@Override
264264
public String post(WxMpApiUrl url, String postData) throws WxErrorException {
265-
return this.post(url.getUrl(), postData);
265+
return this.post(url.getUrl(this.getWxMpConfigStorage()), postData);
266266
}
267267

268268
@Override
269269
public <T, E> T execute(RequestExecutor<T, E> executor, WxMpApiUrl url, E data) throws WxErrorException {
270-
return this.execute(executor, url.getUrl(), data);
270+
return this.execute(executor, url.getUrl(this.getWxMpConfigStorage()), data);
271271
}
272272

273273
/**

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
99
import me.chanjar.weixin.mp.api.WxMpService;
1010
import me.chanjar.weixin.mp.enums.AiLangType;
11-
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
1211
import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor;
1312

1413
import java.io.File;
@@ -34,7 +33,7 @@ public void uploadVoice(String voiceId, AiLangType lang, File voiceFile) throws
3433
}
3534

3635
this.wxMpService.execute(VoiceUploadRequestExecutor.create(this.wxMpService.getRequestHttp()),
37-
String.format(VOICE_UPLOAD_URL.getUrl(), "mp3", voiceId, lang.getCode()),
36+
String.format(VOICE_UPLOAD_URL.getUrl(this.wxMpService.getWxMpConfigStorage()), "mp3", voiceId, lang.getCode()),
3837
voiceFile);
3938
}
4039

@@ -46,7 +45,8 @@ public String recogniseVoice(String voiceId, AiLangType lang, File voiceFile) th
4645

4746
@Override
4847
public String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException {
49-
String response = this.wxMpService.post(String.format(TRANSLATE_URL.getUrl(), langFrom.getCode(), langTo.getCode()), content);
48+
String response = this.wxMpService.post(String.format(TRANSLATE_URL.getUrl(this.wxMpService.getWxMpConfigStorage()),
49+
langFrom.getCode(), langTo.getCode()), content);
5050

5151
WxError error = WxError.fromJson(response, WxType.MP);
5252
if (error.getErrorCode() != 0) {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
1414
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest;
1515
import me.chanjar.weixin.mp.bean.kefu.result.*;
16-
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
1716

1817
import java.io.File;
1918
import java.util.Date;
@@ -68,13 +67,14 @@ public boolean kfAccountInviteWorker(WxMpKfAccountRequest request) throws WxErro
6867
public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException {
6968
WxMediaUploadResult responseContent = this.wxMpService
7069
.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()),
71-
String.format(KFACCOUNT_UPLOAD_HEAD_IMG.getUrl(), kfAccount), imgFile);
70+
String.format(KFACCOUNT_UPLOAD_HEAD_IMG.getUrl(this.wxMpService.getWxMpConfigStorage()), kfAccount), imgFile);
7271
return responseContent != null;
7372
}
7473

7574
@Override
7675
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
77-
String responseContent = this.wxMpService.get(String.format(KFACCOUNT_DEL.getUrl(), kfAccount), null);
76+
String responseContent = this.wxMpService.get(String.format(KFACCOUNT_DEL.getUrl(this.wxMpService.getWxMpConfigStorage()),
77+
kfAccount), null);
7878
return responseContent != null;
7979
}
8080

@@ -94,13 +94,15 @@ public boolean kfSessionClose(String openid, String kfAccount) throws WxErrorExc
9494

9595
@Override
9696
public WxMpKfSessionGetResult kfSessionGet(String openid) throws WxErrorException {
97-
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION.getUrl(), openid), null);
97+
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION
98+
.getUrl(this.wxMpService.getWxMpConfigStorage()), openid), null);
9899
return WxMpKfSessionGetResult.fromJson(responseContent);
99100
}
100101

101102
@Override
102103
public WxMpKfSessionList kfSessionList(String kfAccount) throws WxErrorException {
103-
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION_LIST.getUrl(), kfAccount), null);
104+
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION_LIST
105+
.getUrl(this.wxMpService.getWxMpConfigStorage()), kfAccount), null);
104106
return WxMpKfSessionList.fromJson(responseContent);
105107
}
106108

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputS
5353

5454
@Override
5555
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
56-
String url = String.format(MEDIA_UPLOAD_URL.getUrl(), mediaType);
56+
String url = String.format(MEDIA_UPLOAD_URL.getUrl(this.wxMpService.getWxMpConfigStorage()), mediaType);
5757
return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file);
5858
}
5959

@@ -72,7 +72,7 @@ public WxMediaImgUploadResult mediaImgUpload(File file) throws WxErrorException
7272

7373
@Override
7474
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
75-
String url = String.format(MATERIAL_ADD_URL.getUrl(), mediaType);
75+
String url = String.format(MATERIAL_ADD_URL.getUrl(this.wxMpService.getWxMpConfigStorage()), mediaType);
7676
return this.wxMpService.execute(MaterialUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, material);
7777
}
7878

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public File qrCodePicture(WxMpQrCodeTicket ticket) throws WxErrorException {
105105
@Override
106106
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException {
107107
try {
108-
String resultUrl = String.format(SHOW_QRCODE_WITH_TICKET.getUrl(), URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
108+
String resultUrl = String.format(SHOW_QRCODE_WITH_TICKET.getUrl(this.wxMpService.getWxMpConfigStorage()),
109+
URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
109110
if (needShortUrl) {
110111
return this.wxMpService.shortUrl(resultUrl);
111112
}

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
99
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
1010
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
11-
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
1211
import org.apache.http.HttpHost;
1312
import org.apache.http.client.config.RequestConfig;
1413
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -19,7 +18,7 @@
1918
import java.io.IOException;
2019
import java.util.concurrent.locks.Lock;
2120

22-
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*;
21+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
2322

2423
/**
2524
* apache http client方式实现.
@@ -67,20 +66,20 @@ public void initHttp() {
6766

6867
@Override
6968
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
70-
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
71-
return this.getWxMpConfigStorage().getAccessToken();
69+
final WxMpConfigStorage config = this.getWxMpConfigStorage();
70+
if (!config.isAccessTokenExpired() && !forceRefresh) {
71+
return config.getAccessToken();
7272
}
7373

74-
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
74+
Lock lock = config.getAccessTokenLock();
7575
lock.lock();
7676
try {
77-
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(),
78-
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
77+
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
7978
try {
8079
HttpGet httpGet = new HttpGet(url);
8180
if (this.getRequestHttpProxy() != null) {
82-
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
83-
httpGet.setConfig(config);
81+
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
82+
httpGet.setConfig(requestConfig);
8483
}
8584
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
8685
String resultContent = new BasicResponseHandler().handleResponse(response);
@@ -89,8 +88,8 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
8988
throw new WxErrorException(error);
9089
}
9190
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
92-
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
93-
return this.getWxMpConfigStorage().getAccessToken();
91+
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
92+
return config.getAccessToken();
9493
} finally {
9594
httpGet.releaseConnection();
9695
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ public void initHttp() {
5151

5252
@Override
5353
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
54-
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
55-
return this.getWxMpConfigStorage().getAccessToken();
54+
final WxMpConfigStorage config = this.getWxMpConfigStorage();
55+
if (!config.isAccessTokenExpired() && !forceRefresh) {
56+
return config.getAccessToken();
5657
}
5758

58-
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
59+
Lock lock = config.getAccessTokenLock();
5960
lock.lock();
6061
try {
61-
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(),
62-
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
62+
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
6363

6464
HttpRequest request = HttpRequest.get(url);
6565

@@ -76,9 +76,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
7676
throw new WxErrorException(error);
7777
}
7878
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
79-
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
79+
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
8080

81-
return this.getWxMpConfigStorage().getAccessToken();
81+
return config.getAccessToken();
8282
} finally {
8383
lock.unlock();
8484
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
import me.chanjar.weixin.common.util.http.HttpType;
88
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
99
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
10-
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
1110
import okhttp3.*;
1211

1312
import java.io.IOException;
1413
import java.util.concurrent.locks.Lock;
1514

16-
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*;
15+
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
1716

1817
/**
1918
* okhttp实现.
@@ -41,15 +40,15 @@ public HttpType getRequestType() {
4140

4241
@Override
4342
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
44-
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
45-
return this.getWxMpConfigStorage().getAccessToken();
43+
final WxMpConfigStorage config = this.getWxMpConfigStorage();
44+
if (!config.isAccessTokenExpired() && !forceRefresh) {
45+
return config.getAccessToken();
4646
}
4747

48-
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
48+
Lock lock = config.getAccessTokenLock();
4949
lock.lock();
5050
try {
51-
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(),
52-
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
51+
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
5352

5453
Request request = new Request.Builder().url(url).get().build();
5554
Response response = getRequestHttpClient().newCall(request).execute();
@@ -59,9 +58,9 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
5958
throw new WxErrorException(error);
6059
}
6160
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
62-
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
61+
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
6362

64-
return this.getWxMpConfigStorage().getAccessToken();
63+
return config.getAccessToken();
6564
} catch (IOException e) {
6665
throw new RuntimeException(e);
6766
} finally {

0 commit comments

Comments
 (0)