MOVerifyCodeVC.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // MOVerifyCodeVC.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/6/3.
  6. //
  7. #import "MOVerifyCodeVC.h"
  8. #import "UIImage+YYAdd.h"
  9. #import "MOInputCodeView.h"
  10. #import "MOLoginManager.h"
  11. #define kMOVerifycodeTime 120
  12. @interface MOVerifyCodeVC ()
  13. @property (nonatomic, strong) UIImageView *bgImgView;
  14. @property (nonatomic, strong) BigBtn *backButton;
  15. @property (nonatomic, strong) UILabel *titleLabel;
  16. @property (nonatomic, strong) UILabel *sendLabel;
  17. @property (nonatomic, strong) UILabel *phoneLabel;
  18. @property (nonatomic, strong) MOInputCodeView *codeView;
  19. @property (nonatomic, strong) UILabel *countdownLabel;
  20. @property (nonatomic, strong) UIButton *resendButton;
  21. @property (nonatomic, copy) NSString *countryCode;
  22. @property (nonatomic, copy) NSString *phone;
  23. @property (nonatomic, strong) NSTimer *countdownTimer;
  24. @property (nonatomic, assign) NSInteger countdownSeconds;
  25. /** 是否越狱 */
  26. @property (nonatomic, assign) BOOL isRootMobile;
  27. /** 是否虚拟设备 */
  28. @property (nonatomic, assign) BOOL isVirtual;
  29. @end
  30. @implementation MOVerifyCodeVC
  31. - (void)viewWillAppear:(BOOL)animated {
  32. [super viewWillAppear:animated];
  33. [self.navigationController setNavigationBarHidden:YES animated:animated];
  34. }
  35. - (void)viewWillDisappear:(BOOL)animated {
  36. [super viewWillDisappear:animated];
  37. [self stopTimer];
  38. }
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. [self setupUI];
  42. self.isRootMobile = [SecurityBridge isJailbroken];//是否越狱
  43. self.isVirtual = [SecurityBridge isRunningInEmulator];//是否虚拟机
  44. [self startTimer];
  45. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  46. [self.codeView showKeyboard];
  47. });
  48. }
  49. - (void)setupUI {
  50. [self.view addSubview:self.bgImgView];
  51. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.edges.mas_equalTo(0);
  53. }];
  54. [self.view addSubview:self.backButton];
  55. [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.top.mas_equalTo(kTopSafeAreaInset + 10);
  57. make.left.mas_equalTo(12);
  58. make.size.mas_equalTo(CGSizeMake(30, 30));
  59. }];
  60. [self.view addSubview:self.titleLabel];
  61. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.top.equalTo(self.backButton.mas_bottom).offset(30);
  63. make.left.mas_equalTo(24);
  64. make.right.mas_equalTo(-36);
  65. }];
  66. [self.view addSubview:self.sendLabel];
  67. [self.sendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.top.equalTo(self.titleLabel.mas_bottom).offset(16);
  69. make.left.mas_equalTo(24);
  70. make.right.mas_equalTo(-24);
  71. }];
  72. [self.view addSubview:self.phoneLabel];
  73. [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.top.equalTo(self.sendLabel.mas_bottom).offset(2);
  75. make.left.mas_equalTo(24);
  76. make.right.mas_equalTo(-24);
  77. }];
  78. [self.view addSubview:self.codeView];
  79. [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.top.equalTo(self.phoneLabel.mas_bottom).offset(24);
  81. make.left.mas_equalTo(24);
  82. make.right.mas_equalTo(-24);
  83. make.height.mas_equalTo(kScaleWidth(64));
  84. }];
  85. [self.view addSubview:self.countdownLabel];
  86. [self.countdownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  87. make.top.equalTo(self.codeView.mas_bottom).offset(40);
  88. make.left.mas_equalTo(20);
  89. make.right.mas_equalTo(-24);
  90. }];
  91. [self.view addSubview:self.resendButton];
  92. [self.resendButton mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.centerY.equalTo(self.countdownLabel);
  94. make.left.mas_equalTo(20);
  95. }];
  96. }
  97. - (void)startTimer {
  98. if (self.countryCode.length == 0 || self.phone.length == 0) {
  99. return;
  100. }
  101. self.countdownSeconds = kMOVerifycodeTime;
  102. self.resendButton.hidden = YES;
  103. self.countdownLabel.hidden = NO;
  104. self.countdownLabel.text = [NSString stringWithFormat:@"%@ %zds", NSLocalString(@"mimo_login_resend_code_countdown"), self.countdownSeconds];
  105. self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
  106. }
  107. - (void)updateCountdown {
  108. self.countdownSeconds--;
  109. if (self.countdownSeconds <= 0) {
  110. [self stopTimer];
  111. self.countdownLabel.hidden = YES;
  112. self.resendButton.hidden = NO;
  113. } else {
  114. self.countdownLabel.text = [NSString stringWithFormat:@"%@ %zds", NSLocalString(@"mimo_login_resend_code_countdown"), self.countdownSeconds];
  115. }
  116. }
  117. - (void)stopTimer {
  118. [self.countdownTimer invalidate];
  119. self.countdownTimer = nil;
  120. }
  121. - (void)resendButtonAction {
  122. WEAKSELF
  123. [self sendSmsClickAndBlock:^(BOOL isSuccess) {
  124. if (isSuccess) {
  125. [weakSelf startTimer];
  126. }
  127. }];
  128. }
  129. -(void)sendSmsClickAndBlock:(void (^)(BOOL isSuccess))block{
  130. [MBProgressHUD showActivityMessageInView:@""];
  131. WEAKSELF
  132. NSDictionary *dic = @{@"code":self.countryCode,
  133. @"num":self.phone};
  134. [kHttpManager loginAndGetMobileCodeWithParams:dic andBlock:^(id _Nonnull data, NSError * _Nonnull error) {
  135. [MBProgressHUD hideHUD];
  136. if(kCode_Success){
  137. [MBProgressHUD showTipMessageInView:NSLocalString(@"mimo_login_hint_verify_code")];
  138. block ? block(YES) : nil;
  139. }
  140. else{
  141. kShowNetError(data)
  142. block ? block(NO) : nil;
  143. }
  144. }];
  145. }
  146. - (void)setupCountryCode:(NSString *)countryCode phone:(NSString *)phone {
  147. self.phoneLabel.text = [NSString stringWithFormat:@"%@ %@", countryCode, phone];
  148. _countryCode = countryCode;
  149. _phone = phone;
  150. }
  151. - (void)backButtonAction {
  152. [self.navigationController popViewControllerAnimated:YES];
  153. }
  154. - (UIImageView *)bgImgView {
  155. if (!_bgImgView) {
  156. _bgImgView = [[UIImageView alloc] init];
  157. _bgImgView.image = [UIImage imageNamed:@"img_login_bg"];
  158. }
  159. return _bgImgView;
  160. }
  161. - (BigBtn *)backButton {
  162. if (!_backButton) {
  163. _backButton = [[BigBtn alloc] init];
  164. [_backButton setImage:[UIImage imageNamed:@"icon_login_back_white"] forState:UIControlStateNormal];
  165. [_backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
  166. }
  167. return _backButton;
  168. }
  169. - (UILabel *)titleLabel {
  170. if (!_titleLabel) {
  171. _titleLabel = [[UILabel alloc] init];
  172. _titleLabel.text = NSLocalString(@"mimo_login_input_code");
  173. _titleLabel.font = [MOTextTools poppinsBoldFont:24];
  174. _titleLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF"];
  175. _titleLabel.numberOfLines = 0;
  176. }
  177. return _titleLabel;
  178. }
  179. - (UILabel *)sendLabel {
  180. if (!_sendLabel) {
  181. _sendLabel = [[UILabel alloc] init];
  182. _sendLabel.text = NSLocalString(@"mimo_login_send_code");
  183. _sendLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:0.5];
  184. _sendLabel.font = [MOTextTools regularFont:14];
  185. }
  186. return _sendLabel;
  187. }
  188. - (UILabel *)phoneLabel {
  189. if (!_phoneLabel) {
  190. _phoneLabel = [[UILabel alloc] init];
  191. _phoneLabel.textColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:0.5];
  192. _phoneLabel.font = [MOTextTools regularFont:14];
  193. }
  194. return _phoneLabel;
  195. }
  196. - (MOInputCodeView *)codeView {
  197. if (!_codeView) {
  198. _codeView = [[MOInputCodeView alloc] init];
  199. WEAKSELF
  200. _codeView.inputCompleted = ^(NSString *code) {
  201. [MBProgressHUD showActivityMessageInView:@""];
  202. NSDictionary *mobileDict = @{@"code": weakSelf.countryCode,
  203. @"num": weakSelf.phone};
  204. NSDictionary *dict = @{@"mobile": mobileDict,
  205. @"code": code,
  206. @"root": @(weakSelf.isRootMobile),
  207. @"virtual": @(weakSelf.isVirtual)};
  208. [[MOLoginManager shareManager] loginByMobileCodeWithDict:dict];
  209. };
  210. }
  211. return _codeView;
  212. }
  213. - (UILabel *)countdownLabel {
  214. if (!_countdownLabel) {
  215. _countdownLabel = [[UILabel alloc] init];
  216. _countdownLabel.textColor = [MOTools colorWithHexString:@"#999999"];
  217. _countdownLabel.font = [MOTextTools regularFont:14];
  218. _countdownLabel.text = NSLocalString(@"mimo_login_resend_code_countdown");
  219. _countdownLabel.numberOfLines = 0;
  220. }
  221. return _countdownLabel;
  222. }
  223. - (UIButton *)resendButton {
  224. if (!_resendButton) {
  225. _resendButton = [[UIButton alloc] init];
  226. _resendButton.titleLabel.font = [MOTextTools regularFont:14];
  227. [_resendButton setTitleColor:[MOTools colorWithHexString:@"#999999"] forState:UIControlStateNormal];
  228. [_resendButton setTitle:NSLocalString(@"mimo_login_resend_code") forState:UIControlStateNormal];
  229. [_resendButton addTarget:self action:@selector(resendButtonAction) forControlEvents:UIControlEventTouchUpInside];
  230. _resendButton.hidden = YES;
  231. }
  232. return _resendButton;
  233. }
  234. @end