1313import me .chanjar .weixin .common .util .crypto .SHA1 ;
1414import me .chanjar .weixin .common .util .http .*;
1515import me .chanjar .weixin .mp .api .*;
16- import me .chanjar .weixin .mp .bean .*;
17- import me .chanjar .weixin .mp .bean .result .*;
16+ import me .chanjar .weixin .mp .bean .WxMpSemanticQuery ;
17+ import me .chanjar .weixin .mp .bean .result .WxMpCurrentAutoReplyInfo ;
18+ import me .chanjar .weixin .mp .bean .result .WxMpOAuth2AccessToken ;
19+ import me .chanjar .weixin .mp .bean .result .WxMpSemanticQueryResult ;
20+ import me .chanjar .weixin .mp .bean .result .WxMpUser ;
1821import org .apache .commons .lang3 .StringUtils ;
1922import org .slf4j .Logger ;
2023import org .slf4j .LoggerFactory ;
2124
2225import java .io .IOException ;
2326import java .util .concurrent .locks .Lock ;
2427
25- public abstract class WxMpServiceAbstractImpl <H , P > implements WxMpService , RequestHttp <H , P > {
28+ public abstract class WxMpServiceBaseImpl <H , P > implements WxMpService , RequestHttp <H , P > {
2629
2730 private static final JsonParser JSON_PARSER = new JsonParser ();
2831
@@ -92,14 +95,14 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
9295 @ Override
9396 public WxJsapiSignature createJsapiSignature (String url ) throws WxErrorException {
9497 long timestamp = System .currentTimeMillis () / 1000 ;
95- String noncestr = RandomUtils .getRandomStr ();
98+ String randomStr = RandomUtils .getRandomStr ();
9699 String jsapiTicket = getJsapiTicket (false );
97100 String signature = SHA1 .genWithAmple ("jsapi_ticket=" + jsapiTicket ,
98- "noncestr=" + noncestr , "timestamp=" + timestamp , "url=" + url );
101+ "noncestr=" + randomStr , "timestamp=" + timestamp , "url=" + url );
99102 WxJsapiSignature jsapiSignature = new WxJsapiSignature ();
100103 jsapiSignature .setAppId (this .getWxMpConfigStorage ().getAppId ());
101104 jsapiSignature .setTimestamp (timestamp );
102- jsapiSignature .setNonceStr (noncestr );
105+ jsapiSignature .setNonceStr (randomStr );
103106 jsapiSignature .setUrl (url );
104107 jsapiSignature .setSignature (signature );
105108 return jsapiSignature ;
@@ -111,10 +114,10 @@ public String getAccessToken() throws WxErrorException {
111114 }
112115
113116 @ Override
114- public String shortUrl (String long_url ) throws WxErrorException {
117+ public String shortUrl (String longUrl ) throws WxErrorException {
115118 JsonObject o = new JsonObject ();
116119 o .addProperty ("action" , "long2short" );
117- o .addProperty ("long_url" , long_url );
120+ o .addProperty ("long_url" , longUrl );
118121 String responseContent = this .post (WxMpService .SHORTURL_API_URL , o .toString ());
119122 JsonElement tmpJsonElement = JSON_PARSER .parse (responseContent );
120123 return tmpJsonElement .getAsJsonObject ().get ("short_url" ).getAsString ();
@@ -161,12 +164,12 @@ public WxMpOAuth2AccessToken oauth2refreshAccessToken(String refreshToken) throw
161164 }
162165
163166 @ Override
164- public WxMpUser oauth2getUserInfo (WxMpOAuth2AccessToken oAuth2AccessToken , String lang ) throws WxErrorException {
167+ public WxMpUser oauth2getUserInfo (WxMpOAuth2AccessToken token , String lang ) throws WxErrorException {
165168 if (lang == null ) {
166169 lang = "zh_CN" ;
167170 }
168171
169- String url = String .format (WxMpService .OAUTH2_USERINFO_URL , oAuth2AccessToken .getAccessToken (), oAuth2AccessToken .getOpenId (), lang );
172+ String url = String .format (WxMpService .OAUTH2_USERINFO_URL , token .getAccessToken (), token .getOpenId (), lang );
170173
171174 try {
172175 RequestExecutor <String , String > executor = SimpleGetRequestExecutor .create (this );
@@ -178,8 +181,8 @@ public WxMpUser oauth2getUserInfo(WxMpOAuth2AccessToken oAuth2AccessToken, Strin
178181 }
179182
180183 @ Override
181- public boolean oauth2validateAccessToken (WxMpOAuth2AccessToken oAuth2AccessToken ) {
182- String url = String .format (WxMpService .OAUTH2_VALIDATE_TOKEN_URL , oAuth2AccessToken .getAccessToken (), oAuth2AccessToken .getOpenId ());
184+ public boolean oauth2validateAccessToken (WxMpOAuth2AccessToken token ) {
185+ String url = String .format (WxMpService .OAUTH2_VALIDATE_TOKEN_URL , token .getAccessToken (), token .getOpenId ());
183186
184187 try {
185188 SimpleGetRequestExecutor .create (this ).execute (url , null );
@@ -226,7 +229,7 @@ public String post(String url, String postData) throws WxErrorException {
226229 }
227230
228231 /**
229- * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
232+ * 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
230233 */
231234 @ Override
232235 public <T , E > T execute (RequestExecutor <T , E > executor , String uri , E data ) throws WxErrorException {
@@ -265,6 +268,7 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
265268 if (uri .contains ("access_token=" )) {
266269 throw new IllegalArgumentException ("uri参数中不允许有access_token: " + uri );
267270 }
271+
268272 String accessToken = getAccessToken (false );
269273
270274 String uriWithAccessToken = uri + (uri .contains ("?" ) ? "&" : "?" ) + "access_token=" + accessToken ;
@@ -296,7 +300,7 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
296300 return null ;
297301 } catch (IOException e ) {
298302 this .log .error ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【异常信息】:{}" , uriWithAccessToken , data , e .getMessage ());
299- throw new RuntimeException ( e );
303+ throw new WxErrorException ( WxError . builder (). errorMsg ( e . getMessage ()). build (), e );
300304 }
301305 }
302306
0 commit comments