15
15
*/
16
16
package top .gcszhn .autocard .service ;
17
17
18
- import java .awt .image .BufferedImage ;
19
- import java .io .IOException ;
20
18
import java .text .SimpleDateFormat ;
21
19
import java .util .ArrayList ;
22
20
import java .util .Date ;
29
27
import lombok .Getter ;
30
28
import top .gcszhn .autocard .AppConfig ;
31
29
import top .gcszhn .autocard .utils .DigestUtils ;
32
- import top .gcszhn .autocard .utils .HttpDataPair ;
33
30
import top .gcszhn .autocard .utils .ImageUtils ;
34
31
import top .gcszhn .autocard .utils .LogUtils ;
35
- import top .gcszhn .autocard .utils .ocr .OCRUtils ;
36
32
import top .gcszhn .autocard .utils .StatusCode ;
37
33
38
34
import org .apache .http .NameValuePair ;
45
41
import org .springframework .context .annotation .Scope ;
46
42
import org .springframework .stereotype .Service ;
47
43
48
- import javax .imageio .ImageIO ;
49
-
50
44
/**
51
45
* 健康打卡实现类
52
46
* @author Zhang.H.N
@@ -65,8 +59,7 @@ public class AutoCardService implements AppService {
65
59
private String reportUrl ;
66
60
@ Value ("${app.autoCard.submitUrl}" )
67
61
private String submitUrl ;
68
- @ Value ("${app.autoCard.codeUrl}" )
69
- private String codeUrl ;
62
+
70
63
/**浙大通行证客户端 */
71
64
@ Autowired
72
65
private ZJUClientService client ;
@@ -201,55 +194,7 @@ public ArrayList<NameValuePair> getOldInfo(String page) {
201
194
}
202
195
return res ;
203
196
}
204
- public BufferedImage getCodeImage () {
205
- if (isOnline ()) {
206
- LogUtils .printMessage ("获取验证码" , LogUtils .Level .INFO );
207
- return Optional .ofNullable (client .doGet (codeUrl + "?_t=" + Math .random ())).map ((HttpDataPair pair )-> {
208
- try {
209
- if (pair .getResponse () != null && pair .getResponse ().getEntity () != null ) {
210
- BufferedImage image = ImageIO .read (pair .getResponse ().getEntity ().getContent ());
211
- return image ;
212
197
213
- }
214
- } catch (IOException e ) {
215
- LogUtils .printMessage (null , e , LogUtils .Level .ERROR );
216
- } finally {
217
- try {
218
- pair .close ();
219
- } catch (IOException e ) {
220
- e .printStackTrace ();
221
- }
222
- }
223
- return null ;
224
- }).orElse (null );
225
- } else {
226
- LogUtils .printMessage ("用户未登录" , LogUtils .Level .ERROR );
227
- }
228
- return null ;
229
- }
230
- public String getCode (BufferedImage codeImage ) {
231
- Optional <String > code = Optional .ofNullable (codeImage ).map ((BufferedImage image )->{
232
- try {
233
- LogUtils .printMessage ("识别验证码" , LogUtils .Level .INFO );
234
- String value = OCRUtils .instance (appConfig .getOcrEngine ()).recognize (image );
235
- if (value != null ) {
236
- value = value .strip ().toUpperCase ();
237
- value .replaceAll ("1" , "I" );
238
- if (value .length () != 4 ) {
239
- value = null ;
240
- }
241
- }
242
- return value ;
243
- } catch (Exception e ) {
244
- LogUtils .printMessage ("验证码识别异常" , e , LogUtils .Level .ERROR );
245
- return null ;
246
- }
247
- });
248
- return code .isPresent () ? code .get (): null ;
249
- }
250
- public String getCode () {
251
- return getCode (getCodeImage ());
252
- }
253
198
/**
254
199
* 用于提交打卡信息
255
200
* @param username 用户名
@@ -278,13 +223,9 @@ public StatusCode submit(String username, String password) {
278
223
LogUtils .printMessage (statusCode .getMessage (), LogUtils .Level .ERROR );
279
224
return statusCode ;
280
225
}
281
- int validCodeMaxTrial = 20 ;
282
- JSONObject resp = null ;
283
- LogUtils .Level level = null ;
284
- int status = 3 ;
285
- NameValuePair codePair = null ;
226
+ JSONObject resp ;
227
+ LogUtils .Level level ;
286
228
String area = null ;
287
-
288
229
ArrayList <NameValuePair > info = getOldInfo (page );
289
230
if (info ==null ) {
290
231
LogUtils .printMessage ("打卡信息获取失败" , LogUtils .Level .ERROR );
@@ -299,45 +240,22 @@ public StatusCode submit(String username, String password) {
299
240
break ;
300
241
}
301
242
}
302
- SAVE : while (validCodeMaxTrial > 0 ) {
303
-
304
- while (validCodeMaxTrial > 0 ) {
305
- validCodeMaxTrial --;
306
- String code = getCode ();
307
- if (code != null ) {
308
- if (codePair != null ) info .remove (codePair );
309
- codePair = new BasicNameValuePair ("verifyCode" , code );
310
- info .add (codePair );
311
- break ;
312
- } else {
313
- LogUtils .printMessage ("验证码识别错误,剩余机会:" + validCodeMaxTrial , LogUtils .Level .ERROR );
314
- }
315
- }
316
243
317
- try {
318
- LogUtils .printMessage ("准备提交打卡 " + username );
319
- resp = JSONObject .parseObject (client .doPostText (submitUrl , info ));
320
- } catch (Exception e ) {
321
- resp = new JSONObject ();
322
- resp .put ("e" , 3 );
323
- resp .put ("m" , "打卡提交失败" );
324
- }
244
+ try {
245
+ LogUtils .printMessage ("准备提交打卡 " + username );
246
+ resp = JSONObject .parseObject (client .doPostText (submitUrl , info ));
247
+ } catch (Exception e ) {
248
+ resp = new JSONObject ();
249
+ resp .put ("e" , 3 );
250
+ resp .put ("m" , "打卡提交失败" );
251
+ }
325
252
326
- status = resp .getIntValue ("e" );
327
- switch (status ) {
328
- case 0 :{level = LogUtils .Level .INFO ;break ;}
329
- case 1 :{
330
- if (validCodeMaxTrial > 0 && resp .getString ("m" ).equals ("验证码错误" )) {
331
- LogUtils .printMessage ("验证码识别错误,剩余机会:" + validCodeMaxTrial , LogUtils .Level .ERROR );
332
- Thread .sleep (2000 );
333
- continue SAVE ;
334
- }
335
- }
336
- default : {
337
- level = LogUtils .Level .ERROR ;
338
- }
253
+ int status = resp .getIntValue ("e" );
254
+ switch (status ) {
255
+ case 0 :{level = LogUtils .Level .INFO ;break ;}
256
+ default : {
257
+ level = LogUtils .Level .ERROR ;
339
258
}
340
- break ;
341
259
}
342
260
343
261
String message = String .format ("%s,你好,今日自动健康打卡状态:%s,打卡地区为:%s(如若区域不符,请次日手动打卡更改地址)" ,
0 commit comments