RQActivateVIPViewModel.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // RQActivateVIPViewModel.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/9/9.
  6. //
  7. #import "RQActivateVIPViewModel.h"
  8. @interface RQActivateVIPViewModel ()
  9. /// activateVIPCode
  10. @property (nonatomic, readwrite, copy) NSString *activateVIPCode;
  11. /// error (PS;这个记录请求过程中的发生的错误,请求之前必须置nil)
  12. @property (nonatomic, readwrite, strong) NSError *error;
  13. /// 开通按钮有效性
  14. @property (nonatomic, readwrite, strong) RACSignal *validOpenSignal;
  15. /// openCommand
  16. @property (nonatomic, readwrite, strong) RACCommand *openCommand;
  17. /// getVipPriceCommand
  18. @property (nonatomic, readwrite, strong) RACCommand *getVipPriceCommand;
  19. /// getVipPriceOrriginalCommand
  20. //@property (nonatomic, readwrite, strong) RACCommand *getVipPriceOrriginalCommand;
  21. /// closeCommand
  22. @property (nonatomic, readwrite, strong) RACCommand *closeCommand;
  23. /// vipPrice
  24. @property (nonatomic, readwrite, copy) NSString *vipPrice;
  25. /// vipPriceOriginal
  26. @property (nonatomic, readwrite, copy) NSString *vipPriceOriginal;
  27. @property (nonatomic, readwrite, strong) NSDictionary *allVipTypeDic;
  28. @property (nonatomic, readwrite, strong) RQDictTypeModel *currentDictTypeModel;
  29. @end
  30. @implementation RQActivateVIPViewModel
  31. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params {
  32. if (self = [super initWithServices:services params:params]) {
  33. }
  34. return self;
  35. }
  36. - (void)initialize {
  37. [super initialize];
  38. self.title = @"开通VIP";
  39. self.prefersNavigationBarHidden = YES;
  40. @weakify(self);
  41. /// 按钮的有效性
  42. self.validOpenSignal = [[RACSignal
  43. combineLatest:@[RACObserve(self, activateVIPCode)]
  44. reduce:^(NSString *activateVIPCode) {
  45. return @(RQStringIsNotEmpty(activateVIPCode));
  46. }]
  47. distinctUntilChanged];
  48. void (^doNext)(RQBaseModel *) = ^(RQBaseModel *baseModel){
  49. if ([baseModel isKindOfClass:[RQPrepareOrderModel class]]) {
  50. RQPrepareOrderModel *prepareOrderModel = (RQPrepareOrderModel *)baseModel;
  51. [RQ_WECHAT_MANAGER clickToWechatPayWithPrepareOrder:prepareOrderModel];
  52. }
  53. };
  54. self.openCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
  55. @strongify(self);
  56. /// 将self.error = nil;
  57. self.error = nil;
  58. return [[self buy] doNext:doNext];
  59. return [RACSignal empty];
  60. }];
  61. self.closeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
  62. @strongify(self);
  63. [self.services popViewModelAnimated:YES];
  64. return [RACSignal empty];
  65. }];
  66. [[[RQNotificationCenter rac_addObserverForName:RQUserNotificationAlipayOrWechatSuccess object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification * note) {
  67. PayResp *resp = (PayResp *)note.object;
  68. NSLog(@"----微信支付成功:%@",resp.returnKey);
  69. [RQ_CACHE_MANAGER removeCacheWithPath:RQ_GET_VipInfo parameters:nil];
  70. [RQ_USER_MANAGER isVipWithComplete:^(NSInteger isVip) {
  71. if (isVip > 0) {
  72. [MBProgressHUD rq_showTips:@"购买VIP成功"];
  73. } else {
  74. [MBProgressHUD rq_showTips:@"VIP激活中!请稍等片刻!!!"];
  75. }
  76. [self.closeCommand execute:nil];
  77. }];
  78. }];
  79. }
  80. #pragma mark - PrivateMethods
  81. - (RACSignal *)open {
  82. @weakify(self);
  83. return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  84. [[RQ_HTTP_Service putVipCodeWithVipCode:self.activateVIPCode] subscribeNext:^(RQBaseModel *baseModel) {
  85. [subscriber sendNext:baseModel];
  86. } error:^(NSError * _Nullable error) {
  87. [subscriber sendError:error];
  88. } completed:^{
  89. [subscriber sendCompleted];
  90. }];
  91. return [RACScopedDisposable disposableWithBlock:^{
  92. /// 取消任务
  93. }];
  94. }] doError:^(NSError * _Nonnull error) {
  95. @strongify(self);
  96. self.error = error;
  97. }];
  98. }
  99. - (RACSignal *)buy {
  100. @weakify(self);
  101. return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  102. [[RQ_HTTP_Service postPrepareOrderWithDictCode:[NSString qmui_stringWithNSInteger:self.currentDictTypeModel.dictCode]] subscribeNext:^(RQPrepareOrderModel *prepareOrderModel) {
  103. [subscriber sendNext:prepareOrderModel];
  104. } error:^(NSError * _Nullable error) {
  105. [subscriber sendError:error];
  106. } completed:^{
  107. [subscriber sendCompleted];
  108. }];
  109. return [RACScopedDisposable disposableWithBlock:^{
  110. /// 取消任务
  111. }];
  112. }] doError:^(NSError * _Nonnull error) {
  113. @strongify(self);
  114. self.error = error;
  115. }];
  116. }
  117. - (RACSignal *)getVipPrice {
  118. @weakify(self);
  119. return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  120. [[RQ_HTTP_Service getConfigKeyWithPath:RQVipPricePath] subscribeNext:^(NSString *vipPriceStr) {
  121. [subscriber sendNext:vipPriceStr];
  122. } error:^(NSError * _Nullable error) {
  123. [subscriber sendError:error];
  124. } completed:^{
  125. [subscriber sendCompleted];
  126. }];
  127. return [RACScopedDisposable disposableWithBlock:^{
  128. /// 取消任务
  129. }];
  130. }] doError:^(NSError * _Nonnull error) {
  131. @strongify(self);
  132. self.error = error;
  133. }];
  134. }
  135. - (RACCommand *)getVipPriceCommand {
  136. @weakify(self);
  137. return [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
  138. @strongify(self);
  139. /// 将self.error = nil;
  140. self.error = nil;
  141. // [[RQ_HTTP_Service getConfigKeyWithPath:RQVipPricePath] subscribeNext:^(NSString *vipPriceStr) {
  142. // self.vipPrice = vipPriceStr;
  143. // } error:^(NSError * _Nullable error) {
  144. // @strongify(self);
  145. // self.error = error;
  146. // } completed:^{
  147. //
  148. // }];
  149. [[RQ_HTTP_Service getDictTypeWithPath:RQVipTypePath] subscribeNext:^(id _Nullable x) {
  150. NSArray *a = RQObjectIsNil(x)? @[] : x;
  151. NSString *vipPriceStr = [[a.rac_sequence.signal filter:^BOOL(RQDictTypeModel *dictTypeModel) {
  152. @strongify(self);
  153. NSString *key = [NSString qmui_stringWithNSInteger:RQ_USER_MANAGER.isVip];
  154. return [dictTypeModel.dictLabel isEqualToString:self.allVipTypeDic[key]];
  155. }].toArray.rac_sequence.signal map:^id _Nullable(RQDictTypeModel *dictTypeModel) {
  156. @strongify(self);
  157. self.currentDictTypeModel = dictTypeModel;
  158. return dictTypeModel.dictValue;
  159. }].toArray.firstObject;
  160. self.vipPrice = [vipPriceStr componentsSeparatedByString:@","].firstObject;
  161. self.vipPriceOriginal = [vipPriceStr componentsSeparatedByString:@","].lastObject;
  162. } error:^(NSError * _Nullable error) {
  163. @strongify(self);
  164. self.error = error;
  165. } completed:^{
  166. }];
  167. return [RACSignal empty];
  168. }];
  169. }
  170. //- (RACCommand *)getVipPriceOrriginalCommand {
  171. // @weakify(self);
  172. // return [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
  173. // @strongify(self);
  174. // /// 将self.error = nil;
  175. // self.error = nil;
  176. // [[RQ_HTTP_Service getConfigKeyWithPath:RQVipPriceOriginalPath] subscribeNext:^(NSString *vipPriceOriginalStr) {
  177. // self.vipPriceOriginal = vipPriceOriginalStr;
  178. // } error:^(NSError * _Nullable error) {
  179. // @strongify(self);
  180. // self.error = error;
  181. // } completed:^{
  182. //
  183. // }];
  184. // return [RACSignal empty];
  185. // }];
  186. //}
  187. - (NSDictionary *)allVipTypeDic {
  188. return @{
  189. @"2" : @"sdjk_up_sp_vip",
  190. @"0" : @"sdjk_sp_vip",
  191. };
  192. }
  193. @end