Skip to content

Commit

Permalink
🎨 #1897 【小程序】接口请求地址的域名增加自定义设置的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Jan 27, 2021
1 parent 584c7ac commit 87431f3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package cn.binarywang.wx.miniapp.api;

import cn.binarywang.wx.miniapp.bean.*;
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.bean.WxMaUpdatableMsg;
import com.google.gson.JsonObject;
import me.chanjar.weixin.common.error.WxErrorException;

/**
* <pre>
* 消息发送接口
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E d
}
String accessToken = getAccessToken(false);

if(StringUtils.isNotEmpty(this.getWxMaConfig().getApiHostUrl())){
uri = uri.replace("https://api.weixin.qq.com",this.getWxMaConfig().getApiHostUrl() );
}

String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static WxMaPubTemplateTitleListResult fromJson(String json) {
}

@Data
public static class TemplateItem {
public static class TemplateItem implements Serializable {
private static final long serialVersionUID = 6888726696879905332L;

private Integer type;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cn.binarywang.wx.miniapp.config;

import java.util.concurrent.locks.Lock;

import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

import java.util.concurrent.locks.Lock;

/**
* 小程序配置
*
Expand Down Expand Up @@ -219,4 +219,19 @@ public interface WxMaConfig {
*/
boolean autoRefreshToken();

/**
* 设置自定义的apiHost地址
* 具体取值,可以参考https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
*
* @param apiHostUrl api域名地址
*/
void setApiHostUrl(String apiHostUrl);

/**
* 获取自定义的apiHost地址,用于替换原请求中的https://api.weixin.qq.com
* 具体取值,可以参考https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Interface_field_description.html
*
* @return 自定义的api域名地址
*/
String getApiHostUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import lombok.Getter;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;

Expand All @@ -14,6 +15,7 @@
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Getter
public class WxMaDefaultConfigImpl implements WxMaConfig {
protected volatile String appid;
protected volatile String token;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class WxMaDefaultConfigImpl implements WxMaConfig {
protected volatile Lock jsapiTicketLock = new ReentrantLock();
protected volatile Lock cardApiTicketLock = new ReentrantLock();
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
private String apiHostUrl;

/**
* 会过期的数据提前过期时间,默认预留200秒的时间
Expand Down Expand Up @@ -124,7 +127,6 @@ public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
this.jsapiTicketExpiresTime = expiresAheadInMillis(expiresInSeconds);
}


@Override
public String getCardApiTicket() {
return this.cardApiTicket;
Expand Down Expand Up @@ -274,6 +276,11 @@ public boolean autoRefreshToken() {
return true;
}

@Override
public void setApiHostUrl(String apiHostUrl) {
this.apiHostUrl = apiHostUrl;
}

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

0 comments on commit 87431f3

Please sign in to comment.