|
| 1 | +// |
| 2 | +// SettingEmailViewController.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 16/5/23. |
| 6 | +// Copyright © 2016年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "SettingEmailViewController.h" |
| 10 | +#import "Input_OnlyText_Cell.h" |
| 11 | +#import "TPKeyboardAvoidingTableView.h" |
| 12 | +#import "Coding_NetAPIManager.h" |
| 13 | +#import "Login.h" |
| 14 | + |
| 15 | +@interface SettingEmailViewController ()<UITableViewDataSource, UITableViewDelegate> |
| 16 | +@property (strong, nonatomic) TPKeyboardAvoidingTableView *myTableView; |
| 17 | +@property (assign, nonatomic) BOOL is2FAOpen; |
| 18 | +@property (strong, nonatomic) NSString *email, *j_captcha, *two_factor_code; |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation SettingEmailViewController |
| 22 | + |
| 23 | +- (void)viewDidLoad{ |
| 24 | + [super viewDidLoad]; |
| 25 | + self.title = @"绑定邮箱"; |
| 26 | + self.email = [Login curLoginUser].email; |
| 27 | + _myTableView = ({ |
| 28 | + TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; |
| 29 | + tableView.backgroundColor = kColorTableSectionBg; |
| 30 | + tableView.dataSource = self; |
| 31 | + tableView.delegate = self; |
| 32 | + tableView.separatorStyle = UITableViewCellSeparatorStyleNone; |
| 33 | + [tableView registerClass:[Input_OnlyText_Cell class] forCellReuseIdentifier:kCellIdentifier_Input_OnlyText_Cell_Text]; |
| 34 | + [tableView registerClass:[Input_OnlyText_Cell class] forCellReuseIdentifier:kCellIdentifier_Input_OnlyText_Cell_Captcha]; |
| 35 | + [tableView registerClass:[Input_OnlyText_Cell class] forCellReuseIdentifier:kCellIdentifier_Input_OnlyText_Cell_Password]; |
| 36 | + [self.view addSubview:tableView]; |
| 37 | + [tableView mas_makeConstraints:^(MASConstraintMaker *make) { |
| 38 | + make.edges.equalTo(self.view); |
| 39 | + }]; |
| 40 | + tableView; |
| 41 | + }); |
| 42 | + self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(doneBtnClicked:)]; |
| 43 | +} |
| 44 | + |
| 45 | +- (void)viewWillAppear:(BOOL)animated{ |
| 46 | + [super viewWillAppear:animated]; |
| 47 | + [self refresh2FA]; |
| 48 | +} |
| 49 | + |
| 50 | +- (void)refresh2FA{ |
| 51 | + __weak typeof(self) weakSelf = self; |
| 52 | + [[Coding_NetAPIManager sharedManager] get_is2FAOpenBlock:^(BOOL data, NSError *error) { |
| 53 | + if (!error) { |
| 54 | + weakSelf.is2FAOpen = data; |
| 55 | + [weakSelf.myTableView reloadData]; |
| 56 | + } |
| 57 | + }]; |
| 58 | +} |
| 59 | + |
| 60 | +#pragma mark TableM |
| 61 | + |
| 62 | +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ |
| 63 | + return 3; |
| 64 | +} |
| 65 | +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 66 | + NSString *identifier = (indexPath.row == 0? kCellIdentifier_Input_OnlyText_Cell_Text: |
| 67 | + indexPath.row == 1? (!_is2FAOpen? kCellIdentifier_Input_OnlyText_Cell_Password: |
| 68 | + kCellIdentifier_Input_OnlyText_Cell_Text): |
| 69 | + kCellIdentifier_Input_OnlyText_Cell_Captcha); |
| 70 | + Input_OnlyText_Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; |
| 71 | + |
| 72 | + __weak typeof(self) weakSelf = self; |
| 73 | + if (indexPath.row == 0) { |
| 74 | + cell.textField.keyboardType = UIKeyboardTypeEmailAddress; |
| 75 | + [cell setPlaceholder:@" 邮箱" value:self.email]; |
| 76 | + cell.textValueChangedBlock = ^(NSString *valueStr){ |
| 77 | + weakSelf.email = valueStr; |
| 78 | + }; |
| 79 | + }else if (indexPath.row == 1){ |
| 80 | + [cell setPlaceholder:!_is2FAOpen? @" 输入密码": @" 输入两步验证码" value:_two_factor_code]; |
| 81 | + cell.textValueChangedBlock = ^(NSString *valueStr){ |
| 82 | + weakSelf.two_factor_code = valueStr; |
| 83 | + }; |
| 84 | + }else{ |
| 85 | + [cell setPlaceholder:@" 验证码" value:self.j_captcha]; |
| 86 | + cell.textValueChangedBlock = ^(NSString *valueStr){ |
| 87 | + weakSelf.j_captcha = valueStr; |
| 88 | + }; |
| 89 | + } |
| 90 | + [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kPaddingLeftWidth]; |
| 91 | + return cell; |
| 92 | +} |
| 93 | + |
| 94 | +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ |
| 95 | + UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 20)]; |
| 96 | + headerView.backgroundColor = kColorTableSectionBg; |
| 97 | + return headerView; |
| 98 | +} |
| 99 | + |
| 100 | +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ |
| 101 | + return 20.0; |
| 102 | +} |
| 103 | + |
| 104 | +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ |
| 105 | + return 0.5; |
| 106 | +} |
| 107 | + |
| 108 | +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ |
| 109 | + [tableView deselectRowAtIndexPath:indexPath animated:YES]; |
| 110 | +} |
| 111 | + |
| 112 | +#pragma mark DoneBtn Clicked |
| 113 | +- (void)doneBtnClicked:(id)sender{ |
| 114 | + NSString *tipStr; |
| 115 | + if (![_email isEmail]) { |
| 116 | + tipStr = @"邮箱格式有误"; |
| 117 | + }else if (_two_factor_code.length <= 0){ |
| 118 | + tipStr = !_is2FAOpen? @"请填写密码": @"请填写两步验证码"; |
| 119 | + }else if (_j_captcha.length <= 0){ |
| 120 | + tipStr = @"请填写验证码"; |
| 121 | + } |
| 122 | + if (tipStr.length > 0) { |
| 123 | + [NSObject showHudTipStr:tipStr]; |
| 124 | + return; |
| 125 | + } |
| 126 | + NSDictionary *params = @{@"email": _email, |
| 127 | + @"j_captcha": _j_captcha, |
| 128 | + @"two_factor_code": !_is2FAOpen? [_two_factor_code sha1Str]: _two_factor_code}; |
| 129 | + __weak typeof(self) weakSelf = self; |
| 130 | + [[CodingNetAPIClient sharedJsonClient] requestJsonDataWithPath:@"api/account/email/change/send" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) { |
| 131 | + if (data) { |
| 132 | + [NSObject showHudTipStr:@"发送验证邮件成功"]; |
| 133 | + [weakSelf.navigationController popViewControllerAnimated:YES]; |
| 134 | + } |
| 135 | + }]; |
| 136 | +} |
| 137 | + |
| 138 | +@end |
0 commit comments