RegisterViewController.m 10.3 KB
//
//  ResgisterViewController.m
//  CNLiveUserSystemDemo
//
//  Created by iOS on 16/9/14.
//  Copyright © 2016年 zhulin. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegistBasicInformationView.h"

@interface RegisterViewController ()

@property (nonatomic, strong) RegistBasicInformationView *registBasicInformationView;
@property (nonatomic, strong) UIButton *verificationBtn;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"back";
    self.navigationItem.backBarButtonItem = backItem;
    
    if ([_type isEqualToString:@"register"]) {
        self.title = @"注册";
    } else if ([_type isEqualToString:@"quicklyLogin"]) {
        self.title = @"快捷登录";
    } else {
        self.title = @"修改密码";
    }

    [self.view addSubview:self.registBasicInformationView];
    [self.view addSubview:self.verificationBtn];
    [self.view addSubview:self.loginBtn];

}

#pragma mark - action

- (void)verificationBtnAction
{
    [self startTimer];

    if ([_type isEqualToString:@"register"]) {
        [CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeRegister mobile:_registBasicInformationView.phoneTF.text success:^(id result) {
            
            [Tools toast:result[@"errorMessage"]];
            
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
            
            [self stopTimer];
           
        }];
        
    } else if ([_type isEqualToString:@"quicklyLogin"]) {
    
        [CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeShortcutLogin mobile:_registBasicInformationView.phoneTF.text success:^(id result) {
          
            [Tools toast:result[@"errorMessage"]];
            
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];

            [self stopTimer];

        }];
        
    } else {
        
        [CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeRetrievePassword mobile:_registBasicInformationView.phoneTF.text success:^(id result) {
            
            [Tools toast:result[@"errorMessage"]];
            
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
            
            [self stopTimer];
        }];
    }
}

- (void)stopTimer
{
    [self.verificationBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    self.verificationBtn.userInteractionEnabled = YES;
    dispatch_cancel(self.timer);
    self.timer = nil;
}

- (void)loginBtnAction
{
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD show];
    
    if ([_type isEqualToString:@"register"]) {
        
        [CNLiveUserSystemCenter registerWithUserName:_registBasicInformationView.phoneTF.text password:_registBasicInformationView.setPwdTF.text verificationCode:_registBasicInformationView.verificationTF.text invitationCode:nil frmId:nil success:^(id result) {
            
            NSLog(@"result --- %@",result);
            [Tools setUserLogin:YES];
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            [UserInfoSava saveUserinfoDic:result[@"data"]];
            [self.navigationController popToRootViewControllerAnimated:YES];

        } failure:^(NSDictionary *errorDic) {
            [SVProgressHUD dismiss];
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
        }];
        
    } else if ([_type isEqualToString:@"quicklyLogin"]) {

        [CNLiveUserSystemCenter shortcutLoginWithUserName:_registBasicInformationView.phoneTF.text verificationCode:_registBasicInformationView.verificationTF.text frmId:nil success:^(id result) {
            
            [Tools setUserLogin:YES];
            [SVProgressHUD showSuccessWithStatus:@"成功"];
            [UserInfoSava saveUserinfoDic:result[@"data"]];
            [self.navigationController popToRootViewControllerAnimated:YES];
 
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
            [SVProgressHUD dismiss];
        }];
        
    } else {
        [CNLiveUserSystemCenter changePasswordWithMobile:_registBasicInformationView.phoneTF.text verificationCode:_registBasicInformationView.verificationTF.text newPassword:_registBasicInformationView.setPwdTF.text success:^(id result) {

            [SVProgressHUD showSuccessWithStatus:@"密码修改成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
            [SVProgressHUD dismiss];
        }];
    }
    
}

- (void)startTimer
{
    __block int timeout = 60; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    self.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<=0){ //倒计时结束,关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [self.verificationBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                self.verificationBtn.userInteractionEnabled = YES;
                
            });
            
        }else{
            
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                
                [self.verificationBtn setTitle:[NSString stringWithFormat:@"%@秒",strTime] forState:UIControlStateNormal];
                
                self.verificationBtn.userInteractionEnabled = NO;
                
                
            });
            
            timeout--;
            
        }
        
    });
    
    dispatch_resume(_timer);
    
}

#pragma mark - getter

- (RegistBasicInformationView *)registBasicInformationView
{
    if (!_registBasicInformationView) {
        if ([_type isEqualToString:@"quicklyLogin"]) {
            _registBasicInformationView = [[RegistBasicInformationView alloc] initWithFrame:CGRectMake(0, 70, KScreenWidth, 171) hasPwd:NO isPhone:YES];
        } else if ([_type isEqualToString:@"register"]) {
            _registBasicInformationView = [[RegistBasicInformationView alloc] initWithFrame:CGRectMake(0, 70, KScreenWidth, 171) hasPwd:YES isPhone:NO];
        } else {
            _registBasicInformationView = [[RegistBasicInformationView alloc] initWithFrame:CGRectMake(0, 70, KScreenWidth, 171) hasPwd:YES isPhone:YES];
        }
    }
    return _registBasicInformationView;
}

- (UIButton *)verificationBtn
{
    if (!_verificationBtn) {
        _verificationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_verificationBtn setFrame:CGRectMake(KScreenWidth - 105, 80, 95, 37)];
        [_verificationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_verificationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_verificationBtn addTarget:self action:@selector(verificationBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _verificationBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_verificationBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _verificationBtn.layer.cornerRadius = 10;
        _verificationBtn.layer.masksToBounds = YES;
        _verificationBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _verificationBtn.layer.borderWidth = 1;
    }
    return _verificationBtn;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setFrame:CGRectMake(10, 280, KScreenWidth - 20, 40)];
        if ([_type isEqualToString:@"register"]) {
            [_loginBtn setTitle:@"注  册" forState:UIControlStateNormal];
        } else if ([_type isEqualToString:@"quicklyLogin"]) {
            [_loginBtn setTitle:@"快捷登录" forState:UIControlStateNormal];
        } else {
            [_loginBtn setTitle:@"确  认" forState:UIControlStateNormal];
        }
        [_loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_loginBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _loginBtn.layer.borderWidth = 1;
    }
    return _loginBtn;
}

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