// // SPSBRecoverPasswordTableViewController.m // 我的社保 // // Created by shanp on 2021/5/22. // #import "SPSBRecoverPasswordTableViewController.h" #import "SPSBUIGeneralHeader.h" #import "UIViewController+SPSBNetworkManager.h" #import "SPSBVerificationCodeManager.h" #import "SPSBTextField.h" #import "SPSBVerificationProfile.h" #import #import "SPSBTipsWordProfile.h" #import "SPSBKeyProfile.h" #define spsb_recoverPasswordTextFieldTag 3000 @interface SPSBRecoverPasswordTableViewController () { SPSBVerificationCodeManager *_verificationCodeManager; } @end @implementation SPSBRecoverPasswordTableViewController - (instancetype)init { self = [super initWithStyle:UITableViewStyleGrouped backgroundColor:SPSBTableViewBackgroundColorGray]; if (!self) return nil; return self; } - (void)viewDidLoad { [super viewDidLoad]; [self initData]; [self setupUI]; } - (void)dealloc { [_verificationCodeManager invalidateTimer]; } - (void)initData { _verificationCodeManager = [[SPSBVerificationCodeManager alloc] initWithBaseController:self]; } #pragma mark - Action - (void)commitAction { jxh_endEdit(); NSString *phone = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:spsb_recoverPasswordTextFieldTag]).text; NSString *verificationCode = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] viewWithTag:spsb_recoverPasswordTextFieldTag + 1]).text; NSString *newPassword = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] viewWithTag:spsb_recoverPasswordTextFieldTag + 2]).text; if (phone.length != SPSBPhoneNumLength || !jxh_isNumber(phone)) { [self.view showToastWithTitle:SPSBInvalidFormatOfMobilePhone]; } else if (verificationCode.length != SPSBVerifyCodeLength || !jxh_isNumber(verificationCode)) { [self.view showToastWithTitle:@"请输入4位验证码"]; } else if (!spsb_matchPassword(newPassword) || newPassword.length > SPSBMaxPasswordLength || newPassword.length < SPSBMinPasswordLength) { [self.view showToastWithTitle:SPSBInvalidFormatOfPassword duration:3]; } else { jxh_endEdit(); [self recoverPasswordWithPhone:phone verificationCode:verificationCode password:jxh_md5(newPassword).lowercaseString]; } } #pragma mark - Network Action - (void)recoverPasswordWithPhone:(NSString *)phone verificationCode:(NSString *)verificationCode password:(NSString *)password { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:@"正在提交" isLogin:false url:spsb_appUrl(SPSBUrlFindbackPassword) urlParameters:nil parameters:^NSDictionary * _Nonnull{ return @{@"phone" : phone, @"password" : password, @"verify_code" : verificationCode}; } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) if (self->_spsb_phone) { [jxh_userDefaults() setObject:password forKey:SPSBUserCode]; [jxh_userDefaults() synchronize]; [self.navigationController popViewControllerAnimated:true]; return @"找回密码成功"; } else { [self.navigationController popViewControllerAnimated:true]; return @"密码设置成功,请重新登录"; } } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { return @"找回密码失败"; }]; } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { jxh_endEdit(); return true; } #pragma mark - Delegate & DataSource - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; cell.selectionStyle = UITableViewCellSelectionStyleNone; NSArray *placeholderArray = @[@"请输入手机号码", @"请输入验证码", @"请设置新密码"]; NSArray *keyboardArray = @[@(UIKeyboardTypeNumberPad), @(UIKeyboardTypeNumberPad), @(UIKeyboardTypeASCIICapable)]; NSArray *countArray = @[@(SPSBPhoneNumLength), @(SPSBVerifyCodeLength), @(SPSBMaxPasswordLength)]; SPSBTextField *textField = [SPSBTextField convenienceWithFont:spsb_font(16) placeholder:placeholderArray[indexPath.section * 2 + indexPath.row] placeholderColor:spsb_CCCCCC_color() textColor:spsb_333333_color() clearButtonMode:UITextFieldViewModeWhileEditing returnType:UIReturnKeyDone keyboardType:[keyboardArray[indexPath.section * 2 + indexPath.row] integerValue] delegate:self]; textField.spsb_maxCount = [countArray[indexPath.section * 2 + indexPath.row] integerValue]; textField.tag = spsb_recoverPasswordTextFieldTag + indexPath.section * 2 + indexPath.row; [cell.contentView addSubview:textField]; if (indexPath.row == 1) { [textField makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-115); make.leading.equalTo(15); make.top.and.bottom.equalTo(0); }]; @weakify(self) UIButton *verificationButton = [_verificationCodeManager createVerificationCodeButtonWithGetPhone:^NSString * _Nonnull{ @strongify(self) NSString *phone = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:spsb_recoverPasswordTextFieldTag]).text; return phone; }]; [cell.contentView addSubview:verificationButton]; [verificationButton makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-15); make.top.and.bottom.equalTo(0); make.width.equalTo(100); }]; [cell.contentView createLineWithLocation:JXHLineLocationTop headOffset:16 footOffset:0]; } else { [textField makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-15); make.leading.equalTo(15); make.top.and.bottom.equalTo(0); }]; if (indexPath.section == 0) { if (_spsb_phone) { textField.text = _spsb_phone; textField.userInteractionEnabled = false; } } else { textField.secureTextEntry = true; } } return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) return 2; return 1; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:false]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 12; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { return UIView.new; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return jxh_onePixe(); } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return UIView.new; } #pragma mark - UI - (void)setupUI { self.title = @"找回密码"; [self setupTableView]; } - (void)setupTableView { self.tableView.rowHeight = 50.f; UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, jxh_screenWidth(), 69)]; UIButton *commitButton = [UIButton convenienceWithFont:spsb_semiboldFont(16) target:self action:@selector(commitAction)]; [commitButton setTitle:@"确定" titleColor:spsb_FFFFFF_color(1.f) image:nil backgroundImage:jxh_getImage(confirm_n) state:JXHButtonControlStateNormal]; [commitButton setBackgroundImage:jxh_getImage(confirm_h) state:JXHButtonControlStateHighlighted]; [commitButton setLayerCornerRadius:22.5 clipToBounds:true]; [footerView addSubview:commitButton]; [commitButton makeConstraints:^(JXHConstraintMaker *make) { make.top.equalTo(24); make.leading.equalTo(15); make.trailing.equalTo(-15); make.height.equalTo(45); }]; self.tableView.tableFooterView = footerView; } @end #undef spsb_recoverPasswordTextFieldTag