RQHomePageViewModel.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // RQHomePageViewModel.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/21.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQHomePageViewModel.h"
  9. @interface RQHomePageViewModel ()
  10. @property (nonatomic, readwrite, strong) RACCommand *requestReLoginCommand;
  11. /// The current `user`.
  12. @property (nonatomic, readwrite , strong) RQUserModel *user;
  13. @end
  14. @implementation RQHomePageViewModel
  15. #pragma mark - Public Method
  16. - (instancetype)initWithServices:(id<RQViewModelServices>)services params:(NSDictionary *)params{
  17. if (self = [super initWithServices:services params:params]) {
  18. /// 获取user
  19. self.user = [RQ_USER_MANAGER currentUser];
  20. }
  21. return self;
  22. }
  23. - (void)initialize {
  24. [super initialize];
  25. self.title = @"";
  26. /// 隐藏导航栏的细线
  27. self.prefersNavigationBarBottomLineHidden = YES;
  28. ///配置数据
  29. [self rq_configureData];
  30. }
  31. #pragma mark - PrivateMethod
  32. - (void)rq_configureData {
  33. @weakify(self);
  34. /// 获取网络数据+本地数据
  35. RACSignal *fetchLocalDataSignal = [RACSignal return:[self fetchLocalData]];
  36. RACSignal *requestRemoteDataSignal = self.requestRemoteDataCommand.executionSignals.switchToLatest;
  37. [[[fetchLocalDataSignal
  38. merge:requestRemoteDataSignal]
  39. deliverOnMainThread]
  40. subscribeNext:^(RQUserModel *user) {
  41. @strongify(self)
  42. [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) {}];
  43. [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {}];
  44. [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectOne] subscribeNext:^(NSArray *examResultModelArr) {
  45. RQ_COMMON_MANAGER.myExamOneCount = examResultModelArr.count;
  46. RQ_COMMON_MANAGER.examResultOneListArr = examResultModelArr;
  47. }];
  48. [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectFour] subscribeNext:^(NSArray *examResultModelArr) {
  49. RQ_COMMON_MANAGER.myExamFourCount = examResultModelArr.count;
  50. RQ_COMMON_MANAGER.examResultFourListArr = examResultModelArr;
  51. }];
  52. [self willChangeValueForKey:@"user"];
  53. /// user模型的数据 重置,但是user的 指针地址不变
  54. [self.user mergeValuesForKeysFromModel:user];
  55. [self didChangeValueForKey:@"user"];
  56. }];
  57. [self.requestReLoginCommand execute:nil];
  58. }
  59. - (RACCommand *)requestReLoginCommand {
  60. @weakify(self)
  61. return [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
  62. @strongify(self)
  63. return [[self requestReLoginSignal] takeUntil:self.rac_willDeallocSignal];
  64. }];
  65. }
  66. /// 获取本地的用户数据
  67. - (RQUserModel *)fetchLocalData{
  68. return [RQ_USER_MANAGER currentUser];
  69. }
  70. /// 获取网络的用户数据 用于比对
  71. - (RACSignal *)requestReLoginSignal {
  72. return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
  73. if (RQStringIsNotEmpty([SAMKeychain rawLogin]) && RQStringIsNotEmpty([SAMKeychain rawLoginPassword]) && RQ_USER_MANAGER.currentUser) {
  74. RQ_USER_MANAGER.currentUser.channel = RQUserLoginChannelTypeAutoLogin;
  75. [[RQ_HTTP_Service studentLoginWithLoginCode:[SAMKeychain rawLogin] password:[SAMKeychain rawLoginPassword]] subscribeNext:^(RQUserModel *userModel) {
  76. [subscriber sendNext:userModel];
  77. } error:^(NSError * _Nullable error) {
  78. [subscriber sendError:error];
  79. } completed:^{
  80. [subscriber sendCompleted];
  81. }];
  82. }
  83. return [RACDisposable disposableWithBlock:^{
  84. /// 取消任务
  85. }];
  86. }] doError:^(NSError * _Nonnull error) {
  87. [RQ_USER_MANAGER logoutUser];
  88. /// 延迟一段时间
  89. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  90. /// 这里切换 到账号登录的界面
  91. [RQNotificationCenter postNotificationName:RQSwitchRootViewControllerNotification object:nil userInfo:@{RQSwitchRootViewControllerUserInfoKey:@(RQSwitchRootViewControllerFromTypeLogout)}];
  92. });
  93. [QMUITips showError:[NSError rq_tipsFromError:error]];
  94. }];
  95. }
  96. @end