SPSBVerificationCodeManager.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // SPSBVerificationCodeManager.m
  3. // 我的社保
  4. //
  5. // Created by jiaxian_he on 2021/5/18.
  6. //
  7. #import "SPSBVerificationCodeManager.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import "SPSBKeyProfile.h"
  10. #import <JXHVerificationTools.h>
  11. #import "SPSBVerificationProfile.h"
  12. #import "UIView+SPSBLoadingTipsView.h"
  13. #import "UIViewController+SPSBNetworkManager.h"
  14. #define spsb_verificationCodeTime 60
  15. @interface SPSBVerificationCodeManager () {
  16. dispatch_source_t _timer;
  17. NSInteger _second;
  18. __weak UIButton *_verificationCodeButton;
  19. __weak UIViewController *_viewController;
  20. NSString *(^_getPhone)(void);
  21. NSString *_phone;
  22. }
  23. @end
  24. @implementation SPSBVerificationCodeManager
  25. - (instancetype)initWithBaseController:(UIViewController *)viewController {
  26. self = [super init];
  27. if (!self) return nil;
  28. _viewController = viewController;
  29. return self;
  30. }
  31. - (UIButton *)createVerificationCodeButtonWithGetPhone:(NSString *(^)(void))phone {
  32. UIButton *button = [UIButton convenienceWithFont:spsb_font(15) target:self action:@selector(getVerificationCode)];
  33. [button setTitle:@"获取验证码" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
  34. [button createLineWithLocation:JXHLineLocationLeft headOffset:13 footOffset:13];
  35. _verificationCodeButton = button;
  36. NSTimeInterval difference = [self getVerificationCodeTimeOut];
  37. if (difference < spsb_verificationCodeTime) {
  38. [self reciprocalWithSecond:spsb_verificationCodeTime - difference];
  39. }
  40. _getPhone = phone;
  41. return button;
  42. }
  43. - (void)getVerificationCode {
  44. NSString *phone = _getPhone();
  45. if (phone.length != SPSBPhoneNumLength || !jxh_isNumber(phone)) {
  46. [_viewController.view showToastWithTitle:@"请正确输入手机号码"];
  47. return;
  48. }
  49. _phone = phone;
  50. [self ready];
  51. @weakify(self)
  52. [_viewController networkUseMethod:SPSBNetworkMethodPOST loadingTips:nil isLogin:false url:spsb_appUrl(SPSBUrlGetVerifyCode) urlParameters:nil parameters:^NSDictionary * _Nonnull{
  53. return @{@"phone" : phone};
  54. } success:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  55. @strongify(self)
  56. #ifdef DEBUG
  57. [self showVerifyCode:data];
  58. #endif
  59. [self setVerigicationCodeTimeOut];
  60. [self reciprocalWithSecond:spsb_verificationCodeTime];
  61. return nil;
  62. } failure:^NSString * _Nullable(NSError * _Nonnull error, id _Nullable data) {
  63. @strongify(self)
  64. [self normal];
  65. return @"获取验证码失败";
  66. }];
  67. }
  68. - (void)showVerifyCode:(id)data {
  69. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证码" message:[NSString stringWithFormat:@"%@", data[@"data"]] preferredStyle:UIAlertControllerStyleAlert];
  70. [alert addAction:[UIAlertAction actionWithTitle:@"复制" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  71. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  72. pasteboard.string = [NSString stringWithFormat:@"%@", data[@"data"]];
  73. }]];
  74. [jxh_findPresentedViewControllerStartingFrom(spsb_keyWindow().rootViewController) presentViewController:alert animated:true completion:^{
  75. }];
  76. }
  77. #pragma mark - Status
  78. - (void)normal {
  79. [self invalidateTimer];
  80. _verificationCodeButton.userInteractionEnabled = true;
  81. [_verificationCodeButton setTitle:@"获取验证码" titleColor:spsb_3296FB_color() state:JXHButtonControlStateNormal];
  82. }
  83. - (void)ready {
  84. _verificationCodeButton.userInteractionEnabled = false;
  85. [_verificationCodeButton setTitle:@"正在获取..." titleColor:spsb_999999_color() state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  86. }
  87. - (void)reciprocalWithSecond:(NSInteger)second {
  88. _second = second;
  89. @weakify(self)
  90. _timer = jxh_createDispatchTimer(1, 0, dispatch_get_main_queue(), ^{
  91. @strongify(self)
  92. [self changeSecond];
  93. });
  94. }
  95. #pragma mark - Count Down
  96. - (void)changeSecond {
  97. if (_second > 0) {
  98. _verificationCodeButton.userInteractionEnabled = false;
  99. [_verificationCodeButton setTitle:[NSString stringWithFormat:@"还剩%ld秒", (long)_second] titleColor:spsb_999999_color() state:JXHButtonControlStateNormal | JXHButtonControlStateHighlighted];
  100. _second --;
  101. } else {
  102. [self invalidateTimer];
  103. [self normal];
  104. }
  105. }
  106. - (void)invalidateTimer {
  107. if (_timer) {
  108. dispatch_source_cancel(_timer);
  109. _timer = nil;
  110. }
  111. }
  112. #pragma mark - Time
  113. - (NSTimeInterval)getVerificationCodeTimeOut {
  114. NSString *changeTimeStr = [jxh_userDefaults() objectForKey:SPSBVerifyCodeTime];
  115. if (!changeTimeStr) {
  116. return spsb_verificationCodeTime;
  117. }
  118. NSDate *date = [jxh_dateFormatter() dateFromString:changeTimeStr];
  119. NSTimeInterval difference = fabs([date timeIntervalSinceNow]);
  120. return difference;
  121. }
  122. - (void)setVerigicationCodeTimeOut {
  123. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  124. NSDate *date = [NSDate new];
  125. NSString *dateString = [jxh_dateFormatter() stringFromDate:date];
  126. [jxh_userDefaults() setObject:dateString forKey:SPSBVerifyCodeTime];
  127. [jxh_userDefaults() synchronize];
  128. });
  129. }
  130. @end
  131. #undef spsb_verificationCodeTime