@@ -13,8 +13,11 @@ import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1 ;
import me.chanjar.weixin.common.util.http.* ;
import me.chanjar.weixin.mp.api.* ;
import me.chanjar.weixin.mp.bean.* ;
import me.chanjar.weixin.mp.bean.result.* ;
import me.chanjar.weixin.mp.bean.WxMpSemanticQuery ;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo ;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken ;
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult ;
import me.chanjar.weixin.mp.bean.result.WxMpUser ;
import org.apache.commons.lang3.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
@@ -22,7 +25,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException ;
import java.util.concurrent.locks.Lock ;
public abstract class WxMpServiceAbstract Impl < H , P > implements WxMpService , RequestHttp < H , P > {
public abstract class WxMpServiceBase Impl < H , P > implements WxMpService , RequestHttp < H , P > {
private static final JsonParser JSON_PARSER = new JsonParser ( ) ;
@@ -92,14 +95,14 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
@Override
public WxJsapiSignature createJsapiSignature ( String url ) throws WxErrorException {
long timestamp = System . currentTimeMillis ( ) / 1000 ;
String nonces tr = RandomUtils . getRandomStr ( ) ;
String randomS tr = RandomUtils . getRandomStr ( ) ;
String jsapiTicket = getJsapiTicket ( false ) ;
String signature = SHA1 . genWithAmple ( " jsapi_ticket= " + jsapiTicket ,
" noncestr= " + nonces tr, " timestamp= " + timestamp , " url= " + url ) ;
" noncestr= " + randomS tr, " timestamp= " + timestamp , " url= " + url ) ;
WxJsapiSignature jsapiSignature = new WxJsapiSignature ( ) ;
jsapiSignature . setAppId ( this . getWxMpConfigStorage ( ) . getAppId ( ) ) ;
jsapiSignature . setTimestamp ( timestamp ) ;
jsapiSignature . setNonceStr ( nonces tr) ;
jsapiSignature . setNonceStr ( randomS tr) ;
jsapiSignature . setUrl ( url ) ;
jsapiSignature . setSignature ( signature ) ;
return jsapiSignature ;
@@ -111,10 +114,10 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
@Override
public String shortUrl ( String long_u rl ) throws WxErrorException {
public String shortUrl ( String longU rl ) throws WxErrorException {
JsonObject o = new JsonObject ( ) ;
o . addProperty ( " action " , " long2short " ) ;
o . addProperty ( " long_url " , long_u rl ) ;
o . addProperty ( " long_url " , longU rl ) ;
String responseContent = this . post ( WxMpService . SHORTURL_API_URL , o . toString ( ) ) ;
JsonElement tmpJsonElement = JSON_PARSER . parse ( responseContent ) ;
return tmpJsonElement . getAsJsonObject ( ) . get ( " short_url " ) . getAsString ( ) ;
@@ -161,12 +164,12 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
@Override
public WxMpUser oauth2getUserInfo ( WxMpOAuth2AccessToken oAuth2AccessT oken, String lang ) throws WxErrorException {
public WxMpUser oauth2getUserInfo ( WxMpOAuth2AccessToken t oken, String lang ) throws WxErrorException {
if ( lang = = null ) {
lang = " zh_CN " ;
}
String url = String . format ( WxMpService . OAUTH2_USERINFO_URL , oAuth2AccessT oken. getAccessToken ( ) , oAuth2AccessT oken. getOpenId ( ) , lang ) ;
String url = String . format ( WxMpService . OAUTH2_USERINFO_URL , t oken. getAccessToken ( ) , t oken. getOpenId ( ) , lang ) ;
try {
RequestExecutor < String , String > executor = SimpleGetRequestExecutor . create ( this ) ;
@@ -178,8 +181,8 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
@Override
public boolean oauth2validateAccessToken ( WxMpOAuth2AccessToken oAuth2AccessT oken) {
String url = String . format ( WxMpService . OAUTH2_VALIDATE_TOKEN_URL , oAuth2AccessT oken. getAccessToken ( ) , oAuth2AccessT oken. getOpenId ( ) ) ;
public boolean oauth2validateAccessToken ( WxMpOAuth2AccessToken t oken) {
String url = String . format ( WxMpService . OAUTH2_VALIDATE_TOKEN_URL , t oken. getAccessToken ( ) , t oken. getOpenId ( ) ) ;
try {
SimpleGetRequestExecutor . create ( this ) . execute ( url , null ) ;
@@ -226,7 +229,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
}
/ * *
* 向微信端发送请求 , 在这里执行的策略是当发生access_token过期时才去刷新 , 然后重新执行请求 , 而不是全局定时请求
* 向微信端发送请求 , 在这里执行的策略是当发生access_token过期时才去刷新 , 然后重新执行请求 , 而不是全局定时请求 .
* /
@Override
public < T , E > T execute ( RequestExecutor < T , E > executor , String uri , E data ) throws WxErrorException {
@@ -265,6 +268,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
if ( uri . contains ( " access_token= " ) ) {
throw new IllegalArgumentException ( " uri参数中不允许有access_token: " + uri ) ;
}
String accessToken = getAccessToken ( false ) ;
String uriWithAccessToken = uri + ( uri . contains ( " ? " ) ? " & " : " ? " ) + " access_token= " + accessToken ;
@@ -296,7 +300,7 @@ public abstract class WxMpServiceAbstractImpl<H, P> implements WxMpService, Requ
return null ;
} catch ( IOException e ) {
this . log . error ( " \ n【请求地址】: {} \ n【请求参数】: {} \ n【异常信息】: {} " , uriWithAccessToken , data , e . getMessage ( ) ) ;
throw new Runtime Exception( e ) ;
throw new WxError Exception(WxError . builder ( ) . errorMsg ( e . getMessage ( ) ) . build ( ) , e ) ;
}
}