mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
#1039 微信公众号接口地址域名部分进行可配置化 改造
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
|
||||
import me.chanjar.weixin.mp.enums.TicketType;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
/**
|
||||
* 微信客户端配置存储.
|
||||
*
|
||||
@@ -96,4 +97,8 @@ public interface WxMpConfigStorage {
|
||||
*/
|
||||
boolean autoRefreshToken();
|
||||
|
||||
/**
|
||||
* 得到微信接口地址域名部分的自定义设置信息.
|
||||
*/
|
||||
WxMpHostConfig getHostConfig();
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
|
||||
import me.chanjar.weixin.mp.enums.TicketType;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
@@ -179,4 +180,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage, Serializabl
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpHostConfig getHostConfig() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -157,13 +157,13 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
@Override
|
||||
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
|
||||
return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(),
|
||||
return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(this.getWxMpConfigStorage()),
|
||||
this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildQrConnectUrl(String redirectURI, String scope, String state) {
|
||||
return String.format(QRCONNECT_URL.getUrl(), this.getWxMpConfigStorage().getAppId(),
|
||||
return String.format(QRCONNECT_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(),
|
||||
URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state));
|
||||
}
|
||||
|
||||
@@ -179,14 +179,14 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
@Override
|
||||
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
|
||||
String url = String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(), this.getWxMpConfigStorage().getAppId(),
|
||||
String url = String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(),
|
||||
this.getWxMpConfigStorage().getSecret(), code);
|
||||
return this.getOAuth2AccessToken(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
|
||||
String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(), this.getWxMpConfigStorage().getAppId(), refreshToken);
|
||||
String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), this.getWxMpConfigStorage().getAppId(), refreshToken);
|
||||
return this.getOAuth2AccessToken(url);
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
lang = "zh_CN";
|
||||
}
|
||||
|
||||
String url = String.format(OAUTH2_USERINFO_URL.getUrl(), token.getAccessToken(), token.getOpenId(), lang);
|
||||
String url = String.format(OAUTH2_USERINFO_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId(), lang);
|
||||
|
||||
try {
|
||||
RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this);
|
||||
@@ -209,7 +209,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
@Override
|
||||
public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken token) {
|
||||
String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(), token.getAccessToken(), token.getOpenId());
|
||||
String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(this.getWxMpConfigStorage()), token.getAccessToken(), token.getOpenId());
|
||||
|
||||
try {
|
||||
SimpleGetRequestExecutor.create(this).execute(url, null);
|
||||
@@ -252,7 +252,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
@Override
|
||||
public String get(WxMpApiUrl url, String queryParam) throws WxErrorException {
|
||||
return this.get(url.getUrl(), queryParam);
|
||||
return this.get(url.getUrl(this.getWxMpConfigStorage()), queryParam);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -262,12 +262,12 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
|
||||
@Override
|
||||
public String post(WxMpApiUrl url, String postData) throws WxErrorException {
|
||||
return this.post(url.getUrl(), postData);
|
||||
return this.post(url.getUrl(this.getWxMpConfigStorage()), postData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, E> T execute(RequestExecutor<T, E> executor, WxMpApiUrl url, E data) throws WxErrorException {
|
||||
return this.execute(executor, url.getUrl(), data);
|
||||
return this.execute(executor, url.getUrl(this.getWxMpConfigStorage()), data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpAiOpenService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.enums.AiLangType;
|
||||
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor;
|
||||
|
||||
import java.io.File;
|
||||
@@ -34,7 +33,7 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
|
||||
}
|
||||
|
||||
this.wxMpService.execute(VoiceUploadRequestExecutor.create(this.wxMpService.getRequestHttp()),
|
||||
String.format(VOICE_UPLOAD_URL.getUrl(), "mp3", voiceId, lang.getCode()),
|
||||
String.format(VOICE_UPLOAD_URL.getUrl(this.wxMpService.getWxMpConfigStorage()), "mp3", voiceId, lang.getCode()),
|
||||
voiceFile);
|
||||
}
|
||||
|
||||
@@ -46,7 +45,8 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService {
|
||||
|
||||
@Override
|
||||
public String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException {
|
||||
String response = this.wxMpService.post(String.format(TRANSLATE_URL.getUrl(), langFrom.getCode(), langTo.getCode()), content);
|
||||
String response = this.wxMpService.post(String.format(TRANSLATE_URL.getUrl(this.wxMpService.getWxMpConfigStorage()),
|
||||
langFrom.getCode(), langTo.getCode()), content);
|
||||
|
||||
WxError error = WxError.fromJson(response, WxType.MP);
|
||||
if (error.getErrorCode() != 0) {
|
||||
|
||||
@@ -13,7 +13,6 @@ import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
||||
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
|
||||
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest;
|
||||
import me.chanjar.weixin.mp.bean.kefu.result.*;
|
||||
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
@@ -68,13 +67,14 @@ public class WxMpKefuServiceImpl implements WxMpKefuService {
|
||||
public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException {
|
||||
WxMediaUploadResult responseContent = this.wxMpService
|
||||
.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()),
|
||||
String.format(KFACCOUNT_UPLOAD_HEAD_IMG.getUrl(), kfAccount), imgFile);
|
||||
String.format(KFACCOUNT_UPLOAD_HEAD_IMG.getUrl(this.wxMpService.getWxMpConfigStorage()), kfAccount), imgFile);
|
||||
return responseContent != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean kfAccountDel(String kfAccount) throws WxErrorException {
|
||||
String responseContent = this.wxMpService.get(String.format(KFACCOUNT_DEL.getUrl(), kfAccount), null);
|
||||
String responseContent = this.wxMpService.get(String.format(KFACCOUNT_DEL.getUrl(this.wxMpService.getWxMpConfigStorage()),
|
||||
kfAccount), null);
|
||||
return responseContent != null;
|
||||
}
|
||||
|
||||
@@ -94,13 +94,15 @@ public class WxMpKefuServiceImpl implements WxMpKefuService {
|
||||
|
||||
@Override
|
||||
public WxMpKfSessionGetResult kfSessionGet(String openid) throws WxErrorException {
|
||||
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION.getUrl(), openid), null);
|
||||
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION
|
||||
.getUrl(this.wxMpService.getWxMpConfigStorage()), openid), null);
|
||||
return WxMpKfSessionGetResult.fromJson(responseContent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpKfSessionList kfSessionList(String kfAccount) throws WxErrorException {
|
||||
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION_LIST.getUrl(), kfAccount), null);
|
||||
String responseContent = this.wxMpService.get(String.format(KFSESSION_GET_SESSION_LIST
|
||||
.getUrl(this.wxMpService.getWxMpConfigStorage()), kfAccount), null);
|
||||
return WxMpKfSessionList.fromJson(responseContent);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException {
|
||||
String url = String.format(MEDIA_UPLOAD_URL.getUrl(), mediaType);
|
||||
String url = String.format(MEDIA_UPLOAD_URL.getUrl(this.wxMpService.getWxMpConfigStorage()), mediaType);
|
||||
return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService {
|
||||
|
||||
@Override
|
||||
public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException {
|
||||
String url = String.format(MATERIAL_ADD_URL.getUrl(), mediaType);
|
||||
String url = String.format(MATERIAL_ADD_URL.getUrl(this.wxMpService.getWxMpConfigStorage()), mediaType);
|
||||
return this.wxMpService.execute(MaterialUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, material);
|
||||
}
|
||||
|
||||
|
||||
@@ -105,7 +105,8 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService {
|
||||
@Override
|
||||
public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException {
|
||||
try {
|
||||
String resultUrl = String.format(SHOW_QRCODE_WITH_TICKET.getUrl(), URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
|
||||
String resultUrl = String.format(SHOW_QRCODE_WITH_TICKET.getUrl(this.wxMpService.getWxMpConfigStorage()),
|
||||
URLEncoder.encode(ticket, StandardCharsets.UTF_8.name()));
|
||||
if (needShortUrl) {
|
||||
return this.wxMpService.shortUrl(resultUrl);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.HttpType;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
@@ -19,7 +18,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
|
||||
|
||||
/**
|
||||
* apache http client方式实现.
|
||||
@@ -67,20 +66,20 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
|
||||
return this.getWxMpConfigStorage().getAccessToken();
|
||||
final WxMpConfigStorage config = this.getWxMpConfigStorage();
|
||||
if (!config.isAccessTokenExpired() && !forceRefresh) {
|
||||
return config.getAccessToken();
|
||||
}
|
||||
|
||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||
Lock lock = config.getAccessTokenLock();
|
||||
lock.lock();
|
||||
try {
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(),
|
||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
|
||||
try {
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
if (this.getRequestHttpProxy() != null) {
|
||||
RequestConfig config = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(config);
|
||||
RequestConfig requestConfig = RequestConfig.custom().setProxy(this.getRequestHttpProxy()).build();
|
||||
httpGet.setConfig(requestConfig);
|
||||
}
|
||||
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
|
||||
String resultContent = new BasicResponseHandler().handleResponse(response);
|
||||
@@ -89,8 +88,8 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
return this.getWxMpConfigStorage().getAccessToken();
|
||||
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
return config.getAccessToken();
|
||||
} finally {
|
||||
httpGet.releaseConnection();
|
||||
}
|
||||
|
||||
@@ -51,15 +51,15 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
|
||||
return this.getWxMpConfigStorage().getAccessToken();
|
||||
final WxMpConfigStorage config = this.getWxMpConfigStorage();
|
||||
if (!config.isAccessTokenExpired() && !forceRefresh) {
|
||||
return config.getAccessToken();
|
||||
}
|
||||
|
||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||
Lock lock = config.getAccessTokenLock();
|
||||
lock.lock();
|
||||
try {
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(),
|
||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
|
||||
|
||||
HttpRequest request = HttpRequest.get(url);
|
||||
|
||||
@@ -76,9 +76,9 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
|
||||
return this.getWxMpConfigStorage().getAccessToken();
|
||||
return config.getAccessToken();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
@@ -7,13 +7,12 @@ import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.HttpType;
|
||||
import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo;
|
||||
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*;
|
||||
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.GET_ACCESS_TOKEN_URL;
|
||||
|
||||
/**
|
||||
* okhttp实现.
|
||||
@@ -41,15 +40,15 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH
|
||||
|
||||
@Override
|
||||
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
|
||||
if (!this.getWxMpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
|
||||
return this.getWxMpConfigStorage().getAccessToken();
|
||||
final WxMpConfigStorage config = this.getWxMpConfigStorage();
|
||||
if (!config.isAccessTokenExpired() && !forceRefresh) {
|
||||
return config.getAccessToken();
|
||||
}
|
||||
|
||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||
Lock lock = config.getAccessTokenLock();
|
||||
lock.lock();
|
||||
try {
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(),
|
||||
this.getWxMpConfigStorage().getAppId(), this.getWxMpConfigStorage().getSecret());
|
||||
String url = String.format(GET_ACCESS_TOKEN_URL.getUrl(config), config.getAppId(), config.getSecret());
|
||||
|
||||
Request request = new Request.Builder().url(url).get().build();
|
||||
Response response = getRequestHttpClient().newCall(request).execute();
|
||||
@@ -59,9 +58,9 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH
|
||||
throw new WxErrorException(error);
|
||||
}
|
||||
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
|
||||
this.getWxMpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
config.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
|
||||
|
||||
return this.getWxMpConfigStorage().getAccessToken();
|
||||
return config.getAccessToken();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
|
||||
@@ -24,7 +24,7 @@ public class WxMpSubscribeMsgServiceImpl implements WxMpSubscribeMsgService {
|
||||
@Override
|
||||
public String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved) {
|
||||
WxMpConfigStorage storage = this.wxMpService.getWxMpConfigStorage();
|
||||
return String.format(SUBSCRIBE_MESSAGE_AUTHORIZE_URL.getUrl(), storage.getAppId(), scene, storage.getTemplateId(),
|
||||
return String.format(SUBSCRIBE_MESSAGE_AUTHORIZE_URL.getUrl(storage), storage.getAppId(), scene, storage.getTemplateId(),
|
||||
URIUtil.encodeURIComponent(redirectURI), reserved);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user