123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- //
- // SPSBVerificationCodeManager.m
- // 我的社保
- //
- // Created by jiaxian_he on 2021/5/18.
- //
- #import "SPSBVerificationCodeManager.h"
- #import "SPSBUIGeneralHeader.h"
- #import "SPSBKeyProfile.h"
- #import <JXHVerificationTools.h>
- #import "SPSBVerificationProfile.h"
- #import "UIView+SPSBLoadingTipsView.h"
- #import "UIViewController+SPSBNetworkManager.h"
- #define spsb_verificationCodeTime 60
- @interface SPSBVerificationCodeManager () {
- dispatch_source_t _timer;
- NSInteger _second;
- __weak UIButton *_verificationCodeButton;
- __weak UIViewController *_viewController;
- NSString *(^_getPhone)(void);
- NSString *_phone;
- }
- @end
- @implementation SPSBVerificationCodeManager
- - (instancetype)initWithBaseController:(UIViewController *)viewController {
- self = [super init];
- if (!self) return nil;
- _viewController = viewController;
- return self;
- }
- - (UIButton *)createVerificationCodeButtonWithGetPhone:(NSString *(^)(void))phone {
- UIButton *button = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(getVerificationCode)];
- [button setTitle:@"获取验证码" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
- [button createLineWithLocation:JXHLineLocationLeft headOffset:13 footOffset:13];
- _verificationCodeButton = button;
- NSTimeInterval difference = [self getVerificationCodeTimeOut];
- if (difference < spsb_verificationCodeTime) {
- [self reciprocalWithSecond:spsb_verificationCodeTime - difference];
- }
- _getPhone = phone;
- return button;
- }
- - (void)getVerificationCode {
- NSString *phone = _getPhone();
- if (phone.length != SPSBPhoneNumLength || !jxh_isNumber(phone)) {
- [_viewController.view showToastWithTitle:@"请正确输入手机号码"];
- return;
- }
- _phone = phone;
- [self ready];
- @weakify(self)
- [_viewController networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:false url:spsb_appUrl(SPSBUrlGetVerifyCode) urlParameters:nil parameters:^NSDictionary * _Nonnull{
- return @{@"phone" : phone};
- } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
- @strongify(self)
- #ifdef DEBUG
- [self showVerifyCode:data];
- #endif
- [self setVerigicationCodeTimeOut];
- [self reciprocalWithSecond:spsb_verificationCodeTime];
- return nil;
- } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
- @strongify(self)
- [self normal];
- return @"获取验证码失败";
- }];
- }
- - (void)showVerifyCode:(id)data {
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证码" message:[NSString stringWithFormat:@"%@", data[@"data"]] preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:[UIAlertAction actionWithTitle:@"复制" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- pasteboard.string = [NSString stringWithFormat:@"%@", data[@"data"]];
- }]];
-
- [jxh_findPresentedViewControllerStartingFrom(spsb_keyWindow().rootViewController) presentViewController:alert animated:true completion:^{
- }];
- }
- #pragma mark - Status
- - (void)normal {
- [self invalidateTimer];
- _verificationCodeButton.userInteractionEnabled = true;
- [_verificationCodeButton setTitle:@"获取验证码" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
-
- }
- - (void)ready {
- _verificationCodeButton.userInteractionEnabled = false;
- [_verificationCodeButton setTitle:@"正在获取..." titleColor:spsb_999999_color() state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
- }
- - (void)reciprocalWithSecond:(NSInteger)second {
- _second = second;
- @weakify(self)
- _timer = jxh_createDispatchTimer(1, 0, dispatch_get_main_queue(), ^{
- @strongify(self)
- [self changeSecond];
- });
- }
- #pragma mark - Count Down
- - (void)changeSecond {
- if (_second > 0) {
- _verificationCodeButton.userInteractionEnabled = false;
- [_verificationCodeButton setTitle:[NSString stringWithFormat:@"还剩%ld秒", (long)_second] titleColor:spsb_999999_color() state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
-
- _second --;
- } else {
- [self invalidateTimer];
- [self normal];
- }
- }
- - (void)invalidateTimer {
- if (_timer) {
- dispatch_source_cancel(_timer);
- _timer = nil;
- }
- }
- #pragma mark - Time
- - (NSTimeInterval)getVerificationCodeTimeOut {
- NSString *changeTimeStr = [jxh_userDefaults() objectForKey:SPSBVerifyCodeTime];
- if (!changeTimeStr) {
- return spsb_verificationCodeTime;
- }
- NSDate *date = [jxh_dateFormatter() dateFromString:changeTimeStr];
- NSTimeInterval difference = fabs([date timeIntervalSinceNow]);
- return difference;
- }
- - (void)setVerigicationCodeTimeOut {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSDate *date = [NSDate new];
- NSString *dateString = [jxh_dateFormatter() stringFromDate:date];
- [jxh_userDefaults() setObject:dateString forKey:SPSBVerifyCodeTime];
- [jxh_userDefaults() synchronize];
- });
- }
- @end
- #undef spsb_verificationCodeTime
|