LoginViewController.m 8.17 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;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.view.backgroundColor = [UIColor whiteColor];
    [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];
    
    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
{
    [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)loginSuccess
{
    [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];
    }
}
#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.backgroundColor = [UIColor orangeColor];
        [_loginBtn setFrame:CGRectMake(10, 250, KScreenWidth - 20, 40)];
        _loginBtn.layer.cornerRadius = 20;
        _loginBtn.layer.masksToBounds = YES;
        [_loginBtn setTitle:@"登  录" forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    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];
    }
    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];
    }
    return _registBtn;
}

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

- (UIButton *)weChatBtn
{
    if (!_weChatBtn) {
        _weChatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _weChatBtn.backgroundColor = [UIColor orangeColor];
        [_weChatBtn setFrame:CGRectMake(SpaceWidth, KScreenHeight-150, 80, 40)];
        _weChatBtn.layer.cornerRadius = 20;
        _weChatBtn.layer.masksToBounds = YES;
        [_weChatBtn setTitle:@"微信" forState:UIControlStateNormal];
        [_weChatBtn addTarget:self action:@selector(weChatBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weChatBtn;
}

- (UIButton *)sinaBtn
{
    if (!_sinaBtn) {
        _sinaBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sinaBtn.backgroundColor = [UIColor orangeColor];
        [_sinaBtn setFrame:CGRectMake(SpaceWidth*2+80, KScreenHeight-150, 80, 40)];
        _sinaBtn.layer.cornerRadius = 20;
        _sinaBtn.layer.masksToBounds = YES;
        [_sinaBtn setTitle:@"微博" forState:UIControlStateNormal];
        [_sinaBtn addTarget:self action:@selector(sinaBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sinaBtn;
}

- (UIButton *)qqBtn
{
    if (!_qqBtn) {
        _qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _qqBtn.backgroundColor = [UIColor orangeColor];
        [_qqBtn setFrame:CGRectMake(SpaceWidth*3+80*2, KScreenHeight-150, 80, 40)];
        _qqBtn.layer.cornerRadius = 20;
        _qqBtn.layer.masksToBounds = YES;
        [_qqBtn setTitle:@"QQ" forState:UIControlStateNormal];
        [_qqBtn addTarget:self action:@selector(qqBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _qqBtn;
}

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