CaptchaCenter.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #import "CaptchaCenter.h"
  2. #define captchaID @"629e4f9060c50e8ec0c0ba2db0580db0"
  3. @interface CaptchaCenter ()<AlicomCaptcha4SessionTaskDelegate>
  4. @property (nonatomic, strong) AlicomCaptcha4Session *captchaSession;
  5. @property (nonatomic, copy) void (^verifyBlock)(NSString *status, NSDictionary *result);
  6. @end
  7. @implementation CaptchaCenter
  8. static id _instance;
  9. + (instancetype)defaultCenter {
  10. static dispatch_once_t onceToken;
  11. dispatch_once(&onceToken, ^{
  12. _instance = [[self alloc] init];
  13. });
  14. return _instance;
  15. }
  16. - (instancetype)init {
  17. self = [super init];
  18. if (self) {
  19. [self captchaSession];
  20. }
  21. return self;
  22. }
  23. // 防止通过拷贝创建新实例
  24. - (id)copyWithZone:(NSZone *)zone {
  25. return _instance;
  26. }
  27. // 防止通过归档创建新实例
  28. - (id)mutableCopyWithZone:(NSZone *)zone {
  29. return _instance;
  30. }
  31. /// 获取验证码
  32. - (void)getCaptchaType:(void(^)(NSNumber * _Nullable type, NSError * _Nullable error))completion {
  33. [LCHttpHelper requestWithURLString:GetCaptchaType parameters:@{} needToken:NO type:HttpRequestTypePost success:^(id responseObject) {
  34. NSDictionary* dict = (NSDictionary*)responseObject;
  35. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  36. if (code == 0) {
  37. NSDictionary *dataDict = [dict objectForKey:@"data"];
  38. NSNumber *type = [dataDict objectForKey:@"type"];
  39. completion(type, nil);
  40. } else {
  41. NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:123 userInfo:@{@"msg": @"服务端返回失败"}];
  42. completion(nil, error);
  43. }
  44. } failure:^(NSError *error) {
  45. completion(nil, error);
  46. }];
  47. }
  48. - (AlicomCaptcha4Session *)captchaSession {
  49. if (!_captchaSession) {
  50. _captchaSession = [AlicomCaptcha4Session sessionWithCaptchaID:captchaID];
  51. /// 如需修改默认配置
  52. /// 可选择下⾯注释的⽅式创建实例
  53. // AlicomCaptcha4SessionConfiguration *config = [AlicomCaptcha4SessionConfiguration defaultConfiguration];
  54. // config.timeout = 8.0f;
  55. // ...
  56. // _captchaSession = [AlicomCaptcha4Session sessionWithCaptchaID:captchaID configuration:config];
  57. _captchaSession.delegate = self;
  58. }
  59. return _captchaSession;
  60. }
  61. - (void)verifyCompletion:(void(^)(NSString *status, NSDictionary *result))completion {
  62. [self.captchaSession verify];
  63. self.verifyBlock = completion;
  64. }
  65. - (void)alicomCaptchaSession:(AlicomCaptcha4Session *)captchaSession didReceive:(NSString *)status result:(nullable NSDictionary *)result {
  66. NSLog(@"status: %@" , status);
  67. NSLog(@"result: %@" , result);
  68. // status 为 @"1"则为用户完成了验证,为@"0"则为用户验证失败
  69. self.verifyBlock(status, result);
  70. }
  71. - (void)alicomCaptchaSession:(AlicomCaptcha4Session *)captchaSession didReceiveError:(AlicomC4Error *)error {
  72. NSLog(@"error: %@", error.description);
  73. }
  74. @end