// // RDSResetPwdVC.m // Temperature // // Created by RD on 2022/11/23. // #import "RDSResetPwdVC.h" #import "UIView+RDSClip.h" #import "NSString+verify.h" #import "NSString+Hash.h"// md5 #import "NSString+extension.h" #import "RDSRootControl.h" @interface RDSResetPwdVC () @property (weak, nonatomic) IBOutlet UIView *phoneBgView; @property (weak, nonatomic) IBOutlet UITextField *phoneTF; @property (weak, nonatomic) IBOutlet UIView *codeBgView; @property (weak, nonatomic) IBOutlet UITextField *codeTF; @property (weak, nonatomic) IBOutlet UIButton *codeBtn; @property (weak, nonatomic) IBOutlet UIView *pwdBgView; @property (weak, nonatomic) IBOutlet UITextField *pwdTF; @property (weak, nonatomic) IBOutlet UIButton *stateBtn; @property (weak, nonatomic) IBOutlet UIView *surePwdBgView; @property (weak, nonatomic) IBOutlet UITextField *surePwdTF; @property (weak, nonatomic) IBOutlet UIButton *sureStateBtn; @property (weak, nonatomic) IBOutlet UIButton *sureBtn; @end @implementation RDSResetPwdVC - (void)viewDidLoad { [super viewDidLoad]; [self createUI]; [self p_setupTF]; _phoneTF.text = TheDataManager.phone; } -(void)createUI { self.phoneBgView.layer.cornerRadius = 22.5; self.phoneBgView.layer.borderWidth = 1.0; self.phoneBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor; self.codeBgView.layer.cornerRadius = 22.5; self.codeBgView.layer.borderWidth = 1.0; self.codeBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor; self.pwdBgView.layer.cornerRadius = 22.5; self.pwdBgView.layer.borderWidth = 1.0; self.pwdBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor; self.pwdTF.secureTextEntry = YES; self.surePwdBgView.layer.cornerRadius = 22.5; self.surePwdBgView.layer.borderWidth = 1.0; self.surePwdBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor; self.surePwdTF.secureTextEntry = YES; } - (IBAction)getCodeBtnClick:(UIButton *)sender { if (![self.phoneTF.text isPhoneNum]) { [RDSHudShower showCenterToast:@"请输入正确的手机号码"]; return; } _codeTF.text = @""; [_codeTF becomeFirstResponder]; [RDSHudShower showWithStatus:@"正在获取验证码…" autoDismiss:YES]; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_getVerifyCodePhone:self.phoneTF.text type:2 success:^(id responseObject) { if ([responseObject[@"code"] intValue] != 0) { [RDSHudShower showCenterToast:responseObject[@"message"]]; } else{ [weakSelf timerButton:weakSelf.codeBtn]; //[RDSHudShower showCenterToast:@"获取验证码成功!"]; } } failure:^(NSError *error) { DDLog(@"json data error:/n%@", error.description); [RDSHudShower showCenterToast:@"连接服务器失败"]; }]; } - (IBAction)changePwdState:(id)sender { self.pwdTF.secureTextEntry = !self.pwdTF.secureTextEntry; self.stateBtn.selected = !self.stateBtn.selected; } - (IBAction)changeSurePwdState:(id)sender { self.surePwdTF.secureTextEntry = !self.surePwdTF.secureTextEntry; self.sureStateBtn.selected = !self.sureStateBtn.selected; } - (IBAction)onSureBtnClick{ [self.view endEditing:YES]; if (self.codeTF.text.length < 4){ [RDSHudShower showCenterToast:@"请输入验证码"]; return; } if (![self.pwdTF.text isPwd]){ [RDSHudShower showCenterToast:@"请按规则输入新密码"]; return; } if (![self.surePwdTF.text isPwd]){ [RDSHudShower showCenterToast:@"请按规则输入验证密码"]; return; } if (![self.surePwdTF.text isEqualToString:self.pwdTF.text]){ [RDSHudShower showCenterToast:@"输入密码不一致"]; return; } // md5加密 NSString *pwd = [self.pwdTF.text md5String]; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_resetPwdPhone:_phoneTF.text password:pwd code:self.codeTF.text success:^(id responseObject) { if ([responseObject[@"code"] intValue] != 0) { [RDSHudShower showCenterToast:responseObject[@"message"]]; }else{ [weakSelf logoutAlert]; } } failure:^(NSError *error) { [RDSHudShower showCenterToast:@"连接服务器失败"]; }]; } #pragma mark - UITextFieldDelegate - (void)p_setupTF { self.phoneTF.delegate = self; self.codeTF.delegate = self; self.pwdTF.delegate = self; self.surePwdTF.delegate = self; } - (void)textFieldDidEndEditing:(UITextField *)textField{ textField.text = [textField.text removeAllSapce]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField == self.codeTF) { [self.pwdTF becomeFirstResponder]; } else if (textField == self.pwdTF) { [self onSureBtnClick]; } return YES; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } #pragma - mark 倒计时 -(void)timerButton:(UIButton *)button { __block int timeout = 60; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_timer, ^{ if(timeout <= 0) //倒计时结束,关闭 { dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示 根据自己需求设置 button.userInteractionEnabled = YES; [button setTitle:@"获取验证码" forState:UIControlStateNormal]; }); } else { NSString *strTime = [NSString stringWithFormat:@"%ds重新获取",timeout]; dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示 根据自己需求设置 [button setTitle:strTime forState:UIControlStateNormal]; button.userInteractionEnabled = NO; }); timeout--; } }); dispatch_resume(_timer); } -(void)logoutAlert { RDS_WEAKSELF(weakSelf) UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"修改成功,请重新登录" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self logoutAction]; }]; [alertController addAction:sureAction]; // UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { // // }]; // [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil]; } -(void)logoutAction { [RDSHudShower showWithStatus:@"加载中..." autoDismiss:NO]; [RDSDemoApiHelper rds_logoutSuccess:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower dismissHUD]; [RDSRootControl shareControl].isLoginSuccess = NO; } else{ [RDSHudShower showErrorWithStatus:responseObject[@"message"]]; } } failure:^(NSError *error) { [RDSHudShower showBottomToast:@"连接服务器失败"]; [RDSRootControl shareControl].isLoginSuccess = NO; }]; TheDataManager.token = @""; } @end