// // SPSBChangePasswordTableViewController.m // 我的社保 // // Created by shanp on 2021/5/21. // #import "SPSBChangePasswordTableViewController.h" #import "SPSBUIGeneralHeader.h" #import "UIViewController+SPSBNetworkManager.h" #import "SPSBTextField.h" #import "SPSBVerificationProfile.h" #import "SPSBKeyProfile.h" #import "SPSBVerificationProfile.h" #import "SPSBRecoverPasswordTableViewController.h" #define spsb_changePasswordTextFieldTag 3000 @interface SPSBChangePasswordTableViewController () @end @implementation SPSBChangePasswordTableViewController - (instancetype)init { self = [super initWithStyle:UITableViewStyleGrouped backgroundColor:SPSBTableViewBackgroundColorGray]; if (!self) return nil; return self; } - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } #pragma mark - Action - (void)forgetPasswordAction { SPSBRecoverPasswordTableViewController *vc = SPSBRecoverPasswordTableViewController.new; vc.spsb_phone = [jxh_userDefaults() objectForKey:SPSBUserName]; [self.navigationController pushViewController:vc animated:true]; } - (void)commitAction { jxh_endEdit(); NSString *prePassword = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:spsb_changePasswordTextFieldTag]).text; NSString *newPassword = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] viewWithTag:spsb_changePasswordTextFieldTag + 2]).text; NSString *newPasswordAgain = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]] viewWithTag:spsb_changePasswordTextFieldTag + 3]).text; if (![jxh_md5(prePassword).lowercaseString isEqualToString:[jxh_userDefaults() objectForKey:SPSBUserCode]]) { [self.view showToastWithTitle:@"原密码错误"]; } else if (newPassword.length > SPSBMaxPasswordLength || newPassword.length < SPSBMinPasswordLength || !spsb_matchPassword(newPassword)) { [self.view showToastWithTitle:@"请输入6-18位密码"]; } else if (![newPassword isEqualToString:newPasswordAgain]) { [self.view showToastWithTitle:@"两次密码不相同"]; } else { [self changePasswordWithPre:jxh_md5(prePassword).lowercaseString new:jxh_md5(newPassword).lowercaseString]; } } #pragma mark - Network Action - (void)changePasswordWithPre:(NSString *)pre new:(NSString *)new { @weakify(self) [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:@"正在修改" isLogin:true url:spsb_appUrl(SPSBUrlChangePassword) urlParameters:nil parameters:^NSDictionary * _Nonnull{ return @{@"prePassword" : pre, @"password" : new}; } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) { @strongify(self) [jxh_userDefaults() setObject:new forKey:SPSBUserCode]; [jxh_userDefaults() synchronize]; [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 *titleArray = @[@"原密码", @"", @"新密码", @"确认密码"]; NSArray *placeholderArray = @[@"请输入原密码", @"", @"请设置新密码", @"请确认新密码"]; UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(16) text:titleArray[indexPath.section * 2 + indexPath.row] textColor:spsb_333333_color()]; [cell.contentView addSubview:titleLabel]; [titleLabel makeConstraints:^(JXHConstraintMaker *make) { make.left.equalTo(15); make.centerY.equalTo(0); }]; SPSBTextField *textField = [SPSBTextField convenienceWithFont:spsb_font(16) placeholder:placeholderArray[indexPath.section * 2 + indexPath.row] placeholderColor:spsb_CCCCCC_color() textColor:spsb_333333_color() clearButtonMode:UITextFieldViewModeNever returnType:UIReturnKeyDone keyboardType:UIKeyboardTypeASCIICapable delegate:self]; textField.spsb_maxCount = SPSBMaxPasswordLength; textField.tag = spsb_changePasswordTextFieldTag + indexPath.section * 2 + indexPath.row; textField.secureTextEntry = true; [cell.contentView addSubview:textField]; [textField makeConstraints:^(JXHConstraintMaker *make) { make.trailing.equalTo(-15); make.leading.equalTo(119); make.top.and.bottom.equalTo(0); }]; if (indexPath.row == 1) { [cell.contentView createLineWithLocation:JXHLineLocationTop headOffset:16 footOffset:0]; } return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) return 1; return 2; } - (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 createRightButton]; [self setupTableView]; } - (void)createRightButton { UIButton *rightButton = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(forgetPasswordAction)]; [rightButton setTitle:@"忘记密码" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal]; [rightButton sizeToFit]; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem = item; } - (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_changePasswordTextFieldTag