// // KXLittleLogginView.m // CAISHEN // // Created by kuxuan on 2017/8/28. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXLittleLogginView.h" #define baseTag 444 static int count = 0; // 短信倒计时计数 @implementation KXLittleLogginView { UIView *_whiteView; UIButton *_testButton; UIButton *_completeButton; NSTimer *_timer; UIButton *_soundButton; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4]; [self setupUI]; } return self; } - (void)setupUI { _whiteView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0-150, 180*SCREEN_MUTI, 300, 250)]; _whiteView.layer.cornerRadius = 8; _whiteView.layer.masksToBounds = YES; _whiteView.backgroundColor = [UIColor whiteColor]; [self addSubview:_whiteView]; UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, 300, 20)]; titlelabel.textColor = [UIColor titleColor]; titlelabel.font = FONT_BOLD(16); titlelabel.textAlignment = NSTextAlignmentCenter; titlelabel.text = @"验证手机号"; [_whiteView addSubview:titlelabel]; NSArray *placeholderArray=@[@"请输入手机号",@"请输入验证码"]; for (int i=0; i<2; i++) { CGFloat width = i==0?CGRectGetWidth(_whiteView.frame)-40:Fitsize(160); UITextField *someTextField=[[UITextField alloc]initWithFrame:CGRectMake(20, 70+48*i,width, 28)]; someTextField.textColor = [UIColor titleColor]; someTextField.tag=baseTag+i; someTextField.borderStyle=UITextBorderStyleNone; someTextField.keyboardType=UIKeyboardTypeNumberPad; if (i==1) { someTextField.secureTextEntry=YES; someTextField.keyboardType=UIKeyboardTypeNumbersAndPunctuation; } someTextField.attributedPlaceholder=[[NSAttributedString alloc] initWithString:placeholderArray[i] attributes:@{NSForegroundColorAttributeName:[UIColor detailTitleColor]}]; someTextField.font=FONT_SYS(12); someTextField.leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 28)]; someTextField.leftViewMode=UITextFieldViewModeAlways; someTextField.returnKeyType=UIReturnKeyDone; someTextField.delegate=self; [_whiteView addSubview:someTextField]; } for (int i=0; i<2; i++) { UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 97 + 48*i, 260, 1)]; lineLabel.backgroundColor=[UIColor lineColor]; [_whiteView addSubview:lineLabel]; } _testButton=[UIButton buttonWithType:UIButtonTypeCustom]; _testButton.frame=CGRectMake(160, 112, 300-162, 30); _testButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0); _testButton.titleLabel.font=FONT_SYS(12*SCREEN_MUTI); [_testButton setTitle:@"点击获取验证码" forState:UIControlStateNormal]; [_testButton setTitleColor:[UIColor titleColor] forState:UIControlStateNormal]; [_testButton addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside]; [_whiteView addSubview:_testButton]; _completeButton=[UIButton buttonWithType:UIButtonTypeCustom]; _completeButton.frame=CGRectMake(20, 165, 300-40, 45); _completeButton.layer.cornerRadius=6; _completeButton.layer.masksToBounds=YES; _completeButton.backgroundColor=[UIColor KXColorWithHex:0xcccccc]; _completeButton.titleLabel.font=FONT_SYS(16); [_completeButton setTitle:@"完成" forState:UIControlStateNormal]; [_completeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_completeButton addTarget:self action:@selector(logginAction) forControlEvents:UIControlEventTouchUpInside]; [_whiteView addSubview:_completeButton]; UIButton *codeButton=[UIButton buttonWithType:UIButtonTypeSystem]; codeButton.frame=CGRectMake(20, 220, 100, 15); codeButton.titleLabel.font=FONT_SYS(12); [codeButton setTitle:@"账户密码验证" forState:UIControlStateNormal]; [codeButton setTitleColor:[UIColor KXColorWithHex:0xe42a2a] forState:UIControlStateNormal]; codeButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft; [codeButton addTarget:self action:@selector(codeAction:) forControlEvents:UIControlEventTouchUpInside]; [_whiteView addSubview:codeButton]; _soundButton=[UIButton buttonWithType:UIButtonTypeCustom]; _soundButton.frame=CGRectMake(100, 220, 180, 15); _soundButton.titleLabel.font=FONT_SYS(12); _soundButton.hidden = NO; NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:@"收不到短信?请使用语音验证码"]; [att addAttribute:NSForegroundColorAttributeName value:[UIColor detailTitleColor] range:NSMakeRange(0, 9)]; [att addAttribute:NSForegroundColorAttributeName value:[UIColor KXColorWithHex:0xe42a2a] range:NSMakeRange(9, 5)]; [_soundButton setAttributedTitle:att forState:UIControlStateNormal]; _soundButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight; [_soundButton addTarget:self action:@selector(soundAction:) forControlEvents:UIControlEventTouchUpInside]; [_whiteView addSubview:_soundButton]; UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom]; [closeButton setImage:[UIImage imageNamed:@"main_little_close"] forState:UIControlStateNormal]; closeButton.frame = CGRectMake(240, 0, 60, 60); [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside]; [_whiteView addSubview:closeButton]; } - (void)closeAction { [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj removeFromSuperview]; }]; [self removeFromSuperview]; } -(void)testAction:(UIButton *)btn { _soundButton.hidden = YES; _testButton.enabled=NO; UITextField *phoneTF=[_whiteView viewWithTag:baseTag]; NSString *urlString=[NSString stringWithFormat:@"%@/user/register",URL]; [KXHTTP post:urlString params:@{@"phone":phoneTF.text,@"qrcode":@"1",@"sign":[[NSString stringWithFormat:@"%@%@",phoneTF.text,KXREGISTER_SECRET] md5],@"version":@"2"} success:^(id json) { if ([json[@"success"] isEqual:@(1)]) { _testButton.enabled = YES; _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes]; [_timer fire]; } } failure:^(NSError *error) { _testButton.enabled=YES; }]; } - (void)soundAction:(UIButton *)btn { NSString *urlString=[NSString stringWithFormat:@"%@/user/register",URL]; UITextField *phoneTF=[self viewWithTag:baseTag]; NSDictionary *params = @{@"phone":phoneTF.text,@"qrcode":@"1",@"smstype":@"2",@"sign":[[NSString stringWithFormat:@"%@%@",phoneTF.text,KXREGISTER_SECRET] md5],@"version":@"2"}; [KXHTTP post:urlString params:params success:^(id json) { BOOL isSuccess = json[@"success"]; if (isSuccess) { [self updateTime]; } } failure:^(NSError *error) { }]; } -(void)updateTime { count ++; if (count >= 60) { count = 0; _testButton.enabled = YES; _testButton.titleLabel.text = [NSString stringWithFormat:@"点击获取验证码"]; [_timer invalidate]; return; } _soundButton.hidden = YES; _testButton.titleLabel.text = [NSString stringWithFormat:@"重新发送%ds", 60 - count]; } -(void)codeAction:(UIButton *)btn { UITextField *codeTF=[_whiteView viewWithTag:baseTag+1]; if ([codeTF.attributedPlaceholder.string isEqualToString:@"请输入密码"]) { codeTF.attributedPlaceholder=[[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:[UIColor detailTitleColor]}]; _testButton.hidden = NO; if (count >= 60||count == 0) { _soundButton.hidden = NO; }else{ _soundButton.hidden = YES; } [btn setTitle:@"账号密码登录" forState:UIControlStateNormal]; }else{ codeTF.attributedPlaceholder=[[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:[UIColor detailTitleColor]}]; _testButton.hidden = YES; _soundButton.hidden = YES; [btn setTitle:@"手机号验证码登录" forState:UIControlStateNormal]; } } -(void)logginAction { UITextField *phoneTF=[_whiteView viewWithTag:baseTag]; UITextField *codeTF=[_whiteView viewWithTag:baseTag+1]; if ([codeTF.placeholder isEqualToString:@"请输入密码"]) { NSString *urlString=[NSString stringWithFormat:@"%@/user/login",URL]; [KXHTTP post:urlString params:@{@"phone":phoneTF.text,@"password":codeTF.text} success:^(id json) { [[NSUserDefaults standardUserDefaults]setObject:phoneTF.text forKey:USER_NAME]; [[NSUserDefaults standardUserDefaults]setObject:codeTF.text forKey:USER_PASSWORD]; [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:USER_TOKEN]; [[NSUserDefaults standardUserDefaults]setObject:json[@"user"][@"PHPSESSID"] forKey:kloginSessionId]; [[NSUserDefaults standardUserDefaults]synchronize]; [_delegate littleLogginViewSuccess]; [self closeAction]; } failure:^(NSError *error) { }]; }else{ NSString *urlString=[NSString stringWithFormat:@"%@/user/loginCode",URL]; [KXHTTP post:urlString params:@{@"phone":phoneTF.text,@"code":codeTF.text,@"source":KXCHANNEL_ID} success:^(id json) { [[NSUserDefaults standardUserDefaults]setObject:phoneTF.text forKey:USER_NAME]; [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:USER_PASSWORD]; NSDictionary *uuser = json[@"user"]; [[NSUserDefaults standardUserDefaults]setObject:uuser[@"user_token"] forKey:USER_TOKEN]; [[NSUserDefaults standardUserDefaults]setObject:json[@"user"][@"PHPSESSID"] forKey:kloginSessionId]; [[NSUserDefaults standardUserDefaults]synchronize]; [_delegate littleLogginViewSuccess]; [self closeAction]; } failure:^(NSError *error) { }]; } } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; { UITextField *phoneTF=[_whiteView viewWithTag:baseTag]; UITextField *codeTF=[_whiteView viewWithTag:baseTag+1]; if (phoneTF.text.length!=0&&codeTF.text.length!=0) { _completeButton.backgroundColor = [UIColor baseColor]; }else{ _completeButton.backgroundColor = [UIColor KXColorWithHex:0xcccccc]; } return YES; } @end