🎨 #3570 【小程序】添加微信云托管支持
This commit is contained in:
@@ -427,8 +427,9 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
|||||||
}
|
}
|
||||||
String accessToken = getAccessToken(false);
|
String accessToken = getAccessToken(false);
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl())) {
|
String effectiveApiHostUrl = this.getWxMaConfig().getEffectiveApiHostUrl();
|
||||||
uri = uri.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl());
|
if (!WxMaConfig.DEFAULT_API_HOST_URL.equals(effectiveApiHostUrl)) {
|
||||||
|
uri = uri.replace(WxMaConfig.DEFAULT_API_HOST_URL, effectiveApiHostUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
String uriWithAccessToken =
|
String uriWithAccessToken =
|
||||||
|
|||||||
@@ -64,12 +64,10 @@ public class WxMaServiceHttpClientImpl extends BaseWxMaServiceImpl {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doGetAccessTokenRequest() throws IOException {
|
protected String doGetAccessTokenRequest() throws IOException {
|
||||||
|
|
||||||
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
||||||
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
|
this.getWxMaConfig().getAccessTokenUrl() :
|
||||||
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
|
WxMaService.GET_ACCESS_TOKEN_URL.replace(
|
||||||
WxMaService.GET_ACCESS_TOKEN_URL;
|
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
|
||||||
|
|
||||||
|
|
||||||
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
|
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
|
||||||
|
|
||||||
@@ -84,9 +82,9 @@ public class WxMaServiceHttpClientImpl extends BaseWxMaServiceImpl {
|
|||||||
@Override
|
@Override
|
||||||
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
||||||
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
||||||
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
|
this.getWxMaConfig().getAccessTokenUrl() :
|
||||||
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
|
GET_STABLE_ACCESS_TOKEN.replace(
|
||||||
GET_STABLE_ACCESS_TOKEN;
|
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
|
||||||
|
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
if (this.getRequestHttpProxy() != null) {
|
if (this.getRequestHttpProxy() != null) {
|
||||||
|
|||||||
@@ -50,9 +50,9 @@ public class WxMaServiceJoddHttpImpl extends BaseWxMaServiceImpl<HttpConnectionP
|
|||||||
@Override
|
@Override
|
||||||
protected String doGetAccessTokenRequest() throws IOException {
|
protected String doGetAccessTokenRequest() throws IOException {
|
||||||
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
||||||
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
|
this.getWxMaConfig().getAccessTokenUrl() :
|
||||||
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
|
WxMaService.GET_ACCESS_TOKEN_URL.replace(
|
||||||
WxMaService.GET_ACCESS_TOKEN_URL;
|
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
|
||||||
|
|
||||||
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
|
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
|
||||||
HttpRequest request = HttpRequest.get(url);
|
HttpRequest request = HttpRequest.get(url);
|
||||||
@@ -67,11 +67,10 @@ public class WxMaServiceJoddHttpImpl extends BaseWxMaServiceImpl<HttpConnectionP
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
||||||
|
|
||||||
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
||||||
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
|
this.getWxMaConfig().getAccessTokenUrl() :
|
||||||
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
|
GET_STABLE_ACCESS_TOKEN.replace(
|
||||||
GET_STABLE_ACCESS_TOKEN;
|
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
|
||||||
|
|
||||||
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
|
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
|
||||||
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
|
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
|
||||||
|
|||||||
@@ -65,9 +65,9 @@ public class WxMaServiceOkHttpImpl extends BaseWxMaServiceImpl<OkHttpClient, OkH
|
|||||||
@Override
|
@Override
|
||||||
protected String doGetAccessTokenRequest() throws IOException {
|
protected String doGetAccessTokenRequest() throws IOException {
|
||||||
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
||||||
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
|
this.getWxMaConfig().getAccessTokenUrl() :
|
||||||
WxMaService.GET_ACCESS_TOKEN_URL.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
|
WxMaService.GET_ACCESS_TOKEN_URL.replace(
|
||||||
WxMaService.GET_ACCESS_TOKEN_URL;
|
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
|
||||||
|
|
||||||
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
|
url = String.format(url, this.getWxMaConfig().getAppid(), this.getWxMaConfig().getSecret());
|
||||||
Request request = new Request.Builder().url(url).get().build();
|
Request request = new Request.Builder().url(url).get().build();
|
||||||
@@ -79,9 +79,10 @@ public class WxMaServiceOkHttpImpl extends BaseWxMaServiceImpl<OkHttpClient, OkH
|
|||||||
@Override
|
@Override
|
||||||
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
protected String doGetStableAccessTokenRequest(boolean forceRefresh) throws IOException {
|
||||||
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
String url = StringUtils.isNotEmpty(this.getWxMaConfig().getAccessTokenUrl()) ?
|
||||||
this.getWxMaConfig().getAccessTokenUrl() : StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl()) ?
|
this.getWxMaConfig().getAccessTokenUrl() :
|
||||||
GET_STABLE_ACCESS_TOKEN.replace("https://api.weixin.qq.com", this.getWxMaConfig().getApiHostUrl()) :
|
GET_STABLE_ACCESS_TOKEN.replace(
|
||||||
GET_STABLE_ACCESS_TOKEN;
|
WxMaConfig.DEFAULT_API_HOST_URL, this.getWxMaConfig().getEffectiveApiHostUrl());
|
||||||
|
|
||||||
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
|
WxMaStableAccessTokenRequest wxMaAccessTokenRequest = new WxMaStableAccessTokenRequest();
|
||||||
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
|
wxMaAccessTokenRequest.setAppid(this.getWxMaConfig().getAppid());
|
||||||
wxMaAccessTokenRequest.setSecret(this.getWxMaConfig().getSecret());
|
wxMaAccessTokenRequest.setSecret(this.getWxMaConfig().getSecret());
|
||||||
|
|||||||
@@ -319,4 +319,50 @@ public interface WxMaConfig {
|
|||||||
|
|
||||||
/** 密钥对应的小程序 ID(普通小程序为 appId,托管第三方平台为 componentAppId) */
|
/** 密钥对应的小程序 ID(普通小程序为 appId,托管第三方平台为 componentAppId) */
|
||||||
String getWechatMpAppid();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
|||||||
private String apiHostUrl;
|
private String apiHostUrl;
|
||||||
private String accessTokenUrl;
|
private String accessTokenUrl;
|
||||||
|
|
||||||
|
/** 是否使用微信云托管模式(使用 HTTP 协议访问内网地址) */
|
||||||
|
@Getter(AccessLevel.NONE)
|
||||||
|
private boolean useWxCloudRun = false;
|
||||||
|
|
||||||
/** 自定义配置token的消费者 */
|
/** 自定义配置token的消费者 */
|
||||||
@Setter private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
|
@Setter private Consumer<WxAccessTokenEntity> updateAccessTokenBefore;
|
||||||
|
|
||||||
@@ -388,6 +392,16 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
|
|||||||
this.accessTokenUrl = accessTokenUrl;
|
this.accessTokenUrl = accessTokenUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isUseWxCloudRun() {
|
||||||
|
return this.useWxCloudRun;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUseWxCloudRun(boolean useWxCloudRun) {
|
||||||
|
this.useWxCloudRun = useWxCloudRun;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getAppid() {
|
public String getAppid() {
|
||||||
return appid;
|
return appid;
|
||||||
|
|||||||
Reference in New Issue
Block a user