// // SMCaptchaPopupView.m // MSYOUPAI // // Created by 陈民 on 2025/9/20. // Copyright © 2025 MS. All rights reserved. // #import "SMCaptchaPopupView.h" #import @interface SMCaptchaPopupView () @property (nonatomic, strong) UIView *backgroundMaskView; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) SmCaptchaWKWebView* webview; @end @implementation SMCaptchaPopupView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupUI]; [self initSmCaptcha]; } return self; } - (void)setupUI { self.backgroundColor = [UIColor clearColor]; self.backgroundMaskView = [[UIView alloc] initWithFrame:self.bounds]; self.backgroundMaskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; self.backgroundMaskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self addSubview:self.backgroundMaskView]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]; [self.backgroundMaskView addGestureRecognizer:tapGesture]; self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth*0.6*1.3, KScreenWidth*0.5*1.3)]; self.contentView.backgroundColor = [UIColor whiteColor]; self.contentView.layer.cornerRadius = 10.0; self.contentView.center=CGPointMake(KScreenWidth/2.0, KScreenHeight/2.0); self.contentView.layer.borderWidth = 1; self.contentView.clipsToBounds = YES; [self addSubview:self.contentView]; } -(void) initSmCaptcha { [_webview removeFromSuperview]; SmCaptchaOption *caOption = [[SmCaptchaOption alloc] init]; [caOption setOrganization:@"TZKcuB7qk1IuTCOfo6Hi"]; [caOption setMode: SM_MODE_SLIDE]; [caOption setAppId:@"default"]; // [caOption setCaptchaHtml:_captchaHtml.text]; // 自定义样式 NSDictionary *extraOption = @{ @"style": @{ @"customFont": @{ // 自定义字体 @"name": @"Walsheim", // 字体名,没有特别的限制 // 这个ttf文件必须由客户配置,必须是绝对地址,以https或是http开头的url,且必须支持跨域(设置CORS)。 @"url": @"http://castatic-dev.fengkongcloud.com/pr/v1.0.4/assets/GT-Walsheim-Pro-Bold.ttf", }, @"fontFamily": @"Arial", // 与customFont只能存在一种,不然会覆盖自定义字体 @"fontWeight": @(600), // 字体粗度 @"headerTitle": @"Vertification1", // 顶部自定义标题 @"hideRefreshOnImage": @(YES), // 是否隐藏图片上的刷新按钮,默认是有的 @"slideBar": @{ // 进度条自定义样式 @"color": @"#141C30", // 进度条上默认字体颜色 @"successColor": @"#FFF", // 成功时字体颜色 @"showTipWhenMove":@(YES), // 当拖动的时候后面文案依旧显示 @"process": @{ @"border": @"none", // 进度条边框,目前只是用来设置清除边框 @"background": @"#F3F8CE", // 默认进度条背景色 @"successBackground": @"#25BC73", // 成功时背景色 @"failBackground": @"#F5E0E2", // 失败时背景色 }, @"button": @{ @"boxShadow": @"none", // 按钮阴影设置,一般用来去除默认阴影的作用 @"color": @"#333", // 默认按钮中的icon颜色 @"background": @"#F6FF7E", // 默认按钮背景色 @"successBackground": @"#25BC73", // 成功时按钮背景色 @"failBackground": @"#ED816E", // 失败时按钮背景色 } }, }, }; NSMutableDictionary *extra = [NSMutableDictionary dictionaryWithDictionary:extraOption]; [caOption setExtOption:extra]; // 测试新增字段captchaUuid,不设置,SDK内部生成一个32位的 NSString *newUUID = [[NSUUID UUID] UUIDString]; [caOption setCaptchaUuid:newUUID]; _webview = [[SmCaptchaWKWebView alloc] init]; CGSize parentSize = self.contentView.bounds.size; [_webview setFrame:CGRectMake(0, 0, parentSize.width,parentSize.height)]; // (选填项)连接新加坡机房特殊配置项,仅供验证码数据上报新加坡机房客户使用 // [caOption setCdnHost:@"castatic-xjp.fengkongcloud.com"]; // [caOption setHost:@"captcha-xjp.fengkongcloud.com"]; // (选填项)连接美国机房特殊配置项,仅供验证码数据上报美国机房客户使用 // [caOption setCdnHost:@"castatic-fjny.fengkongcloud.com"]; // [caOption setHost:@"captcha-fjny.fengkongcloud.com"]; NSInteger code = [_webview createWithOption:caOption delegate:self]; if (SmCaptchaSuccess != code) { NSLog(@"SmCaptchaWKWebView init failed %ld", code); } else { [self.contentView addSubview:_webview]; } } #pragma mark - Public Methods - (void)showInView:(UIView *)view { self.frame = view.bounds; [view addSubview:self]; self.alpha = 0; self.contentView.transform = CGAffineTransformMakeScale(0.5, 0.5); [UIView animateWithDuration:0.3 animations:^{ self.alpha = 1; self.contentView.transform = CGAffineTransformIdentity; }]; } - (void)dismiss { [UIView animateWithDuration:0.3 animations:^{ self.alpha = 0; self.contentView.transform = CGAffineTransformMakeScale(0.5, 0.5); } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } - (void)onReady{ NSLog(@"ready"); } - (void)onSuccess:(NSString *)rid pass:(BOOL)pass { NSLog(@"success , rid : %@ , pass : %d",rid,pass); NSString *result = [[NSString alloc] initWithFormat:@"ispass:%@, rid:%@ ", pass ? @"pass": @"reject", rid]; } - (void)onError:(NSInteger)code { NSLog(@"error : %zd",code); } - (void)onClose { NSLog(@"onClose"); } - (void)onInitWithContent:(NSDictionary *)content { NSLog(@"onInitWithContent content : %@",content); } - (void)onReadyWithContent:(NSDictionary *)content { NSLog(@"onReadyWithContent content : %@",content); } - (void)onCloseWithContent:(NSDictionary *)content { NSLog(@"onCloseWithContent content : %@",content); } - (void)onErrorWithContent:(NSDictionary *)content { NSLog(@"onErrorWithContent content : %@",content); } - (void)onSuccessWithContent:(NSDictionary *)content { NSLog(@"onSuccessWithContent content : %@",content); bool pass = content[@"pass"]; if (pass) { if (self.confirmButtonTappedBlock) { self.confirmButtonTappedBlock([NSString stringWithFormat:@"%@",content[@"rid"]]); } [self dismiss]; } } @end