cdts/xdts-ios 3/TreeHole/Code/Utility/InputView/PYGetSmsCodeButton.m
2023-07-27 09:20:00 +08:00

87 lines
2.5 KiB
Objective-C
Executable File

//
// PYGetSmsCodeButton.m
// Food
//
// Created by ko1o on 2019/7/23.
// Copyright © 2019年 ko1o. All rights reserved.
//
#import "PYGetSmsCodeButton.h"
@interface PYGetSmsCodeButton ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int currentTimeInterval;
@end
@implementation PYGetSmsCodeButton
+ (instancetype)smsCodeButton
{
PYGetSmsCodeButton *smsCodeButton = [PYGetSmsCodeButton buttonWithType:UIButtonTypeCustom];
smsCodeButton.size = CGSizeMake(FIX_SIZE(124), 52);
[smsCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
[smsCodeButton setTitleColor:COLOR_WITH_RGB(0x040000) forState:UIControlStateNormal];
smsCodeButton.titleLabel.font = MT_FONT_MEDIUM_SIZE(15);
smsCodeButton.layer.cornerRadius = FIX_SIZE(20);
smsCodeButton.backgroundColor = THEME_COLOR;
smsCodeButton.clipsToBounds = YES;
[smsCodeButton addTarget:smsCodeButton action:@selector(sendSmsCode:) forControlEvents:UIControlEventTouchUpInside];
return smsCodeButton;
}
- (void)sendSmsCode:(UIButton *)sender
{
sender.enabled = NO;
NSString *phone = self.getSmsWithPhoneBlock();
if (![PYAppService isPhoneVaild:phone]) {
sender.enabled = YES;
return;
}
[LoginService getSmsCodeByPhone:phone completion:^(id _Nullable rsp, NSError * _Nullable error) {
if (!error) {
[SVProgressHUD showSuccessWithStatus:@"发送成功"];
[self startTimer];
} else {
sender.enabled = YES;
}
}];
}
- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
self.alpha = enabled ? 1.0 : 0.4;
}
- (void)startTimer
{
self.currentTimeInterval = 1;
[self setTitle:[NSString stringWithFormat:@"重新发送(%d)", maxGetSmsCodeTimeInterval - self.currentTimeInterval] forState:UIControlStateNormal];
WeakSelf(self);
self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
StrongSelf(self);
self.currentTimeInterval++;
if (self.currentTimeInterval >= maxGetSmsCodeTimeInterval) {
[self setTitle:@"重新发送" forState:UIControlStateNormal];
self.enabled = YES;
[timer invalidate];
} else {
self.enabled = NO;
[self setTitle:[NSString stringWithFormat:@"重新发送(%d)", maxGetSmsCodeTimeInterval - self.currentTimeInterval] forState:UIControlStateNormal];
}
}];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
@end