// // ChangePasswordViewController.m // watch // // Created by xTT on 2017/7/13. // Copyright © 2017年 xTT. All rights reserved. // #import "ChangePasswordViewController.h" @interface ChangePasswordViewController () @property (strong, nonatomic) NSMutableDictionary *parameters; @end @implementation ChangePasswordViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { _myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"旧密码",@"新密码",@"确认密码"]]]; } return _myDataSource; } - (NSMutableDictionary *)parameters{ if (!_parameters) { _parameters = [NSMutableDictionary dictionary]; } return _parameters; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarItemClick:)]; } - (IBAction)rightBarItemClick:(UIBarButtonItem *)sender{ NSString *new_pwd1 = self.parameters[@"新密码"]; NSString *new_pwd2 = self.parameters[@"确认密码"]; if ([self.parameters[@"旧密码"] length] == 0) { [UICommon MessageErrorText:@"请输入旧密码"]; }else if (new_pwd1.length == 0) { [UICommon MessageErrorText:@"请输入新密码"]; }else if (new_pwd2.length == 0) { [UICommon MessageErrorText:@"请输入确认密码"]; }else if(new_pwd1.length < 6 || new_pwd1.length > 16){ [UICommon MessageErrorText:@"新密码为6~16位"]; }else if (![new_pwd1 isEqualToString:new_pwd2]){ [UICommon MessageErrorText:@"新密码输入不一致"]; }else{ if(!cUser){ //如果是nil的 返回 多次点击会出现的问题 return; } NSString *urlStr = [MyHttp getURL:@"getway/accounts/[openid]" objArr:@[cUser]]; [self.parameters setObject:self.parameters[@"旧密码"] forKey:@"password"]; [self.parameters setObject:self.parameters[@"新密码"] forKey:@"newPassword"]; // [self.parameters removeObjectForKey:@"旧密码"]; // [self.parameters removeObjectForKey:@"新密码"]; // [self.parameters removeObjectForKey:@"确认密码"]; [xMyHttp URL:urlStr method:@"PATCH" parameters:self.parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { [UICommon MessageErrorText:@"修改成功,请重新登录"]; [User setCurrentUser:nil]; if (!cUser) { //清空记住密码的 [UserDefaults setBool:NO forKey:kifRememberPWDKey]; [UICommon HidenLoading]; [UserDefaults removeObjectForKey:@"lastpwd"]; AfterDispatch(1, ^{ [[NSNotificationCenter defaultCenter] postNotificationName:@"outLogin" object:nil]; }); } } } failure:^(NSURLSessionDataTask *task, NSError *error) {}]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; TextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCell"]; if (!cell) { cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TextFieldCell"]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textField.keyboardType = UIKeyboardTypeASCIICapable; cell.textField.delegate = self; } cell.textLabel.text = title; if ([title isEqualToString:@"旧密码"]) { cell.textField.placeholder = @"旧密码"; }else if ([title isEqualToString:@"新密码"]){ cell.textField.placeholder = @"6~16位"; }else if([title isEqualToString:@"确认密码"]){ cell.textField.placeholder = @"6~16位"; } WEAKSELF cell.block = ^(NSString *text, TextFieldCell *blockCell){ [weakSelf.parameters setObject:text forKey:title]; }; return cell; } //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ // if (section == self.myDataSource.count - 1) { // return 10 + 44 + 10; // } // return [super tableView:tableView heightForFooterInSection:section]; //} // //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ // UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; // if (section == self.myDataSource.count - 1) { // UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(14, 10, // SWIDTH-28, 44)]; // [btn setTitle:@"确定" forState:UIControlStateNormal]; // [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // [btn setBackgroundColor:mainColor]; // [btn addTarget:self action:@selector(rightBarItemClick:) forControlEvents:UIControlEventTouchUpInside]; // btn.layer.cornerRadius = 3; // btn.layer.masksToBounds = YES; // // [view addSubview:btn]; // } // return view; //} - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if ([string isEqualToString:@" "]) { return NO; } // 最终的结果 NSString *str = textField.text; NSString *resultStr = [str stringByReplacingCharactersInRange:range withString:string]; if (resultStr.length>16){ return NO; } BOOL isCorrect = YES; NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789.QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; if(![string isEqualToString:filtered]) { isCorrect = NO; textField.text = filtered; } return isCorrect; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end