RCTGeetestSensebot.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #import "RCTGeetestSensebot.h"
  2. #import "GSColorHelper.h"
  3. #define GSResolveEvent(event) [NSNumber numberWithInteger:event]
  4. #define GSResolveBool(bool) [NSNumber numberWithBool:bool]
  5. static NSString* RCTGSEventName = @"RNGeetestSensebotEvent";
  6. typedef NS_ENUM(NSInteger, RCTGSEventType) {
  7. GS_CAPTCHA = 1,
  8. GS_ERROR = -1
  9. };
  10. @implementation RCTGeetestSensebot {
  11. GT3CaptchaManager *_captchaManager;
  12. }
  13. + (BOOL)requiresMainQueueSetup
  14. {
  15. return YES;
  16. }
  17. - (dispatch_queue_t)methodQueue
  18. {
  19. return dispatch_get_main_queue();
  20. }
  21. RCT_EXPORT_MODULE()
  22. #pragma export methods
  23. RCT_REMAP_METHOD(initCaptchaMgr,
  24. maskColorNum:(nonnull NSNumber *)aMaskColorNum
  25. isDebug:(BOOL)isDebug
  26. resolver:(RCTPromiseResolveBlock)resolve
  27. rejecter:(RCTPromiseRejectBlock)reject)
  28. {
  29. // 初始化 captcha manager
  30. _captchaManager = [[GT3CaptchaManager alloc] initWithAPI1:nil API2:nil timeout:5.0];
  31. _captchaManager.delegate = self;
  32. UIColor *aMaskColor = [GSColorHelper parseColor:aMaskColorNum];
  33. _captchaManager.maskColor = aMaskColor;
  34. [_captchaManager enableDebugMode:isDebug];
  35. resolve(GSResolveBool(YES));
  36. }
  37. RCT_REMAP_METHOD(captcha,
  38. captchaWithSuccess:(nonnull NSNumber *)success
  39. gt:(NSString *)gt
  40. challenge:(NSString *)challenge
  41. api2:(NSString *)api2
  42. resolver:(RCTPromiseResolveBlock)resolve
  43. rejecter:(RCTPromiseRejectBlock)reject)
  44. {
  45. if (!_captchaManager) {
  46. resolve(GSResolveBool(NO));
  47. return;
  48. }
  49. // 行为验证
  50. [_captchaManager configureGTest:gt challenge:challenge success:success withAPI2:api2];
  51. [_captchaManager startGTCaptchaWithAnimated:YES];
  52. resolve(GSResolveBool(YES));
  53. }
  54. RCT_REMAP_METHOD(stopCaptcha,
  55. stopCaptchaResolver:(RCTPromiseResolveBlock)resolve
  56. rejecter:(RCTPromiseRejectBlock)reject)
  57. {
  58. [self stopCaptchaAndClean];
  59. resolve(GSResolveBool(YES));
  60. }
  61. #pragma other methods
  62. - (void)stopCaptchaAndClean
  63. {
  64. if (_captchaManager) {
  65. [_captchaManager stopGTCaptcha];
  66. [_captchaManager closeGTViewIfIsOpen];
  67. _captchaManager = nil;
  68. }
  69. }
  70. #pragma gt3 delegate
  71. // disable sdk request api1
  72. - (BOOL)shouldUseDefaultRegisterAPI:(GT3CaptchaManager *)manager
  73. {
  74. return NO;
  75. }
  76. // get captcha result
  77. - (void)gtCaptcha:(GT3CaptchaManager *)manager didReceiveCaptchaCode:(NSString *)code result:(NSDictionary *)result message:(NSString *)message
  78. {
  79. [self sendEvent:@{
  80. @"type": GSResolveEvent(GS_CAPTCHA),
  81. @"payload": @{
  82. @"code": code,
  83. @"message": message,
  84. @"result": result
  85. }
  86. }];
  87. }
  88. // send user close view event to js
  89. - (void)gtCaptchaUserDidCloseGTView:(GT3CaptchaManager *)manager
  90. {
  91. [self sendEvent:@{
  92. @"type": GSResolveEvent(GS_CAPTCHA),
  93. @"errCode": @"close view",
  94. @"errMsg": @"user close captcha view."
  95. }];
  96. }
  97. // disable sdk request api2
  98. - (BOOL)shouldUseDefaultSecondaryValidate:(GT3CaptchaManager *)manager
  99. {
  100. return NO;
  101. }
  102. - (void)gtCaptcha:(GT3CaptchaManager *)manager didReceiveSecondaryCaptchaData:(NSData *)data response:(NSURLResponse *)response error:(GT3Error *)error decisionHandler:(void (^)(GT3SecondaryCaptchaPolicy))decisionHandler
  103. {
  104. // no use
  105. }
  106. // manager error
  107. - (void)gtCaptcha:(GT3CaptchaManager *)manager errorHandler:(GT3Error *)error
  108. {
  109. [self sendEvent:@{
  110. @"type": GSResolveEvent(GS_ERROR),
  111. @"errCode": error.error_code,
  112. @"errMsg": error.gtDescription
  113. }];
  114. }
  115. #pragma event
  116. - (NSArray<NSString *> *)supportedEvents
  117. {
  118. return @[RCTGSEventName];
  119. }
  120. - (void)sendEvent:(id)body {
  121. [self sendEventWithName:RCTGSEventName body:body];
  122. }
  123. @end