IVTBindingTelViewController.m 11.8 KB
//
//  IVTBindingTelViewController.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/17.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "IVTBindingTelViewController.h"
#import "NSString+IVTMeString.h"
#import "AppDelegate.h"
#import "XMPPManager.h"

@interface IVTBindingTelViewController ()<UITextFieldDelegate>
{
    UITextField  *mobileField;
    UITextField  *captchaField;
    dispatch_source_t timer;
    NSInteger countdown;
    UIButton *captchaButton;
    UIButton *loginButton;
}
@end

@implementation IVTBindingTelViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    
    [self timerCount];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTap)];
    [self.view addGestureRecognizer:tap];
}

//注册登录页面
- (void)initUI{
    __unsafe_unretained typeof (self) wself = self;
    //整体背景nav view
    UIView* navBgView = [[UIView alloc] init];
    [self.view addSubview:navBgView];
    navBgView.backgroundColor = [UIColor IVTLightRedColor];
    [navBgView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(@0);
        make.height.equalTo(@58);
    }];
    
    
    UILabel *message = [[UILabel alloc]init];
    [navBgView addSubview:message];
    message.text = @"绑定手机";
    message.textAlignment = 1;
    message.textColor = [UIColor IVTTitleColor];
    message.font = [UIFont YXFontOfSize:19];
    [message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(navBgView.mas_centerX);
        make.top.equalTo(wself.view.mas_top).with.offset(29);
        make.size.mas_equalTo(CGSizeMake(130.0f, 19.0f));
    }];
    
    UIImageView *inputPhoneView = [[UIImageView alloc]init];
    inputPhoneView.image =[UIImage imageNamed:@"请输入手机号框"];
    inputPhoneView.userInteractionEnabled = YES;
    [self.view addSubview:inputPhoneView];
    [inputPhoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navBgView.mas_bottom).with.offset(34);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(259.0f, 39.0f));
    }];
    
    
    mobileField = [[UITextField alloc]init];
    mobileField.backgroundColor = [UIColor clearColor];
    mobileField.placeholder = @"请输入手机号";
    [mobileField setValue:[UIColor YXColorWithHexCode:@"#A0A0A0"] forKeyPath:@"_placeholderLabel.textColor"];
    mobileField.font = [UIFont YXFontOfSize:13];
    mobileField.keyboardType = UIKeyboardTypeNumberPad;
    mobileField.delegate = self;
    [inputPhoneView addSubview:mobileField];
    [mobileField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(inputPhoneView.mas_leading).with.offset(43);
        make.top.equalTo(inputPhoneView.mas_top).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(200, 39));
    }];
    
    UIImageView *captchaView = [[UIImageView alloc]init];
    captchaView.image =[UIImage imageNamed:@"输入验证码框"];
    captchaView.userInteractionEnabled  = YES;
    [self.view addSubview:captchaView];
    [captchaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(inputPhoneView.mas_bottom).with.offset(10);
        make.leading.equalTo(inputPhoneView.mas_leading);
        make.size.mas_equalTo(CGSizeMake(151.5f, 39.5f));
    }];
    
    
    captchaField = [[UITextField alloc]init];
    captchaField.backgroundColor = [UIColor clearColor];
    captchaField.placeholder = @"请输入验证码";
    [captchaField setValue:[UIColor YXColorWithHexCode:@"#A0A0A0"] forKeyPath:@"_placeholderLabel.textColor"];
    captchaField.font = [UIFont YXFontOfSize:13];
    captchaField.keyboardType = UIKeyboardTypeNumberPad;
    captchaField.delegate = self;
    [captchaView addSubview:captchaField];
    [captchaField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(captchaView.mas_leading).with.offset(43.0f);
        make.size.mas_equalTo(CGSizeMake(110, 39));
        make.top.equalTo(inputPhoneView.mas_bottom).with.offset(10);
        
    }];
    
    
    captchaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [captchaButton setBackgroundColor:[UIColor YXColorWithHexCode:@"#ffa800"]];
    [captchaButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    captchaButton.titleLabel.font = [UIFont YXFontOfSize:14];
    [captchaButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:captchaButton];
    [captchaButton addTarget:self action:@selector(captchaClick:) forControlEvents:UIControlEventTouchUpInside];
    [captchaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(97.0f, 39.0f));
        make.top.equalTo(inputPhoneView.mas_bottom).with.offset(10);
        make.leading.equalTo(captchaView.mas_trailing).with.offset(11);
    }];
    
    
    //登录
    loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setBackgroundImage:[UIImage imageNamed:@"MeOK"] forState:UIControlStateNormal];
    [loginButton setBackgroundImage:[UIImage imageNamed:@"MeOkHL"] forState:UIControlStateHighlighted];
    [self.view addSubview:loginButton];
    [loginButton addTarget:self action:@selector(bindingTelClick:) forControlEvents:UIControlEventTouchUpInside];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(259.0f, 38.0f));
        make.top.equalTo(captchaButton.mas_bottom).with.offset(10);
        make.centerX.equalTo(self.view);
    }];
    
    UILabel *BBlabel = [[UILabel alloc] init];
    BBlabel.text = @"为了账号安全,请绑定手机号";
    BBlabel.textAlignment = 1;
    BBlabel.textColor = [UIColor IVTGrayColor];
    BBlabel.font = [UIFont IVTH3Font];
    BBlabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:BBlabel];
    [BBlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(259.0f, 38.0f));
        make.top.equalTo(loginButton.mas_bottom).with.offset(15);
        make.centerX.equalTo(wself.view);
    }];
}

-(void)doTap{
    [self.view endEditing:YES];
}

- (void)timerCount {
    countdown = 60;
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 1 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        [captchaButton setEnabled:NO];
        [captchaButton setTitle:[NSString stringWithFormat:@"%ld秒后重发",(long)countdown] forState:UIControlStateDisabled];
        if (countdown == 0) {
            dispatch_suspend(timer);
            [captchaButton setTitle:@"重新发送" forState:UIControlStateDisabled];
            [captchaButton setTitle:@"重新发送" forState:UIControlStateNormal];
            [captchaButton setEnabled:YES];
        } else {
            countdown--;
        }
    });
}

#pragma mark -  textField  delegate
//textField的字数控制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *behoverString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([textField isEqual:mobileField]) {
        if (behoverString.length > 11) {
            return NO;
        }
        if ([behoverString isMobileNumber]) {
            captchaButton.enabled  = YES;
            if (captchaField.text.length==4) {
                loginButton.enabled = YES;
            }
        }
    }
    
    if ([textField isEqual:captchaField]) {
        if (range.location >= 4) {
            return NO;
        }
    }
    return YES;

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [mobileField resignFirstResponder];
    [captchaField resignFirstResponder];
    return NO;
}

#pragma mark - 点击获取验证码
-(void)captchaClick:(id)sender{
    if (mobileField.text.length > 0 && [mobileField.text isMobileNumber]) {

        captchaButton.enabled = NO;

        [self performSelector:@selector(timeEnough:) withObject:nil afterDelay:3.0];
        //你要实现的button点击事件
        [HUDUtil showLoading];
        [self.view endEditing:YES];

        [[IVTNetworkAPIClient sharedClient] getCaptchaWithPhone:mobileField.text success:^(NSDictionary *successData) {
            countdown = 60;
            dispatch_resume(timer);

            [HUDUtil showSuccess:@"验证码已发送"];
            
            [captchaField becomeFirstResponder];

        } error:^(NSDictionary *successData) {
            NSString *msg = successData[@"description"];
            [HUDUtil showError:msg];
            captchaButton.enabled = YES;
            countdown = 0;
        } failure:^(NSError *failureError) {
            [HUDUtil showError:@"网络异常"];
            captchaButton.enabled = YES;
            countdown = 0;
        }];
    }else{
        [HUDUtil showError:@"请输入手机号,并再次请求验证码"];
    }
}

-(void)timeEnough:(UIButton *)btn
{
    captchaButton.enabled = YES;
}

//  点击确认绑定
- (void)bindingTelClick:(id)sender{
    if (![mobileField.text isMobileNumber] || captchaField.text.length <= 0) {
        [HUDUtil showError:@"请输入手机号或验证码"];
        return;
    }
    
    NSString *accessToken = nil;
    if (self.platform == kSinaType) {
        accessToken = [AppDelegate appDelegate].wbCurrentUserID;
    }
    else if (self.platform == kWeChatType){
        accessToken = [AppDelegate appDelegate].wxOpenId;
    }
    else if (self.platform == kQQType){
        accessToken = [AppDelegate appDelegate].tencentOpenId;
    }
    
    loginButton.enabled = NO;
    
    [HUDUtil showLoading];
    [self.view endEditing:YES];
    
    [[IVTNetworkAPIClient sharedClient] thirdPartLoginWithPhoneNumber:mobileField.text captcha:captchaField.text platformName:self.platform value:accessToken success:^(NSDictionary *successData) {
        if (_platform == kSinaType) {
            [IVTAccountTool setSinaBinding:YES];
        }
        else if (_platform == kWeChatType){
            [IVTAccountTool setWeChatBinding:YES];
        }
        else if (_platform == kQQType){
            [IVTAccountTool setQQBinding:YES];
        }
        [self bindThirdPartSuccess:mobileField.text captchaField:captchaField.text];
    } error:^(NSDictionary *successData) {
        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];
        
        loginButton.enabled = YES;
    } failure:^(NSError *failureError) {
        [HUDUtil showError:@"网络异常"];
        loginButton.enabled = YES;
    }];
}


-(void)bindThirdPartSuccess:(NSString *)mobilText captchaField:(NSString *)captchaText{
    [[IVTNetworkAPIClient sharedClient] loginWithPhone:mobileField.text captcha:captchaField.text success:^(NSDictionary *successData) {
        [HUDUtil hide];
        [[XMPPManager sharedInstance] loginWithUserId:[IVTAccountTool getUserId] pwd:captchaField.text];
        
        [IVTAccountTool getFollowListRequest];
        
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    } error:^(NSDictionary *successData) {
        NSString *msg = successData[@"description"];
        [HUDUtil showError:msg];
        
        loginButton.enabled = YES;

    } failure:^(NSError *failureError) {
        [HUDUtil showError:@"网络异常"];
        loginButton.enabled = YES;
    }];
}


- (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