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

#import "CNLiveRegisterEmailViewController.h"
#import "CNLiveLoginInputView.h"

@interface CNLiveRegisterEmailViewController ()<TYAttributedLabelDelegate>
@property (nonatomic, strong) CNLiveLoginInputView *email;
@property (nonatomic, strong) CNLiveLoginInputView *pwd;
@property (nonatomic, strong) UIButton *agreeButton;
@end

@implementation CNLiveRegisterEmailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [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{
    
    _email = [[CNLiveLoginInputView alloc] initWithPic:@"cnlive_email" placeholder:CustomStr(@"PleaseFillInYourEmail") type:CNLiveButtonTypeNormal];
    [self.view addSubview:_email];
    [_email 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);
        
    }];

    
    _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->_email.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(20);
        make.left.equalTo(self.view.mas_left).with.offset(10);
        make.right.equalTo(self.view.mas_right).with.offset(-10);
        make.height.offset(40);
        
    }];
    
    
}
#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];
    }
}

- (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.
 }
 */
#pragma mark - Action
- (void)allResignFirstResponder{
    [_email.textField resignFirstResponder];
    [_pwd.textField resignFirstResponder];
    
}

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

    if(![_email.textField.text isValidEmail]){
        [AlertViewTool showHUDViewText:CustomStr(@"EmailError")];
        btn.userInteractionEnabled = YES;
        return;
        
    }
    
    if(![_pwd.textField.text isValidPassWordLegal]){
        [AlertViewTool showHUDViewText:CustomStr(@"PasswordError")];
        btn.userInteractionEnabled = YES;
        return;
        
    }
    
    if(_agreeButton.isSelected){
        [AlertViewTool showHUDViewText:CustomStr(@"PleaseAgreeToTheGuoXinUserAgreementTerms")];
        btn.userInteractionEnabled = YES;
        return;
    }
    [AlertViewTool showHUDView];

    // 邮箱注册
    [CNLiveUserSystemCenter registerWithUserName:_email.textField.text password:_pwd.textField.text verificationCode:nil 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;
    }
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self allResignFirstResponder];
    
}

@end