Files
yinmeng-ios/xplan-ios/Main/Login/View/LoginVerifCodeViewController.m
2021-09-14 15:43:18 +08:00

214 lines
6.4 KiB
Objective-C

//
// LoginVerifCodeViewController.m
// xplan-ios
//
// Created by 冯硕 on 2021/9/8.
//
#import "LoginVerifCodeViewController.h"
///第三方
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>
///Tool
#import "ThemeColor.h"
///Presenter
#import "LoginVerifCodePresent.h"
///Protocole
#import "LoginVerifCodeProtocol.h"
///View
#import "LoginVerifCodeView.h"
@interface LoginVerifCodeViewController ()<LoginVerifCodeProtocol>
///标题的容器
@property (nonatomic,strong) UIStackView *titleStackView;
///标题
@property (nonatomic,strong) UILabel *titleLabel;
///未注册手机号自定登录
@property (nonatomic,strong) UILabel *codeSendLabel;
///输入验证码
@property (nonatomic,strong) LoginVerifCodeView *codeView;
///重新获取验证码 和 倒计时
@property (nonatomic,strong) UIStackView *retryStackView;
///显示倒计时
@property (nonatomic,strong) UILabel *cutdownLabel;
///重新获得验证码
@property (nonatomic,strong) UIButton *retryCodeButton;
@end
@implementation LoginVerifCodeViewController
- (void)dealloc
{
NSLog(@"销毁");
}
- (LoginVerifCodePresent *)createPresenter {
return [[LoginVerifCodePresent alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
[self httpRequestPhoneSmsCode];
[self initEvents];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:NO];
}
#pragma mark - Private Method
- (void)initSubViews {
[self.view addSubview:self.titleStackView];
[self.view addSubview:self.codeView];
[self.view addSubview:self.retryStackView];
[self.titleStackView addArrangedSubview:self.titleLabel];
[self.titleStackView addArrangedSubview:self.codeSendLabel];
[self.retryStackView addArrangedSubview:self.cutdownLabel];
[self.retryStackView addArrangedSubview:self.retryCodeButton];
}
- (void)initSubViewConstraints {
[self.titleStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view);
make.top.mas_equalTo(self.view).offset(75);
}];
[self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.titleStackView.mas_bottom).offset(50);
make.centerX.mas_equalTo(self.view);
}];
[self.retryStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.codeView.mas_bottom).offset(16);
make.centerX.mas_equalTo(self.view);
make.centerX.mas_equalTo(self.view);
}];
}
- (void)httpRequestPhoneSmsCode {
[self.presenter phoneSmsCode:self.phone type:1];
}
- (void)initEvents {
@weakify(self);
self.codeView.textFieldChangeBlock = ^(NSString * _Nonnull code) {
@strongify(self);
if (self.phone.length > 0 && code.length == 5) {
[self.presenter loginWithPhone:self.phone code:code];
}
};
}
- (NSAttributedString *)createRetrySendCodeWithTime:(int)time {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
NSString * timeString = [NSString stringWithFormat:@"%ds ", time];
NSString * anewString = @"后可重新获取验证码";
NSAttributedString * timeAttribute = [[NSAttributedString alloc] initWithString:timeString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:[ThemeColor appMainColor]}];
NSAttributedString * anewAttribute = [[NSAttributedString alloc] initWithString:anewString attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15], NSForegroundColorAttributeName:[ThemeColor secondTextColor]}];
[attribute appendAttributedString:timeAttribute];
[attribute appendAttributedString:anewAttribute];
return attribute;
}
#pragma mark - LoginProtocol
- (void)phoneSmsCodeSuccess {
self.codeSendLabel.hidden = NO;
self.codeSendLabel.text = [NSString stringWithFormat:@"验证码已发送至:%@", self.phone];
}
- (void)countDownFinish {
self.retryCodeButton.hidden = NO;
self.cutdownLabel.hidden = YES;
self.codeSendLabel.hidden = YES;
}
- (void)countDownWithCurrent:(int)current {
self.cutdownLabel.attributedText = [self createRetrySendCodeWithTime:current];
self.retryCodeButton.hidden = YES;
self.cutdownLabel.hidden = NO;
}
- (void)loginSuccess {
[self showSuccessToast:@"登录成功"];
[self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - Getters And Setters
- (UIStackView *)titleStackView {
if (!_titleStackView) {
_titleStackView = [[UIStackView alloc] init];
_titleStackView.axis = UILayoutConstraintAxisVertical;
_titleStackView.distribution = UIStackViewDistributionFillEqually;
_titleStackView.alignment = UIStackViewAlignmentCenter;
_titleStackView.spacing = 10;
}
return _titleStackView;
}
- (UILabel *)codeSendLabel {
if (!_codeSendLabel) {
_codeSendLabel = [[UILabel alloc] init];
_codeSendLabel.textAlignment = NSTextAlignmentCenter;
_codeSendLabel.font = [UIFont systemFontOfSize:11];
_codeSendLabel.textColor = [ThemeColor secondTextColor];
_codeSendLabel.hidden = YES;
}
return _codeSendLabel;
}
- (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
_titleLabel.text = @"填写验证码";
_titleLabel.textColor = [ThemeColor mainTextColor];
}
return _titleLabel;
}
- (LoginVerifCodeView *)codeView {
if (!_codeView) {
_codeView= [[LoginVerifCodeView alloc] init];
_codeView.number = 5;
}
return _codeView;
}
- (UIStackView *)retryStackView {
if (!_retryStackView) {
_retryStackView = [[UIStackView alloc] init];
_retryStackView.axis = UILayoutConstraintAxisVertical;
_retryStackView.distribution = UIStackViewDistributionFill;
_retryStackView.alignment = UIStackViewAlignmentCenter;
_retryStackView.spacing = 0;
}
return _retryStackView;
}
- (UILabel *)cutdownLabel {
if (!_cutdownLabel) {
_cutdownLabel = [[UILabel alloc] init];
_cutdownLabel.textAlignment = NSTextAlignmentCenter;
_cutdownLabel.font = [UIFont boldSystemFontOfSize:11];
_cutdownLabel.textColor = [ThemeColor mainTextColor];
_cutdownLabel.hidden = NO;
}
return _cutdownLabel;
}
- (UIButton *)retryCodeButton {
if (!_retryCodeButton) {
_retryCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_retryCodeButton setTitle:@"重新获取验证码" forState:UIControlStateNormal];
[_retryCodeButton setTitleColor:[ThemeColor appMainColor] forState:UIControlStateNormal];
_retryCodeButton.titleLabel.font = [UIFont systemFontOfSize:11];
_retryCodeButton.hidden = YES;
}
return _retryCodeButton;
}
@end