1
1
package cn .binarywang .wx .miniapp .api .impl ;
2
2
3
- import java .io .File ;
4
3
import java .io .IOException ;
5
4
import java .util .HashMap ;
6
5
import java .util .Map ;
12
11
import org .apache .http .client .methods .HttpGet ;
13
12
import org .apache .http .impl .client .BasicResponseHandler ;
14
13
import org .apache .http .impl .client .CloseableHttpClient ;
15
- import org .slf4j .Logger ;
16
- import org .slf4j .LoggerFactory ;
17
14
18
15
import cn .binarywang .wx .miniapp .api .WxMaAnalysisService ;
19
16
import cn .binarywang .wx .miniapp .api .WxMaCodeService ;
22
19
import cn .binarywang .wx .miniapp .api .WxMaMsgService ;
23
20
import cn .binarywang .wx .miniapp .api .WxMaQrcodeService ;
24
21
import cn .binarywang .wx .miniapp .api .WxMaRunService ;
22
+ import cn .binarywang .wx .miniapp .api .WxMaSecCheckService ;
25
23
import cn .binarywang .wx .miniapp .api .WxMaService ;
26
24
import cn .binarywang .wx .miniapp .api .WxMaSettingService ;
27
25
import cn .binarywang .wx .miniapp .api .WxMaShareService ;
31
29
import cn .binarywang .wx .miniapp .config .WxMaConfig ;
32
30
import com .google .common .base .Joiner ;
33
31
import com .google .gson .Gson ;
32
+ import lombok .extern .slf4j .Slf4j ;
34
33
import me .chanjar .weixin .common .bean .WxAccessToken ;
35
- import me .chanjar .weixin .common .bean .result .WxMediaUploadResult ;
36
34
import me .chanjar .weixin .common .error .WxError ;
37
35
import me .chanjar .weixin .common .error .WxErrorException ;
38
36
import me .chanjar .weixin .common .util .DataUtils ;
39
37
import me .chanjar .weixin .common .util .crypto .SHA1 ;
40
38
import me .chanjar .weixin .common .util .http .HttpType ;
41
- import me .chanjar .weixin .common .util .http .MediaUploadRequestExecutor ;
42
39
import me .chanjar .weixin .common .util .http .RequestExecutor ;
43
40
import me .chanjar .weixin .common .util .http .RequestHttp ;
44
41
import me .chanjar .weixin .common .util .http .SimpleGetRequestExecutor ;
51
48
/**
52
49
* @author <a href="https://github.com/binarywang">Binary Wang</a>
53
50
*/
51
+ @ Slf4j
54
52
public class WxMaServiceImpl implements WxMaService , RequestHttp <CloseableHttpClient , HttpHost > {
55
- private final Logger log = LoggerFactory .getLogger (this .getClass ());
56
-
57
53
private CloseableHttpClient httpClient ;
58
54
private HttpHost httpProxy ;
59
55
private WxMaConfig wxMaConfig ;
@@ -69,6 +65,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
69
65
private WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl (this );
70
66
private WxMaShareService shareService = new WxMaShareServiceImpl (this );
71
67
private WxMaRunService runService = new WxMaRunServiceImpl (this );
68
+ private WxMaSecCheckService secCheckService = new WxMaSecCheckServiceImpl (this );
72
69
73
70
private int retrySleepMillis = 1000 ;
74
71
private int maxRetryTimes = 5 ;
@@ -153,13 +150,6 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
153
150
return this .getWxMaConfig ().getAccessToken ();
154
151
}
155
152
156
- @ Override
157
- public boolean imgSecCheck (File file ) throws WxErrorException {
158
- //这里只是借用MediaUploadRequestExecutor,并不使用其返回值WxMediaUploadResult
159
- WxMediaUploadResult result = this .execute (MediaUploadRequestExecutor .create (this .getRequestHttp ()), IMG_SEC_CHECK_URL , file );
160
- return result != null ;
161
- }
162
-
163
153
@ Override
164
154
public WxMaJscode2SessionResult jsCode2SessionInfo (String jsCode ) throws WxErrorException {
165
155
final WxMaConfig config = getWxMaConfig ();
@@ -209,7 +199,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
209
199
return this .executeInternal (executor , uri , data );
210
200
} catch (WxErrorException e ) {
211
201
if (retryTimes + 1 > this .maxRetryTimes ) {
212
- this . log .warn ("重试达到最大次数【{}】" , maxRetryTimes );
202
+ log .warn ("重试达到最大次数【{}】" , maxRetryTimes );
213
203
//最后一次重试失败后,直接抛出异常,不再等待
214
204
throw new RuntimeException ("微信服务端异常,超出重试次数" );
215
205
}
@@ -219,7 +209,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
219
209
if (error .getErrorCode () == -1 ) {
220
210
int sleepMillis = this .retrySleepMillis * (1 << retryTimes );
221
211
try {
222
- this . log .warn ("微信系统繁忙,{} ms 后重试(第{}次)" , sleepMillis , retryTimes + 1 );
212
+ log .warn ("微信系统繁忙,{} ms 后重试(第{}次)" , sleepMillis , retryTimes + 1 );
223
213
Thread .sleep (sleepMillis );
224
214
} catch (InterruptedException e1 ) {
225
215
throw new RuntimeException (e1 );
@@ -230,7 +220,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
230
220
}
231
221
} while (retryTimes ++ < this .maxRetryTimes );
232
222
233
- this . log .warn ("重试达到最大次数【{}】" , this .maxRetryTimes );
223
+ log .warn ("重试达到最大次数【{}】" , this .maxRetryTimes );
234
224
throw new RuntimeException ("微信服务端异常,超出重试次数" );
235
225
}
236
226
@@ -246,7 +236,7 @@ private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E d
246
236
247
237
try {
248
238
T result = executor .execute (uriWithAccessToken , data );
249
- this . log .debug ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【响应数据】:{}" , uriWithAccessToken , dataForLog , result );
239
+ log .debug ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【响应数据】:{}" , uriWithAccessToken , dataForLog , result );
250
240
return result ;
251
241
} catch (WxErrorException e ) {
252
242
WxError error = e .getError ();
@@ -264,12 +254,12 @@ private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E d
264
254
}
265
255
266
256
if (error .getErrorCode () != 0 ) {
267
- this . log .error ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【错误信息】:{}" , uriWithAccessToken , dataForLog , error );
257
+ log .error ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【错误信息】:{}" , uriWithAccessToken , dataForLog , error );
268
258
throw new WxErrorException (error , e );
269
259
}
270
260
return null ;
271
261
} catch (IOException e ) {
272
- this . log .error ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【异常信息】:{}" , uriWithAccessToken , dataForLog , e .getMessage ());
262
+ log .error ("\n 【请求地址】: {}\n 【请求参数】:{}\n 【异常信息】:{}" , uriWithAccessToken , dataForLog , e .getMessage ());
273
263
throw new RuntimeException (e );
274
264
}
275
265
}
@@ -349,4 +339,9 @@ public WxMaShareService getShareService() {
349
339
public WxMaRunService getRunService () {
350
340
return this .runService ;
351
341
}
342
+
343
+ @ Override
344
+ public WxMaSecCheckService getSecCheckService () {
345
+ return this .secCheckService ;
346
+ }
352
347
}
0 commit comments