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. NSString *curTitle = NSLocalizedString(@"input_8_secret_key",nil);
  52. UILabel *titleLab = [[UILabel alloc]init];
  53. titleLab.text = curTitle;
  54. titleLab.textColor = [UIColor hwColor:@"#333333" alpha:1.0];
  55. titleLab.font = [UIFont systemFontOfSize:16];
  56. titleLab.textAlignment = NSTextAlignmentCenter;
  57. [_backGround addSubview:titleLab];
  58. [titleLab sizeToFit];
  59. [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.left.mas_equalTo(0);
  61. make.right.mas_equalTo(0);
  62. make.top.mas_equalTo(40);
  63. make.height.mas_equalTo(30);
  64. }];
  65. self.tfArr = [NSMutableArray array];
  66. // 八个输入框
  67. NSInteger numOfInputs = 8;
  68. CGFloat padding = 8;
  69. CGFloat leftRightPadding = 15;
  70. CGFloat allPadding = leftRightPadding *2 + (numOfInputs - 1)*padding;
  71. CGFloat width = (SCREEN_W - allPadding)/8.0;
  72. for (int i = 1 ; i <= numOfInputs; i ++) {
  73. AuthCodeTextField *textF = [[AuthCodeTextField alloc]init];
  74. textF.tag = i;
  75. textF.auth_delegate = self;
  76. textF.delegate = self;
  77. [_backGround addSubview:textF];
  78. [self.tfArr addObject:textF];
  79. if(i == 1){
  80. [textF becomeFirstResponder];
  81. }
  82. // CGFloat offset = 0;
  83. // if(i <= 3){
  84. // offset = -width*(3-i) - padding *(3-i) - padding/2 - width/2;
  85. // }else{
  86. // offset = width*(i-4) + padding *(i-4) + padding/2 + width/2;
  87. // }
  88. [textF mas_makeConstraints:^(MASConstraintMaker *make) {
  89. make.top.equalTo(titleLab.mas_bottom).offset(25);
  90. make.width.mas_equalTo(width);
  91. make.height.mas_equalTo(85/2);
  92. //make.centerX.mas_equalTo(offset);
  93. make.left.mas_equalTo(leftRightPadding + (padding+width)*(i-1));
  94. }];
  95. }
  96. }
  97. #pragma mark UITextFieldDelegate
  98. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  99. {
  100. // 获取短信中提取的验证码,自动输入到输入框中
  101. if(string.length > 1){
  102. for (int i = 0 ; i < self.tfArr.count; i++) {
  103. AuthCodeTextField *tf = self.tfArr[i];
  104. tf.text = [string substringWithRange:NSMakeRange(i, 1)];
  105. }
  106. return NO;
  107. }
  108. NSInteger nextTag = textField.tag + 1;
  109. UITextField *nextTextField = [self viewWithTag:nextTag];
  110. if(string.length > 0){
  111. textField.text = string;
  112. if (nextTextField && [nextTextField.text isEqualToString:@""]) {
  113. [nextTextField becomeFirstResponder];
  114. }
  115. else
  116. {
  117. self.codeStr = @"";
  118. for (AuthCodeTextField *tf in self.tfArr) {
  119. self.codeStr = [self.codeStr stringByAppendingString:tf.text];
  120. }
  121. HLog(@"输入的验证码:%@",self.codeStr);
  122. if(self.codeStr.length == 8){
  123. [self checkAuthcode];
  124. }
  125. }
  126. return NO;
  127. }
  128. else
  129. {
  130. return YES;
  131. }
  132. }
  133. // 调用接口验证验证码正确性
  134. -(void)checkAuthcode
  135. {
  136. NSString *snStr = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
  137. NSString *secretkey = [RSATool sha256_8:snStr];
  138. NSString*inputCodeStr = [self.codeStr uppercaseString];
  139. if([secretkey isEqualToString:inputCodeStr]){
  140. if(_authSuccess){
  141. _authSuccess(YES);
  142. }
  143. }
  144. else{
  145. NSString* curTipStr = NSLocalizedString(@"logo_input_pwd_fail",nil);
  146. [[iToast makeText:curTipStr] show];
  147. }
  148. }
  149. -(void)closeAuthCodeInputView{
  150. [self removeFromSuperview];
  151. }
  152. #pragma mark AuthCodeDeleteDelegate 删除输入的值
  153. -(void)authCodeTextFieldDeleteBackward:(AuthCodeTextField *)textField
  154. { self.codeStr = @"";
  155. NSInteger lastTag = textField.tag - 1;
  156. AuthCodeTextField *lastTextField = [self viewWithTag:lastTag];
  157. [lastTextField becomeFirstResponder];
  158. }
  159. #pragma mark NSNotificationCenter
  160. -(void)keyboardWillShow:(NSNotification *)notify
  161. {
  162. // NSDictionary *userInfo = [notify userInfo];
  163. // NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  164. // CGRect keyboardRect = [aValue CGRectValue];
  165. // //height 就是键盘的高度
  166. // int height = keyboardRect.size.height;
  167. // [_backGround mas_remakeConstraints:^(MASConstraintMaker *make) {
  168. // make.left.mas_equalTo(0);
  169. // make.right.mas_equalTo(0);
  170. // make.height.mas_equalTo(320);
  171. // make.bottom.mas_equalTo(-height+10);
  172. // }];
  173. }
  174. @end