AuthCodeInputView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // AuthCodeInputView.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/11/7.
  6. //
  7. #import "AuthCodeInputView.h"
  8. #import "AuthCodeTextField.h"
  9. #import "connectDeviceManager.h"
  10. #import "RSATool.h"
  11. @interface AuthCodeInputView () <UITextFieldDelegate,AuthCodeDeleteDelegate>
  12. @property (nonatomic,strong) NSString *phone;
  13. @property (nonatomic,strong) UIView *backGround;
  14. @property (nonatomic,strong) NSMutableArray *tfArr;
  15. @property (nonatomic,strong) NSString *codeStr;
  16. @end
  17. @implementation AuthCodeInputView
  18. -(instancetype)initWithFrame:(CGRect)frame{
  19. if(self = [super initWithFrame:frame])
  20. {
  21. //_phone = phone;
  22. [self createSubviews];
  23. [self sendSMSMessage];
  24. [[NSNotificationCenter defaultCenter] addObserver:self
  25. selector:@selector(keyboardWillShow:)
  26. name:UIKeyboardWillShowNotification
  27. object:nil];
  28. }
  29. return self;
  30. }
  31. -(void)dealloc{
  32. [[NSNotificationCenter defaultCenter]removeObserver:self];
  33. }
  34. // 发送短信验证码
  35. -(void)sendSMSMessage
  36. {
  37. }
  38. -(void)createSubviews
  39. {
  40. _backGround = [[UIView alloc]init];
  41. _backGround.backgroundColor = HWF5F7FAColor;
  42. [self addSubview:_backGround];
  43. // _backGround.layer.cornerRadius = 10;
  44. // _backGround.layer.masksToBounds = YES;
  45. [_backGround mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.left.mas_equalTo(0);
  47. make.right.mas_equalTo(0);
  48. make.top.mas_equalTo(0);
  49. make.bottom.mas_equalTo(0);
  50. }];
  51. UILabel *titleLab = [[UILabel alloc]init];
  52. titleLab.text = @"请输入8位密钥";
  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. // 获取短信中提取的验证码,自动输入到输入框中
  100. if(string.length > 1){
  101. for (int i = 0 ; i < self.tfArr.count; i++) {
  102. AuthCodeTextField *tf = self.tfArr[i];
  103. tf.text = [string substringWithRange:NSMakeRange(i, 1)];
  104. }
  105. return NO;
  106. }
  107. NSInteger nextTag = textField.tag + 1;
  108. UITextField *nextTextField = [self viewWithTag:nextTag];
  109. if(string.length > 0){
  110. textField.text = string;
  111. if (nextTextField && [nextTextField.text isEqualToString:@""]) {
  112. [nextTextField becomeFirstResponder];
  113. }
  114. else
  115. {
  116. self.codeStr = @"";
  117. for (AuthCodeTextField *tf in self.tfArr) {
  118. self.codeStr = [self.codeStr stringByAppendingString:tf.text];
  119. }
  120. HLog(@"输入的验证码:%@",self.codeStr);
  121. if(self.codeStr.length == 8){
  122. [self checkAuthcode];
  123. }
  124. }
  125. return NO;
  126. }
  127. else
  128. {
  129. return YES;
  130. }
  131. }
  132. // 调用接口验证验证码正确性
  133. -(void)checkAuthcode
  134. {
  135. NSString *snStr = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
  136. NSString *secretkey = [RSATool sha256_8:snStr];
  137. NSString*inputCodeStr = [self.codeStr uppercaseString];
  138. if([secretkey isEqualToString:inputCodeStr]){
  139. if(_authSuccess){
  140. _authSuccess(YES);
  141. }
  142. }
  143. else{
  144. NSString* curTipStr = NSLocalizedString(@"logo_input_pwd_fail",nil);
  145. [[iToast makeText:curTipStr] show];
  146. }
  147. }
  148. -(void)closeAuthCodeInputView{
  149. [self removeFromSuperview];
  150. }
  151. #pragma mark AuthCodeDeleteDelegate 删除输入的值
  152. -(void)authCodeTextFieldDeleteBackward:(AuthCodeTextField *)textField
  153. { self.codeStr = @"";
  154. NSInteger lastTag = textField.tag - 1;
  155. AuthCodeTextField *lastTextField = [self viewWithTag:lastTag];
  156. [lastTextField becomeFirstResponder];
  157. }
  158. #pragma mark NSNotificationCenter
  159. -(void)keyboardWillShow:(NSNotification *)notify
  160. {
  161. // NSDictionary *userInfo = [notify userInfo];
  162. // NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  163. // CGRect keyboardRect = [aValue CGRectValue];
  164. // //height 就是键盘的高度
  165. // int height = keyboardRect.size.height;
  166. // [_backGround mas_remakeConstraints:^(MASConstraintMaker *make) {
  167. // make.left.mas_equalTo(0);
  168. // make.right.mas_equalTo(0);
  169. // make.height.mas_equalTo(320);
  170. // make.bottom.mas_equalTo(-height+10);
  171. // }];
  172. }
  173. @end