Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,9 @@ private <R, T> R executeInternal(
}
String accessToken = getAccessToken(false);

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

String uriWithAccessToken =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ public HttpClientType getRequestType() {

@Override
protected String doGetAccessTokenRequest() throws IOException {

String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
WxMaService.GET_ACCESS_TOKEN_URL;

this.getWxMaConfig().getAccessTokenUrl() :
WxMaService.GET_ACCESS_TOKEN_URL.replace(
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());

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

Expand All @@ -84,9 +82,9 @@ protected String doGetAccessTokenRequest() throws IOException {
@Override
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
GET_STABLE_ACCESS_TOKEN;
this.getWxMaConfig().getAccessTokenUrl() :
GET_STABLE_ACCESS_TOKEN.replace(
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());

HttpPost httpPost = new HttpPost(url);
if (this.getRequestHttpProxy() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public HttpClientType getRequestType() {
@Override
protected String doGetAccessTokenRequest() throws IOException {
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
WxMaService.GET_ACCESS_TOKEN_URL;
this.getWxMaConfig().getAccessTokenUrl() :
WxMaService.GET_ACCESS_TOKEN_URL.replace(
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());

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

@Override
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {

String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
GET_STABLE_ACCESS_TOKEN;
this.getWxMaConfig().getAccessTokenUrl() :
GET_STABLE_ACCESS_TOKEN.replace(
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());

WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public HttpClientType getRequestType() {
@Override
protected String doGetAccessTokenRequest() throws IOException {
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
WxMaService.GET_ACCESS_TOKEN_URL;
this.getWxMaConfig().getAccessTokenUrl() :
WxMaService.GET_ACCESS_TOKEN_URL.replace(
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());

url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
Request request = new Request.Builder().url(url).get().build();
Expand All @@ -79,9 +79,10 @@ protected String doGetAccessTokenRequest() throws IOException {
@Override
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
GET_STABLE_ACCESS_TOKEN;
this.getWxMaConfig().getAccessTokenUrl() :
GET_STABLE_ACCESS_TOKEN.replace(
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());

WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
wxMaAccessTokenRequest.setSecret(this.getWxMaConfig().getSecret());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,50 @@ default void updateAccessTokenBefore(WxAccessTokenEntity wxAccessTokenEntity) {}

/** 密钥对应的小程序 ID(普通小程序为 appId,托管第三方平台为 componentAppId) */
String getWechatMpAppid();

/** 微信 API 默认主机地址 */
String DEFAULT_API_HOST_URL = "https://api.weixin.qq.com";
/** 微信云托管使用的 HTTP 协议主机地址 */
String CLOUD_RUN_API_HOST_URL = "http://api.weixin.qq.com";

/**
* 是否使用微信云托管内网模式
* 当部署在微信云托管环境时,api.weixin.qq.com 会被解析为内网地址,此时需要使用 HTTP 协议访问
* 开启此配置后,SDK 会自动将 https://api.weixin.qq.com 替换为 http://api.weixin.qq.com
*
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloudservice/wxcloudrun/src/guide/weixin/open.html">微信云托管内网调用微信接口</a>
* @return 是否使用微信云托管模式
*/
default boolean isUseWxCloudRun() {
return false;
}

/**
* 设置是否使用微信云托管内网模式
* 当部署在微信云托管环境时,api.weixin.qq.com 会被解析为内网地址,此时需要使用 HTTP 协议访问
* 开启此配置后,SDK 会自动将 https://api.weixin.qq.com 替换为 http://api.weixin.qq.com
*
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/wxcloudservice/wxcloudrun/src/guide/weixin/open.html">微信云托管内网调用微信接口</a>
* @param useWxCloudRun 是否使用微信云托管模式
*/
default void setUseWxCloudRun(boolean useWxCloudRun) {
// 默认空实现
}

/**
* 根据配置获取实际应使用的 API 主机地址
* 优先级:自定义 apiHostUrl > 微信云托管模式 > 默认 HTTPS 地址
*
* @return 实际应使用的 API 主机地址
*/
default String getEffectiveApiHostUrl() {
String apiHostUrl = getApiHostUrl();
if (apiHostUrl != null && !apiHostUrl.isEmpty()) {
return apiHostUrl;
}
if (isUseWxCloudRun()) {
return CLOUD_RUN_API_HOST_URL;
}
return DEFAULT_API_HOST_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
private String apiHostUrl;
private String accessTokenUrl;

/** 是否使用微信云托管模式(使用 HTTP 协议访问内网地址) */
@Getter(AccessLevel.NONE)
private boolean useWxCloudRun = false;

/** 自定义配置token的消费者 */
@Setter private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;

Expand Down Expand Up @@ -388,6 +392,16 @@ public void setAccessTokenUrl(String accessTokenUrl) {
this.accessTokenUrl = accessTokenUrl;
}

@Override
public boolean isUseWxCloudRun() {
return this.useWxCloudRun;
}

@Override
public void setUseWxCloudRun(boolean useWxCloudRun) {
this.useWxCloudRun = useWxCloudRun;
}

@Override
public String getAppid() {
return appid;
Expand Down