// // MOVerifyCodeVC.m // MiMoLive // // Created by MiMo on 2025/6/3. // #import "MOVerifyCodeVC.h" #import "UIImage+YYAdd.h" #import "MOInputCodeView.h" #import "MOLoginManager.h" #define kMOVerifycodeTime 120 @interface MOVerifyCodeVC () @property (nonatomic, strong) UIImageView *bgImgView; @property (nonatomic, strong) BigBtn *backButton; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *sendLabel; @property (nonatomic, strong) UILabel *phoneLabel; @property (nonatomic, strong) MOInputCodeView *codeView; @property (nonatomic, strong) UILabel *countdownLabel; @property (nonatomic, strong) UIButton *resendButton; @property (nonatomic, copy) NSString *countryCode; @property (nonatomic, copy) NSString *phone; @property (nonatomic, strong) NSTimer *countdownTimer; @property (nonatomic, assign) NSInteger countdownSeconds; /** 是否越狱 */ @property (nonatomic, assign) BOOL isRootMobile; /** 是否虚拟设备 */ @property (nonatomic, assign) BOOL isVirtual; @end @implementation MOVerifyCodeVC - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self stopTimer]; } - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; self.isRootMobile = [SecurityBridge isJailbroken];//是否越狱 self.isVirtual = [SecurityBridge isRunningInEmulator];//是否虚拟机 [self startTimer]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.codeView showKeyboard]; }); } - (void)setupUI { [self.view addSubview:self.bgImgView]; [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(0); }]; [self.view addSubview:self.backButton]; [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(kTopSafeAreaInset + 10); make.left.mas_equalTo(12); make.size.mas_equalTo(CGSizeMake(30, 30)); }]; [self.view addSubview:self.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.backButton.mas_bottom).offset(30); make.left.mas_equalTo(24); make.right.mas_equalTo(-36); }]; [self.view addSubview:self.sendLabel]; [self.sendLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.titleLabel.mas_bottom).offset(16); make.left.mas_equalTo(24); make.right.mas_equalTo(-24); }]; [self.view addSubview:self.phoneLabel]; [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.sendLabel.mas_bottom).offset(2); make.left.mas_equalTo(24); make.right.mas_equalTo(-24); }]; [self.view addSubview:self.codeView]; [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.phoneLabel.mas_bottom).offset(24); make.left.mas_equalTo(24); make.right.mas_equalTo(-24); make.height.mas_equalTo(kScaleWidth(64)); }]; [self.view addSubview:self.countdownLabel]; [self.countdownLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.codeView.mas_bottom).offset(40); make.left.mas_equalTo(20); make.right.mas_equalTo(-24); }]; [self.view addSubview:self.resendButton]; [self.resendButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(self.countdownLabel); make.left.mas_equalTo(20); }]; } - (void)startTimer { if (self.countryCode.length == 0 || self.phone.length == 0) { return; } self.countdownSeconds = kMOVerifycodeTime; self.resendButton.hidden = YES; self.countdownLabel.hidden = NO; self.countdownLabel.text = [NSString stringWithFormat:@"%@ %zds", NSLocalString(@"mimo_login_resend_code_countdown"), self.countdownSeconds]; self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES]; } - (void)updateCountdown { self.countdownSeconds--; if (self.countdownSeconds <= 0) { [self stopTimer]; self.countdownLabel.hidden = YES; self.resendButton.hidden = NO; } else { self.countdownLabel.text = [NSString stringWithFormat:@"%@ %zds", NSLocalString(@"mimo_login_resend_code_countdown"), self.countdownSeconds]; } } - (void)stopTimer { [self.countdownTimer invalidate]; self.countdownTimer = nil; } - (void)resendButtonAction { WEAKSELF [self sendSmsClickAndBlock:^(BOOL isSuccess) { if (isSuccess) { [weakSelf startTimer]; } }]; } -(void)sendSmsClickAndBlock:(void (^)(BOOL isSuccess))block{ [MBProgressHUD showActivityMessageInView:@""]; WEAKSELF NSDictionary *dic = @{@"code":self.countryCode, @"num":self.phone}; [kHttpManager loginAndGetMobileCodeWithParams:dic andBlock:^(id _Nonnull data, NSError * _Nonnull error) { [MBProgressHUD hideHUD]; if(kCode_Success){ [MBProgressHUD showTipMessageInView:NSLocalString(@"mimo_login_hint_verify_code")]; block ? block(YES) : nil; } else{ kShowNetError(data) block ? block(NO) : nil; } }]; } - (void)setupCountryCode:(NSString *)countryCode phone:(NSString *)phone { self.phoneLabel.text = [NSString stringWithFormat:@"%@ %@", countryCode, phone]; _countryCode = countryCode; _phone = phone; } - (void)backButtonAction { [self.navigationController popViewControllerAnimated:YES]; } - (UIImageView *)bgImgView { if (!_bgImgView) { _bgImgView = [[UIImageView alloc] init]; _bgImgView.image = [UIImage imageNamed:@"img_login_bg"]; } return _bgImgView; } - (BigBtn *)backButton { if (!_backButton) { _backButton = [[BigBtn alloc] init]; [_backButton setImage:[UIImage imageNamed:@"icon_login_back_white"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.text = NSLocalString(@"mimo_login_input_code"); _titleLabel.font = [MOTextTools poppinsBoldFont:24]; _titleLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF"]; _titleLabel.numberOfLines = 0; } return _titleLabel; } - (UILabel *)sendLabel { if (!_sendLabel) { _sendLabel = [[UILabel alloc] init]; _sendLabel.text = NSLocalString(@"mimo_login_send_code"); _sendLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:0.5]; _sendLabel.font = [MOTextTools regularFont:14]; } return _sendLabel; } - (UILabel *)phoneLabel { if (!_phoneLabel) { _phoneLabel = [[UILabel alloc] init]; _phoneLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:0.5]; _phoneLabel.font = [MOTextTools regularFont:14]; } return _phoneLabel; } - (MOInputCodeView *)codeView { if (!_codeView) { _codeView = [[MOInputCodeView alloc] init]; WEAKSELF _codeView.inputCompleted = ^(NSString *code) { [MBProgressHUD showActivityMessageInView:@""]; NSDictionary *mobileDict = @{@"code": weakSelf.countryCode, @"num": weakSelf.phone}; NSDictionary *dict = @{@"mobile": mobileDict, @"code": code, @"root": @(weakSelf.isRootMobile), @"virtual": @(weakSelf.isVirtual)}; [[MOLoginManager shareManager] loginByMobileCodeWithDict:dict]; }; } return _codeView; } - (UILabel *)countdownLabel { if (!_countdownLabel) { _countdownLabel = [[UILabel alloc] init]; _countdownLabel.textColor = [MOTools colorWithHexString:@"#999999"]; _countdownLabel.font = [MOTextTools regularFont:14]; _countdownLabel.text = NSLocalString(@"mimo_login_resend_code_countdown"); _countdownLabel.numberOfLines = 0; } return _countdownLabel; } - (UIButton *)resendButton { if (!_resendButton) { _resendButton = [[UIButton alloc] init]; _resendButton.titleLabel.font = [MOTextTools regularFont:14]; [_resendButton setTitleColor:[MOTools colorWithHexString:@"#999999"] forState:UIControlStateNormal]; [_resendButton setTitle:NSLocalString(@"mimo_login_resend_code") forState:UIControlStateNormal]; [_resendButton addTarget:self action:@selector(resendButtonAction) forControlEvents:UIControlEventTouchUpInside]; _resendButton.hidden = YES; } return _resendButton; } @end