Browse Source

修复变量名,保持跟set方法统一

master
Binary Wang 8 years ago
parent
commit
2e3a33a74e
1 changed files with 33 additions and 33 deletions
  1. +33
    -33
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

+ 33
- 33
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java View File

@@ -37,7 +37,7 @@ public class WxMpServiceImpl implements WxMpService {


protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final Logger log = LoggerFactory.getLogger(this.getClass());
protected WxSessionManager sessionManager = new StandardSessionManager(); protected WxSessionManager sessionManager = new StandardSessionManager();
private WxMpConfigStorage configStorage;
private WxMpConfigStorage wxMpConfigStorage;
private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this); private WxMpKefuService kefuService = new WxMpKefuServiceImpl(this);
private WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this); private WxMpMaterialService materialService = new WxMpMaterialServiceImpl(this);
private WxMpMenuService menuService = new WxMpMenuServiceImpl(this); private WxMpMenuService menuService = new WxMpMenuServiceImpl(this);
@@ -59,7 +59,7 @@ public class WxMpServiceImpl implements WxMpService {
@Override @Override
public boolean checkSignature(String timestamp, String nonce, String signature) { public boolean checkSignature(String timestamp, String nonce, String signature) {
try { try {
return SHA1.gen(this.configStorage.getToken(), timestamp, nonce)
return SHA1.gen(this.wxMpConfigStorage.getToken(), timestamp, nonce)
.equals(signature); .equals(signature);
} catch (Exception e) { } catch (Exception e) {
return false; return false;
@@ -73,18 +73,18 @@ public class WxMpServiceImpl implements WxMpService {


@Override @Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException { public String getAccessToken(boolean forceRefresh) throws WxErrorException {
Lock lock = this.configStorage.getAccessTokenLock();
Lock lock = this.wxMpConfigStorage.getAccessTokenLock();
try { try {
lock.lock(); lock.lock();


if (forceRefresh) { if (forceRefresh) {
this.configStorage.expireAccessToken();
this.wxMpConfigStorage.expireAccessToken();
} }


if (this.configStorage.isAccessTokenExpired()) {
if (this.wxMpConfigStorage.isAccessTokenExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=" + this.configStorage.getAppId() + "&secret="
+ this.configStorage.getSecret();
"&appid=" + this.wxMpConfigStorage.getAppId() + "&secret="
+ this.wxMpConfigStorage.getSecret();
try { try {
HttpGet httpGet = new HttpGet(url); HttpGet httpGet = new HttpGet(url);
if (this.httpProxy != null) { if (this.httpProxy != null) {
@@ -98,7 +98,7 @@ public class WxMpServiceImpl implements WxMpService {
throw new WxErrorException(error); throw new WxErrorException(error);
} }
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent); WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
this.configStorage.updateAccessToken(accessToken.getAccessToken(),
this.wxMpConfigStorage.updateAccessToken(accessToken.getAccessToken(),
accessToken.getExpiresIn()); accessToken.getExpiresIn());
} finally { } finally {
httpGet.releaseConnection(); httpGet.releaseConnection();
@@ -110,7 +110,7 @@ public class WxMpServiceImpl implements WxMpService {
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return this.configStorage.getAccessToken();
return this.wxMpConfigStorage.getAccessToken();
} }


@Override @Override
@@ -120,27 +120,27 @@ public class WxMpServiceImpl implements WxMpService {


@Override @Override
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException { public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
Lock lock = this.configStorage.getJsapiTicketLock();
Lock lock = this.wxMpConfigStorage.getJsapiTicketLock();
try { try {
lock.lock(); lock.lock();


if (forceRefresh) { if (forceRefresh) {
this.configStorage.expireJsapiTicket();
this.wxMpConfigStorage.expireJsapiTicket();
} }


if (this.configStorage.isJsapiTicketExpired()) {
if (this.wxMpConfigStorage.isJsapiTicketExpired()) {
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi"; String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi";
String responseContent = execute(new SimpleGetRequestExecutor(), url, null); String responseContent = execute(new SimpleGetRequestExecutor(), url, null);
JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent); JsonElement tmpJsonElement = JSON_PARSER.parse(responseContent);
JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject(); JsonObject tmpJsonObject = tmpJsonElement.getAsJsonObject();
String jsapiTicket = tmpJsonObject.get("ticket").getAsString(); String jsapiTicket = tmpJsonObject.get("ticket").getAsString();
int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt(); int expiresInSeconds = tmpJsonObject.get("expires_in").getAsInt();
this.configStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
this.wxMpConfigStorage.updateJsapiTicket(jsapiTicket, expiresInSeconds);
} }
} finally { } finally {
lock.unlock(); lock.unlock();
} }
return this.configStorage.getJsapiTicket();
return this.wxMpConfigStorage.getJsapiTicket();
} }


@Override @Override
@@ -151,7 +151,7 @@ public class WxMpServiceImpl implements WxMpService {
String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket, String signature = SHA1.genWithAmple("jsapi_ticket=" + jsapiTicket,
"noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url); "noncestr=" + noncestr, "timestamp=" + timestamp, "url=" + url);
WxJsapiSignature jsapiSignature = new WxJsapiSignature(); WxJsapiSignature jsapiSignature = new WxJsapiSignature();
jsapiSignature.setAppid(this.configStorage.getAppId());
jsapiSignature.setAppid(this.wxMpConfigStorage.getAppId());
jsapiSignature.setTimestamp(timestamp); jsapiSignature.setTimestamp(timestamp);
jsapiSignature.setNoncestr(noncestr); jsapiSignature.setNoncestr(noncestr);
jsapiSignature.setUrl(url); jsapiSignature.setUrl(url);
@@ -216,7 +216,7 @@ public class WxMpServiceImpl implements WxMpService {
public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) { public String oauth2buildAuthorizationUrl(String redirectURI, String scope, String state) {
StringBuilder url = new StringBuilder(); StringBuilder url = new StringBuilder();
url.append("https://open.weixin.qq.com/connect/oauth2/authorize?"); url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
url.append("appid=").append(this.configStorage.getAppId());
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI)); url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
url.append("&response_type=code"); url.append("&response_type=code");
url.append("&scope=").append(scope); url.append("&scope=").append(scope);
@@ -232,7 +232,7 @@ public class WxMpServiceImpl implements WxMpService {
String state) { String state) {
StringBuilder url = new StringBuilder(); StringBuilder url = new StringBuilder();
url.append("https://open.weixin.qq.com/connect/qrconnect?"); url.append("https://open.weixin.qq.com/connect/qrconnect?");
url.append("appid=").append(this.configStorage.getAppId());
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI)); url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectURI));
url.append("&response_type=code"); url.append("&response_type=code");
url.append("&scope=").append(scope); url.append("&scope=").append(scope);
@@ -258,8 +258,8 @@ public class WxMpServiceImpl implements WxMpService {
public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException { public WxMpOAuth2AccessToken oauth2getAccessToken(String code) throws WxErrorException {
StringBuilder url = new StringBuilder(); StringBuilder url = new StringBuilder();
url.append("https://api.weixin.qq.com/sns/oauth2/access_token?"); url.append("https://api.weixin.qq.com/sns/oauth2/access_token?");
url.append("appid=").append(this.configStorage.getAppId());
url.append("&secret=").append(this.configStorage.getSecret());
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
url.append("&secret=").append(this.wxMpConfigStorage.getSecret());
url.append("&code=").append(code); url.append("&code=").append(code);
url.append("&grant_type=authorization_code"); url.append("&grant_type=authorization_code");


@@ -270,7 +270,7 @@ public class WxMpServiceImpl implements WxMpService {
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException { public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throws WxErrorException {
StringBuilder url = new StringBuilder(); StringBuilder url = new StringBuilder();
url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?"); url.append("https://api.weixin.qq.com/sns/oauth2/refresh_token?");
url.append("appid=").append(this.configStorage.getAppId());
url.append("appid=").append(this.wxMpConfigStorage.getAppId());
url.append("&grant_type=refresh_token"); url.append("&grant_type=refresh_token");
url.append("&refresh_token=").append(refreshToken); url.append("&refresh_token=").append(refreshToken);


@@ -397,8 +397,8 @@ public class WxMpServiceImpl implements WxMpService {
*/ */
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) { if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001) {
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token // 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
this.configStorage.expireAccessToken();
if (this.configStorage.autoRefreshToken()) {
this.wxMpConfigStorage.expireAccessToken();
if (this.wxMpConfigStorage.autoRefreshToken()) {
return this.execute(executor, uri, data); return this.execute(executor, uri, data);
} }
} }
@@ -424,26 +424,26 @@ public class WxMpServiceImpl implements WxMpService {
} }


private void initHttpClient() { private void initHttpClient() {
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage
ApacheHttpClientBuilder apacheHttpClientBuilder = this.wxMpConfigStorage
.getApacheHttpClientBuilder(); .getApacheHttpClientBuilder();
if (null == apacheHttpClientBuilder) { if (null == apacheHttpClientBuilder) {
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get(); apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
} }


apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
.httpProxyPort(this.configStorage.getHttpProxyPort())
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
apacheHttpClientBuilder.httpProxyHost(this.wxMpConfigStorage.getHttpProxyHost())
.httpProxyPort(this.wxMpConfigStorage.getHttpProxyPort())
.httpProxyUsername(this.wxMpConfigStorage.getHttpProxyUsername())
.httpProxyPassword(this.wxMpConfigStorage.getHttpProxyPassword());


// if (this.configStorage.getSSLContext() != null) {
// if (this.wxMpConfigStorage.getSSLContext() != null) {
// SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( // SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
// this.configStorage.getSSLContext(), new String[] { "TLSv1" }, null,
// this.wxMpConfigStorage.getSSLContext(), new String[] { "TLSv1" }, null,
// new DefaultHostnameVerifier()); // new DefaultHostnameVerifier());
// apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf); // apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
// } // }


if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());
if (this.wxMpConfigStorage.getHttpProxyHost() != null && this.wxMpConfigStorage.getHttpProxyPort() > 0) {
this.httpProxy = new HttpHost(this.wxMpConfigStorage.getHttpProxyHost(), this.wxMpConfigStorage.getHttpProxyPort());
} }


this.httpClient = apacheHttpClientBuilder.build(); this.httpClient = apacheHttpClientBuilder.build();
@@ -451,12 +451,12 @@ public class WxMpServiceImpl implements WxMpService {


@Override @Override
public WxMpConfigStorage getWxMpConfigStorage() { public WxMpConfigStorage getWxMpConfigStorage() {
return this.configStorage;
return this.wxMpConfigStorage;
} }


@Override @Override
public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) { public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
this.configStorage = wxConfigProvider;
this.wxMpConfigStorage = wxConfigProvider;
this.initHttpClient(); this.initHttpClient();
} }




Loading…
Cancel
Save