RQLoginViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // RQLoginViewController.m
  3. // YueXueChe
  4. //
  5. // Created by 张嵘 on 2018/12/13.
  6. // Copyright © 2018 lee. All rights reserved.
  7. //
  8. #import "RQLoginViewController.h"
  9. #import "RQRegisterViewController.h"
  10. #import "RQVerificationSmsCodeViewController.h"
  11. #import "RQPhoneView.h"
  12. #import "RQPasswordView.h"
  13. @interface RQLoginViewController ()
  14. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *closeBtnTopToTop;
  15. @property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
  16. @property (weak, nonatomic) IBOutlet UIView *containerView;
  17. @property (nonatomic, readonly, strong) RQLoginViewModel *viewModel;
  18. @property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
  19. @property (strong, readwrite, nonatomic) RQPhoneView *phoneView;
  20. @property (strong, readwrite, nonatomic) RQPasswordView *passwordView;
  21. @property (strong, readwrite, nonatomic) RQAgreementView *agreementView;
  22. @property (weak, nonatomic) IBOutlet UIButton *loginBtn;
  23. @property (weak, nonatomic) IBOutlet UIButton *closeBtn;
  24. /// 第三方登录相关控件
  25. @property (weak, nonatomic) IBOutlet UILabel *thirdLoginLabel;
  26. @property (weak, nonatomic) IBOutlet UIView *thirdLoginLineLeft;
  27. @property (weak, nonatomic) IBOutlet UIView *thirdLoginLineRight;
  28. @property (weak, nonatomic) IBOutlet UIButton *weChatLoginBtn;
  29. @end
  30. @implementation RQLoginViewController
  31. @dynamic viewModel;
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. /// 设置
  35. [self rq_setup];
  36. /// 设置子控件
  37. [self rq_setupSubViews];
  38. /// 初始化部分数据
  39. [self rq_configureData];
  40. }
  41. - (void)dealloc {
  42. [RQNotificationCenter removeObserver:self];
  43. self.viewModel.isPush = NO;
  44. }
  45. #pragma mark - 初始化
  46. - (void)rq_setup {
  47. /// 设置contentSize
  48. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.width.mas_equalTo(RQ_SCREEN_WIDTH);
  50. make.height.mas_equalTo(RQ_SCREEN_HEIGHT);
  51. }];
  52. UIScrollView *scrollView = (UIScrollView *)self.view;
  53. scrollView.alwaysBounceVertical = YES;
  54. scrollView.showsHorizontalScrollIndicator = NO;
  55. scrollView.showsVerticalScrollIndicator = NO;
  56. /// 适配 iOS 11
  57. RQAdjustsScrollViewInsets_Never(scrollView);
  58. [RQNotificationCenter addObserver:self selector:@selector(wxLoginNotif:) name:RQWeChatOnRespNotification object:nil]; //注册微信登录
  59. [RQNotificationCenter addObserver:self selector:@selector(loginUserNotif:) name:RQWeBanindOnRespNotification object:nil];
  60. }
  61. - (void)wxLoginNotif:(NSNotification *)notif{
  62. if (myDelegate.isLogin) {
  63. return;
  64. }
  65. NSString *code = [notif.object stringValue];
  66. //提交-web
  67. [MBProgressHUD rq_showProgressHUD:@""];
  68. [jiaPeiManager NYRequestAnythingDataWithURL:RQ_POST_Jsjpcode contentType:nil parameters:@{@"authorizationCode":code} completion:^(NSDictionary *dict) {
  69. if([dict[@"code"] intValue]==200){
  70. NYJsCodeModel *obj = [NYJsCodeModel yy_modelWithDictionary:dict[@"data"]];
  71. if(obj.gzptUserInfo==NULL){//如果-没有绑定-跳绑
  72. NYLoginBindViewModel *viewModel = [[NYLoginBindViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{}];
  73. viewModel.openid = obj.openid;
  74. [RQ_APPDELEGATE.services pushViewModel:viewModel animated:YES];
  75. }else{//绑定过-登录成功
  76. RQUserModel * user = obj.gzptUserInfo;
  77. myDelegate.isLogin = YES;
  78. /// 存储用户数据
  79. [RQ_USER_MANAGER loginUser:user];
  80. [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) {}];
  81. [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {}];
  82. [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectOne] subscribeNext:^(NSArray *examResultModelArr) {
  83. RQ_COMMON_MANAGER.myExamOneCount = examResultModelArr.count;
  84. RQ_COMMON_MANAGER.examResultOneListArr = examResultModelArr;
  85. }];
  86. [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectFour] subscribeNext:^(NSArray *examResultModelArr) {
  87. RQ_COMMON_MANAGER.myExamFourCount = examResultModelArr.count;
  88. RQ_COMMON_MANAGER.examResultFourListArr = examResultModelArr;
  89. }];
  90. if (user.channel == RQUserLoginChannelTypePhone) {
  91. /// 切换更控制器
  92. dispatch_async(dispatch_get_main_queue(), ^{
  93. /// 发通知
  94. [[NSNotificationCenter defaultCenter] postNotificationName:RQSwitchRootViewControllerNotification object:nil userInfo:@{RQSwitchRootViewControllerUserInfoKey:@(RQSwitchRootViewControllerFromTypeLogin)}];
  95. });
  96. }
  97. }
  98. }else{
  99. }
  100. [MBProgressHUD rq_hideHUD];
  101. }];
  102. }
  103. - (void)loginUserNotif:(NSNotification *)notif{
  104. if (!myDelegate.isLogin) {
  105. RQUserModel *user = notif.object;
  106. [self loginUserdo:user];
  107. }
  108. }
  109. - (void)loginUserdo:(RQUserModel *)user{
  110. if(user==NULL){
  111. return;
  112. }
  113. }
  114. #pragma mark - 设置子控件
  115. - (void)rq_setupSubViews {
  116. @weakify(self)
  117. _closeBtnTopToTop.constant = RQ_APPLICATION_STATUS_BAR_HEIGHT;
  118. /// 手机号码
  119. RQPhoneView *phoneView = [RQPhoneView loginCodeView];
  120. self.phoneView = phoneView;
  121. [self.containerView addSubview:phoneView];
  122. /// 密码
  123. RQPasswordView *passwordView = [RQPasswordView passwordView];
  124. self.passwordView = passwordView;
  125. [self.containerView addSubview:passwordView];
  126. /// 隐私协议
  127. RQAgreementView *agreementView = [RQAgreementView agreementView];
  128. self.agreementView = agreementView;
  129. [self.containerView addSubview:agreementView];
  130. /// 布局
  131. [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
  132. @strongify(self)
  133. make.top.mas_equalTo(self.welcomeLabel.mas_bottom).mas_offset(40.f);
  134. make.left.mas_equalTo(self.containerView).mas_offset(16.f);
  135. make.right.mas_equalTo(self.containerView).mas_offset(-16.f);
  136. make.height.mas_offset(RQ_FIT_HORIZONTAL(44.f));
  137. phoneView.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
  138. }];
  139. [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
  140. @strongify(self)
  141. make.top.mas_equalTo(self.phoneView.mas_bottom).mas_offset(16.f);
  142. make.left.mas_equalTo(self.containerView).mas_offset(16.f);
  143. make.right.mas_equalTo(self.containerView).mas_offset(-16.f);
  144. make.height.mas_offset(RQ_FIT_HORIZONTAL(44.f));
  145. passwordView.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
  146. }];
  147. [agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
  148. @strongify(self)
  149. make.top.mas_equalTo(self.passwordView.mas_bottom).mas_offset(16.f);
  150. make.left.mas_equalTo(self.containerView).mas_offset(16.f);
  151. make.right.mas_equalTo(self.containerView).mas_offset(-16.f);
  152. make.height.mas_offset(RQ_FIT_HORIZONTAL(25.f));
  153. }];
  154. [self.loginBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
  155. @strongify(self)
  156. make.top.mas_equalTo(self.agreementView.mas_bottom).mas_offset(41.5f);
  157. _loginBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
  158. }];
  159. // self.weChatLoginBtn.hidden = ![WXApi isWXAppInstalled];
  160. self.weChatLoginBtn.hidden = NO;
  161. self.thirdLoginLineLeft.hidden = self.weChatLoginBtn.hidden;
  162. self.thirdLoginLabel.hidden = self.weChatLoginBtn.hidden;
  163. self.thirdLoginLineRight.hidden = self.weChatLoginBtn.hidden;
  164. [self.loginBtn setBackgroundImage:[UIImage imageWithColor:RQ_MAIN_COLOR] forState:UIControlStateNormal];
  165. }
  166. #pragma mark - Override
  167. - (void)bindViewModel{
  168. [super bindViewModel];
  169. @weakify(self);
  170. /// 手机号
  171. RAC(self.viewModel , phone) = [[RACSignal merge:@[RACObserve(self.phoneView.loginCodeTextField, text),self.phoneView.loginCodeTextField.rac_textSignal]] takeUntil:self.rac_willDeallocSignal];
  172. /// 密码
  173. RAC(self.viewModel , password) = [[RACSignal merge:@[RACObserve(self.passwordView.passwordTextField, text),self.passwordView.passwordTextField.rac_textSignal]] takeUntil:self.rac_willDeallocSignal];
  174. /// 登录按钮有效性
  175. RAC(self.loginBtn , enabled) = [[[RACSignal
  176. combineLatest:@[self.viewModel.validLoginSignal,RACObserve(self.agreementView.agreeBtn, selected)]
  177. reduce:^(NSNumber *isValid , NSNumber *selected) {
  178. return @([isValid boolValue] && [selected boolValue]);
  179. }]
  180. distinctUntilChanged] takeUntil:self.rac_willDeallocSignal];
  181. /// show HUD
  182. [[[self.viewModel.loginCommand.executing
  183. skip:1]
  184. doNext:^(id x) {
  185. @strongify(self)
  186. [self.view endEditing:YES];
  187. }]
  188. subscribeNext:^(NSNumber * showHud) {
  189. if (showHud.boolValue) {
  190. [MBProgressHUD rq_showProgressHUD:@"请稍后..."];
  191. }
  192. // else if(!self.viewModel.error){
  193. // [MBProgressHUD rq_hideHUD];
  194. // }
  195. }];
  196. /// show errors
  197. [self.viewModel.loginCommand.errors subscribeNext:^(NSError * error) {
  198. [MBProgressHUD rq_hideHUD];
  199. [MBProgressHUD rq_showErrorTips:error];
  200. }];
  201. }
  202. #pragma mark - 初始化部分数据
  203. - (void)rq_configureData {
  204. /// 获取沙盒里面的用户数据
  205. // RQUserModel *user = RQ_USER_MANAGER.currentUser;
  206. if (RQStringIsNotEmpty([SAMKeychain rawLogin])) {
  207. self.phoneView.loginCodeTextField.text = [SAMKeychain rawLogin];
  208. }
  209. if (RQStringIsNotEmpty([SAMKeychain rawLoginPassword])) {
  210. self.passwordView.passwordTextField.text = [SAMKeychain rawLoginPassword];
  211. }
  212. };
  213. - (IBAction)registerAction:(id)sender {
  214. [self.viewModel.registerCommand execute:nil];
  215. }
  216. - (IBAction)changePasswordAction:(id)sender {
  217. [self.viewModel.editPasswordCommand execute:nil];
  218. }
  219. - (IBAction)loginBtnDidClicked:(id)sender {
  220. /// 密码 6~20位 且 不能含有中文
  221. if(self.passwordView.passwordTextField.text.length < 5
  222. || self.passwordView.passwordTextField.text.length > 20
  223. || [NSString rq_isContainChinese:self.passwordView.passwordTextField.text] ){
  224. [NSObject rq_showAlertViewWithTitle:@"账号或密码错误,请重新填写。" message:nil confirmTitle:@"确定"];
  225. return ;
  226. }
  227. ///登录
  228. [self.viewModel.loginCommand execute:nil];
  229. }
  230. - (IBAction)backBtnAction:(id)sender {
  231. [self.viewModel.closeCommand execute:nil];
  232. }
  233. - (IBAction)wechatLoginBtnAction:(id)sender {
  234. [RQ_WECHAT_MANAGER wechatLogin];
  235. }
  236. @end