RNLGeetestSensebot.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #import <GT3Captcha/GT3Captcha.h>
  2. #import <React/RCTConvert.h>
  3. #import <React/RCTUtils.h>
  4. #import "RNLGeetestSensebot.h"
  5. typedef NS_ENUM(NSUInteger, RNLGSEvent) {
  6. RNLGSResultEvent = 1,
  7. RNLGSClosedEvent = 2,
  8. RNLGSFailedEvent = 3,
  9. RNLGSErrorEvent = 0,
  10. };
  11. static NSNumber* RNLGSGetEventCode(RNLGSEvent event) {
  12. return [NSNumber numberWithUnsignedInteger:event];
  13. }
  14. typedef NS_ENUM(NSInteger, RNLGSError) {
  15. RNLGSParameterParseError = -1,
  16. };
  17. static NSNumber* RNLGSGetErrorCode(RNLGSError error) {
  18. return [NSNumber numberWithInteger:error];
  19. }
  20. @interface RNLGeetestSensebot () <GT3CaptchaManagerDelegate, GT3CaptchaManagerViewDelegate>
  21. @end
  22. @implementation RNLGeetestSensebot {
  23. GT3CaptchaManager *_manager;
  24. }
  25. RCT_EXPORT_METHOD(start:(NSDictionary *)option)
  26. {
  27. if (_manager == nil) {
  28. _manager = [GT3CaptchaManager alloc];
  29. _manager.delegate = self;
  30. _manager.viewDelegate = self;
  31. }
  32. @try {
  33. // view load timeout
  34. NSTimeInterval timeout = [RCTConvert NSTimeInterval:option[@"loadTimeout"]];
  35. // init manager
  36. _manager = [_manager initWithAPI1:nil API2:nil timeout:timeout];
  37. // debug
  38. BOOL enableDebugMode = [RCTConvert BOOL:option[@"debug"]];
  39. [_manager enableDebugMode: enableDebugMode];
  40. // request timeout
  41. NSTimeInterval gtViewTimeout = [RCTConvert NSTimeInterval:option[@"reqTimeout"]];
  42. [_manager useGTViewWithTimeout:gtViewTimeout];
  43. // lang
  44. GT3LanguageType lang = [RNLGeetestSensebot parseLanguag:
  45. [RCTConvert NSString:option[@"lang"]]];
  46. [_manager useLanguage:lang];
  47. // enable background cancel
  48. BOOL disableBackgroundUserInteraction = ![RCTConvert BOOL:option[@"enableBackgroundCancel"]];
  49. [_manager disableBackgroundUserInteraction:disableBackgroundUserInteraction];
  50. // background color
  51. UIColor *maskColor = [RCTConvert UIColor:option[@"backgroundColorIOS"]];
  52. if (maskColor != nil) {
  53. _manager.maskColor = maskColor;
  54. }
  55. // background blur effect
  56. NSInteger blurEffectStyle = [RCTConvert NSInteger:option[@"backgroundBlurEffectIOS"]];
  57. if (blurEffectStyle >= UIBlurEffectStyleExtraLight && blurEffectStyle <= UIBlurEffectStyleProminent) {
  58. // enum bound
  59. [_manager useVisualViewWithEffect:
  60. [UIBlurEffect effectWithStyle:(UIBlurEffectStyle)blurEffectStyle]];
  61. }
  62. // api1 json result
  63. NSDictionary *api1JSON = RCTJSONParse([RCTConvert NSString:option[@"api1Result"]], nil);
  64. [_manager configureGTest:[api1JSON objectForKey:@"gt"]
  65. challenge:[api1JSON objectForKey:@"challenge"]
  66. success:[api1JSON objectForKey:@"success"]
  67. withAPI2:nil];
  68. } @catch (NSException *e) {
  69. NSMutableString *errorMessage = [NSMutableString new];
  70. [errorMessage appendString:[e name]];
  71. if ([e reason] != nil) {
  72. [errorMessage appendFormat:@", %@", [e reason]];
  73. }
  74. if ([e userInfo] != nil) {
  75. [errorMessage appendFormat:@", %@", [e userInfo]];
  76. }
  77. [self sendEvent:@[
  78. RNLGSGetEventCode(RNLGSErrorEvent),
  79. RNLGSGetErrorCode(RNLGSParameterParseError),
  80. errorMessage]];
  81. return;
  82. }
  83. // registe and start validate
  84. [_manager registerCaptcha:nil];
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. [_manager startGTCaptchaWithAnimated:YES];
  87. });
  88. }
  89. RCT_EXPORT_METHOD(stop:(RCTResponseSenderBlock)callback)
  90. {
  91. if (_manager != nil) {
  92. [_manager stopGTCaptcha];
  93. callback(nil);
  94. }
  95. }
  96. +(GT3LanguageType)parseLanguag:(NSString *)lang
  97. {
  98. if ([lang isEqualToString:@"system"]) {
  99. return GT3LANGTYPE_AUTO;
  100. } else if ([lang isEqualToString:@"zh"]) {
  101. return GT3LANGTYPE_ZH_CN;
  102. } else if ([lang isEqualToString:@"zh-tw"]) {
  103. return GT3LANGTYPE_ZH_TW;
  104. } else if ([lang isEqualToString:@"zh-hk"]) {
  105. return GT3LANGTYPE_ZH_HK;
  106. } else if ([lang isEqualToString:@"en"]) {
  107. return GT3LANGTYPE_EN;
  108. } else if ([lang isEqualToString:@"id"]) {
  109. return GT3LANGTYPE_ID;
  110. } else if ([lang isEqualToString:@"ja"]) {
  111. return GT3LANGTYPE_JA_JP;
  112. } else if ([lang isEqualToString:@"ko"]) {
  113. return GT3LANGTYPE_KO_KR;
  114. } else if ([lang isEqualToString:@"ru"]) {
  115. return GT3LANGTYPE_RU;
  116. } else if ([lang isEqualToString:@"ar"]) {
  117. return GT3LANGTYPE_AR;
  118. } else if ([lang isEqualToString:@"es"]) {
  119. return GT3LANGTYPE_ES;
  120. } else if ([lang isEqualToString:@"pt-pt"]) {
  121. return GT3LANGTYPE_PT_PT;
  122. } else if ([lang isEqualToString:@"fr"]) {
  123. return GT3LANGTYPE_FR;
  124. } else if ([lang isEqualToString:@"de"]) {
  125. return GT3LANGTYPE_DE;
  126. }
  127. return GT3LANGTYPE_AUTO;
  128. }
  129. #pragma GT3 Delegate
  130. - (void)gtCaptcha:(GT3CaptchaManager *)manager errorHandler:(GT3Error *)error
  131. {
  132. NSMutableString *errorMessage = [NSMutableString new];
  133. [errorMessage appendString:[error error_code]];
  134. [errorMessage appendFormat:@", %@", [error gtDescription]];
  135. [self sendEvent:@[RNLGSGetEventCode(RNLGSFailedEvent),
  136. errorMessage]];
  137. }
  138. - (void)gtCaptcha:(GT3CaptchaManager *)manager didReceiveCaptchaCode:(NSString *)code result:(NSDictionary *)result message:(NSString *)message
  139. {
  140. if ([code isEqualToString:@"1"]) {
  141. [self sendEvent:@[RNLGSGetEventCode(RNLGSResultEvent),
  142. RCTJSONStringify(result, nil)]];
  143. } else if ([code isEqualToString:@"0"] && [result count] > 1) {
  144. // code == 0 代表失败, 用户滑动不准确时也会触发这个条件, 过滤 dict 里面字段数量为 1 时的信息
  145. [self sendEvent:@[RNLGSGetEventCode(RNLGSFailedEvent),
  146. RCTJSONStringify(result, nil)]];
  147. }
  148. }
  149. - (void)gtCaptcha:(GT3CaptchaManager *)manager didReceiveSecondaryCaptchaData:(NSData *)data response:(NSURLResponse *)response error:(GT3Error *)error decisionHandler:(void (^)(GT3SecondaryCaptchaPolicy))decisionHandler
  150. {
  151. }
  152. - (void)gtCaptchaUserDidCloseGTView:(GT3CaptchaManager *)manager
  153. {
  154. [self sendEvent:@[RNLGSGetEventCode(RNLGSClosedEvent)]];
  155. }
  156. - (BOOL)shouldUseDefaultRegisterAPI:(GT3CaptchaManager *)manager
  157. {
  158. return NO;
  159. }
  160. - (BOOL)shouldUseDefaultSecondaryValidate:(GT3CaptchaManager *)manager
  161. {
  162. return NO;
  163. }
  164. #pragma react native bridge
  165. RCT_EXPORT_MODULE(RNLGeetestSensebot)
  166. + (BOOL)requiresMainQueueSetup
  167. {
  168. return NO;
  169. }
  170. static NSString* EventName = @"RNLGeetestSensebotEvent";
  171. - (NSArray<NSString *> *)supportedEvents
  172. {
  173. return @[EventName];
  174. }
  175. - (void)sendEvent:(id)body
  176. {
  177. [self sendEventWithName:EventName body:body];
  178. }
  179. @end