| @@ -1,12 +1,13 @@ | |||||
| package me.chanjar.weixin.mp.api; | 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.bean.WxAccessToken; | ||||
| import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | 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.enums.TicketType; | ||||
| import java.io.File; | |||||
| import java.util.concurrent.locks.Lock; | |||||
| /** | /** | ||||
| * 微信客户端配置存储. | * 微信客户端配置存储. | ||||
| * | * | ||||
| @@ -96,4 +97,8 @@ public interface WxMpConfigStorage { | |||||
| */ | */ | ||||
| boolean autoRefreshToken(); | boolean autoRefreshToken(); | ||||
| /** | |||||
| * 得到微信接口地址域名部分的自定义设置信息. | |||||
| */ | |||||
| WxMpHostConfig getHostConfig(); | |||||
| } | } | ||||
| @@ -8,6 +8,7 @@ import java.util.concurrent.locks.ReentrantLock; | |||||
| import lombok.Data; | import lombok.Data; | ||||
| import me.chanjar.weixin.common.bean.WxAccessToken; | import me.chanjar.weixin.common.bean.WxAccessToken; | ||||
| import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | 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.enums.TicketType; | ||||
| import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | ||||
| @@ -179,4 +180,9 @@ public class WxMpInMemoryConfigStorage implements WxMpConfigStorage, Serializabl | |||||
| return true; | return true; | ||||
| } | } | ||||
| @Override | |||||
| public WxMpHostConfig getHostConfig() { | |||||
| return null; | |||||
| } | |||||
| } | } | ||||
| @@ -157,13 +157,13 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH | |||||
| @Override | @Override | ||||
| public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { | 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)); | this.getWxMpConfigStorage().getAppId(), URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state)); | ||||
| } | } | ||||
| @Override | @Override | ||||
| public String buildQrConnectUrl(String redirectURI, String scope, String state) { | 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)); | URIUtil.encodeURIComponent(redirectURI), scope, StringUtils.trimToEmpty(state)); | ||||
| } | } | ||||
| @@ -179,14 +179,14 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH | |||||
| @Override | @Override | ||||
| public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { | 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); | this.getWxMpConfigStorage().getSecret(), code); | ||||
| return this.getOAuth2AccessToken(url); | return this.getOAuth2AccessToken(url); | ||||
| } | } | ||||
| @Override | @Override | ||||
| public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { | 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); | return this.getOAuth2AccessToken(url); | ||||
| } | } | ||||
| @@ -196,7 +196,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH | |||||
| lang = "zh_CN"; | 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 { | try { | ||||
| RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this); | RequestExecutor<String, String> executor = SimpleGetRequestExecutor.create(this); | ||||
| @@ -209,7 +209,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH | |||||
| @Override | @Override | ||||
| public boolean oauth2validateAccessToken(WxMpOAuth2AccessToken token) { | 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 { | try { | ||||
| SimpleGetRequestExecutor.create(this).execute(url, null); | SimpleGetRequestExecutor.create(this).execute(url, null); | ||||
| @@ -252,7 +252,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH | |||||
| @Override | @Override | ||||
| public String get(WxMpApiUrl url, String queryParam) throws WxErrorException { | public String get(WxMpApiUrl url, String queryParam) throws WxErrorException { | ||||
| return this.get(url.getUrl(), queryParam); | |||||
| return this.get(url.getUrl(this.getWxMpConfigStorage()), queryParam); | |||||
| } | } | ||||
| @Override | @Override | ||||
| @@ -262,12 +262,12 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH | |||||
| @Override | @Override | ||||
| public String post(WxMpApiUrl url, String postData) throws WxErrorException { | public String post(WxMpApiUrl url, String postData) throws WxErrorException { | ||||
| return this.post(url.getUrl(), postData); | |||||
| return this.post(url.getUrl(this.getWxMpConfigStorage()), postData); | |||||
| } | } | ||||
| @Override | @Override | ||||
| public <T, E> T execute(RequestExecutor<T, E> executor, WxMpApiUrl url, E data) throws WxErrorException { | 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.WxMpAiOpenService; | ||||
| import me.chanjar.weixin.mp.api.WxMpService; | import me.chanjar.weixin.mp.api.WxMpService; | ||||
| import me.chanjar.weixin.mp.enums.AiLangType; | import me.chanjar.weixin.mp.enums.AiLangType; | ||||
| import me.chanjar.weixin.mp.enums.WxMpApiUrl; | |||||
| import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor; | import me.chanjar.weixin.mp.util.requestexecuter.voice.VoiceUploadRequestExecutor; | ||||
| import java.io.File; | import java.io.File; | ||||
| @@ -34,7 +33,7 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService { | |||||
| } | } | ||||
| this.wxMpService.execute(VoiceUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), | 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); | voiceFile); | ||||
| } | } | ||||
| @@ -46,7 +45,8 @@ public class WxMpAiOpenServiceImpl implements WxMpAiOpenService { | |||||
| @Override | @Override | ||||
| public String translate(AiLangType langFrom, AiLangType langTo, String content) throws WxErrorException { | 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); | WxError error = WxError.fromJson(response, WxType.MP); | ||||
| if (error.getErrorCode() != 0) { | 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.WxMpKfAccountRequest; | ||||
| import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest; | import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfSessionRequest; | ||||
| import me.chanjar.weixin.mp.bean.kefu.result.*; | import me.chanjar.weixin.mp.bean.kefu.result.*; | ||||
| import me.chanjar.weixin.mp.enums.WxMpApiUrl; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.util.Date; | import java.util.Date; | ||||
| @@ -68,13 +67,14 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||||
| public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException { | public boolean kfAccountUploadHeadImg(String kfAccount, File imgFile) throws WxErrorException { | ||||
| WxMediaUploadResult responseContent = this.wxMpService | WxMediaUploadResult responseContent = this.wxMpService | ||||
| .execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), | .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; | return responseContent != null; | ||||
| } | } | ||||
| @Override | @Override | ||||
| public boolean kfAccountDel(String kfAccount) throws WxErrorException { | 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; | return responseContent != null; | ||||
| } | } | ||||
| @@ -94,13 +94,15 @@ public class WxMpKefuServiceImpl implements WxMpKefuService { | |||||
| @Override | @Override | ||||
| public WxMpKfSessionGetResult kfSessionGet(String openid) throws WxErrorException { | 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); | return WxMpKfSessionGetResult.fromJson(responseContent); | ||||
| } | } | ||||
| @Override | @Override | ||||
| public WxMpKfSessionList kfSessionList(String kfAccount) throws WxErrorException { | 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); | return WxMpKfSessionList.fromJson(responseContent); | ||||
| } | } | ||||
| @@ -53,7 +53,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||||
| @Override | @Override | ||||
| public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { | 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); | return this.wxMpService.execute(MediaUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, file); | ||||
| } | } | ||||
| @@ -72,7 +72,7 @@ public class WxMpMaterialServiceImpl implements WxMpMaterialService { | |||||
| @Override | @Override | ||||
| public WxMpMaterialUploadResult materialFileUpload(String mediaType, WxMpMaterial material) throws WxErrorException { | 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); | return this.wxMpService.execute(MaterialUploadRequestExecutor.create(this.wxMpService.getRequestHttp()), url, material); | ||||
| } | } | ||||
| @@ -105,7 +105,8 @@ public class WxMpQrcodeServiceImpl implements WxMpQrcodeService { | |||||
| @Override | @Override | ||||
| public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException { | public String qrCodePictureUrl(String ticket, boolean needShortUrl) throws WxErrorException { | ||||
| try { | 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) { | if (needShortUrl) { | ||||
| return this.wxMpService.shortUrl(resultUrl); | 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.ApacheHttpClientBuilder; | ||||
| import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; | import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; | ||||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | import me.chanjar.weixin.mp.api.WxMpConfigStorage; | ||||
| import me.chanjar.weixin.mp.enums.WxMpApiUrl; | |||||
| import org.apache.http.HttpHost; | import org.apache.http.HttpHost; | ||||
| import org.apache.http.client.config.RequestConfig; | import org.apache.http.client.config.RequestConfig; | ||||
| import org.apache.http.client.methods.CloseableHttpResponse; | import org.apache.http.client.methods.CloseableHttpResponse; | ||||
| @@ -19,7 +18,7 @@ import org.apache.http.impl.client.CloseableHttpClient; | |||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.util.concurrent.locks.Lock; | 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方式实现. | * apache http client方式实现. | ||||
| @@ -67,20 +66,20 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | 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(); | lock.lock(); | ||||
| try { | 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 { | try { | ||||
| HttpGet httpGet = new HttpGet(url); | HttpGet httpGet = new HttpGet(url); | ||||
| if (this.getRequestHttpProxy() != null) { | 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)) { | try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) { | ||||
| String resultContent = new BasicResponseHandler().handleResponse(response); | String resultContent = new BasicResponseHandler().handleResponse(response); | ||||
| @@ -89,8 +88,8 @@ public class WxMpServiceHttpClientImpl extends BaseWxMpServiceImpl<CloseableHttp | |||||
| throw new WxErrorException(error); | throw new WxErrorException(error); | ||||
| } | } | ||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | 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 { | } finally { | ||||
| httpGet.releaseConnection(); | httpGet.releaseConnection(); | ||||
| } | } | ||||
| @@ -51,15 +51,15 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | 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(); | lock.lock(); | ||||
| try { | 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); | HttpRequest request = HttpRequest.get(url); | ||||
| @@ -76,9 +76,9 @@ public class WxMpServiceJoddHttpImpl extends BaseWxMpServiceImpl<HttpConnectionP | |||||
| throw new WxErrorException(error); | throw new WxErrorException(error); | ||||
| } | } | ||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | 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 { | } finally { | ||||
| lock.unlock(); | 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.HttpType; | ||||
| import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; | import me.chanjar.weixin.common.util.http.okhttp.OkHttpProxyInfo; | ||||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | import me.chanjar.weixin.mp.api.WxMpConfigStorage; | ||||
| import me.chanjar.weixin.mp.enums.WxMpApiUrl; | |||||
| import okhttp3.*; | import okhttp3.*; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.util.concurrent.locks.Lock; | 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实现. | * okhttp实现. | ||||
| @@ -41,15 +40,15 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH | |||||
| @Override | @Override | ||||
| public String getAccessToken(boolean forceRefresh) throws WxErrorException { | 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(); | lock.lock(); | ||||
| try { | 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(); | Request request = new Request.Builder().url(url).get().build(); | ||||
| Response response = getRequestHttpClient().newCall(request).execute(); | Response response = getRequestHttpClient().newCall(request).execute(); | ||||
| @@ -59,9 +58,9 @@ public class WxMpServiceOkHttpImpl extends BaseWxMpServiceImpl<OkHttpClient, OkH | |||||
| throw new WxErrorException(error); | throw new WxErrorException(error); | ||||
| } | } | ||||
| WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); | 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) { | } catch (IOException e) { | ||||
| throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
| } finally { | } finally { | ||||
| @@ -24,7 +24,7 @@ public class WxMpSubscribeMsgServiceImpl implements WxMpSubscribeMsgService { | |||||
| @Override | @Override | ||||
| public String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved) { | public String subscribeMsgAuthorizationUrl(String redirectURI, int scene, String reserved) { | ||||
| WxMpConfigStorage storage = this.wxMpService.getWxMpConfigStorage(); | 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); | URIUtil.encodeURIComponent(redirectURI), reserved); | ||||
| } | } | ||||
| @@ -0,0 +1,56 @@ | |||||
| package me.chanjar.weixin.mp.bean; | |||||
| import lombok.AllArgsConstructor; | |||||
| import lombok.Builder; | |||||
| import lombok.Data; | |||||
| import lombok.NoArgsConstructor; | |||||
| /** | |||||
| * 微信接口地址域名部分的自定义设置信息. | |||||
| * | |||||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||||
| * @date 2019-06-09 | |||||
| */ | |||||
| @Data | |||||
| @Builder | |||||
| @NoArgsConstructor | |||||
| @AllArgsConstructor | |||||
| public class WxMpHostConfig { | |||||
| public static final String API_DEFAULT_HOST_URL = "https://api.weixin.qq.com"; | |||||
| public static final String MP_DEFAULT_HOST_URL = "https://mp.weixin.qq.com"; | |||||
| public static final String OPEN_DEFAULT_HOST_URL = "https://open.weixin.qq.com"; | |||||
| /** | |||||
| * 对应于:https://api.weixin.qq.com | |||||
| */ | |||||
| private String apiHost; | |||||
| /** | |||||
| * 对应于:https://open.weixin.qq.com | |||||
| */ | |||||
| private String openHost; | |||||
| /** | |||||
| * 对应于:https://mp.weixin.qq.com | |||||
| */ | |||||
| private String mpHost; | |||||
| public static String buildUrl(WxMpHostConfig hostConfig, String prefix, String path) { | |||||
| if (hostConfig == null) { | |||||
| return prefix + path; | |||||
| } | |||||
| if (hostConfig.getApiHost() != null && prefix.equals(API_DEFAULT_HOST_URL)) { | |||||
| return hostConfig.getApiHost() + path; | |||||
| } | |||||
| if (hostConfig.getMpHost() != null && prefix.equals(MP_DEFAULT_HOST_URL)) { | |||||
| return hostConfig.getMpHost() + path; | |||||
| } | |||||
| if (hostConfig.getOpenHost() != null && prefix.equals(OPEN_DEFAULT_HOST_URL)) { | |||||
| return hostConfig.getOpenHost() + path; | |||||
| } | |||||
| return prefix + path; | |||||
| } | |||||
| } | |||||
| @@ -17,6 +17,9 @@ import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage; | |||||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry; | import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry; | ||||
| import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; | import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; | ||||
| /** | |||||
| * @author someone | |||||
| */ | |||||
| public class WxMpGsonBuilder { | public class WxMpGsonBuilder { | ||||
| private static final GsonBuilder INSTANCE = new GsonBuilder(); | private static final GsonBuilder INSTANCE = new GsonBuilder(); | ||||
| @@ -11,6 +11,7 @@ import cn.binarywang.wx.miniapp.config.WxMaConfig; | |||||
| import me.chanjar.weixin.common.bean.WxAccessToken; | import me.chanjar.weixin.common.bean.WxAccessToken; | ||||
| import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; | ||||
| import me.chanjar.weixin.mp.api.WxMpConfigStorage; | import me.chanjar.weixin.mp.api.WxMpConfigStorage; | ||||
| import me.chanjar.weixin.mp.bean.WxMpHostConfig; | |||||
| import me.chanjar.weixin.mp.enums.TicketType; | import me.chanjar.weixin.mp.enums.TicketType; | ||||
| import me.chanjar.weixin.open.api.WxOpenConfigStorage; | import me.chanjar.weixin.open.api.WxOpenConfigStorage; | ||||
| import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken; | import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken; | ||||
| @@ -543,10 +544,14 @@ public class WxOpenInMemoryConfigStorage implements WxOpenConfigStorage { | |||||
| return wxOpenConfigStorage.getApacheHttpClientBuilder(); | return wxOpenConfigStorage.getApacheHttpClientBuilder(); | ||||
| } | } | ||||
| @Override | @Override | ||||
| public boolean autoRefreshToken() { | public boolean autoRefreshToken() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| @Override | |||||
| public WxMpHostConfig getHostConfig() { | |||||
| return null; | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -9,7 +9,7 @@ import me.chanjar.weixin.open.api.WxOpenComponentService; | |||||
| /** | /** | ||||
| * @author <a href="https://github.com/007gzs">007</a> | * @author <a href="https://github.com/007gzs">007</a> | ||||
| */ | */ | ||||
| /* package */ class WxOpenMpServiceImpl extends WxMpServiceImpl { | |||||
| /* package(无需对外暴露) */ class WxOpenMpServiceImpl extends WxMpServiceImpl { | |||||
| private WxOpenComponentService wxOpenComponentService; | private WxOpenComponentService wxOpenComponentService; | ||||
| private WxMpConfigStorage wxMpConfigStorage; | private WxMpConfigStorage wxMpConfigStorage; | ||||
| private String appId; | private String appId; | ||||