Skip to content

Commit 11c80ef

Browse files
committed
防机器人验证码
1 parent 075b7b5 commit 11c80ef

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

Coding_iOS/Util/OC_Category/NSObject+Common.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
+ (void)showStatusBarSuccessStr:(NSString *)tipStr;
2121
+ (void)showStatusBarErrorStr:(NSString *)errorStr;
2222
+ (void)showStatusBarError:(NSError *)error;
23-
+ (void)showCaptchaView;
23+
+ (void)showCaptchaViewParams:(NSMutableDictionary *)params;
2424

2525
#pragma mark BaseURL
2626
+ (NSString *)baseURLStr;

Coding_iOS/Util/OC_Category/NSObject+Common.m

+43-16
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ -(id)handleResponse:(id)responseJSON autoShowError:(BOOL)autoShowError{
338338
});
339339
}
340340
}else{
341+
NSDictionary *msgDict = [responseJSON valueForKey:@"msg"];
342+
__block BOOL need_captcha = NO;
343+
[[msgDict allKeys] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
344+
if ([obj rangeOfString:@"need_captcha"].location != NSNotFound) {
345+
need_captcha = YES;
346+
}
347+
}];
348+
if (need_captcha) {
349+
[NSObject showCaptchaViewParams:nil];
350+
}
341351
if (autoShowError) {
342352
[NSObject showError:error];
343353
}
@@ -347,25 +357,28 @@ -(id)handleResponse:(id)responseJSON autoShowError:(BOOL)autoShowError{
347357
}
348358

349359

350-
+ (void)showCaptchaView{
360+
+ (void)showCaptchaViewParams:(NSMutableDictionary *)params{
361+
//Data
362+
if (!params) {
363+
params = @{}.mutableCopy;
364+
}
365+
if (!params[@"type"]) {
366+
params[@"type"] = @1;
367+
}
368+
NSString *path = @"api/request_valid";
369+
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@api/getCaptcha?type=%@", [NSObject baseURLStr], params[@"type"]]];
370+
//UI
351371
SDCAlertController *alertV = [SDCAlertController alertControllerWithTitle:@"提示" message:@"亲,您操作这么快,不会是机器人吧?\n来,输个验证码先?" preferredStyle:SDCAlertControllerStyleAlert];
352-
353372
UITextField *textF = [UITextField new];
354373
textF.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);
355374
textF.backgroundColor = [UIColor whiteColor];
356375
[textF doBorderWidth:0.5 color:nil cornerRadius:2.0];
357-
358376
UIImageView *imageV = [UIImageView new];
359377
imageV.backgroundColor = [UIColor lightGrayColor];
360378
imageV.contentMode = UIViewContentModeScaleAspectFit;
361379
imageV.clipsToBounds = YES;
362380
imageV.userInteractionEnabled = YES;
363381
[textF doBorderWidth:0.5 color:nil cornerRadius:2.0];
364-
__weak typeof(imageV) weakImageV = imageV;
365-
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@api/getCaptcha", [NSObject baseURLStr]]];
366-
[imageV bk_whenTapped:^{
367-
[weakImageV sd_setImageWithURL:imageURL placeholderImage:nil options:(SDWebImageRetryFailed | SDWebImageRefreshCached | SDWebImageHandleCookies)];
368-
}];
369382
[imageV sd_setImageWithURL:imageURL placeholderImage:nil options:(SDWebImageRetryFailed | SDWebImageRefreshCached | SDWebImageHandleCookies)];
370383

371384
[alertV.contentView addSubview:textF];
@@ -382,17 +395,31 @@ + (void)showCaptchaView{
382395
make.height.mas_equalTo(25);
383396
make.centerY.equalTo(textF);
384397
}];
385-
[alertV addAction:[SDCAlertAction actionWithTitle:@"取消" style:SDCAlertActionStyleDefault handler:nil]];
386-
[alertV addAction:[SDCAlertAction actionWithTitle:@"还真不是" style:SDCAlertActionStyleDefault handler:^(SDCAlertAction *action) {
387-
//校验验证码的接口。。。。
388-
389-
NSLog(@"textF.text ===== %@", textF.text);
390-
}]];
391-
398+
//Action
399+
__weak typeof(imageV) weakImageV = imageV;
400+
[imageV bk_whenTapped:^{
401+
[weakImageV sd_setImageWithURL:imageURL placeholderImage:nil options:(SDWebImageRetryFailed | SDWebImageRefreshCached | SDWebImageHandleCookies)];
402+
}];
403+
__weak typeof(alertV) weakAlertV = alertV;
404+
[alertV addAction:[SDCAlertAction actionWithTitle:@"取消" style:SDCAlertActionStyleCancel handler:nil]];
405+
[alertV addAction:[SDCAlertAction actionWithTitle:@"还真不是" style:SDCAlertActionStyleDefault handler:nil]];
406+
alertV.shouldDismissBlock = ^BOOL (SDCAlertAction *action){
407+
BOOL shouldDismiss = [action.title isEqualToString:@"取消"];
408+
if (!shouldDismiss) {
409+
params[@"j_captcha"] = textF.text;
410+
[[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:path withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
411+
if (data) {
412+
[weakAlertV dismissWithCompletion:^{
413+
[NSObject showHudTipStr:@"验证码正确"];
414+
}];
415+
}
416+
}];
417+
}
418+
return shouldDismiss;
419+
};
392420
[alertV presentWithCompletion:^{
393421
[textF becomeFirstResponder];
394422
}];
395423
}
396424

397-
398425
@end

0 commit comments

Comments
 (0)