SMCaptchaPopupView.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // SMCaptchaPopupView.m
  3. // MSYOUPAI
  4. //
  5. // Created by 陈民 on 2025/9/20.
  6. // Copyright © 2025 MS. All rights reserved.
  7. //
  8. #import "SMCaptchaPopupView.h"
  9. #import <SmCaptcha/SmCaptchaWebView.h>
  10. @interface SMCaptchaPopupView ()<SmCaptchaProtocol>
  11. @property (nonatomic, strong) UIView *backgroundMaskView;
  12. @property (nonatomic, strong) UIView *contentView;
  13. @property (nonatomic, strong) SmCaptchaWKWebView* webview;
  14. @end
  15. @implementation SMCaptchaPopupView
  16. - (instancetype)initWithFrame:(CGRect)frame {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. [self setupUI];
  20. [self initSmCaptcha];
  21. }
  22. return self;
  23. }
  24. - (void)setupUI {
  25. self.backgroundColor = [UIColor clearColor];
  26. self.backgroundMaskView = [[UIView alloc] initWithFrame:self.bounds];
  27. self.backgroundMaskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
  28. self.backgroundMaskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  29. [self addSubview:self.backgroundMaskView];
  30. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  31. [self.backgroundMaskView addGestureRecognizer:tapGesture];
  32. self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth*0.6*1.3, KScreenWidth*0.5*1.3)];
  33. self.contentView.backgroundColor = [UIColor whiteColor];
  34. self.contentView.layer.cornerRadius = 10.0;
  35. self.contentView.center=CGPointMake(KScreenWidth/2.0, KScreenHeight/2.0);
  36. self.contentView.layer.borderWidth = 1;
  37. self.contentView.clipsToBounds = YES;
  38. [self addSubview:self.contentView];
  39. }
  40. -(void) initSmCaptcha
  41. {
  42. [_webview removeFromSuperview];
  43. SmCaptchaOption *caOption = [[SmCaptchaOption alloc] init];
  44. [caOption setOrganization:@"TZKcuB7qk1IuTCOfo6Hi"];
  45. [caOption setMode: SM_MODE_SLIDE];
  46. [caOption setAppId:@"default"];
  47. // [caOption setCaptchaHtml:_captchaHtml.text];
  48. // 自定义样式
  49. NSDictionary *extraOption = @{
  50. @"style": @{
  51. @"customFont": @{ // 自定义字体
  52. @"name": @"Walsheim", // 字体名,没有特别的限制
  53. // 这个ttf文件必须由客户配置,必须是绝对地址,以https或是http开头的url,且必须支持跨域(设置CORS)。
  54. @"url": @"http://castatic-dev.fengkongcloud.com/pr/v1.0.4/assets/GT-Walsheim-Pro-Bold.ttf",
  55. },
  56. @"fontFamily": @"Arial", // 与customFont只能存在一种,不然会覆盖自定义字体
  57. @"fontWeight": @(600), // 字体粗度
  58. @"headerTitle": @"Vertification1", // 顶部自定义标题
  59. @"hideRefreshOnImage": @(YES), // 是否隐藏图片上的刷新按钮,默认是有的
  60. @"slideBar": @{ // 进度条自定义样式
  61. @"color": @"#141C30", // 进度条上默认字体颜色
  62. @"successColor": @"#FFF", // 成功时字体颜色
  63. @"showTipWhenMove":@(YES), // 当拖动的时候后面文案依旧显示
  64. @"process": @{
  65. @"border": @"none", // 进度条边框,目前只是用来设置清除边框
  66. @"background": @"#F3F8CE", // 默认进度条背景色
  67. @"successBackground": @"#25BC73", // 成功时背景色
  68. @"failBackground": @"#F5E0E2", // 失败时背景色
  69. },
  70. @"button": @{
  71. @"boxShadow": @"none", // 按钮阴影设置,一般用来去除默认阴影的作用
  72. @"color": @"#333", // 默认按钮中的icon颜色
  73. @"background": @"#F6FF7E", // 默认按钮背景色
  74. @"successBackground": @"#25BC73", // 成功时按钮背景色
  75. @"failBackground": @"#ED816E", // 失败时按钮背景色
  76. }
  77. },
  78. },
  79. };
  80. NSMutableDictionary *extra = [NSMutableDictionary dictionaryWithDictionary:extraOption];
  81. [caOption setExtOption:extra];
  82. // 测试新增字段captchaUuid,不设置,SDK内部生成一个32位的
  83. NSString *newUUID = [[NSUUID UUID] UUIDString];
  84. [caOption setCaptchaUuid:newUUID];
  85. _webview = [[SmCaptchaWKWebView alloc] init];
  86. CGSize parentSize = self.contentView.bounds.size;
  87. [_webview setFrame:CGRectMake(0, 0, parentSize.width,parentSize.height)];
  88. // (选填项)连接新加坡机房特殊配置项,仅供验证码数据上报新加坡机房客户使用
  89. // [caOption setCdnHost:@"castatic-xjp.fengkongcloud.com"];
  90. // [caOption setHost:@"captcha-xjp.fengkongcloud.com"];
  91. // (选填项)连接美国机房特殊配置项,仅供验证码数据上报美国机房客户使用
  92. // [caOption setCdnHost:@"castatic-fjny.fengkongcloud.com"];
  93. // [caOption setHost:@"captcha-fjny.fengkongcloud.com"];
  94. NSInteger code = [_webview createWithOption:caOption delegate:self];
  95. if (SmCaptchaSuccess != code) {
  96. NSLog(@"SmCaptchaWKWebView init failed %ld", code);
  97. } else {
  98. [self.contentView addSubview:_webview];
  99. }
  100. }
  101. #pragma mark - Public Methods
  102. - (void)showInView:(UIView *)view {
  103. self.frame = view.bounds;
  104. [view addSubview:self];
  105. self.alpha = 0;
  106. self.contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
  107. [UIView animateWithDuration:0.3 animations:^{
  108. self.alpha = 1;
  109. self.contentView.transform = CGAffineTransformIdentity;
  110. }];
  111. }
  112. - (void)dismiss {
  113. [UIView animateWithDuration:0.3 animations:^{
  114. self.alpha = 0;
  115. self.contentView.transform = CGAffineTransformMakeScale(0.5, 0.5);
  116. } completion:^(BOOL finished) {
  117. [self removeFromSuperview];
  118. }];
  119. }
  120. - (void)onReady{
  121. NSLog(@"ready");
  122. }
  123. - (void)onSuccess:(NSString *)rid pass:(BOOL)pass {
  124. NSLog(@"success , rid : %@ , pass : %d",rid,pass);
  125. NSString *result = [[NSString alloc] initWithFormat:@"ispass:%@, rid:%@ ", pass ? @"pass": @"reject", rid];
  126. }
  127. - (void)onError:(NSInteger)code {
  128. NSLog(@"error : %zd",code);
  129. }
  130. - (void)onClose {
  131. NSLog(@"onClose");
  132. }
  133. - (void)onInitWithContent:(NSDictionary *)content
  134. {
  135. NSLog(@"onInitWithContent content : %@",content);
  136. }
  137. - (void)onReadyWithContent:(NSDictionary *)content
  138. {
  139. NSLog(@"onReadyWithContent content : %@",content);
  140. }
  141. - (void)onCloseWithContent:(NSDictionary *)content
  142. {
  143. NSLog(@"onCloseWithContent content : %@",content);
  144. }
  145. - (void)onErrorWithContent:(NSDictionary *)content
  146. {
  147. NSLog(@"onErrorWithContent content : %@",content);
  148. }
  149. - (void)onSuccessWithContent:(NSDictionary *)content
  150. {
  151. NSLog(@"onSuccessWithContent content : %@",content);
  152. bool pass = content[@"pass"];
  153. if (pass) {
  154. if (self.confirmButtonTappedBlock) {
  155. self.confirmButtonTappedBlock([NSString stringWithFormat:@"%@",content[@"rid"]]);
  156. }
  157. [self dismiss];
  158. }
  159. }
  160. @end