// // KBRegistVerEmailVC.m // keyBoard // // Created by Mac on 2025/12/3. // #import "KBRegistVerEmailVC.h" #import "CRBoxInputView.h" #import "KBLoginVM.h" #import "KBUser.h" #import "AppDelegate.h" #import "KBEmailLoginVC.h" @interface KBRegistVerEmailVC () @property (nonatomic, strong) UILabel *titleLabel; // Verify Email @property (nonatomic, strong) UILabel *subTitleLabel; // Enter Email Verification Code @property (nonatomic, strong) CRBoxInputView *codeInputView; @property (nonatomic, strong) UILabel *descLabel; // 多行提示文案 @property (nonatomic, strong) UIButton *confirmButton; // Confirm @property (nonatomic, strong) KBLoginVM *loginVM; // Confirm @end @implementation KBRegistVerEmailVC - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.loginVM = [[KBLoginVM alloc] init]; self.kb_titleLabel.text = KBLocalized(@"Verify Email"); [self kb_addTapToDismissKeyboard]; [self setupUI]; // 获取验证码 NSString *email = self.params[@"mailAddress"]; [self.loginVM sendVerifyMailWithEmail:email withCompletion:^(BOOL success, NSError * _Nullable error) { }]; } #pragma mark - UI - (void)setupUI { [self.view addSubview:self.titleLabel]; [self.view addSubview:self.subTitleLabel]; [self.view addSubview:self.codeInputView]; [self.view addSubview:self.descLabel]; [self.view addSubview:self.confirmButton]; // 标题 [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(24); make.top.equalTo(self.view).offset(KB_NAV_TOTAL_HEIGHT + 32); }]; // 副标题 [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.titleLabel); make.top.equalTo(self.titleLabel.mas_bottom).offset(16); }]; // 验证码输入 [self.codeInputView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.subTitleLabel.mas_bottom).offset(24); make.left.equalTo(self.view).offset(24); make.right.equalTo(self.view).offset(-24); make.height.mas_equalTo(56); }]; // 描述文案 [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.codeInputView.mas_bottom).offset(24); make.left.equalTo(self.view).offset(24); make.right.equalTo(self.view).offset(-24); }]; // Confirm 按钮 [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.descLabel.mas_bottom).offset(32); make.left.equalTo(self.view).offset(24); make.right.equalTo(self.view).offset(-24); make.height.mas_equalTo(56); }]; } #pragma mark - Actions - (void)onTapConfirm { NSString *code = self.codeInputView.textValue ?: @""; if (code.length == 0) { [KBHUD showInfo:KBLocalized(@"Enter Email Verification Code")]; return; } KBLOG(@"KBRegistVerEmailVC confirm with code=%@", code); self.params[@"verifyCode"] = code; NSNumber *genderNumber = [self kb_localGenderParamIfAvailable]; if (genderNumber != nil) { self.params[@"gender"] = genderNumber; } [self.loginVM emailRegisterParams:self.params withCompletion:^(BOOL success, NSError * _Nullable error) { if (success) { [KBHUD showInfo:KBLocalized(@"Signed in successfully")]; // 登录成功后切换到主 TabBar dispatch_async(dispatch_get_main_queue(), ^{ KBEmailLoginVC *vc = [[KBEmailLoginVC alloc] init]; [KB_CURRENT_NAV pushViewController:vc animated:true]; // id appDelegate = UIApplication.sharedApplication.delegate; // if ([appDelegate respondsToSelector:@selector(setupRootVC)]) { // AppDelegate *delegate = (AppDelegate *)appDelegate; //// [delegate toMainTabbarVC]; // delegate.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // delegate.window.backgroundColor = [UIColor whiteColor]; // [delegate.window makeKeyAndVisible]; // UIViewController *rootVC = nil; // rootVC = [[BaseTabBarController alloc] init]; // self.window.rootViewController = rootVC; // } }); } else { NSString *msg = error.localizedDescription ?: KBLocalized(@"Sign-in failed"); [KBHUD showInfo:msg]; } }]; } - (nullable NSNumber *)kb_localGenderParamIfAvailable { NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; // 只有在用户真正看过性别选择页后,才认为本地的性别有效 BOOL hasShownSexVC = [ud boolForKey:KBSexSelectShownKey]; if (!hasShownSexVC) { return nil; } NSInteger value = [ud integerForKey:KBSexSelectedGenderKey]; if (value < UserSexMan || value > UserSexTwoSex) { return nil; } return @(value); } #pragma mark - Lazy UI - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [UILabel new]; _titleLabel.text = KBLocalized(@"Verify Email"); _titleLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _titleLabel.font = [KBFont bold:20]; _titleLabel.textAlignment = NSTextAlignmentLeft; } return _titleLabel; } - (UILabel *)subTitleLabel { if (!_subTitleLabel) { _subTitleLabel = [UILabel new]; _subTitleLabel.text = KBLocalized(@"Enter Email Verification Code"); _subTitleLabel.textColor = [UIColor colorWithHex:KBBlackValue]; _subTitleLabel.font = [KBFont regular:14]; _subTitleLabel.textAlignment = NSTextAlignmentLeft; } return _subTitleLabel; } - (CRBoxInputView *)codeInputView { if (!_codeInputView) { _codeInputView = [[CRBoxInputView alloc] initWithCodeLength:6]; _codeInputView.backgroundColor = [UIColor clearColor]; _codeInputView.ifNeedCursor = YES; _codeInputView.keyBoardType = UIKeyboardTypeNumberPad; _codeInputView.inputType = CRInputType_Number; // CRBoxFlowLayout *layout = [[CRBoxFlowLayout alloc] init]; // layout.itemSize = CGSizeMake(40, 48); // layout.minimumInteritemSpacing = 12; // layout.minimumLineSpacing = 12; // layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // _codeInputView.boxFlowLayout = layout; CRBoxInputCellProperty *prop = [[CRBoxInputCellProperty alloc] init]; prop.borderWidth = 0; prop.cornerRadius = 8.0; prop.cellBgColorNormal = [UIColor colorWithHex:0xF7F7F7]; prop.cellBgColorSelected = [UIColor colorWithHex:0xE0F5F2]; prop.cellCursorColor = [UIColor colorWithHex:KBBlackValue]; prop.cellFont = [KBFont bold:20]; prop.cellTextColor = [UIColor colorWithHex:KBBlackValue]; _codeInputView.customCellProperty = prop; [_codeInputView loadAndPrepareView]; } return _codeInputView; } - (UILabel *)descLabel { if (!_descLabel) { _descLabel = [UILabel new]; _descLabel.textColor = [UIColor colorWithHex:0x717171]; _descLabel.font = [KBFont regular:12]; _descLabel.numberOfLines = 0; _descLabel.textAlignment = NSTextAlignmentLeft; // 这里用 %@ 预留邮箱地址占位 NSString *template = KBLocalized(@"We have already sent it to the email address %@. Please enter the 6-digit verification code from the email to verify your mailbox."); // 使用上个页面传入的邮箱;若为空则使用占位邮箱 NSString *email = self.params[@"mailAddress"]; NSString *reEmail = email.length > 0 ? email : @"example@mail.com"; _descLabel.text = [NSString stringWithFormat:template, reEmail]; } return _descLabel; } - (UIButton *)confirmButton { if (!_confirmButton) { _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_confirmButton setTitle:KBLocalized(@"Confirm") forState:UIControlStateNormal]; [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _confirmButton.titleLabel.font = [KBFont medium:16]; _confirmButton.backgroundColor = [UIColor colorWithHex:KBColorValue]; _confirmButton.layer.cornerRadius = 11.0; _confirmButton.layer.masksToBounds = YES; [_confirmButton addTarget:self action:@selector(onTapConfirm) forControlEvents:UIControlEventTouchUpInside]; } return _confirmButton; } - (NSMutableDictionary *)params{ if (!_params) { _params = [NSMutableDictionary dictionary]; } return _params; } @end