AuthCodeInputView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // AuthCodeInputView.m
  3. // GBOX
  4. //
  5. // Created by David on 2023/11/7.
  6. //
  7. #import "AuthCodeInputView.h"
  8. #import "AuthCodeTextField.h"
  9. #import "RSATool.h"
  10. @interface AuthCodeInputView () <UITextFieldDelegate,AuthCodeDeleteDelegate>
  11. @property (nonatomic,strong) NSString *phone;
  12. @property (nonatomic,strong) UIView *backGround;
  13. @property (nonatomic,strong) NSMutableArray *tfArr;
  14. @property (nonatomic,strong) NSString *codeStr;
  15. @end
  16. @implementation AuthCodeInputView
  17. -(instancetype)initWithFrame:(CGRect)frame{
  18. if(self = [super initWithFrame:frame])
  19. {
  20. //_phone = phone;
  21. [self createSubviews];
  22. [self sendSMSMessage];
  23. [[NSNotificationCenter defaultCenter] addObserver:self
  24. selector:@selector(keyboardWillShow:)
  25. name:UIKeyboardWillShowNotification
  26. object:nil];
  27. }
  28. return self;
  29. }
  30. -(void)dealloc{
  31. [[NSNotificationCenter defaultCenter]removeObserver:self];
  32. }
  33. // 发送短信验证码
  34. -(void)sendSMSMessage
  35. {
  36. }
  37. -(void)createSubviews
  38. {
  39. _backGround = [[UIView alloc]init];
  40. _backGround.backgroundColor = HWF5F7FAColor;
  41. [self addSubview:_backGround];
  42. // _backGround.layer.cornerRadius = 10;
  43. // _backGround.layer.masksToBounds = YES;
  44. [_backGround mas_makeConstraints:^(MASConstraintMaker *make) {
  45. make.left.mas_equalTo(0);
  46. make.right.mas_equalTo(0);
  47. make.top.mas_equalTo(0);
  48. make.bottom.mas_equalTo(0);
  49. }];
  50. NSString *curTitle = NSLocalizedString(@"input_8_secret_key",nil);
  51. UILabel *titleLab = [[UILabel alloc]init];
  52. titleLab.text = curTitle;
  53. titleLab.textColor = [UIColor hwColor:@"#333333" alpha:1.0];
  54. titleLab.font = [UIFont systemFontOfSize:16];
  55. titleLab.textAlignment = NSTextAlignmentCenter;
  56. [_backGround addSubview:titleLab];
  57. [titleLab sizeToFit];
  58. [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.left.mas_equalTo(0);
  60. make.right.mas_equalTo(0);
  61. make.top.mas_equalTo(40);
  62. make.height.mas_equalTo(30);
  63. }];
  64. self.tfArr = [NSMutableArray array];
  65. // 八个输入框
  66. NSInteger numOfInputs = 8;
  67. CGFloat padding = 8;
  68. CGFloat leftRightPadding = 15;
  69. CGFloat allPadding = leftRightPadding *2 + (numOfInputs - 1)*padding;
  70. CGFloat width = (SCREEN_W - allPadding)/8.0;
  71. for (int i = 1 ; i <= numOfInputs; i ++) {
  72. AuthCodeTextField *textF = [[AuthCodeTextField alloc]init];
  73. textF.tag = i;
  74. textF.auth_delegate = self;
  75. textF.delegate = self;
  76. [_backGround addSubview:textF];
  77. [self.tfArr addObject:textF];
  78. if(i == 1){
  79. [textF becomeFirstResponder];
  80. }
  81. // CGFloat offset = 0;
  82. // if(i <= 3){
  83. // offset = -width*(3-i) - padding *(3-i) - padding/2 - width/2;
  84. // }else{
  85. // offset = width*(i-4) + padding *(i-4) + padding/2 + width/2;
  86. // }
  87. [textF mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.top.equalTo(titleLab.mas_bottom).offset(25);
  89. make.width.mas_equalTo(width);
  90. make.height.mas_equalTo(85/2);
  91. //make.centerX.mas_equalTo(offset);
  92. make.left.mas_equalTo(leftRightPadding + (padding+width)*(i-1));
  93. }];
  94. }
  95. }
  96. #pragma mark UITextFieldDelegate
  97. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  98. {
  99. if(string.length > 1){
  100. for (int i = 0 ; i < self.tfArr.count; i++) {
  101. AuthCodeTextField *tf = self.tfArr[i];
  102. if(string.length >= i+1)
  103. {
  104. tf.text = [string substringWithRange:NSMakeRange(i, 1)];
  105. }
  106. }
  107. return NO;
  108. }
  109. NSInteger nextTag = textField.tag + 1;
  110. UITextField *nextTextField = [self viewWithTag:nextTag];
  111. if(string.length > 0){
  112. textField.text = string;
  113. if (nextTextField && [nextTextField.text isEqualToString:@""]) {
  114. [nextTextField becomeFirstResponder];
  115. }
  116. else
  117. {
  118. self.codeStr = @"";
  119. for (AuthCodeTextField *tf in self.tfArr) {
  120. self.codeStr = [self.codeStr stringByAppendingString:tf.text];
  121. }
  122. HLog(@"输入的验证码:%@",self.codeStr);
  123. if(self.codeStr.length == 8){
  124. [self checkAuthcode];
  125. }
  126. }
  127. return NO;
  128. }
  129. else
  130. {
  131. return YES;
  132. }
  133. }
  134. // 调用接口验证验证码正确性
  135. -(void)checkAuthcode
  136. {
  137. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  138. NSString *secretkey = [RSATool sha256_8:snStr];
  139. NSString*inputCodeStr = [self.codeStr uppercaseString];
  140. if([secretkey isEqualToString:inputCodeStr]){
  141. if(_authSuccess){
  142. _authSuccess(YES);
  143. }
  144. }
  145. else{
  146. NSString* curTipStr = NSLocalizedString(@"logo_input_pwd_fail",nil);
  147. [[iToast makeText:curTipStr] show];
  148. }
  149. }
  150. -(void)closeAuthCodeInputView{
  151. [self removeFromSuperview];
  152. }
  153. #pragma mark AuthCodeDeleteDelegate 删除输入的值
  154. -(void)authCodeTextFieldDeleteBackward:(AuthCodeTextField *)textField
  155. { self.codeStr = @"";
  156. NSInteger lastTag = textField.tag - 1;
  157. AuthCodeTextField *lastTextField = [self viewWithTag:lastTag];
  158. [lastTextField becomeFirstResponder];
  159. }
  160. #pragma mark NSNotificationCenter
  161. -(void)keyboardWillShow:(NSNotification *)notify
  162. {
  163. // NSDictionary *userInfo = [notify userInfo];
  164. // NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  165. // CGRect keyboardRect = [aValue CGRectValue];
  166. // //height 就是键盘的高度
  167. // int height = keyboardRect.size.height;
  168. // [_backGround mas_remakeConstraints:^(MASConstraintMaker *make) {
  169. // make.left.mas_equalTo(0);
  170. // make.right.mas_equalTo(0);
  171. // make.height.mas_equalTo(320);
  172. // make.bottom.mas_equalTo(-height+10);
  173. // }];
  174. }
  175. @end