AuthCodeInputView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if(string.length > 1){
  101. for (int i = 0 ; i < self.tfArr.count; i++) {
  102. AuthCodeTextField *tf = self.tfArr[i];
  103. if(string.length >= i+1)
  104. {
  105. tf.text = [string substringWithRange:NSMakeRange(i, 1)];
  106. }
  107. }
  108. return NO;
  109. }
  110. NSInteger nextTag = textField.tag + 1;
  111. UITextField *nextTextField = [self viewWithTag:nextTag];
  112. if(string.length > 0){
  113. textField.text = string;
  114. if (nextTextField && [nextTextField.text isEqualToString:@""]) {
  115. [nextTextField becomeFirstResponder];
  116. }
  117. else
  118. {
  119. self.codeStr = @"";
  120. for (AuthCodeTextField *tf in self.tfArr) {
  121. self.codeStr = [self.codeStr stringByAppendingString:tf.text];
  122. }
  123. HLog(@"输入的验证码:%@",self.codeStr);
  124. if(self.codeStr.length == 8){
  125. [self checkAuthcode];
  126. }
  127. }
  128. return NO;
  129. }
  130. else
  131. {
  132. return YES;
  133. }
  134. }
  135. // 调用接口验证验证码正确性
  136. -(void)checkAuthcode
  137. {
  138. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  139. NSString *secretkey = [RSATool sha256_8:snStr];
  140. NSString*inputCodeStr = [self.codeStr uppercaseString];
  141. if([secretkey isEqualToString:inputCodeStr]){
  142. if(_authSuccess){
  143. _authSuccess(YES);
  144. }
  145. }
  146. else{
  147. NSString* curTipStr = NSLocalizedString(@"logo_input_pwd_fail",nil);
  148. [[iToast makeText:curTipStr] show];
  149. }
  150. }
  151. -(void)closeAuthCodeInputView{
  152. [self removeFromSuperview];
  153. }
  154. #pragma mark AuthCodeDeleteDelegate 删除输入的值
  155. -(void)authCodeTextFieldDeleteBackward:(AuthCodeTextField *)textField
  156. { self.codeStr = @"";
  157. NSInteger lastTag = textField.tag - 1;
  158. AuthCodeTextField *lastTextField = [self viewWithTag:lastTag];
  159. [lastTextField becomeFirstResponder];
  160. }
  161. #pragma mark NSNotificationCenter
  162. -(void)keyboardWillShow:(NSNotification *)notify
  163. {
  164. // NSDictionary *userInfo = [notify userInfo];
  165. // NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  166. // CGRect keyboardRect = [aValue CGRectValue];
  167. // //height 就是键盘的高度
  168. // int height = keyboardRect.size.height;
  169. // [_backGround mas_remakeConstraints:^(MASConstraintMaker *make) {
  170. // make.left.mas_equalTo(0);
  171. // make.right.mas_equalTo(0);
  172. // make.height.mas_equalTo(320);
  173. // make.bottom.mas_equalTo(-height+10);
  174. // }];
  175. }
  176. @end