YMVerifyCodeLoginViewModel.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // YMVerifyCodeLoginViewModel.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/4.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMVerifyCodeLoginViewModel.h"
  9. #import "YMRetrievePasswordViewModel.h"
  10. #import "YMRegisterViewModel.h"
  11. #import "YMImproveInfoViewModel.h"
  12. @interface YMVerifyCodeLoginViewModel ()
  13. /// 登录按钮有效性
  14. @property (nonatomic, strong, readwrite) RACSignal *validLoginSignal;
  15. @end
  16. @implementation YMVerifyCodeLoginViewModel
  17. - (void)ym_initialize{
  18. [super ym_initialize];
  19. /// 登录按钮有效性
  20. self.validLoginSignal = [[[RACSignal combineLatest:@[
  21. RACObserve(self, mobile),
  22. RACObserve(self, verifyCode)
  23. ] reduce:^(NSString *mobile, NSString *verifyCode) {
  24. //是否不为空
  25. return @(!OCStringIsEmpty(mobile) && !OCStringIsEmpty(verifyCode));
  26. }] distinctUntilChanged] takeUntil:self.rac_willDeallocSignal];
  27. }
  28. /// 获取验证码
  29. - (void)getVerifyCodeWithCaptcha:(id)captcha Handler:(void(^)(NSDictionary *dic, NSError * _Nullable error))handler{
  30. NSMutableDictionary *dictm = [NSMutableDictionary dictionary];
  31. dictm[@"mobile"] = self.mobile;
  32. dictm[@"type"] = @"login";
  33. if ([captcha isKindOfClass:[NSString class]]) {
  34. dictm[@"captcha"] = captcha;
  35. }
  36. if ([captcha isKindOfClass:[NSDictionary class]]) {
  37. [dictm addEntriesFromDictionary:captcha];
  38. }
  39. @weakify(self)
  40. [ZCHUDHelper showWithStatus:@"获取验证码中"];
  41. [LCHttpHelper requestWithURLString:PhoneCode parameters:dictm needToken:NO type:HttpRequestTypePost success:^(id responseObject) {
  42. @strongify(self)
  43. NSDictionary* dict = (NSDictionary*)responseObject;
  44. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  45. if (code == 0) {
  46. [ZCHUDHelper showTitle:@"验证码已发送"];
  47. handler(dict?:@{},nil);
  48. }else{
  49. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  50. }
  51. } failure:^(NSError *error) {
  52. [ZCHUDHelper showTitle:error.localizedDescription];
  53. }];
  54. }
  55. - (void)loginRequest{
  56. @weakify(self)
  57. [ZCHUDHelper showWithStatus:@"加载中..."];
  58. [LCHttpHelper requestWithURLString:PhoneCodeLogin parameters:@{
  59. @"mobile":self.mobile?:@"",
  60. @"phone_code":self.verifyCode?:@"",
  61. @"agreement":@"1",
  62. } needToken:NO type:HttpRequestTypePost success:^(id responseObject) {
  63. @strongify(self)
  64. NSDictionary* dict = (NSDictionary*)responseObject;
  65. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  66. if (code == 0) {
  67. [ZCHUDHelper dismiss];
  68. YOUPAILCUserModel* userModel = [YOUPAILCUserModel mj_objectWithKeyValues:[dict objectForKey:@"data"]];
  69. [LCSaveModel saveUserModel:userModel];
  70. [LCSaveData saveTokenString:userModel.youpaipuserinfo.youpaiptoken];//保存token
  71. [LCSaveData saveLoginMark:YES];
  72. [OCUserDefaults setObject:userModel.youpaipuserinfo.youpaipuser_id forKey:kUSER_ID];
  73. [OCUserDefaults setBool:userModel.youpaipuserinfo.youpaipgender == 1 ? YES : NO forKey:kIS_FEMALE_GENDER];
  74. [OCUserDefaults setBool:userModel.youpaipuserinfo.youpaipis_anchor == 1 ? YES : NO forKey:kIS_GODDESS_CERTIFIED];
  75. [OCUserDefaults setBool:userModel.youpaipuserinfo.youpaipvip == 0 ? NO : YES forKey:kIS_VIP];
  76. [OCUserDefaults synchronize];
  77. [LCHttpHelper requestWithURLString:GiftConfig parameters:nil needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  78. NSDictionary* dict = (NSDictionary*)responseObject;
  79. NSLog(@"GiftConfig == %@",dict);
  80. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  81. if (code==0) {//成功
  82. NSMutableArray *giftArray = [NSMutableArray array];
  83. giftArray = [YOUPAILCGiftModel mj_objectArrayWithKeyValuesArray:[[dict objectForKey:@"data"]objectForKey:@"gift_list"]];
  84. for (int i = 0; i<giftArray.count; i++) {
  85. YOUPAILCGiftModel *model = giftArray[i];
  86. if (model.youpaipsvga.length > 0 ) {
  87. [LCTools giftSVGAWithSvgaUrlStr:model.youpaipsvga];
  88. }
  89. }
  90. }
  91. } failure:^(NSError *error) {
  92. }];
  93. if (userModel.youpaipuserinfo.youpaiptype == 2) {
  94. //是特殊用户
  95. [LCSaveData saveIsspecial:YES];
  96. }
  97. //未完善资料,进入完善资料页面
  98. if (userModel.youpaipuserinfo.youpaipfinish_status == 0) {
  99. YMImproveInfoViewModel *improveInfoVM = [[YMImproveInfoViewModel alloc]initWithParams:@{}];
  100. [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_IMPROVE_INFO) withUserInfo:@{
  101. RouterViewModel:improveInfoVM
  102. } completion:nil];
  103. }else{
  104. //已经完善资料
  105. [[YOUPAILCIMLoginManager sharedCenter]IMLogin:[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id token:[LCSaveModel getUserModel].youpaipuserinfo.youpaipim_token];
  106. [LCTools changeRootToTabbar];
  107. }
  108. }else{
  109. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  110. }
  111. } failure:^(NSError *error) {
  112. [ZCHUDHelper showTitle:error.localizedDescription];
  113. }];
  114. }
  115. - (void)gotoRegisterVC{
  116. YMRegisterViewModel *registerVM = [[YMRegisterViewModel alloc]initWithParams:@{}];
  117. [YMRouter openURL:stringFormat(@"%@%@",YM_ROUTER_URL_PREFIX,YM_ROUTER_REGISTER) withUserInfo:@{
  118. RouterViewModel:registerVM
  119. } completion:nil];
  120. }
  121. @end