Skip to content

Commit 155f3f7

Browse files
authored
🎨 #3570 【小程序】添加微信云托管支持
1 parent 26f5887 commit 155f3f7

File tree

6 files changed

+82
-23
lines changed

6 files changed

+82
-23
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ private <R, T> R executeInternal(
427427
}
428428
String accessToken = getAccessToken(false);
429429

430-
if (StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl())) {
431-
uri = uri.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl());
430+
String effectiveApiHostUrl = this.getWxMaConfig().getEffectiveApiHostUrl();
431+
if (!WxMaConfig.DEFAULT_API_HOST_URL.equals(effectiveApiHostUrl)) {
432+
uri = uri.replace(WxMaConfig.DEFAULT_API_HOST_URL, effectiveApiHostUrl);
432433
}
433434

434435
String uriWithAccessToken =

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceHttpClientImpl.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ public HttpClientType getRequestType() {
6464

6565
@Override
6666
protected String doGetAccessTokenRequest() throws IOException {
67-
6867
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
69-
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
70-
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
71-
WxMaService.GET_ACCESS_TOKEN_URL;
72-
68+
this.getWxMaConfig().getAccessTokenUrl() :
69+
WxMaService.GET_ACCESS_TOKEN_URL.replace(
70+
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
7371

7472
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
7573

@@ -84,9 +82,9 @@ protected String doGetAccessTokenRequest() throws IOException {
8482
@Override
8583
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
8684
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
87-
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
88-
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
89-
GET_STABLE_ACCESS_TOKEN;
85+
this.getWxMaConfig().getAccessTokenUrl() :
86+
GET_STABLE_ACCESS_TOKEN.replace(
87+
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
9088

9189
HttpPost httpPost = new HttpPost(url);
9290
if (this.getRequestHttpProxy() != null) {

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceJoddHttpImpl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public HttpClientType getRequestType() {
5050
@Override
5151
protected String doGetAccessTokenRequest() throws IOException {
5252
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
53-
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
54-
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
55-
WxMaService.GET_ACCESS_TOKEN_URL;
53+
this.getWxMaConfig().getAccessTokenUrl() :
54+
WxMaService.GET_ACCESS_TOKEN_URL.replace(
55+
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
5656

5757
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
5858
HttpRequest request = HttpRequest.get(url);
@@ -67,11 +67,10 @@ protected String doGetAccessTokenRequest() throws IOException {
6767

6868
@Override
6969
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
70-
7170
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
72-
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
73-
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
74-
GET_STABLE_ACCESS_TOKEN;
71+
this.getWxMaConfig().getAccessTokenUrl() :
72+
GET_STABLE_ACCESS_TOKEN.replace(
73+
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
7574

7675
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
7776
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceOkHttpImpl.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public HttpClientType getRequestType() {
6565
@Override
6666
protected String doGetAccessTokenRequest() throws IOException {
6767
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
68-
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
69-
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
70-
WxMaService.GET_ACCESS_TOKEN_URL;
68+
this.getWxMaConfig().getAccessTokenUrl() :
69+
WxMaService.GET_ACCESS_TOKEN_URL.replace(
70+
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
7171

7272
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
7373
Request request = new Request.Builder().url(url).get().build();
@@ -79,9 +79,10 @@ protected String doGetAccessTokenRequest() throws IOException {
7979
@Override
8080
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
8181
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
82-
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
83-
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
84-
GET_STABLE_ACCESS_TOKEN;
82+
this.getWxMaConfig().getAccessTokenUrl() :
83+
GET_STABLE_ACCESS_TOKEN.replace(
84+
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
85+
8586
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
8687
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
8788
wxMaAccessTokenRequest.setSecret(this.getWxMaConfig().getSecret());

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/WxMaConfig.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,50 @@ default void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {}
319319

320320
/** 密钥对应的小程序 ID(普通小程序为 appId,托管第三方平台为 componentAppId) */
321321
String getWechatMpAppid();
322+
323+
/** 微信 API 默认主机地址 */
324+
String DEFAULT_API_HOST_URL = "https://api.weixin.qq.com";
325+
/** 微信云托管使用的 HTTP 协议主机地址 */
326+
String CLOUD_RUN_API_HOST_URL = "http://api.weixin.qq.com";
327+
328+
/**
329+
* 是否使用微信云托管内网模式
330+
* 当部署在微信云托管环境时,api.weixin.qq.com 会被解析为内网地址,此时需要使用 HTTP 协议访问
331+
* 开启此配置后,SDK 会自动将 https://api.weixin.qq.com 替换为 http://api.weixin.qq.com
332+
*
333+
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloudservice/wxcloudrun/src/guide/weixin/open.html">微信云托管内网调用微信接口</a>
334+
* @return 是否使用微信云托管模式
335+
*/
336+
default boolean isUseWxCloudRun() {
337+
return false;
338+
}
339+
340+
/**
341+
* 设置是否使用微信云托管内网模式
342+
* 当部署在微信云托管环境时,api.weixin.qq.com 会被解析为内网地址,此时需要使用 HTTP 协议访问
343+
* 开启此配置后,SDK 会自动将 https://api.weixin.qq.com 替换为 http://api.weixin.qq.com
344+
*
345+
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloudservice/wxcloudrun/src/guide/weixin/open.html">微信云托管内网调用微信接口</a>
346+
* @param useWxCloudRun 是否使用微信云托管模式
347+
*/
348+
default void setUseWxCloudRun(boolean useWxCloudRun) {
349+
// 默认空实现
350+
}
351+
352+
/**
353+
* 根据配置获取实际应使用的 API 主机地址
354+
* 优先级:自定义 apiHostUrl > 微信云托管模式 > 默认 HTTPS 地址
355+
*
356+
* @return 实际应使用的 API 主机地址
357+
*/
358+
default String getEffectiveApiHostUrl() {
359+
String apiHostUrl = getApiHostUrl();
360+
if (apiHostUrl != null && !apiHostUrl.isEmpty()) {
361+
return apiHostUrl;
362+
}
363+
if (isUseWxCloudRun()) {
364+
return CLOUD_RUN_API_HOST_URL;
365+
}
366+
return DEFAULT_API_HOST_URL;
367+
}
322368
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/config/impl/WxMaDefaultConfigImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
6969
private String apiHostUrl;
7070
private String accessTokenUrl;
7171

72+
/** 是否使用微信云托管模式(使用 HTTP 协议访问内网地址) */
73+
@Getter(AccessLevel.NONE)
74+
private boolean useWxCloudRun = false;
75+
7276
/** 自定义配置token的消费者 */
7377
@Setter private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
7478

@@ -388,6 +392,16 @@ public void setAccessTokenUrl(String accessTokenUrl) {
388392
this.accessTokenUrl = accessTokenUrl;
389393
}
390394

395+
@Override
396+
public boolean isUseWxCloudRun() {
397+
return this.useWxCloudRun;
398+
}
399+
400+
@Override
401+
public void setUseWxCloudRun(boolean useWxCloudRun) {
402+
this.useWxCloudRun = useWxCloudRun;
403+
}
404+
391405
@Override
392406
public String getAppid() {
393407
return appid;

0 commit comments

Comments
 (0)