RQVerificationSmsCodeViewModel.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // RQVerificationSmsCodeViewModel.m
  3. // YueXueChe
  4. //
  5. // Created by 张嵘 on 2018/12/14.
  6. // Copyright © 2018 lee. All rights reserved.
  7. //
  8. #import "RQVerificationSmsCodeViewModel.h"
  9. #import "YYTimer.h"
  10. @interface RQVerificationSmsCodeViewModel ()
  11. /// closeCommand
  12. @property (nonatomic, readwrite, strong) RACCommand *closeCommand;
  13. /// registerCommand
  14. @property (nonatomic, readwrite, strong) RACCommand *sureCommand;
  15. /// 验证码命令
  16. @property (nonatomic, readwrite, strong) RACCommand *captchaCommand;
  17. /// 注册按钮有效性
  18. @property (nonatomic, readwrite, strong) RACSignal *validSureSignal;
  19. /// 验证码按钮能否点击
  20. @property (nonatomic, readwrite, strong) RACSignal *validCaptchaSignal;
  21. /// 验证码显示
  22. @property (nonatomic, readwrite, copy) NSString *captchaTitle;
  23. /// error (PS;这个记录请求过程中的发生的错误,请求之前必须置nil)
  24. @property (nonatomic, readwrite, strong) NSError *error;
  25. /// Timer
  26. @property (nonatomic, readwrite, strong) YYTimer *timer;
  27. /// Count
  28. @property (nonatomic, readwrite, assign) NSUInteger count;
  29. /// valid (有效性)
  30. @property (nonatomic, readwrite, assign) BOOL valid;
  31. /// phone
  32. @property (nonatomic, readwrite, copy) NSString *phone;
  33. /// captcha
  34. @property (nonatomic, readwrite, copy) NSString *captcha;
  35. @end
  36. @implementation RQVerificationSmsCodeViewModel
  37. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  38. if (self = [super initWithServices:services params:params]) {
  39. self.phone = params[RQMobileLoginPhoneKey];
  40. }
  41. return self;
  42. }
  43. - (void)initialize {
  44. [super initialize];
  45. /// 显示导航栏的细线
  46. self.prefersNavigationBarBottomLineHidden = NO;
  47. self.title = @"修改密码";
  48. @weakify(self);
  49. self.closeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
  50. @strongify(self);
  51. [self.services dismissViewModelAnimated:YES completion:NULL];
  52. return [RACSignal empty];
  53. }];
  54. /// 按钮的有效性
  55. self.validSureSignal = [[RACSignal
  56. combineLatest:@[RACObserve(self, phone),RACObserve(self, captcha)]
  57. reduce:^(NSString *phone , NSString *captcha) {
  58. return @(RQStringIsNotEmpty(phone) && RQStringIsNotEmpty(captcha));
  59. }]
  60. distinctUntilChanged];
  61. /// 验证码按钮有效性
  62. self.captchaTitle = @" 获取验证码 ";
  63. RAC(self, valid) = [[RACObserve(self, timer.valid) map:^id(NSNumber * value) {
  64. return @(!value.boolValue);
  65. }] distinctUntilChanged];
  66. /// 验证码有效性
  67. self.validCaptchaSignal = RACObserve(self, valid);
  68. self.captchaCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
  69. @strongify(self);
  70. /// 将错误置为nil
  71. self.error = nil;
  72. self.valid = NO;
  73. self.captchaTitle = @" 发送中... ";
  74. return [self getSmsCode];
  75. }];
  76. self.sureCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *captchadic) {
  77. @strongify(self);
  78. self.error = nil; // 先置nil
  79. self.captcha = captchadic[RQCaptchaKey];
  80. return [self verificationSmsCode];
  81. }];
  82. }
  83. #pragma mark - Action
  84. - (void)_timerValueChanged:(YYTimer *)timer
  85. {
  86. self.count--;
  87. if (self.count == 0) {
  88. [timer invalidate];
  89. self.timer = nil;
  90. [self _resetCaptcha];
  91. return;
  92. }
  93. self.captchaTitle = [NSString stringWithFormat:@" %zds后重新发送 ", self.count];
  94. }
  95. #pragma mark - 辅助方法
  96. - (void)_resetCaptcha {
  97. self.valid = YES;
  98. self.captchaTitle = @" 获取验证码 ";
  99. }
  100. - (RACSignal *)getSmsCode {
  101. /// 执行
  102. @weakify(self);
  103. return [[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
  104. [[RQ_HTTP_Service getCodeWithTelePhone:self.phone] subscribeNext:^(id _Nullable x) {
  105. [subscriber sendNext:x];
  106. } error:^(NSError * _Nullable error) {
  107. [subscriber sendError:error];
  108. } completed:^{
  109. [subscriber sendCompleted];
  110. }];
  111. return [RACDisposable disposableWithBlock:^{
  112. /// 取消任务
  113. }];
  114. }] doNext:^(id x) {
  115. @strongify(self);
  116. self.count = RQCaptchaFetchMaxWords;
  117. self.captchaTitle = @" 60s后重新发送 ";
  118. self.timer = [YYTimer timerWithTimeInterval:1 target:self selector:@selector(_timerValueChanged:) repeats:YES];
  119. }] doError:^(NSError *error) {
  120. @strongify(self);
  121. self.error = error;
  122. self.valid = YES;
  123. self.captchaTitle = @" 发送验证码 ";
  124. }];
  125. }
  126. - (RACSignal *)verificationSmsCode {
  127. @weakify(self);
  128. return [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  129. [[RQ_HTTP_Service verificationSmsCodeWithMobile:self.phone type:RQSMSCodeFindPassword code:self.captcha] subscribeNext:^(id _Nullable x) {
  130. [subscriber sendNext:x];
  131. } error:^(NSError * _Nullable error) {
  132. [subscriber sendError:error];
  133. } completed:^{
  134. [subscriber sendCompleted];
  135. }];
  136. return [RACDisposable disposableWithBlock:^{
  137. /// 取消任务
  138. }];
  139. }] doNext:^(id _Nullable x) {
  140. RQUpdatePasswordViewModel *model = [[RQUpdatePasswordViewModel alloc] initWithServices:self.services params:nil];
  141. [self.services pushViewModel:model animated:YES];
  142. }] doError:^(NSError * _Nonnull error) {
  143. @strongify(self);
  144. self.error = error;
  145. }] ;
  146. }
  147. @end