CNLiveLoginView.m 13.4 KB
//
//  CNLiveLoginView.m
//  CNLiveLoginModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveLoginView.h"

#import "YYKit.h"

#import "CNLiveLoginCountryView.h"
#import "CNLiveLoginTextField.h"

#import "CNLiveCountDownButton.h"
//#import "XWLoginNotification.h"

@interface CNLiveLoginView()<UITextFieldDelegate, CNLiveLoginCountryViewDelegate,CNLiveCountDownButtonDataSource, CNLiveCountDownButtonDelegate>
{
    NSString *_countryCode;      // 国家Code
    NSString *_verificationCode; // 验证码
}
@property (nonatomic, weak) UIImageView *logo;
@property (nonatomic, weak) CNLiveLoginCountryView *country;
@property (nonatomic, weak) CNLiveLoginTextField *account;
@property (nonatomic, weak) CNLiveLoginTextField *code;

@property (nonatomic, strong) CNLiveCountDownButton *countDown;

@property (nonatomic, weak) UIButton *login;

@end

@implementation CNLiveLoginView
static NSInteger const rowHeight = 40;
static NSInteger const space = 20;

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        _countryCode = @"86";
        [self createSubviews:frame];

    }
    return self;
    
}
- (void)createSubviews:(CGRect)frame{
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-80)/2.0, 0, 80, 105)];
    UIImage *image1 = [self xw_getImageName:@"login_logo" bundleName:@"CNLiveLoginModule" targetClass:[self class]];
    logo.image = image1;
    [self addSubview:logo];
    self.logo = logo;
    
    CNLiveLoginCountryView *country = [[CNLiveLoginCountryView alloc] initWithFrame:CGRectMake(0, logo.bottom+space, frame.size.width, rowHeight)];
    country.delegate = self;
    [self addSubview:country];
    self.country = country;
    
    CNLiveLoginTextField *account = [[CNLiveLoginTextField alloc] initWithFrame:CGRectMake(country.left, country.bottom+space, frame.size.width, rowHeight)];
    account.keyboardType = UIKeyboardTypeNumberPad;
    account.placeholder = @"请输入手机号";
    account.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    account.placeholderColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
    account.font = [UIFont systemFontOfSize:14];
    account.textAlignment = NSTextAlignmentLeft;
    [account addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    account.clearButtonMode = UITextFieldViewModeWhileEditing;
    account.delegate = self;
    [self addSubview:account];
    self.account = account;
    
    CNLiveLoginTextField *code = [[CNLiveLoginTextField alloc] initWithFrame:CGRectMake(country.left, account.bottom+space, frame.size.width/2.0, rowHeight)];
    code.keyboardType = UIKeyboardTypeNumberPad;
    code.placeholder = @"请输入验证码";
    code.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    code.placeholderColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0];
    code.font = [UIFont systemFontOfSize:14];
    code.textAlignment = NSTextAlignmentLeft;
    [code addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    code.clearButtonMode = UITextFieldViewModeWhileEditing;
    code.delegate = self;
    [self addSubview:code];
    self.code = code;
    
    CNLiveCountDownButton *countDown = [CNLiveCountDownButton buttonWithType:UIButtonTypeCustom];
    countDown.dataSource = self;
    countDown.delegate = self;
    countDown.frame = CGRectMake(frame.size.width-80, account.bottom+space+10, 80, rowHeight-10);
    UIImage *image2 = [self xw_getImageName:@"login_get_code" bundleName:@"CNLiveLoginModule" targetClass:[self class]];
    [countDown setImage:image2 forState:UIControlStateNormal];
    countDown.adjustsImageWhenHighlighted = NO;
    [countDown setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    countDown.titleLabel.font = [UIFont systemFontOfSize:12.0];
    countDown.layer.cornerRadius = 15;
    countDown.layer.masksToBounds = YES;
    [self addSubview:countDown];
    self.countDown = countDown;
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(5, code.bottom+space, frame.size.width-10, rowHeight);
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.titleLabel.font = [UIFont systemFontOfSize:15.0];
    login.backgroundColor = [UIColor colorWithRed:35/255.0 green:212/255.0 blue:30/255.0 alpha:1.0];
    login.layer.cornerRadius = 20;
    login.layer.masksToBounds = YES;
    login.adjustsImageWhenHighlighted = NO;
    [login addTarget:self action:@selector(loginDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:login];
    self.login = login;

}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    if ([textField isEqual:_account] || [textField isEqual:_code]) {
        return YES;
        
    }
    else {
        return YES;
        
    }
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (![_account.text isEqualToString:@""] && ![_code.text isEqualToString:@""]) {
        _login.enabled = YES;
        
    }
    else {
        _login.enabled = NO;
        
    }
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([textField isEqual:_account]) {
        NSInteger maxTextLength  = 20;
        if(string.length == 0) {
            //判断是不是删除键
            return YES;
        }
        else if(textField.text.length >= maxTextLength) {
            NSLog(@"输入的字符个数大于20,忽略输入");
            return NO;
        }
        else if (![self isNum:string]) {
            return NO;
        }
        else if (textField.text.length + string.length > maxTextLength){
            NSInteger length = textField.text.length;
            NSInteger needLen = maxTextLength - length;
            textField.text = [textField.text stringByAppendingString:[string substringToIndex:needLen]];
            return NO;
        }
        else{
            return YES;
        }
    }
    else if ([textField isEqual:self.code]){
        NSInteger maxTextLength  = 6;
        if(string.length == 0) {
            //判断是不是删除键
            return YES;
        }
        else if(textField.text.length >= maxTextLength) {
            NSLog(@"输入的字符个数大于6,忽略输入");
            return NO;
        }
        else if (![self isNum:string]) {
            return NO;
        }
        else if (textField.text.length + string.length > maxTextLength){
            NSInteger length = textField.text.length;
            NSInteger needLen = maxTextLength - length;
            textField.text = [textField.text stringByAppendingString:[string substringToIndex:needLen]];
            return NO;
        }
        else{
            return YES;
        }
    }
    else{
        return YES;
    }
}

/** 判断是否为纯数字 */
- (BOOL)isNum:(NSString *)checkedNumString {
    checkedNumString = [checkedNumString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(checkedNumString.length > 0) {
        return NO;
    }
    return YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - XWLoginCountryViewDelegate
- (void)selectCountryName:(NSString *)countryName countryCode:(NSString *)countryCode{
    [_account resignFirstResponder];
    [_code resignFirstResponder];

    if(![_countryCode isEqualToString:countryCode]){
        _countryCode = countryCode;

    }
    
}
#pragma mark - XWCountDownButton DataSource
// 设置倒计时总秒数
- (NSInteger)startCountDownNumOfCountDownButton:(CNLiveCountDownButton *)countdownButton {
    return 60;
}

#pragma mark - XWCountdownButton Delegate
// 倒计时按钮点击
- (void)countDownButtonDidClick:(CNLiveCountDownButton *)button {
    //获取验证码
    __weak typeof(self) weakSelf = self;
    if ([_account.text isEqualToString:@""]) {
//        [XWAlertView alertViewWithFlexTextMessage:@"请输入手机号" view:nil];
        return;
    }
    // 按钮点击后将enabled设置为NO
    button.userInteractionEnabled = NO;
    
//    [XWUserRequest verificationCodeWithType:XWCodeTypeShortcutLogin mobile:_account.text countryCode:_countryCode success:^(id  _Nonnull result) {
//        __strong typeof(weakSelf) strongSelf = weakSelf;
//        if(!strongSelf) return;
//        // 获取的验证码
//        strongSelf->_verificationCode = @"666666";
//
//        [XWAlertView alertSucceedViewWithTextMessage:@"获取成功" view:nil];
//        button.backgroundColor = HexColor(0xdedede);
//        button.frame = CGRectMake(self.frame.size.width-110, self.account.bottom+space+10, 110, rowHeight-10);
//        [button setImage:nil forState:UIControlStateNormal];
//        NSString *title = [NSString stringWithFormat:@"%d秒后重新发送", 60];
//        [button setTitle:title forState:UIControlStateNormal];
//        [button startCountDown];
//
//    } failure:^(NSDictionary * _Nonnull errorDic) {
//        __strong typeof(weakSelf) strongSelf = weakSelf;
//        if(!strongSelf) return;
//        button.userInteractionEnabled = YES;
//        [XWAlertView alertErrorViewWithTextMessage:@"获取失败" view:nil];
//
//    }];
    

}

// 倒计时进行中
- (void)countDownButtonDidCountDown:(CNLiveCountDownButton *)button countDownNum:(NSInteger)countDownNum {
    if(button.frame.size.width != 110){
        button.frame = CGRectMake(self.frame.size.width-110, self.account.bottom+space+10, 110, rowHeight-10);
    }
    NSString *title = [NSString stringWithFormat:@"%ld秒后重新发送", countDownNum];
    [button setTitle:title forState:UIControlStateNormal];
    
}

// 倒计时结束
- (void)countDownButtonDidEndCountDown:(CNLiveCountDownButton *)button {
    button.frame = CGRectMake(self.frame.size.width-80, self.account.bottom+space+10, 80, rowHeight-10);
    button.backgroundColor = [UIColor whiteColor];
    button.userInteractionEnabled = YES;
    [button setTitle:@"" forState:UIControlStateNormal];
    UIImage *image2 = [self xw_getImageName:@"login_get_code" bundleName:@"CNLiveLoginModule" targetClass:[self class]];
    [button setImage:image2 forState:UIControlStateNormal];
}

#pragma mark - Action
- (void)loginDidClicked:(UIButton *)btn{
    //登录
    _verificationCode = @"666666";
    if ([_account.text isEqualToString:@""]) {
//        [XWAlertView alertViewWithFlexTextMessage:@"请输入手机号" view:self.superview];
        return;
    }
    if ([_code.text isEqualToString:@""]) {
//        [XWAlertView alertViewWithFlexTextMessage:@"请输入验证码" view:self.superview];
        return;
    }
    btn.userInteractionEnabled = NO;
    // 隐藏键盘
    [self.superview endEditing:YES];
    __weak typeof(self) weakSelf = self;
//    [XWUserRequest shortcutLoginWithUserName:_account.text verificationCode:_verificationCode countryCode:_countryCode fromId:@"apple" success:^(id _Nonnull result) {
//        btn.userInteractionEnabled = YES;
//        __strong typeof(weakSelf) strongSelf = weakSelf;
//        if(!strongSelf) return;
//        [XWUserManager defaultConfiguration:result];
//        // YES 新用户   NO 老用户
//        BOOL isNewUser = YES;
//        if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(loginSuccess:isNewUser:result:)]) {
//            [strongSelf.delegate loginSuccess:strongSelf isNewUser:isNewUser result:result];
//        }
//
//    } failure:^(NSDictionary * _Nonnull errorDic) {
//        btn.userInteractionEnabled = YES;
//        __strong typeof(weakSelf) strongSelf = weakSelf;
//        if(!strongSelf) return;
////        [XWAlterView alertErrorViewWithTextMessage:@"登录失败" view:strongSelf.superview];
//
//        // ------ 最后删除 ------
//        NSDictionary *dic = @{@"token":@"用户token",@"id":@"用户id",@"tel":@"17600922519",@"faceUrl":@"http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg",@"name":@"张璟熙",@"gender":@"0",@"address":@"北京市海淀区"};
//        [XWUserManager defaultConfiguration:dic];
//        BOOL isNewUser = YES;
//        if (strongSelf.delegate && [strongSelf.delegate respondsToSelector:@selector(loginSuccess:isNewUser:result:)]) {
//            [strongSelf.delegate loginSuccess:strongSelf isNewUser:isNewUser result:errorDic];
//        }
//        // ------ 最后删除 ------
//
//    }];
    
}

/**
 *  创建图片
 *
 *  @param imageName 图片名字
 *  @param bundleName 图片所在的bundle名字
 *  @param targetClass 类和bundle的同级目录
 *  @return 返回UIImage
 */
- (UIImage *)xw_getImageName:(NSString *)imageName bundleName:(NSString *)bundleName targetClass:(Class)targetClass{
    NSBundle *bundle = [NSBundle bundleForClass:targetClass];
    NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
}

@end