YMRegisterViewModel.m 6.0 KB

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