|
|
@@ -42,6 +42,11 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
this.wxOpenService = wxOpenService; |
|
|
|
} |
|
|
|
|
|
|
|
enum RequestMethod { |
|
|
|
POST,GET |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public WxMpService getWxMpServiceByAppid(String appId) { |
|
|
|
WxMpService wxMpService = WX_OPEN_MP_SERVICE_MAP.get(appId); |
|
|
@@ -135,90 +140,44 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String post(String uri, String postData) throws WxErrorException { |
|
|
|
return post(uri, postData, "component_access_token"); |
|
|
|
public String post(String uri, String postData,String tokenKeyName) throws WxErrorException { |
|
|
|
//"component_access_token" |
|
|
|
return postByCommonAccessToken(uri, postData, tokenKeyName); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public String post(String uri, String postData, String accessTokenKey) throws WxErrorException { |
|
|
|
String componentAccessToken = getComponentAccessToken(false); |
|
|
|
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; |
|
|
|
try { |
|
|
|
return getWxOpenService().post(uriWithComponentAccessToken, postData); |
|
|
|
} catch (WxErrorException e) { |
|
|
|
WxError error = e.getError(); |
|
|
|
/* |
|
|
|
* 发生以下情况时尝试刷新access_token |
|
|
|
* 40001 获取access_token时AppSecret错误,或者access_token无效 |
|
|
|
* 42001 access_token超时 |
|
|
|
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口 |
|
|
|
*/ |
|
|
|
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) { |
|
|
|
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token |
|
|
|
Lock lock = this.getWxOpenConfigStorage().getComponentAccessTokenLock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
if (StringUtils.equals(componentAccessToken, this.getWxOpenConfigStorage().getComponentAccessToken())) { |
|
|
|
this.getWxOpenConfigStorage().expireComponentAccessToken(); |
|
|
|
} |
|
|
|
} catch (Exception ex) { |
|
|
|
this.getWxOpenConfigStorage().expireComponentAccessToken(); |
|
|
|
} finally { |
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
|
|
|
|
if (this.getWxOpenConfigStorage().autoRefreshToken()) { |
|
|
|
return this.post(uri, postData, accessTokenKey); |
|
|
|
} |
|
|
|
} |
|
|
|
if (error.getErrorCode() != 0) { |
|
|
|
throw new WxErrorException(error, e); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
public String get(String uri,String tokenKeyName) throws WxErrorException { |
|
|
|
//"component_access_token" |
|
|
|
return getByCommonAccessToken(uri, tokenKeyName); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String getByCommonAccessToken(String uri, String accessTokenKey) throws WxErrorException{ |
|
|
|
String componentAccessToken = getComponentAccessToken(false); |
|
|
|
return excuteRequet(uri, accessTokenKey, componentAccessToken, null, true, RequestMethod.GET); |
|
|
|
} |
|
|
|
private String postByCommonAccessToken(String uri, String postData ,String accessTokenKey) throws WxErrorException{ |
|
|
|
String componentAccessToken = getComponentAccessToken(false); |
|
|
|
return excuteRequet(uri, accessTokenKey, componentAccessToken, postData, true, RequestMethod.POST); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String get(String uri) throws WxErrorException { |
|
|
|
return get(uri, "component_access_token"); |
|
|
|
public String getByAppAccessToken(String appId,String uri, String accessTokenKey) throws WxErrorException{ |
|
|
|
String accessToken = getAuthorizerAccessToken(appId,false); |
|
|
|
return excuteRequet(uri, accessTokenKey, accessToken, null, true, RequestMethod.GET); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public String get(String uri, String accessTokenKey) throws WxErrorException { |
|
|
|
String componentAccessToken = getComponentAccessToken(false); |
|
|
|
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + accessTokenKey + "=" + componentAccessToken; |
|
|
|
try { |
|
|
|
return getWxOpenService().get(uriWithComponentAccessToken, null); |
|
|
|
} catch (WxErrorException e) { |
|
|
|
WxError error = e.getError(); |
|
|
|
/* |
|
|
|
* 发生以下情况时尝试刷新access_token |
|
|
|
* 40001 获取access_token时AppSecret错误,或者access_token无效 |
|
|
|
* 42001 access_token超时 |
|
|
|
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口 |
|
|
|
*/ |
|
|
|
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) { |
|
|
|
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token |
|
|
|
Lock lock = this.getWxOpenConfigStorage().getComponentAccessTokenLock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
if (StringUtils.equals(componentAccessToken, this.getWxOpenConfigStorage().getComponentAccessToken())) { |
|
|
|
this.getWxOpenConfigStorage().expireComponentAccessToken(); |
|
|
|
} |
|
|
|
} catch (Exception ex) { |
|
|
|
this.getWxOpenConfigStorage().expireComponentAccessToken(); |
|
|
|
} finally { |
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
if (this.getWxOpenConfigStorage().autoRefreshToken()) { |
|
|
|
return this.get(uri, accessTokenKey); |
|
|
|
} |
|
|
|
} |
|
|
|
if (error.getErrorCode() != 0) { |
|
|
|
throw new WxErrorException(error, e); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
public String postByAppAccessToken(String appId,String uri,String postData, String accessTokenKey) throws WxErrorException{ |
|
|
|
String accessToken = getAuthorizerAccessToken(appId, false); |
|
|
|
return excuteRequet(uri, accessTokenKey, accessToken, postData, true, RequestMethod.POST); |
|
|
|
} |
|
|
|
|
|
|
|
private String post(String uri, String postData, String accessTokenKey,String accessTokenValue, boolean isCommpontAccessToken) throws WxErrorException { |
|
|
|
return excuteRequet(uri, accessTokenKey, accessTokenValue, postData, isCommpontAccessToken, RequestMethod.POST); |
|
|
|
} |
|
|
|
|
|
|
|
private String get(String uri, String accessTokenKey,String accessTokenValue, boolean isCommpontAccessToken) throws WxErrorException { |
|
|
|
return excuteRequet(uri, accessTokenKey, accessTokenValue, null, isCommpontAccessToken, RequestMethod.GET); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@@ -254,7 +213,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
private String createPreAuthUrl(String redirectURI, String authType, String bizAppid, boolean isMobile) throws WxErrorException { |
|
|
|
JsonObject jsonObject = new JsonObject(); |
|
|
|
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
String responseContent = post(API_CREATE_PREAUTHCODE_URL, jsonObject.toString()); |
|
|
|
String responseContent = post(API_CREATE_PREAUTHCODE_URL, jsonObject.toString(),"component_access_token"); |
|
|
|
jsonObject = WxGsonBuilder.create().fromJson(responseContent, JsonObject.class); |
|
|
|
|
|
|
|
StringBuilder preAuthUrl = new StringBuilder(String.format((isMobile ? COMPONENT_MOBILE_LOGIN_PAGE_URL : COMPONENT_LOGIN_PAGE_URL), |
|
|
@@ -309,7 +268,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
JsonObject jsonObject = new JsonObject(); |
|
|
|
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
jsonObject.addProperty("authorization_code", authorizationCode); |
|
|
|
String responseContent = post(API_QUERY_AUTH_URL, jsonObject.toString()); |
|
|
|
String responseContent = post(API_QUERY_AUTH_URL, jsonObject.toString(),"component_access_token"); |
|
|
|
WxOpenQueryAuthResult queryAuth = WxOpenGsonBuilder.create().fromJson(responseContent, WxOpenQueryAuthResult.class); |
|
|
|
if (queryAuth == null || queryAuth.getAuthorizationInfo() == null) { |
|
|
|
return queryAuth; |
|
|
@@ -330,7 +289,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
JsonObject jsonObject = new JsonObject(); |
|
|
|
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
jsonObject.addProperty("authorizer_appid", authorizerAppid); |
|
|
|
String responseContent = post(API_GET_AUTHORIZER_INFO_URL, jsonObject.toString()); |
|
|
|
String responseContent = post(API_GET_AUTHORIZER_INFO_URL, jsonObject.toString(),"component_access_token"); |
|
|
|
return WxOpenGsonBuilder.create().fromJson(responseContent, WxOpenAuthorizerInfoResult.class); |
|
|
|
} |
|
|
|
|
|
|
@@ -342,7 +301,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
jsonObject.addProperty("offset", begin); |
|
|
|
jsonObject.addProperty("count", len); |
|
|
|
String responseContent = post(API_GET_AUTHORIZER_LIST, jsonObject.toString()); |
|
|
|
String responseContent = post(API_GET_AUTHORIZER_LIST, jsonObject.toString(),"component_access_token"); |
|
|
|
WxOpenAuthorizerListResult ret = WxOpenGsonBuilder.create().fromJson(responseContent, WxOpenAuthorizerListResult.class); |
|
|
|
if (ret != null && ret.getList() != null) { |
|
|
|
for (Map<String, String> data : ret.getList()) { |
|
|
@@ -362,7 +321,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
jsonObject.addProperty("authorizer_appid", authorizerAppid); |
|
|
|
jsonObject.addProperty("option_name", optionName); |
|
|
|
String responseContent = post(API_GET_AUTHORIZER_OPTION_URL, jsonObject.toString()); |
|
|
|
String responseContent = post(API_GET_AUTHORIZER_OPTION_URL, jsonObject.toString(),"component_access_token"); |
|
|
|
return WxOpenGsonBuilder.create().fromJson(responseContent, WxOpenAuthorizerOptionResult.class); |
|
|
|
} |
|
|
|
|
|
|
@@ -373,7 +332,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
jsonObject.addProperty("authorizer_appid", authorizerAppid); |
|
|
|
jsonObject.addProperty("option_name", optionName); |
|
|
|
jsonObject.addProperty("option_value", optionValue); |
|
|
|
post(API_SET_AUTHORIZER_OPTION_URL, jsonObject.toString()); |
|
|
|
post(API_SET_AUTHORIZER_OPTION_URL, jsonObject.toString(),"component_access_token"); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
@@ -392,7 +351,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
jsonObject.addProperty("component_appid", getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
jsonObject.addProperty("authorizer_appid", appId); |
|
|
|
jsonObject.addProperty("authorizer_refresh_token", getWxOpenConfigStorage().getAuthorizerRefreshToken(appId)); |
|
|
|
String responseContent = post(API_AUTHORIZER_TOKEN_URL, jsonObject.toString()); |
|
|
|
String responseContent = post(API_AUTHORIZER_TOKEN_URL, jsonObject.toString(),"component_access_token"); |
|
|
|
|
|
|
|
WxOpenAuthorizerAccessToken wxOpenAuthorizerAccessToken = WxOpenAuthorizerAccessToken.fromJson(responseContent); |
|
|
|
config.updateAuthorizerAccessToken(appId, wxOpenAuthorizerAccessToken); |
|
|
@@ -405,7 +364,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
@Override |
|
|
|
public WxMpOAuth2AccessToken oauth2getAccessToken(String appId, String code) throws WxErrorException { |
|
|
|
String url = String.format(OAUTH2_ACCESS_TOKEN_URL, appId, code, getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
String responseContent = get(url); |
|
|
|
String responseContent = get(url,"component_access_token"); |
|
|
|
return WxMpOAuth2AccessToken.fromJson(responseContent); |
|
|
|
} |
|
|
|
|
|
|
@@ -417,7 +376,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
@Override |
|
|
|
public WxMpOAuth2AccessToken oauth2refreshAccessToken(String appId, String refreshToken) throws WxErrorException { |
|
|
|
String url = String.format(OAUTH2_REFRESH_TOKEN_URL, appId, refreshToken, getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
String responseContent = get(url); |
|
|
|
String responseContent = get(url,"component_access_token"); |
|
|
|
return WxMpOAuth2AccessToken.fromJson(responseContent); |
|
|
|
} |
|
|
|
|
|
|
@@ -430,7 +389,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
@Override |
|
|
|
public WxMaJscode2SessionResult miniappJscode2Session(String appId, String jsCode) throws WxErrorException { |
|
|
|
String url = String.format(MINIAPP_JSCODE_2_SESSION, appId, jsCode, getWxOpenConfigStorage().getComponentAppId()); |
|
|
|
String responseContent = get(url); |
|
|
|
String responseContent = get(url,"component_access_token"); |
|
|
|
return WxMaJscode2SessionResult.fromJson(responseContent); |
|
|
|
} |
|
|
|
|
|
|
@@ -481,11 +440,61 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
JsonObject param = new JsonObject(); |
|
|
|
param.addProperty("appid", appId); |
|
|
|
|
|
|
|
String json = post(CREATE_OPEN_URL, param.toString(), "access_token"); |
|
|
|
String json = postByAppAccessToken(appId, CREATE_OPEN_URL, param.toString(), "access_token"); |
|
|
|
|
|
|
|
return WxOpenCreateResult.fromJson(json); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String excuteRequet(String uri,String tokenKey,String tokenValue,String postData,boolean isCompontAccessToken,RequestMethod method) throws WxErrorException { |
|
|
|
String uriWithComponentAccessToken = uri + (uri.contains("?") ? "&" : "?") + tokenKey + "=" + tokenValue; |
|
|
|
try { |
|
|
|
if (method == RequestMethod.POST) { |
|
|
|
return getWxOpenService().post(uriWithComponentAccessToken, postData); |
|
|
|
}else { |
|
|
|
return getWxOpenService().get(uriWithComponentAccessToken, null); |
|
|
|
} |
|
|
|
} catch (WxErrorException e) { |
|
|
|
WxError error = e.getError(); |
|
|
|
/* |
|
|
|
* 发生以下情况时尝试刷新access_token |
|
|
|
* 40001 获取access_token时AppSecret错误,或者access_token无效 |
|
|
|
* 42001 access_token超时 |
|
|
|
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口 |
|
|
|
*/ |
|
|
|
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) { |
|
|
|
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token |
|
|
|
Lock lock = this.getWxOpenConfigStorage().getComponentAccessTokenLock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
if (isCompontAccessToken && StringUtils.equals(tokenValue, this.getWxOpenConfigStorage().getComponentAccessToken())) { |
|
|
|
this.getWxOpenConfigStorage().expireComponentAccessToken(); |
|
|
|
} |
|
|
|
} catch (Exception ex) { |
|
|
|
if (isCompontAccessToken) { |
|
|
|
this.getWxOpenConfigStorage().expireComponentAccessToken(); |
|
|
|
} |
|
|
|
} finally { |
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
|
|
|
|
if (isCompontAccessToken && this.getWxOpenConfigStorage().autoRefreshToken()) { |
|
|
|
if (method == RequestMethod.POST) { |
|
|
|
return this.post(uri, postData, tokenKey); |
|
|
|
}else { |
|
|
|
return this.get(uri, tokenKey); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
if (error.getErrorCode() != 0) { |
|
|
|
throw new WxErrorException(error, e); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public Boolean bindOpenAccount(String appId, String openAppid) throws WxErrorException { |
|
|
@@ -493,7 +502,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
param.addProperty("appid", appId); |
|
|
|
param.addProperty("open_appid", openAppid); |
|
|
|
|
|
|
|
String json = post(BIND_OPEN_URL, param.toString(), "access_token"); |
|
|
|
String json = postByAppAccessToken(appId, BIND_OPEN_URL, param.toString(), "access_token"); |
|
|
|
|
|
|
|
return WxOpenResult.fromJson(json).isSuccess(); |
|
|
|
} |
|
|
@@ -505,7 +514,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
param.addProperty("appid", appId); |
|
|
|
param.addProperty("open_appid", openAppid); |
|
|
|
|
|
|
|
String json = post(UNBIND_OPEN_URL, param.toString(), "access_token"); |
|
|
|
String json = postByAppAccessToken(appId, UNBIND_OPEN_URL, param.toString(), "access_token"); |
|
|
|
|
|
|
|
return WxOpenResult.fromJson(json).isSuccess(); |
|
|
|
} |
|
|
@@ -516,7 +525,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService { |
|
|
|
JsonObject param = new JsonObject(); |
|
|
|
param.addProperty("appid", appId); |
|
|
|
|
|
|
|
String json = post(GET_OPEN_URL, param.toString(), "access_token"); |
|
|
|
String json = postByAppAccessToken(appId, GET_OPEN_URL, param.toString(), "access_token"); |
|
|
|
return WxOpenGetResult.fromJson(json); |
|
|
|
} |
|
|
|
|
|
|
|