CNLiveRegisterPhoneViewController.m 11.3 KB
//
//  CNLiveRegisterPhoneViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/20.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveRegisterPhoneViewController.h"
#import "CNLiveLoginInputView.h"

@interface CNLiveRegisterPhoneViewController ()<TYAttributedLabelDelegate>{
    BOOL _hasGetVerification;

}
@property (nonatomic, strong) CNLiveLoginInputView *phone;
@property (nonatomic, strong) CNLiveLoginInputView *code;
@property (nonatomic, strong) CNLiveLoginInputView *pwd;
@property (nonatomic, strong) UIButton *agreeButton;

@end

@implementation CNLiveRegisterPhoneViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _hasGetVerification = NO;
    [self setUpNavigationBar];
    [self setUpSubControllers];
    // Do any additional setup after loading the view.
}
#pragma mark - UI
- (void)setUpNavigationBar{
    
    self.navigationController.navigationBar.barTintColor = Color_Blue;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    //左
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
    [headerButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
    
    self.navigationItem.title = CustomStr(@"Register");
    
}
- (void)setUpSubControllers{
    
    _phone = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_phone" placeholder:CustomStr(@"PleaseFillInYourPhoneNumber") type:CNLiveButtonTypeNormal];
    [self.view addSubview:_phone];
    [_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.height.offset(40);
        
    }];
    
    _code = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_phone_message" placeholder:CustomStr(@"PleaseFillInTheVerificationCodeReceivedByMobilePhone") type:CNLiveButtonTypeCode];
    __weak typeof(self) weakSelf = self;
    _code.block = ^{
        [weakSelf getVerification];
    };
    [self.view addSubview:_code];
    [_code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_phone.mas_bottom).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.height.offset(40);
        
    }];
    
    _pwd = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_password" placeholder:CustomStr(@"PleaseEnterA6-16-bitPassword") type:CNLiveButtonTypeTextEntry];
    [self.view addSubview:_pwd];
    [_pwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_code.mas_bottom).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.height.offset(40);
        
    }];
    
    
    _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _agreeButton.tag = 10000;
    [_agreeButton addTarget:self action:@selector(clicekdButton:) forControlEvents:UIControlEventTouchUpInside];
    [_agreeButton setImage:[UIImage imageNamed:@"cnlive_login_check"] forState:UIControlStateNormal];
    [_agreeButton setImage:[UIImage imageNamed:@"cnlive_login_check out"] forState:UIControlStateSelected];

    [self.view addSubview:_agreeButton];
    [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self->_pwd.mas_bottom).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.width.offset(20);
        make.height.offset(20);

    }];
    
    TYAttributedLabel *titleLabel = [[TYAttributedLabel alloc] init];
    titleLabel.verticalAlignment = TYVerticalAlignmentCenter;
    titleLabel.delegate = self;
    titleLabel.textColor = HEXCOLOR(0x333333);
    titleLabel.font = [UIFont systemFontOfSize:13];
    titleLabel.text = CustomStr(@"IAgreeTo");
    [self.view addSubview:titleLabel];
    NSString *partText = CustomStr(@"TheGuoXinUserAgreementTerms");
    [titleLabel appendLinkWithText:partText linkFont:[UIFont systemFontOfSize:13] linkColor:Color_Blue underLineStyle:kCTUnderlineStyleNone linkData:@"http://app.scio.gov.cn/xieyi.html"];

    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->_pwd.mas_bottom).with.offset(5);
        make.left.equalTo(self->_agreeButton.mas_right).with.offset(10);
        make.right.equalTo(self.view.mas_right).with.offset(-10);
        make.height.offset(40);
        
    }];
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.tag = 10001;
    registerButton.layer.cornerRadius = 5.0;
    registerButton.layer.masksToBounds = true;
    [registerButton addTarget:self action:@selector(clicekdButton:) forControlEvents:UIControlEventTouchUpInside];
    [registerButton setTitle:CustomStr(@"Register") forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registerButton.titleLabel.font = [UIFont systemFontOfSize:17];
    registerButton.backgroundColor = Color_Blue;
    [self.view addSubview:registerButton];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self->_agreeButton.mas_bottom).with.offset(15);
        make.left.equalTo(self.view.mas_left).with.offset(10);
        make.right.equalTo(self.view.mas_right).with.offset(-10);
        make.height.offset(40);
        
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - TYAttributedLabelDelegate
//TYAttributedLabelDelegate
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)TextRun atPoint:(CGPoint)point {
    if ([TextRun isKindOfClass:[TYLinkTextStorage class]]) {
        NSString *linkStr = ((TYLinkTextStorage*)TextRun).linkData;
        NSLog(@"linkStr === %@",linkStr);
        CNLiveWebViewController *vc = [[CNLiveWebViewController alloc]init];
        vc.navTitle = CustomStr(@"UserAgreement");
        vc.url = linkStr;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}
/*
#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.
}
*/
#pragma mark - Action
- (void)allResignFirstResponder{
    [_phone.textField resignFirstResponder];
    [_pwd.textField resignFirstResponder];
    
}

- (void)sendRegisterRequest:(UIButton *)btn{
    [self allResignFirstResponder];

    if(![_phone.textField.text isValidMobile]){
        [AlertViewTool showHUDViewText:CustomStr(@"PhoneNumberError")];
        btn.userInteractionEnabled = YES;
        return;

    }
    if (!_hasGetVerification) {
        [AlertViewTool showHUDViewText:@"您还没有获取验证码"];
        btn.userInteractionEnabled = YES;
        return;
    }
    if(![_pwd.textField.text isValidPassWordLegal]){
        [AlertViewTool showHUDViewText:CustomStr(@"PasswordError")];
        btn.userInteractionEnabled = YES;
        return;

    }
    if(_pwd.textField.text.length != 6){
        [AlertViewTool showHUDViewText:@"验证码错误"];
        btn.userInteractionEnabled = YES;
        return;
        
    }
    
    if(_agreeButton.isSelected){
        [AlertViewTool showHUDViewText:CustomStr(@"PleaseAgreeToTheGuoXinUserAgreementTerms")];
        btn.userInteractionEnabled = YES;
        return;
    }
    [AlertViewTool showHUDView];

    [CNLiveUserSystemCenter registerWithUserName:_phone.textField.text password:_pwd.textField.text verificationCode:_code.textField.text invitationCode:nil frmId:nil success:^(NSDictionary *result) {
        btn.userInteractionEnabled = YES;
        [AlertViewTool hideHUDView];

        if ([result[@"errorCode"] integerValue] == 0) {
            
//            ZXWUserInformation *share = [ZXWUserInformation shareZXWUserInformation];
//            share.model = [[ZXWUserModel alloc] initWithDictionary:result[@"data"]];
//            [[ZXWUserInformation shareZXWUserInformation] saveToken:@""];
//            [[ZXWUserInformation shareZXWUserInformation] saveUser_id:@""];
//            [[ZXWUserInformation shareZXWUserInformation] setIsLoginSuccess:YES];
//
//            [[ZXWUserInformation shareZXWUserInformation] saveUser_name:result[@"data"][@"nickName"]];
//            [[ZXWUserInformation shareZXWUserInformation] saveUser_pwd:self->_pwd.textField.text];
            [[ZXWUserInformation shareZXWUserInformation] saveUser_phone:result[@"data"][@"mobile"]];
            [[ZXWUserInformation shareZXWUserInformation] saveUser_email:result[@"data"][@"email"]];
//            [[ZXWUserInformation shareZXWUserInformation] saveUser_headimgUrlString:result[@"data"][@"faceUrl"]];

            [AlertViewTool showHUDViewText:CustomStr(@"RegisterSuccess") block:^{
                [self goBack];

            }];

        }
        else{
            [AlertViewTool showHUDViewText:CustomStr(@"RegisterFailure")];
            
        }
    } failure:^(NSDictionary *errorDic) {
        [AlertViewTool hideHUDView];
        [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];
        btn.userInteractionEnabled = YES;

    }];

}
- (void)clicekdButton:(UIButton *)btn{
    switch (btn.tag) {
        case 10000:
        {
            btn.selected = !btn.selected;
        }
            break;
        case 10001:
        {
            btn.userInteractionEnabled = NO;
            [self sendRegisterRequest:btn];
        }
            break;
            
        default:
            break;
    }
    
}
#pragma mark - CNLiveUserSystemSDK 获取验证码
- (void)getVerification{
    [self allResignFirstResponder];
    if(![_phone.textField.text isValidMobile]){
        [AlertViewTool showHUDViewText:CustomStr(@"PhoneNumberError")];
        return;
    }
    
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
        return;
    }
    
    [AlertViewTool showHUDView];
    [CNLiveUserSystemCenter verificationCodeType:VerificationCodeTypeRegister mobile:_phone.textField.text success:^(NSDictionary *result) {
        [AlertViewTool hideHUDView];
        if ([result[@"errorCode"] integerValue] == 0) {
            self->_hasGetVerification = YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:GetCodeSuccessful object:nil];

        }
        [AlertViewTool showHUDViewText:CustomStr(@"CodeSendingSuccessful")];

    } failure:^(NSDictionary *errorDic) {
        [AlertViewTool hideHUDView];
        [AlertViewTool showHUDViewText:errorDic[@"errorMessage"]];
        
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self allResignFirstResponder];

}
@end