123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #import "CaptchaCenter.h"
- #define captchaID @"629e4f9060c50e8ec0c0ba2db0580db0"
- @interface CaptchaCenter ()<AlicomCaptcha4SessionTaskDelegate>
- @property (nonatomic, strong) AlicomCaptcha4Session *captchaSession;
- @property (nonatomic, copy) void (^verifyBlock)(NSString *status, NSDictionary *result);
- @end
- @implementation CaptchaCenter
- static id _instance;
- + (instancetype)defaultCenter {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _instance = [[self alloc] init];
- });
- return _instance;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- [self captchaSession];
- }
- return self;
- }
- // 防止通过拷贝创建新实例
- - (id)copyWithZone:(NSZone *)zone {
- return _instance;
- }
- // 防止通过归档创建新实例
- - (id)mutableCopyWithZone:(NSZone *)zone {
- return _instance;
- }
- /// 获取验证码
- - (void)getCaptchaType:(void(^)(NSNumber * _Nullable type, NSError * _Nullable error))completion {
- [LCHttpHelper requestWithURLString:GetCaptchaType parameters:@{} needToken:NO type:HttpRequestTypePost success:^(id responseObject) {
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code == 0) {
- NSDictionary *dataDict = [dict objectForKey:@"data"];
- NSNumber *type = [dataDict objectForKey:@"type"];
- completion(type, nil);
- } else {
- NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:123 userInfo:@{@"msg": @"服务端返回失败"}];
- completion(nil, error);
- }
- } failure:^(NSError *error) {
- completion(nil, error);
- }];
- }
- - (AlicomCaptcha4Session *)captchaSession {
- if (!_captchaSession) {
- _captchaSession = [AlicomCaptcha4Session sessionWithCaptchaID:captchaID];
- /// 如需修改默认配置
- /// 可选择下⾯注释的⽅式创建实例
- // AlicomCaptcha4SessionConfiguration *config = [AlicomCaptcha4SessionConfiguration defaultConfiguration];
- // config.timeout = 8.0f;
- // ...
- // _captchaSession = [AlicomCaptcha4Session sessionWithCaptchaID:captchaID configuration:config];
- _captchaSession.delegate = self;
- }
- return _captchaSession;
- }
- - (void)verifyCompletion:(void(^)(NSString *status, NSDictionary *result))completion {
- [self.captchaSession verify];
- self.verifyBlock = completion;
- }
- - (void)alicomCaptchaSession:(AlicomCaptcha4Session *)captchaSession didReceive:(NSString *)status result:(nullable NSDictionary *)result {
- NSLog(@"status: %@" , status);
- NSLog(@"result: %@" , result);
- // status 为 @"1"则为用户完成了验证,为@"0"则为用户验证失败
- self.verifyBlock(status, result);
- }
- - (void)alicomCaptchaSession:(AlicomCaptcha4Session *)captchaSession didReceiveError:(AlicomC4Error *)error {
- NSLog(@"error: %@", error.description);
- }
- @end
|