Files
yinmeng-ios/xplan-ios/Main/Login/View/LoginVerifCodeViewController.m
2023-03-20 20:49:47 +08:00

349 lines
11 KiB
Objective-C

//
// LoginVerifCodeViewController.m
// xplan-ios
//
// Created by 冯硕 on 2021/9/8.
//
#import "LoginVerifCodeViewController.h"
///第三方
#import <Masonry/Masonry.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <SVGA.h>
///Tool
#import "ThemeColor.h"
#import "XPMacro.h"
#import "UIImage+Utils.h"
#import "XCHUDTool.h"
///Presenter
#import "LoginVerifCodePresent.h"
///Protocole
#import "LoginVerifCodeProtocol.h"
///View
@interface LoginVerifCodeViewController ()<LoginVerifCodeProtocol>
///背景图
@property (nonatomic,strong) SVGAPlayer *backImageView;
///标题
@property (nonatomic,strong) UILabel *titleLabel;
///验证码
@property (nonatomic,strong) UIView *codeView;
///重新获取验证码 和 倒计时
@property (nonatomic,strong) UIStackView *codeStackView;
///输入框
@property (nonatomic,strong) UITextField *textField;
///显示倒计时
@property (nonatomic,strong) UILabel *cutdownLabel;
///重新获得验证码
@property (nonatomic,strong) UIButton *retryCodeButton;
///验证
@property (nonatomic,strong) UIButton *confirmButton;
///返回按钮
@property (nonatomic,strong) UIButton *backButton;
@property (strong, nonatomic) dispatch_source_t timer;
@end
@implementation LoginVerifCodeViewController
- (BOOL)isHiddenNavBar {
return YES;
}
- (LoginVerifCodePresent *)createPresenter {
return [[LoginVerifCodePresent alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
[self httpRequestPhoneSmsCode];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:NO];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self stopCountDown];
}
#pragma mark - Private Method
- (void)initSubViews {
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.backImageView];
[self.view addSubview:self.backButton];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.codeView];
[self.view addSubview:self.confirmButton];
[self.codeView addSubview:self.codeStackView];
[self.codeStackView addArrangedSubview:self.textField];
[self.codeStackView addArrangedSubview:self.cutdownLabel];
[self.codeStackView addArrangedSubview:self.retryCodeButton];
}
- (void)initSubViewConstraints {
CGFloat kscale = 363.0 / 375.0;
[self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.view);
make.height.mas_equalTo(KScreenWidth * kscale);
}];
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view).mas_offset(10);
make.top.mas_equalTo(statusbarHeight);
make.height.width.mas_equalTo(44);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.view);
make.top.mas_equalTo(self.view).offset(150 + kNavigationHeight);
}];
[self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.view).inset(50);
make.top.mas_equalTo(self.view).offset(329 + kSafeAreaTopHeight);
make.height.mas_equalTo(51);
}];
[self.codeStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self.codeView).inset(15);
make.top.bottom.mas_equalTo(self.codeView);
}];
[self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.height.mas_equalTo(self.codeStackView);
make.top.mas_equalTo(self.codeStackView.mas_bottom).offset(15);
}];
}
- (void)loginAnimation {
SVGAParser *parser = [[SVGAParser alloc] init];
@weakify(self);
[parser parseWithNamed:@"login_logo" inBundle:nil completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
@strongify(self);
if (videoItem != nil) {
self.backImageView.videoItem = videoItem;
[self.backImageView startAnimation];
}
} failureBlock:nil];
}
- (void)httpRequestPhoneSmsCode {
[self.presenter phoneSmsCode:self.phone type:GetSmsType_Regist];
}
- (void)disMissVC {
[self stopCountDown];
UIViewController *vc = self.presentingViewController;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popToRootViewControllerAnimated:NO];
}
// 停止倒计时
- (void)stopCountDown {
if (self.timer != nil) {
dispatch_source_cancel(self.timer);
self.timer = nil;
}
}
// 开始倒计时
- (void)openCountdownWithTime:(int)totalTime{
if (time <= 0) {
return;
}
__block int time = totalTime; //倒计时时间
if (self.timer != nil) {
dispatch_source_cancel(self.timer);
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
__weak typeof(self) weakself = self;
dispatch_source_set_event_handler(self.timer, ^{
__strong typeof(weakself) self = weakself;
if(time <= 0){ //倒计时结束,关闭
dispatch_source_cancel(self.timer);
dispatch_async(dispatch_get_main_queue(), ^{
[self onCountdownFinish];
});
}else{
time--;
dispatch_async(dispatch_get_main_queue(), ^{
//设置按钮显示读秒效果
[self onCountdownOpen:time];
});
}
});
dispatch_resume(self.timer);
}
#pragma mark - LoginVerifCodeProtocol
- (void)loginSuccess {
[self showSuccessToast:@"登录成功"];
[self disMissVC];
}
///绑定手机号成功
- (void)bindPhoneSuccess {
[self disMissVC];
}
#pragma mark - LoginProtocol
- (void)phoneSmsCodeSuccess {
self.titleLabel.hidden = YES;
[XCHUDTool showErrorWithMessage:[NSString stringWithFormat:@"验证码已发送\n+86 %@", self.phone]];
[self openCountdownWithTime:60];
}
#pragma mark - CountDownHelperDelegate
- (void)onCountdownFinish {
self.retryCodeButton.hidden = NO;
self.cutdownLabel.hidden = YES;
self.titleLabel.hidden = YES;
}
- (void)onCountdownOpen:(int)time {
self.cutdownLabel.text = [NSString stringWithFormat:@"%d%@", time, @"S"];
self.retryCodeButton.hidden = YES;
self.cutdownLabel.hidden = NO;
}
#pragma mark - Event Response
- (void)textFieldDidChange:(UITextField *)text {
if (text.text.length > 5) {
text.text = [text.text substringToIndex:5];
}
if (text.text.length == 5) {
self.confirmButton.enabled = YES;
} else {
self.confirmButton.enabled = NO;
}
}
- (void)backButtonClick {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)confirmButtonAction:(UIButton *)sender {
if (self.type == VerifCodeType_Regist) {
[self.presenter loginWithPhone:self.phone code:self.textField.text];
} else if(self.type == VerifCodeType_BindPhone) {
[self.presenter bindWithPhone:self.phone code:self.textField.text];
}
}
#pragma mark - Getters And Setters
- (SVGAPlayer *)backImageView {
if (!_backImageView) {
_backImageView = [[SVGAPlayer alloc] init];
_backImageView.loops = 1;
_backImageView.clearsAfterStop = NO;
}
return _backImageView;
}
- (UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:20];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.numberOfLines = 2;
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UIView *)codeView {
if (!_codeView) {
_codeView = [[UIView alloc] init];
_codeView.backgroundColor = [ThemeColor colorWithHexString:@"#F8F8FB"];
_codeView.layer.masksToBounds = YES;
_codeView.layer.cornerRadius = 51.0/2;
}
return _codeView;
}
- (UIStackView *)codeStackView {
if (!_codeStackView) {
_codeStackView = [[UIStackView alloc] init];
_codeStackView.axis = UILayoutConstraintAxisHorizontal;
_codeStackView.distribution = UIStackViewDistributionFill;
_codeStackView.alignment = UIStackViewAlignmentCenter;
_codeStackView.spacing = 0;
}
return _codeStackView;
}
- (UILabel *)cutdownLabel {
if (!_cutdownLabel) {
_cutdownLabel = [[UILabel alloc] init];
_cutdownLabel.textAlignment = NSTextAlignmentCenter;
_cutdownLabel.font = [UIFont boldSystemFontOfSize:11];
_cutdownLabel.textColor = [ThemeColor secondTextColor];
_cutdownLabel.hidden = NO;
}
return _cutdownLabel;
}
- (UIButton *)retryCodeButton {
if (!_retryCodeButton) {
_retryCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_retryCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
[_retryCodeButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateNormal];
_retryCodeButton.titleLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
_retryCodeButton.hidden = YES;
}
return _retryCodeButton;
}
- (UIButton *)backButton {
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_backButton setImage:[UIImage imageNamed:@"common_nav_back"] forState:UIControlStateNormal];
[_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
}
return _backButton;
}
- (UIButton *)confirmButton{
if (!_confirmButton) {
_confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
_confirmButton.layer.masksToBounds = YES;
_confirmButton.layer.cornerRadius = 51/2.f;
[_confirmButton setTitle:@"验证" forState:UIControlStateNormal];
_confirmButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
[_confirmButton setTitleColor:[ThemeColor disableButtonTextColor] forState:UIControlStateDisabled];
[_confirmButton setTitleColor:[ThemeColor confirmButtonTextColor] forState:UIControlStateNormal];
_confirmButton.enabled = NO;
UIImage *image = [UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)];
[_confirmButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor disableButtonColor], [ThemeColor disableButtonColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateDisabled];
[_confirmButton setBackgroundImage:image forState:UIControlStateNormal];
[_confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _confirmButton;
}
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_textField.textColor = [ThemeColor mainTextColor];
_textField.font = [UIFont systemFontOfSize:18.f];
_textField.borderStyle = UITextBorderStyleNone;
_textField.keyboardType = UIKeyboardTypeNumberPad;
_textField.tintColor = [ThemeColor mainTextColor];
_textField.backgroundColor = [UIColor clearColor];
[_textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
_textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName: [ThemeColor secondTextColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
}
return _textField;
}
@end