// // BindPhoneController.m // model // // Created by JuYi on 2018/7/16. // Copyright © 2018年 Mine. All rights reserved. // 绑定手机 #import "BindPhoneController.h" #import "TableDefaultCell.h" #import "TableWithVerificationCodeCell.h" @interface BindPhoneController () { NSString *zhifubaoAccount; NSString *zhifubaoName; NSString *phoneNum; NSString *verifyCode; NSString *randomCode; } @property(nonatomic, strong) UITableView *tableView; @property(nonatomic, strong) TableDefaultCell *accountCell; @property(nonatomic, strong) TableDefaultCell *nameCell; @property(nonatomic, strong) TableDefaultCell *phoneCell; @property(nonatomic, strong) TableWithVerificationCodeCell *verificationCodeCell; @end @implementation BindPhoneController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)]; //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。 tapGestureRecognizer.cancelsTouchesInView = NO; //将触摸事件添加到当前view [self.view addGestureRecognizer:tapGestureRecognizer]; //设置导航条 [self creatNavBar]; //设置子试图 [self creatSubViews]; } #pragma mark -- 收起键盘 - (void)keyboardHide:(UITapGestureRecognizer *)tap { [self.view endEditing:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)creatNavBar { self.title = @"绑定手机"; // UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 40, 40); [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal]; btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0); [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整 self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem]; } - (void)backClick { [self.navigationController popViewControllerAnimated:YES]; } - (void)creatSubViews { // UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 16)]; headerView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); // UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 220)]; footerView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); UIButton *bindingBtn = [UIButton buttonWithType:UIButtonTypeCustom]; bindingBtn.frame = CGRectMake(20, 39, ScreenWidth - 40, 45); [bindingBtn setBackgroundImage:[UIImage imageNamed:@"tixian-1"] forState:UIControlStateNormal]; [bindingBtn setTitle:@"绑定" forState:UIControlStateNormal]; [bindingBtn setTitleColor:RGBValueColor(0xffffff, 1.0) forState:UIControlStateNormal]; [bindingBtn addTarget:self action:@selector(bindingBtnAction) forControlEvents:UIControlEventTouchUpInside]; [footerView addSubview:bindingBtn]; // self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain]; self.tableView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.tableHeaderView = headerView; self.tableView.tableFooterView = footerView; [self.view addSubview:self.tableView]; [self.tableView registerClass:[TableDefaultCell class] forCellReuseIdentifier:@"DefaultCell"]; [self.tableView registerClass:[TableWithVerificationCodeCell class] forCellReuseIdentifier:@"VerificationCodeCell"]; } #pragma mark -- UITableViewDelegate, UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 4; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) { TableDefaultCell *accountCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath]; accountCell.titleLabel.text = @"提现账户"; accountCell.contentTF.placeholder = @"请输入支付宝账户"; accountCell.contentTF.keyboardType = UIKeyboardTypeDefault; accountCell.contentTF.delegate = self; accountCell.contentTF.tag = 1000; self.accountCell = accountCell; return accountCell; } else if (indexPath.row == 1) { TableDefaultCell *nameCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath]; nameCell.titleLabel.text = @"账户姓名"; nameCell.contentTF.placeholder = @"请输入账户姓名"; nameCell.contentTF.keyboardType = UIKeyboardTypeDefault; nameCell.contentTF.delegate = self; nameCell.contentTF.tag = 1001; self.nameCell = nameCell; return nameCell; } else if (indexPath.row == 2) { TableDefaultCell *phoneCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath]; phoneCell.titleLabel.text = @"手机号"; phoneCell.contentTF.placeholder = @"请输入手机号"; phoneCell.contentTF.keyboardType = UIKeyboardTypePhonePad; phoneCell.contentTF.delegate = self; phoneCell.contentTF.tag = 1002; self.phoneCell = phoneCell; return phoneCell; } else { TableWithVerificationCodeCell *verificationCodeCell = [tableView dequeueReusableCellWithIdentifier:@"VerificationCodeCell" forIndexPath:indexPath]; verificationCodeCell.titleLabel.text = @"验证码"; verificationCodeCell.contentTF.placeholder = @"请输入手机验证码"; verificationCodeCell.contentTF.keyboardType = UIKeyboardTypeNumberPad; verificationCodeCell.contentTF.delegate = self; verificationCodeCell.contentTF.tag = 1003; [verificationCodeCell.codeBtn addTarget:self action:@selector(codeBtnAction) forControlEvents:UIControlEventTouchUpInside]; self.verificationCodeCell = verificationCodeCell; return verificationCodeCell; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } #pragma mark -- 绑定按钮 - (void)bindingBtnAction { if (zhifubaoAccount.length == 0) { [MBProgressHUD showInfo:@"请输入支付宝账户"]; return; } if (zhifubaoName.length == 0) { [MBProgressHUD showInfo:@"请输入账户姓名"]; return; } if (phoneNum.length == 0) { [MBProgressHUD showInfo:@"请填写正确的手机号"]; return; } if (verifyCode.length == 0 || ![verifyCode isEqualToString:randomCode]) { [MBProgressHUD showInfo:@"请填写正确的验证码"]; return; } [self postBindingRequestToNetworking]; } #pragma mark -- 网络请求 - (void)postBindingRequestToNetworking { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; ModelUser *modelUser = [ModelUser user]; NSDictionary *dic = @{@"modelpk": modelUser.modelpk, @"alipayid": zhifubaoAccount, @"name": zhifubaoName}; [YanCNetWorkManager requestPostWithURLStr:Url_alipayoutcashmodify(PublicUrl) parameters:dic finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { [MBProgressHUD showSuccess:@"绑定成功"]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO]; } else { [MBProgressHUD showInfo:@"请求失败!"]; } } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } #pragma mark -- 倒计时 - (void)codeBtnAction { if ([Helper valiMobile:phoneNum]) { [self getVerifyCode]; [self startCountdownAction]; } else { [MBProgressHUD showInfo:@"请填写正确的手机号"]; } } - (void)getVerifyCode { int a = arc4random() % 100000; randomCode = [NSString stringWithFormat:@"%06d", a]; NSString *str = [NSString stringWithFormat:@"%@/model?action=sendsms&phone=%@&code=%@", PublicUrl, phoneNum, randomCode]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:str parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@", result); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"%@", error); }]; } //获取验证码按钮开始倒计时响应事件 - (void)startCountdownAction { __block int timeout = 60;//倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_timer, ^{ if (timeout <= 1) {//倒计时结束,关闭 dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示根据自己需求设置 [self.verificationCodeCell.codeBtn setTitle:@"重新获取" forState:UIControlStateNormal]; self.verificationCodeCell.codeBtn.userInteractionEnabled = YES; }); } else { int seconds = timeout; NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds]; dispatch_async(dispatch_get_main_queue(), ^{ //让按钮变为不可点击的灰色 //并且关掉按钮的交互 self.verificationCodeCell.codeBtn.userInteractionEnabled = NO; //设置界面的按钮显示根据自己需求设置 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; [self.verificationCodeCell.codeBtn setTitle:[NSString stringWithFormat:@"%@s", strTime] forState:UIControlStateNormal]; [UIView commitAnimations]; }); timeout--; } }); dispatch_resume(_timer); } #pragma mark -- UITextFieldDelegate - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([textField viewWithTag:1002]) { if (string.length > 0 && (![NSString amtchString:string checkString:@"[0-9]"] || [textField.text length] == 11)) { return NO; } } else if ([textField viewWithTag:1003]) { if (string.length > 0 && [textField.text length] == 6) { return NO; } } return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField { if ([textField viewWithTag:1000]) { zhifubaoAccount = textField.text; } else if ([textField viewWithTag:1001]) { zhifubaoName = textField.text; } else if ([textField viewWithTag:1002]) { phoneNum = textField.text; } else if ([textField viewWithTag:1003]) { verifyCode = textField.text; } } @end