SPSBChangePasswordTableViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // SPSBChangePasswordTableViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/21.
  6. //
  7. #import "SPSBChangePasswordTableViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "UIViewController+SPSBNetworkManager.h"
  10. #import "SPSBTextField.h"
  11. #import "SPSBVerificationProfile.h"
  12. #import "SPSBKeyProfile.h"
  13. #import "SPSBVerificationProfile.h"
  14. #import "SPSBRecoverPasswordTableViewController.h"
  15. #define spsb_changePasswordTextFieldTag 3000
  16. @interface SPSBChangePasswordTableViewController ()<UITextFieldDelegate>
  17. @end
  18. @implementation SPSBChangePasswordTableViewController
  19. - (instancetype)init {
  20. self = [super initWithStyle:UITableViewStyleGrouped backgroundColor:SPSBTableViewBackgroundColorGray];
  21. if (!self) return nil;
  22. return self;
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self setupUI];
  27. }
  28. #pragma mark - Action
  29. - (void)forgetPasswordAction {
  30. SPSBRecoverPasswordTableViewController *vc = SPSBRecoverPasswordTableViewController.new;
  31. vc.spsb_phone = [jxh_userDefaults() objectForKey:SPSBUserName];
  32. [self.navigationController pushViewController:vc animated:true];
  33. }
  34. - (void)commitAction {
  35. jxh_endEdit();
  36. NSString *prePassword = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:spsb_changePasswordTextFieldTag]).text;
  37. NSString *newPassword = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] viewWithTag:spsb_changePasswordTextFieldTag + 2]).text;
  38. NSString *newPasswordAgain = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]] viewWithTag:spsb_changePasswordTextFieldTag + 3]).text;
  39. if (![jxh_md5(prePassword).lowercaseString isEqualToString:[jxh_userDefaults() objectForKey:SPSBUserCode]]) {
  40. [self.view showToastWithTitle:@"原密码错误"];
  41. } else if (newPassword.length > SPSBMaxPasswordLength || newPassword.length < SPSBMinPasswordLength || !spsb_matchPassword(newPassword)) {
  42. [self.view showToastWithTitle:@"请输入6-18位密码"];
  43. } else if (![newPassword isEqualToString:newPasswordAgain]) {
  44. [self.view showToastWithTitle:@"两次密码不相同"];
  45. } else {
  46. [self changePasswordWithPre:jxh_md5(prePassword).lowercaseString new:jxh_md5(newPassword).lowercaseString];
  47. }
  48. }
  49. #pragma mark - Network Action
  50. - (void)changePasswordWithPre:(NSString *)pre new:(NSString *)new {
  51. @weakify(self)
  52. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:@"正在修改" isLogin:true url:spsb_appUrl(SPSBUrlChangePassword) urlParameters:nil parameters:^NSDictionary * _Nonnull{
  53. return @{@"prePassword" : pre, @"password" : new};
  54. } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  55. @strongify(self)
  56. [jxh_userDefaults() setObject:new forKey:SPSBUserCode];
  57. [jxh_userDefaults() synchronize];
  58. [self.navigationController popViewControllerAnimated:true];
  59. return @"修改成功";
  60. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  61. return @"修改失败";
  62. }];
  63. }
  64. #pragma mark - UITextFieldDelegate
  65. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  66. jxh_endEdit();
  67. return true;
  68. }
  69. #pragma mark - Delegate & DataSource
  70. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  71. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  72. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  73. NSArray *titleArray = @[@"原密码", @"", @"新密码", @"确认密码"];
  74. NSArray *placeholderArray = @[@"请输入原密码", @"", @"请设置新密码", @"请确认新密码"];
  75. UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(16) text:titleArray[indexPath.section * 2 + indexPath.row] textColor:spsb_333333_color()];
  76. [cell.contentView addSubview:titleLabel];
  77. [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
  78. make.left.equalTo(15);
  79. make.centerY.equalTo(0);
  80. }];
  81. 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];
  82. textField.spsb_maxCount = SPSBMaxPasswordLength;
  83. textField.tag = spsb_changePasswordTextFieldTag + indexPath.section * 2 + indexPath.row;
  84. textField.secureTextEntry = true;
  85. [cell.contentView addSubview:textField];
  86. [textField makeConstraints:^(JXHConstraintMaker *make) {
  87. make.trailing.equalTo(-15);
  88. make.leading.equalTo(119);
  89. make.top.and.bottom.equalTo(0);
  90. }];
  91. if (indexPath.row == 1) {
  92. [cell.contentView createLineWithLocation:JXHLineLocationTop headOffset:16 footOffset:0];
  93. }
  94. return cell;
  95. }
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  97. return 2;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  100. if (section == 0) return 1;
  101. return 2;
  102. }
  103. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  104. return 12;
  105. }
  106. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  107. return UIView.new;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  110. return jxh_onePixe();
  111. }
  112. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  113. return UIView.new;
  114. }
  115. #pragma mark - UI
  116. - (void)setupUI {
  117. self.title = @"修改密码";
  118. [self createRightButton];
  119. [self setupTableView];
  120. }
  121. - (void)createRightButton {
  122. UIButton *rightButton = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(forgetPasswordAction)];
  123. [rightButton setTitle:@"忘记密码" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
  124. [rightButton sizeToFit];
  125. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
  126. self.navigationItem.rightBarButtonItem = item;
  127. }
  128. - (void)setupTableView {
  129. self.tableView.rowHeight = 50.f;
  130. UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, jxh_screenWidth(), 69)];
  131. UIButton *commitButton = [UIButton convenienceWithFont:spsb_semiboldFont(16) target:self action:@selector(commitAction)];
  132. [commitButton setTitle:@"确定" titleColor:spsb_FFFFFF_color(1.f) image:nil backgroundImage:jxh_getImage(confirm_n) state:JXHButtonControlStateNormal];
  133. [commitButton setBackgroundImage:jxh_getImage(confirm_h) state:JXHButtonControlStateHighlighted];
  134. [commitButton setLayerCornerRadius:22.5 clipToBounds:true];
  135. [footerView addSubview:commitButton];
  136. [commitButton makeConstraints:^(JXHConstraintMaker *make) {
  137. make.top.equalTo(24);
  138. make.leading.equalTo(15);
  139. make.trailing.equalTo(-15);
  140. make.height.equalTo(45);
  141. }];
  142. self.tableView.tableFooterView = footerView;
  143. }
  144. @end
  145. #undef spsb_changePasswordTextFieldTag