SPSBRecoverPasswordTableViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // SPSBRecoverPasswordTableViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/5/22.
  6. //
  7. #import "SPSBRecoverPasswordTableViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "UIViewController+SPSBNetworkManager.h"
  10. #import "SPSBVerificationCodeManager.h"
  11. #import "SPSBTextField.h"
  12. #import "SPSBVerificationProfile.h"
  13. #import <JXHVerificationTools.h>
  14. #import "SPSBTipsWordProfile.h"
  15. #import "SPSBKeyProfile.h"
  16. #define spsb_recoverPasswordTextFieldTag 3000
  17. @interface SPSBRecoverPasswordTableViewController ()<UITextFieldDelegate> {
  18. SPSBVerificationCodeManager *_verificationCodeManager;
  19. }
  20. @end
  21. @implementation SPSBRecoverPasswordTableViewController
  22. - (instancetype)init {
  23. self = [super initWithStyle:UITableViewStyleGrouped backgroundColor:SPSBTableViewBackgroundColorGray];
  24. if (!self) return nil;
  25. return self;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self initData];
  30. [self setupUI];
  31. }
  32. - (void)dealloc {
  33. [_verificationCodeManager invalidateTimer];
  34. }
  35. - (void)initData {
  36. _verificationCodeManager = [[SPSBVerificationCodeManager alloc] initWithBaseController:self];
  37. }
  38. #pragma mark - Action
  39. - (void)commitAction {
  40. jxh_endEdit();
  41. NSString *phone = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:spsb_recoverPasswordTextFieldTag]).text;
  42. NSString *verificationCode = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] viewWithTag:spsb_recoverPasswordTextFieldTag + 1]).text;
  43. NSString *newPassword = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]] viewWithTag:spsb_recoverPasswordTextFieldTag + 2]).text;
  44. if (phone.length != SPSBPhoneNumLength || !jxh_isNumber(phone)) {
  45. [self.view showToastWithTitle:SPSBInvalidFormatOfMobilePhone];
  46. } else if (verificationCode.length != SPSBVerifyCodeLength || !jxh_isNumber(verificationCode)) {
  47. [self.view showToastWithTitle:@"请输入4位验证码"];
  48. } else if (!spsb_matchPassword(newPassword) || newPassword.length > SPSBMaxPasswordLength || newPassword.length < SPSBMinPasswordLength) {
  49. [self.view showToastWithTitle:SPSBInvalidFormatOfPassword duration:3];
  50. } else {
  51. jxh_endEdit();
  52. [self recoverPasswordWithPhone:phone verificationCode:verificationCode password:jxh_md5(newPassword).lowercaseString];
  53. }
  54. }
  55. #pragma mark - Network Action
  56. - (void)recoverPasswordWithPhone:(NSString *)phone verificationCode:(NSString *)verificationCode password:(NSString *)password {
  57. @weakify(self)
  58. [self networkUseMethod:SPSBNetworkMethodPOST loadingTips:@"正在提交" isLogin:false url:spsb_appUrl(SPSBUrlFindbackPassword) urlParameters:nil parameters:^NSDictionary * _Nonnull{
  59. return @{@"phone" : phone, @"password" : password, @"verify_code" : verificationCode};
  60. } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  61. @strongify(self)
  62. if (self->_spsb_phone) {
  63. [jxh_userDefaults() setObject:password forKey:SPSBUserCode];
  64. [jxh_userDefaults() synchronize];
  65. [self.navigationController popViewControllerAnimated:true];
  66. return @"找回密码成功";
  67. } else {
  68. [self.navigationController popViewControllerAnimated:true];
  69. return @"密码设置成功,请重新登录";
  70. }
  71. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  72. return @"找回密码失败";
  73. }];
  74. }
  75. #pragma mark - UITextFieldDelegate
  76. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  77. jxh_endEdit();
  78. return true;
  79. }
  80. #pragma mark - Delegate & DataSource
  81. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  82. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  83. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  84. NSArray *placeholderArray = @[@"请输入手机号码", @"请输入验证码", @"请设置新密码"];
  85. NSArray *keyboardArray = @[@(UIKeyboardTypeNumberPad), @(UIKeyboardTypeNumberPad), @(UIKeyboardTypeASCIICapable)];
  86. NSArray *countArray = @[@(SPSBPhoneNumLength), @(SPSBVerifyCodeLength), @(SPSBMaxPasswordLength)];
  87. 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];
  88. textField.spsb_maxCount = [countArray[indexPath.section * 2 + indexPath.row] integerValue];
  89. textField.tag = spsb_recoverPasswordTextFieldTag + indexPath.section * 2 + indexPath.row;
  90. [cell.contentView addSubview:textField];
  91. if (indexPath.row == 1) {
  92. [textField makeConstraints:^(JXHConstraintMaker *make) {
  93. make.trailing.equalTo(-115);
  94. make.leading.equalTo(15);
  95. make.top.and.bottom.equalTo(0);
  96. }];
  97. @weakify(self)
  98. UIButton *verificationButton = [_verificationCodeManager createVerificationCodeButtonWithGetPhone:^NSString * _Nonnull{
  99. @strongify(self)
  100. NSString *phone = ((SPSBTextField *)[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] viewWithTag:spsb_recoverPasswordTextFieldTag]).text;
  101. return phone;
  102. }];
  103. [cell.contentView addSubview:verificationButton];
  104. [verificationButton makeConstraints:^(JXHConstraintMaker *make) {
  105. make.trailing.equalTo(-15);
  106. make.top.and.bottom.equalTo(0);
  107. make.width.equalTo(100);
  108. }];
  109. [cell.contentView createLineWithLocation:JXHLineLocationTop headOffset:16 footOffset:0];
  110. } else {
  111. [textField makeConstraints:^(JXHConstraintMaker *make) {
  112. make.trailing.equalTo(-15);
  113. make.leading.equalTo(15);
  114. make.top.and.bottom.equalTo(0);
  115. }];
  116. if (indexPath.section == 0) {
  117. if (_spsb_phone) {
  118. textField.text = _spsb_phone;
  119. textField.userInteractionEnabled = false;
  120. }
  121. } else {
  122. textField.secureTextEntry = true;
  123. }
  124. }
  125. return cell;
  126. }
  127. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  128. return 2;
  129. }
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  131. if (section == 0) return 2;
  132. return 1;
  133. }
  134. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  135. [tableView deselectRowAtIndexPath:indexPath animated:false];
  136. }
  137. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  138. return 12;
  139. }
  140. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  141. return UIView.new;
  142. }
  143. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  144. return jxh_onePixe();
  145. }
  146. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  147. return UIView.new;
  148. }
  149. #pragma mark - UI
  150. - (void)setupUI {
  151. self.title = @"找回密码";
  152. [self setupTableView];
  153. }
  154. - (void)setupTableView {
  155. self.tableView.rowHeight = 50.f;
  156. UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, jxh_screenWidth(), 69)];
  157. UIButton *commitButton = [UIButton convenienceWithFont:spsb_semiboldFont(16) target:self action:@selector(commitAction)];
  158. [commitButton setTitle:@"确定" titleColor:spsb_FFFFFF_color(1.f) image:nil backgroundImage:jxh_getImage(confirm_n) state:JXHButtonControlStateNormal];
  159. [commitButton setBackgroundImage:jxh_getImage(confirm_h) state:JXHButtonControlStateHighlighted];
  160. [commitButton setLayerCornerRadius:22.5 clipToBounds:true];
  161. [footerView addSubview:commitButton];
  162. [commitButton makeConstraints:^(JXHConstraintMaker *make) {
  163. make.top.equalTo(24);
  164. make.leading.equalTo(15);
  165. make.trailing.equalTo(-15);
  166. make.height.equalTo(45);
  167. }];
  168. self.tableView.tableFooterView = footerView;
  169. }
  170. @end
  171. #undef spsb_recoverPasswordTextFieldTag