// // LoginViewController.m // AICity // // Created by 刘伟伟 on 2023/7/13. // Copyright © 2023 wei.z. All rights reserved. // #import "LoginViewController.h" #import "CommonBarView.h" #import "AppDelegate.h" #import "CTTabbarController.h" #import "ComonWKWebViewController.h" @interface LoginViewController () @property(nonatomic,strong)UIImageView *loadNetView; @property(nonatomic,strong)inputView *phoneInput; @property(nonatomic,strong)inputView *codeInput; @property(nonatomic,strong)UIButton *loginButton; @property(nonatomic,strong)UIButton *agreeButton; @end @implementation LoginViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // [self.view addSubview:self.customerbar]; [self setupUI]; self.view.backgroundColor = rgba(18, 23, 41, 1); //https://www.apple.com/v/home/bc/images/logos/iphone-tradein/logo_tradein__d1fpktgipvki_large.png self.loadNetView = [UIImageView new]; [self.loadNetView sd_setImageWithURL:[NSURL URLWithString:@"https://www.apple.com/v/home/bc/images/logos/iphone-tradein/logo_tradein__d1fpktgipvki_large.png"]]; } -(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; } -(void)loginAct{ [self.view endEditing:YES]; // 临时注释协议检查,方便测试 // if(!self.agreeButton.isSelected){ // [MBProgressHUD showMessage:@"请仔细阅读并同意《用户协议》和《隐私政策》"]; // return; // } NSString *account = self.phoneInput.textField.text ?: @""; NSString *password = self.codeInput.textField.text ?: @""; if(account.length == 0 || password.length == 0){ [MBProgressHUD showMessage:@"请输入手机号和验证码"]; return; } NSLog(@"[LoginViewController] 准备发送登录请求: account=%@", account); [RequestTool post:@"user/userLogin" params:@{@"account":account,@"password":password} success:^(id responseObject) { NSLog(@"user/userLogin-%@",responseObject); [[NSUserDefaults standardUserDefaults] setValue:responseObject[@"data"] forKey:@"existUserData"]; AppDelegate *myDelegate =(AppDelegate *) [[UIApplication sharedApplication] delegate]; CTTabbarController *vtab=[[CTTabbarController alloc] init]; myDelegate.window.rootViewController=vtab; } failure:^(NSError *error) { NSLog(@"%@",error); }]; } -(void)setupUI{ UILabel *n = [UILabel new]; n.text = @"手机号登录"; n.font = [UIFont systemFontOfSize:24]; n.textColor = rgba(255, 255, 255, 1); [self.view addSubview:n]; [n mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(40); make.top.equalTo(self.view).offset(152); }]; self.phoneInput = [inputView new]; self.phoneInput.type = 0; [self.view addSubview:self.phoneInput]; [self.phoneInput mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(n.mas_bottom).offset(96); make.height.mas_equalTo(@35); }]; self.codeInput = [inputView new]; self.codeInput.type = 1; self.codeInput.tapBlock = ^{ }; [self.view addSubview:self.codeInput]; [self.codeInput mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.phoneInput.mas_bottom).offset(37); make.height.mas_equalTo(@35); }]; UILabel *lab = [UILabel new]; lab.text = @"忘记密码?"; lab.textColor = rgba(255, 255, 255, 0.4); lab.font = [UIFont systemFontOfSize:12]; [self.view addSubview:lab]; lab.hidden = true; [lab mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.view).offset(-40); make.top.equalTo(self.codeInput.mas_bottom).offset(8); }]; UIButton *btn = [UIButton new]; [btn setBackgroundImage:[UIImage imageNamed:@"Rectangle 19465"] forState:UIControlStateNormal]; [btn setTitle:@"登录" forState:UIControlStateNormal]; [btn setTitleColor:rgba(18, 23, 41, 0.5) forState:UIControlStateNormal]; [btn setTitleColor:rgba(18, 23, 41, 0.3) forState:UIControlStateDisabled]; btn.titleLabel.font = [UIFont systemFontOfSize:16]; [btn addTarget:self action:@selector(loginAct) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; [btn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(40); make.right.equalTo(self.view).offset(-40); make.height.mas_equalTo(@48); make.top.equalTo(self.codeInput.mas_bottom).offset(56); }]; btn.enabled = NO; btn.alpha = 0.5; self.loginButton = btn; [self.phoneInput.textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged]; [self.codeInput.textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged]; self.phoneInput.textField.keyboardType = UIKeyboardTypeNumberPad; self.codeInput.textField.keyboardType = UIKeyboardTypeNumberPad; NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"已仔细阅读并同意"]; [attributedStr addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.4) range:NSMakeRange(0, attributedStr.length)]; [attributedStr addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr.length)]; NSMutableAttributedString *attributedStr2 = [[NSMutableAttributedString alloc] initWithString:@"《用户协议》"]; [attributedStr2 addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 1) range:NSMakeRange(0, attributedStr2.length)]; [attributedStr2 addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr2.length)]; NSURL *baiduURL = [NSURL URLWithString:@"privateProtocal://www.com"]; [attributedStr2 addAttribute:NSLinkAttributeName value:baiduURL range:NSMakeRange(0, attributedStr2.length)]; NSMutableAttributedString *attributedStr3 = [[NSMutableAttributedString alloc] initWithString:@"和"]; [attributedStr3 addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.4) range:NSMakeRange(0, attributedStr3.length)]; [attributedStr3 addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr3.length)]; NSMutableAttributedString *attributedStr4 = [[NSMutableAttributedString alloc] initWithString:@"《隐私政策》"]; [attributedStr4 addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 1) range:NSMakeRange(0, attributedStr4.length)]; [attributedStr4 addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:12.0] range:NSMakeRange(0, attributedStr4.length)]; NSURL *baiduURL2 = [NSURL URLWithString:@"privacyPolicy://www.com"]; [attributedStr4 addAttribute:NSLinkAttributeName value:baiduURL2 range:NSMakeRange(0, attributedStr4.length)]; [attributedStr appendAttributedString:attributedStr2]; [attributedStr appendAttributedString:attributedStr3]; [attributedStr appendAttributedString:attributedStr4]; UITextView *lab2 = [UITextView new]; lab2.backgroundColor = [UIColor clearColor]; lab2.attributedText = attributedStr; lab2.textAlignment = NSTextAlignmentCenter; lab2.linkTextAttributes = @{NSForegroundColorAttributeName:rgba(255, 255, 255, 1)}; lab2.delegate = self; lab2.editable = NO; lab2.scrollEnabled = NO; // lab2.selectable = NO; // lab.textColor = rgba(255, 255, 255, 0.4); // lab.font = [UIFont systemFontOfSize:12]; [self.view addSubview:lab2]; [lab2 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view).offset(7); make.top.equalTo(btn.mas_bottom).offset(12); make.width.mas_equalTo(@270); // make.height.mas_equalTo(@15); }]; UIButton *selbtn = [UIButton new]; selbtn.tag = 130; [selbtn setBackgroundImage:[UIImage imageNamed:@"login_cirle"] forState:UIControlStateNormal]; [selbtn setBackgroundImage:[UIImage imageNamed:@"login_chick"] forState:UIControlStateSelected]; [self.view addSubview:selbtn]; [selbtn addTarget:self action:@selector(changeSelState) forControlEvents:UIControlEventTouchUpInside]; [selbtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(lab2.mas_left).offset(-2); make.size.mas_equalTo(CGSizeMake(12, 12)); make.centerY.equalTo(lab2); }]; self.agreeButton = selbtn; [self updateLoginButtonState]; } -(void)changeSelState{ self.agreeButton.selected = !self.agreeButton.isSelected; [self updateLoginButtonState]; } -(void)textFieldEditingChanged:(UITextField *)textField{ [self updateLoginButtonState]; } -(void)updateLoginButtonState{ BOOL hasPhone = self.phoneInput.textField.text.length > 0; BOOL hasCode = self.codeInput.textField.text.length > 0; // 临时移除协议检查,方便测试 BOOL canLogin = hasPhone && hasCode; // && self.agreeButton.isSelected; self.loginButton.enabled = canLogin; self.loginButton.alpha = canLogin ? 1.0 : 0.5; UIColor *titleColor = canLogin ? rgba(18, 23, 41, 1.0) : rgba(18, 23, 41, 0.5); [self.loginButton setTitleColor:titleColor forState:UIControlStateNormal]; } -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction { if ([[URL scheme] isEqualToString:@"privateProtocal"]) { NSLog(@"看天用户协议"); //用户协议 ComonWKWebViewController *vc =[[ComonWKWebViewController alloc] init]; vc.modalPresentationStyle = UIModalPresentationOverFullScreen; vc.title = @"用户协议"; vc.url = @"https://backend.jsnoopyay.com/user_agreement.html"; [self presentViewController:vc animated:YES completion:nil]; return NO; } else if ([[URL scheme] isEqualToString:@"privacyPolicy"]) { NSLog(@"隐私协议"); //隐私协议 ComonWKWebViewController *vc =[[ComonWKWebViewController alloc] init]; vc.title = @"隐私协议"; vc.url = @"https://backend.jsnoopyay.com/unregister.html"; [self presentViewController:vc animated:YES completion:nil]; return NO; } return YES; } //-(CommonBarView *)customerbar{ // if(!_customerbar){ // CGFloat h=0; // if(@available(iOS 13.0, *)){ // UIStatusBarManager *sc= [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager; // CGRect f=sc.statusBarFrame; // h=f.size.height; // }else{ // h=[UIApplication sharedApplication].statusBarFrame.size.height; // } // if(h>=40){ // h=30; // } // _customerbar =[[CommonBarView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 72-20+h)]; // _customerbar.bgView.backgroundColor = [UIColor clearColor]; // _customerbar.label.text=@"手机号登录"; // _customerbar.bgView.alpha = 1; // _customerbar.label.textColor = rgba(255, 255, 255, 1); // } // return _customerbar; //} @end @interface inputView() @property(nonatomic,strong)NSTimer *mytime; @property(nonatomic,assign)int second; @property(nonatomic,strong)UITextField *textField; @end @implementation inputView - (instancetype)init { self = [super init]; if (self) { self.second = 60; } return self; } //0手机号 1密码 -(void)setType:(int)type{ _type = type; [self initView:type]; } - (void)initView:(int)type{ UIImageView *iv = [UIImageView new]; if(type == 0){ iv.image = [UIImage imageNamed:@"Casino"]; }else{ iv.image = [UIImage imageNamed:@"Casino2"]; } [self addSubview:iv]; [iv mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self).offset(40); make.top.equalTo(self); make.size.mas_equalTo(CGSizeMake(20, 20)); }]; UITextField *tf =[UITextField new]; NSString *placeholder = type == 0 ? @"请输入手机号" : @"请输入验证码"; NSMutableAttributedString *attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:placeholder]; [attributedPlaceholder addAttribute:NSForegroundColorAttributeName value:rgba(255, 255, 255, 0.2) range:NSMakeRange(0, attributedPlaceholder.length)]; [attributedPlaceholder addAttribute:NSFontAttributeName value:[UIFont italicSystemFontOfSize:14.0] range:NSMakeRange(0, attributedPlaceholder.length)]; tf.attributedPlaceholder = attributedPlaceholder; tf.textColor = [UIColor whiteColor]; tf.font = [UIFont italicSystemFontOfSize:14.0]; [self addSubview:tf]; self.textField = tf; if(type == 0){ UILabel *lab = [UILabel new]; lab.text = @"+86"; lab.textColor = rgba(255, 255, 255, 0.4); lab.font = [UIFont systemFontOfSize:14]; [self addSubview:lab]; [lab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iv.mas_right).offset(12); make.centerY.equalTo(iv); }]; [tf mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(lab.mas_right).offset(8); make.centerY.equalTo(iv); make.size.mas_equalTo(CGSizeMake(200, 20)); }]; }else{ [tf mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iv.mas_right).offset(12); make.centerY.equalTo(iv); make.size.mas_equalTo(CGSizeMake(200, 20)); }]; UIButton *showP =[UIButton new]; [showP setTitle:@"获取验证码" forState:UIControlStateNormal]; [showP setTitleColor:rgba(255, 212, 0, 1) forState:UIControlStateNormal]; showP.titleLabel.font = [UIFont systemFontOfSize:14]; [showP addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:showP]; [showP mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self).offset(-40); make.centerY.equalTo(iv); make.size.mas_equalTo(CGSizeMake(80, 20)); }]; } UIView *line = [UIView new]; line.backgroundColor = rgba(255, 255, 255, 0.2); [self addSubview:line]; [line mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(iv); make.right.equalTo(self).offset(-40); make.bottom.equalTo(self); make.height.mas_equalTo(@1); }]; } -(void)getCode:(UIButton *)showP{ if(self.mytime){ return; } self.mytime = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) { [showP setTitle:[NSString stringWithFormat:@"%ds",self.second] forState:UIControlStateNormal]; self.second -=1; if(self.second <= -1){ [self.mytime invalidate]; self.mytime = nil; self.second = 60; [showP setTitle:@"获取验证码" forState:UIControlStateNormal]; } }]; if(self.tapBlock){ self.tapBlock(); } } @end