335 lines
11 KiB
Objective-C
335 lines
11 KiB
Objective-C
//
|
||
// MewLoginPhoneView.m
|
||
// mew-ios
|
||
//
|
||
// Created by 触海 on 2023/11/8.
|
||
//
|
||
|
||
#import "MewLoginPhoneView.h"
|
||
///第三方
|
||
#import <Masonry/Masonry.h>
|
||
#import <ReactiveObjC/ReactiveObjC.h>
|
||
///Presenter
|
||
#import "MewLoginVerifCodePresent.h"
|
||
///Protocole
|
||
#import "MewLoginVerifCodeProtocol.h"
|
||
/// Tool
|
||
#import "MewThemeColor.h"
|
||
#import "MewMacro.h"
|
||
#import "MewHUDTool.h"
|
||
#import "UIImage+MewUtils.h"
|
||
/// View
|
||
#import "MewLoginInputView.h"
|
||
|
||
@interface MewLoginPhoneView()<MewLoginVerifCodeProtocol>
|
||
/// 手机号码输入框
|
||
@property (nonatomic, strong) MewLoginInputView *phoneInputView;
|
||
@property (nonatomic, strong) NSString *phone;
|
||
|
||
///验证码输入框
|
||
@property (nonatomic,strong) UITextField *codeTextField;
|
||
///验证码View
|
||
@property (nonatomic,strong) UIView *codeView;
|
||
///重新获取验证码 和 倒计时
|
||
//@property (nonatomic,strong) UIStackView *codeStackView;
|
||
///显示倒计时
|
||
@property (nonatomic,strong) UILabel *cutdownLabel;
|
||
///重新获得验证码
|
||
@property (nonatomic,strong) UIButton *retryCodeButton;
|
||
/// 确认
|
||
@property (nonatomic, strong) UIButton *confirmButton;
|
||
|
||
/// present
|
||
@property (nonatomic, strong) MewLoginVerifCodePresent *present;
|
||
/// 计时器
|
||
@property (strong, nonatomic) dispatch_source_t timer;
|
||
@end
|
||
|
||
@implementation MewLoginPhoneView
|
||
|
||
#pragma mark - Life Cycle
|
||
- (instancetype)initWithFrame:(CGRect)frame {
|
||
self = [super initWithFrame:frame];
|
||
if (self) {
|
||
[self mew_initSubViews];
|
||
[self mew_initSubViewConstraints];
|
||
[self mew_initEvents];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
/// 销毁
|
||
- (void)dealloc {
|
||
[self mew_stopCountDown];
|
||
}
|
||
|
||
|
||
#pragma mark - MewLoginVerifCodeProtocol
|
||
/// 获取验证码成功
|
||
- (void)mew_phoneSmsCodeSuccess {
|
||
[MewHUDTool showErrorWithMessage:[NSString stringWithFormat:@"验证码已发送\n+86 %@", self.phone]];
|
||
/// 开启定时器
|
||
[self mew_openCountdownWithTime:60];
|
||
}
|
||
|
||
/// 登录成功
|
||
- (void)mew_loginWithPhoneSuccess {
|
||
[MewHUDTool showSuccessWithMessage:@"登录成功"];
|
||
[self mew_stopCountDown];
|
||
if (self.delegate) {
|
||
[self.delegate mew_loginWithPhoneSuccess];
|
||
}
|
||
}
|
||
|
||
/// 绑定手机号码成功
|
||
- (void)mew_bindWithPhoneSuccess {
|
||
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - Action Event
|
||
/// 获取验证码
|
||
- (void)mew_getSmsCodeButtonAction {
|
||
if (self.phone.length < 11 || self.phone == nil) {
|
||
[MewHUDTool showErrorWithMessage:@"请输入手机号码"];
|
||
return;
|
||
}
|
||
|
||
[self.present mew_phoneSmsCode:self.phone type:MewUserLoginType_Regist];
|
||
}
|
||
|
||
- (void)textFieldDidChange:(UITextField *)text {
|
||
if (text.text.length > 5) {
|
||
text.text = [text.text substringToIndex:5];
|
||
}
|
||
|
||
self.confirmButton.enabled = (text.text.length == 5 && self.phone.length == 11);
|
||
}
|
||
|
||
/// 下一步
|
||
- (void)mew_confirmButtonAction {
|
||
[self.present mew_loginWithPhone:self.phone code:self.codeTextField.text];
|
||
}
|
||
|
||
#pragma mark - Private Method
|
||
/// 开启定时器
|
||
- (void)mew_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 mew_onCountdownFinish];
|
||
});
|
||
}else{
|
||
time--;
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
//设置按钮显示读秒效果
|
||
[self mew_onCountdownOpen:time];
|
||
});
|
||
|
||
}
|
||
});
|
||
dispatch_resume(self.timer);
|
||
}
|
||
|
||
/// 倒计时中
|
||
- (void)mew_onCountdownOpen:(int)time {
|
||
self.cutdownLabel.hidden = NO;
|
||
self.retryCodeButton.hidden = YES;
|
||
self.cutdownLabel.text = [NSString stringWithFormat:@"%d %@", time, @"s"];
|
||
}
|
||
|
||
/// 倒计时结束
|
||
- (void)mew_onCountdownFinish {
|
||
self.retryCodeButton.hidden = NO;
|
||
self.cutdownLabel.hidden = YES;
|
||
}
|
||
|
||
// 停止倒计时
|
||
- (void)mew_stopCountDown {
|
||
if (self.timer != nil) {
|
||
dispatch_source_cancel(self.timer);
|
||
self.timer = nil;
|
||
}
|
||
}
|
||
|
||
#pragma mark - Init View
|
||
- (void)mew_initSubViews {
|
||
[self addSubview:self.phoneInputView];
|
||
[self addSubview:self.codeView];
|
||
// [self.codeView addSubview:self.codeStackView];
|
||
[self.codeView addSubview:self.codeTextField];
|
||
[self.codeView addSubview:self.cutdownLabel];
|
||
[self.codeView addSubview:self.retryCodeButton];
|
||
[self addSubview:self.confirmButton];
|
||
}
|
||
|
||
- (void)mew_initSubViewConstraints {
|
||
CGFloat kscale = 363.0 / 375.0;
|
||
[self.phoneInputView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.equalTo(self);
|
||
make.left.mas_equalTo(36);
|
||
make.right.mas_equalTo(-36);
|
||
make.height.mas_equalTo(52);
|
||
}];
|
||
|
||
[self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.equalTo(self.phoneInputView.mas_bottom).offset(20);
|
||
make.left.right.height.equalTo(self.phoneInputView);
|
||
}];
|
||
|
||
[self.retryCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.right.equalTo(self.codeView).offset(-15);
|
||
make.centerY.equalTo(self.codeView);
|
||
}];
|
||
[self.cutdownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.edges.equalTo(self.retryCodeButton);
|
||
}];
|
||
[self.codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.equalTo(self.codeView).offset(15);
|
||
make.centerY.equalTo(self.codeView);
|
||
}];
|
||
|
||
// [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.top.equalTo(self.codeView.mas_bottom).offset(15);
|
||
make.left.right.height.mas_equalTo(self.codeView);
|
||
}];
|
||
}
|
||
|
||
- (void)mew_initEvents {
|
||
@weakify(self);
|
||
|
||
// [self.phoneInputView.textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
|
||
// NSLog(@"value:%@",value);
|
||
// return YES;
|
||
// }];
|
||
[self.phoneInputView.textField.rac_textSignal subscribeNext:^(NSString * _Nullable value) {
|
||
@strongify(self);
|
||
self.phone = value;
|
||
}];
|
||
|
||
// [[self.phoneInputView.textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
|
||
// self.phone = value;
|
||
//
|
||
// return value;
|
||
// }] subscribeNext:^(id _Nullable x) {
|
||
//// @strongify(self);
|
||
//// self.phoneInputView.textField.text = x;
|
||
//// [self updateNextButton];
|
||
// }];
|
||
//
|
||
// [self.phoneInputView.textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
|
||
// @strongify(self);
|
||
// self.phone = value;
|
||
// return YES;
|
||
// }];
|
||
}
|
||
|
||
#pragma mark - Get
|
||
- (MewLoginInputView *)phoneInputView {
|
||
if (!_phoneInputView) {
|
||
_phoneInputView = [[MewLoginInputView alloc] init];
|
||
_phoneInputView.backgroundColor = [MewThemeColor mewColorWithHexString:@"#F8F8FB"];
|
||
_phoneInputView.layer.cornerRadius = 51.0/2;
|
||
_phoneInputView.layer.masksToBounds = YES;
|
||
_phoneInputView.maxLength = 11;
|
||
_phoneInputView.placeHolder = @"请输入手机号码";
|
||
}
|
||
return _phoneInputView;
|
||
}
|
||
|
||
- (UIView *)codeView {
|
||
if (!_codeView) {
|
||
_codeView = [[UIView alloc] init];
|
||
_codeView.backgroundColor = UIColor.whiteColor;
|
||
_codeView.layer.masksToBounds = YES;
|
||
_codeView.layer.cornerRadius = 51.0/2;
|
||
|
||
}
|
||
return _codeView;
|
||
}
|
||
|
||
- (UITextField *)codeTextField {
|
||
if (!_codeTextField) {
|
||
_codeTextField = [[UITextField alloc] init];
|
||
_codeTextField.textColor = [MewThemeColor mewMainTextColor];
|
||
_codeTextField.font = [UIFont systemFontOfSize:18.f];
|
||
_codeTextField.keyboardType = UIKeyboardTypeNumberPad;
|
||
_codeTextField.tintColor = [MewThemeColor mewMainTextColor];
|
||
_codeTextField.backgroundColor = [UIColor clearColor];
|
||
[_codeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
|
||
_codeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName: [MewThemeColor mewSecondTextColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
|
||
}
|
||
return _codeTextField;
|
||
}
|
||
|
||
- (UILabel *)cutdownLabel {
|
||
if (!_cutdownLabel) {
|
||
_cutdownLabel = [[UILabel alloc] init];
|
||
_cutdownLabel.textAlignment = NSTextAlignmentCenter;
|
||
_cutdownLabel.font = [UIFont boldSystemFontOfSize:11];
|
||
_cutdownLabel.textColor = [MewThemeColor mewSecondTextColor];
|
||
_cutdownLabel.hidden = YES;
|
||
}
|
||
return _cutdownLabel;
|
||
}
|
||
|
||
|
||
- (UIButton *)retryCodeButton {
|
||
if (!_retryCodeButton) {
|
||
_retryCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
[_retryCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
|
||
[_retryCodeButton setTitleColor:[MewThemeColor mewColorWithHexString:@"#9552FF"] forState:UIControlStateNormal];
|
||
_retryCodeButton.titleLabel.font = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
|
||
[_retryCodeButton addTarget:self action:@selector(mew_getSmsCodeButtonAction) forControlEvents:UIControlEventTouchUpInside];
|
||
_retryCodeButton.hidden = NO;
|
||
}
|
||
return _retryCodeButton;
|
||
}
|
||
|
||
|
||
|
||
- (UIButton *)confirmButton{
|
||
if (!_confirmButton) {
|
||
_confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
_confirmButton.layer.masksToBounds = YES;
|
||
_confirmButton.layer.cornerRadius = 52/2.f;
|
||
[_confirmButton setImage:[UIImage imageNamed:@"mew_login_next"] forState:UIControlStateNormal];
|
||
_confirmButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
|
||
UIImage *image = [UIImage mew_gradientColorImageFromColors:@[[MewThemeColor mewColorWithHexString:@"#FF60FD"], [MewThemeColor mewColorWithHexString:@"#8974FF"],[MewThemeColor mewColorWithHexString:@"#69EBFF"]] gradientType:MewGradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth - 2*36, 52)];
|
||
_confirmButton.backgroundColor = [UIColor colorWithPatternImage:image];
|
||
[_confirmButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
|
||
[_confirmButton addTarget:self action:@selector(mew_confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _confirmButton;
|
||
}
|
||
|
||
|
||
- (MewLoginVerifCodePresent *)present {
|
||
if (!_present) {
|
||
_present = [[MewLoginVerifCodePresent alloc] init];
|
||
[_present mew_attatchView:self];
|
||
}
|
||
return _present;
|
||
}
|
||
|
||
@end
|