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

#import "LoginViewController.h"
#import "IdAndPwdView.h"
#import "RegisterViewController.h"
#import "CNLiveThirdLoginManager.h"

#define SpaceWidth (KScreenWidth-80*3)/4

@interface LoginViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) IdAndPwdView *idAndPwdView;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *messageLoginBtn;
@property (nonatomic, strong) UIButton *registBtn;
@property (nonatomic, strong) UIButton *weChatBtn;
@property (nonatomic, strong) UIButton *sinaBtn;
@property (nonatomic, strong) UIButton *qqBtn;
@property (nonatomic, strong) UILabel *thirdpartyLoginLbl;

@property (nonatomic, strong) UIButton *checkThirdBtn;
@property (nonatomic, strong) UIButton *bindMobileBtn;
@property (nonatomic ,strong) UITextField *verificationCodeTF;
@property (nonatomic ,strong) NSDictionary *dic;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"back";
    self.navigationItem.backBarButtonItem = backItem;
    
    [self.view addSubview:self.idAndPwdView];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.messageLoginBtn];
    [self.view addSubview:self.weChatBtn];
    [self.view addSubview:self.sinaBtn];
    [self.view addSubview:self.qqBtn];
    [self.view addSubview:self.thirdpartyLoginLbl];
    [self.view addSubview:self.checkThirdBtn];
    [self.view addSubview:self.bindMobileBtn];
    [self.view addSubview:self.verificationCodeTF];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.registBtn];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:ThirdLoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFailure) name:ThirdLoginFailure object:nil];
}

#pragma mark - action
- (void)loginBtnAction
{
    NSDictionary *dic = self.dic;
    if (dic) {//三方强制绑定手机号登录
        [CNLiveUserSystemCenter thirdPartyBindMobileLoginWithThirdPartyPlatType:[dic objectForKey:@"platType"] thirdPartyId:dic[@"partyId"] nickName:@"testNickName" gender:[dic objectForKey:@"gender"] location:[dic objectForKey:@"location"] faceUrl:[dic objectForKey:@"faceUrl"] mobile:[dic objectForKey:@"mobile"] verificationCode:self.verificationCodeTF.text pawd:_idAndPwdView.pwdTF.text frmId:@"" success:^(id result) {
            NSLog(@"BindMobile 登录------%@",result);
            [UserInfoSava saveUserinfoDic:result[@"data"]];
            [Tools setUserLogin:YES];
            [self.navigationController popViewControllerAnimated:YES];
            
        } failure:^(NSDictionary *errorDic) {
            NSLog(@"BindMobile 登录------%@",errorDic);
        }];
    }else
    {//普通登录或三方验证登录
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
        [SVProgressHUD show];
        
        [CNLiveUserSystemCenter loginWithUserName:_idAndPwdView.idTF.text password:_idAndPwdView.pwdTF.text frmId:nil success:^(id result) {
            
            NSLog(@"登录 success------------%@",result);
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
            [UserInfoSava saveUserinfoDic:result[@"data"]];
            [Tools setUserLogin:YES];
            [self.navigationController popViewControllerAnimated:YES];
            
        } failure:^(NSDictionary *errorDic) {
            [Tools toast:[NSString stringWithFormat:@"errorCode : %@ \n errorMessage : %@", errorDic[@"errorCode"],errorDic[@"errorMessage"]]];
            [SVProgressHUD dismiss];
        }];
    }
}

- (void)messageLoginBtnAction
{
    RegisterViewController *regVC = [[RegisterViewController alloc] init];
    regVC.type = @"quicklyLogin";
    [self.navigationController pushViewController:regVC animated:YES];
}

- (void)registBtnAction
{
    RegisterViewController *regVC = [[RegisterViewController alloc] init];
    regVC.type = @"register";
    [self.navigationController pushViewController:regVC animated:YES];
}

- (void)weChatBtnAction
{
    [[CNLiveThirdLoginManager manager] weChatLoginIsBindingThirdParty:NO];
}

- (void)sinaBtnAction
{
    [[CNLiveThirdLoginManager manager] sinaLoginIsBindingThirdParty:NO];
}

- (void)qqBtnAction
{
    [[CNLiveThirdLoginManager manager] qqLoginIsBindingThirdParty:NO];
}

- (void)checkThirdBtnAction:(UIButton *)button
{
    button.selected = !button.selected;
    BOOL isCheck = NO;
    if (button.selected) {
        isCheck = YES;
    }else{
        isCheck = NO;
    }
    [CNLiveThirdLoginManager manager].thirdType = ThirdLoginTypeCheckIsRegister;
}

- (void)bindMobileBtnAction:(UIButton *)button
{
    button.selected = !button.selected;
    BOOL isCheck = NO;
    if (button.selected) {
        isCheck = YES;
    }else{
        isCheck = NO;
    }
    [CNLiveThirdLoginManager manager].thirdType = ThirdLoginTypeBindMobile;
    [CNLiveThirdLoginManager manager].bindMobile = _idAndPwdView.idTF.text;
}

- (void)loginSuccess:(NSNotification *)noti
{
    if (noti.userInfo)
    {//强制绑定手机号
        self.dic = noti.userInfo;
    }else
    {//普通三方登录和验证三方登录
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)loginFailure
{
    [SVProgressHUD showErrorWithStatus:@"登录失败"];
}

#pragma mark - touchesBegan
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([self.idAndPwdView.idTF isFirstResponder]) {
        [self.idAndPwdView.idTF resignFirstResponder];
    }
    if ([self.idAndPwdView.pwdTF isFirstResponder]) {
        [self.idAndPwdView.pwdTF resignFirstResponder];
    }
    if ([self.verificationCodeTF isFirstResponder]) {
        [self.verificationCodeTF resignFirstResponder];
    }
}
#pragma mark - getter

- (IdAndPwdView *)idAndPwdView
{
    if (!_idAndPwdView) {
        _idAndPwdView = [[IdAndPwdView alloc] initWithFrame:CGRectMake(0, 70, KScreenWidth, 114)];
        _idAndPwdView.idTF.delegate = self;
        _idAndPwdView.pwdTF.delegate = self;
    }
    return _idAndPwdView;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setFrame:CGRectMake(10, 250, KScreenWidth - 20, 40)];
        [_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;
}

- (UIButton *)messageLoginBtn
{
    if (!_messageLoginBtn) {
        _messageLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_messageLoginBtn setFrame:CGRectMake(KScreenWidth - 100, 210, 90, 15)];
        [_messageLoginBtn setTitle:@"快捷登录" forState:UIControlStateNormal];
        _messageLoginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_messageLoginBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_messageLoginBtn addTarget:self action:@selector(messageLoginBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_messageLoginBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];

    }
    return _messageLoginBtn;
}

- (UIButton *)registBtn
{
    if (!_registBtn) {
        _registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registBtn setFrame:CGRectMake(0, 0, 40, 15)];
        [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
        _registBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_registBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_registBtn addTarget:self action:@selector(registBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_registBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];

    }
    return _registBtn;
}

- (UILabel *)thirdpartyLoginLbl
{
    if (!_thirdpartyLoginLbl) {
        _thirdpartyLoginLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, KScreenHeight - 200, KScreenWidth, 15)];
        _thirdpartyLoginLbl.textAlignment = NSTextAlignmentCenter;
        _thirdpartyLoginLbl.textColor = [UIColor blackColor];
        _thirdpartyLoginLbl.text = @"第三方账号登录";
        _thirdpartyLoginLbl.font = [UIFont systemFontOfSize:15];
    }
    return _thirdpartyLoginLbl;
}

- (UIButton *)weChatBtn
{
    if (!_weChatBtn) {
        _weChatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_weChatBtn setFrame:CGRectMake(SpaceWidth, KScreenHeight-150, 80, 40)];
        [_weChatBtn setTitle:@"微信" forState:UIControlStateNormal];
        [_weChatBtn addTarget:self action:@selector(weChatBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _weChatBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_weChatBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _weChatBtn.layer.cornerRadius = 20;
        _weChatBtn.layer.masksToBounds = YES;
        _weChatBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _weChatBtn.layer.borderWidth = 1;
    }
    return _weChatBtn;
}

- (UIButton *)sinaBtn
{
    if (!_sinaBtn) {
        _sinaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sinaBtn setFrame:CGRectMake(SpaceWidth*2+80, KScreenHeight-150, 80, 40)];
        [_sinaBtn setTitle:@"微博" forState:UIControlStateNormal];
        [_sinaBtn addTarget:self action:@selector(sinaBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _sinaBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_sinaBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _sinaBtn.layer.cornerRadius = 20;
        _sinaBtn.layer.masksToBounds = YES;
        _sinaBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _sinaBtn.layer.borderWidth = 1;
    }
    return _sinaBtn;
}

- (UIButton *)qqBtn
{
    if (!_qqBtn) {
        _qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_qqBtn setFrame:CGRectMake(SpaceWidth*3+80*2, KScreenHeight-150, 80, 40)];
        [_qqBtn setTitle:@"QQ" forState:UIControlStateNormal];
        [_qqBtn addTarget:self action:@selector(qqBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _qqBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_qqBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _qqBtn.layer.cornerRadius = 20;
        _qqBtn.layer.masksToBounds = YES;
        _qqBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _qqBtn.layer.borderWidth = 1;
    }
    return _qqBtn;
}

- (UIButton *)checkThirdBtn
{
    if (!_checkThirdBtn) {
        _checkThirdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkThirdBtn setFrame:CGRectMake(SpaceWidth, KScreenHeight-100, 150, 40)];
        [_checkThirdBtn setTitle:@"三方登录" forState:UIControlStateNormal];
        [_checkThirdBtn setTitle:@"三方验证并登录" forState:UIControlStateSelected];
        [_checkThirdBtn addTarget:self action:@selector(checkThirdBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _checkThirdBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_checkThirdBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _checkThirdBtn.layer.cornerRadius = 20;
        _checkThirdBtn.layer.masksToBounds = YES;
        _checkThirdBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _checkThirdBtn.layer.borderWidth = 1;
    }
    return _checkThirdBtn;
}

- (UIButton *)bindMobileBtn
{
    if (!_bindMobileBtn) {
        _bindMobileBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bindMobileBtn setFrame:CGRectMake(SpaceWidth, KScreenHeight-50, 180, 40)];
        [_bindMobileBtn setTitle:@"三方登录" forState:UIControlStateNormal];
        [_bindMobileBtn setTitle:@"三方绑定手机号登录" forState:UIControlStateSelected];
        [_bindMobileBtn addTarget:self action:@selector(bindMobileBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _bindMobileBtn.titleLabel.font = [UIFont systemFontOfSize:18];
        [_bindMobileBtn setTitleColor:[UIColor colorWithRed:23/255.0 green:110/255.0 blue:254/255.0 alpha:1] forState:UIControlStateNormal];
        _bindMobileBtn.layer.cornerRadius = 20;
        _bindMobileBtn.layer.masksToBounds = YES;
        _bindMobileBtn.layer.borderColor = [UIColor blackColor].CGColor;
        _bindMobileBtn.layer.borderWidth = 1;
    }
    return _bindMobileBtn;
}

- (UITextField *)verificationCodeTF
{
    if (!_verificationCodeTF) {
        _verificationCodeTF = [[UITextField alloc] initWithFrame:CGRectMake(SpaceWidth + 200, KScreenHeight-50, self.view.bounds.size.width - SpaceWidth - 200, 40)];
        _verificationCodeTF.font = [UIFont systemFontOfSize:17];
        _verificationCodeTF.keyboardType = UIKeyboardTypeNumberPad;
        _verificationCodeTF.placeholder = @"请输入验证码";
        _verificationCodeTF.returnKeyType = UIReturnKeyDone;
    }
    return _verificationCodeTF;
}

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