123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- //
- // RQLoginViewController.m
- // YueXueChe
- //
- // Created by 张嵘 on 2018/12/13.
- // Copyright © 2018 lee. All rights reserved.
- //
- #import "RQLoginViewController.h"
- #import "RQRegisterViewController.h"
- #import "RQVerificationSmsCodeViewController.h"
- #import "RQPhoneView.h"
- #import "RQPasswordView.h"
- @interface RQLoginViewController ()
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *closeBtnTopToTop;
- @property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
- @property (weak, nonatomic) IBOutlet UIView *containerView;
- @property (nonatomic, readonly, strong) RQLoginViewModel *viewModel;
- @property (weak, nonatomic) IBOutlet UILabel *welcomeLabel;
- @property (strong, readwrite, nonatomic) RQPhoneView *phoneView;
- @property (strong, readwrite, nonatomic) RQPasswordView *passwordView;
- @property (strong, readwrite, nonatomic) RQAgreementView *agreementView;
- @property (weak, nonatomic) IBOutlet UIButton *loginBtn;
- @property (weak, nonatomic) IBOutlet UIButton *closeBtn;
- /// 第三方登录相关控件
- @property (weak, nonatomic) IBOutlet UILabel *thirdLoginLabel;
- @property (weak, nonatomic) IBOutlet UIView *thirdLoginLineLeft;
- @property (weak, nonatomic) IBOutlet UIView *thirdLoginLineRight;
- @property (weak, nonatomic) IBOutlet UIButton *weChatLoginBtn;
- @end
- @implementation RQLoginViewController
- @dynamic viewModel;
- - (void)viewDidLoad {
- [super viewDidLoad];
- /// 设置
- [self rq_setup];
- /// 设置子控件
- [self rq_setupSubViews];
- /// 初始化部分数据
- [self rq_configureData];
- }
- - (void)dealloc {
-
- [RQNotificationCenter removeObserver:self];
- self.viewModel.isPush = NO;
- }
- #pragma mark - 初始化
- - (void)rq_setup {
- /// 设置contentSize
- [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(RQ_SCREEN_WIDTH);
- make.height.mas_equalTo(RQ_SCREEN_HEIGHT);
- }];
- UIScrollView *scrollView = (UIScrollView *)self.view;
- scrollView.alwaysBounceVertical = YES;
- scrollView.showsHorizontalScrollIndicator = NO;
- scrollView.showsVerticalScrollIndicator = NO;
-
- /// 适配 iOS 11
- RQAdjustsScrollViewInsets_Never(scrollView);
- [RQNotificationCenter addObserver:self selector:@selector(wxLoginNotif:) name:RQWeChatOnRespNotification object:nil]; //注册微信登录
- [RQNotificationCenter addObserver:self selector:@selector(loginUserNotif:) name:RQWeBanindOnRespNotification object:nil];
-
- }
- - (void)wxLoginNotif:(NSNotification *)notif{
- if (myDelegate.isLogin) {
- return;
- }
- NSString *code = [notif.object stringValue];
- //提交-web
- [MBProgressHUD rq_showProgressHUD:@""];
- [jiaPeiManager NYRequestAnythingDataWithURL:RQ_POST_Jsjpcode contentType:nil parameters:@{@"authorizationCode":code} completion:^(NSDictionary *dict) {
- if([dict[@"code"] intValue]==200){
- NYJsCodeModel *obj = [NYJsCodeModel yy_modelWithDictionary:dict[@"data"]];
- if(obj.gzptUserInfo==NULL){//如果-没有绑定-跳绑
- NYLoginBindViewModel *viewModel = [[NYLoginBindViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{}];
- viewModel.openid = obj.openid;
- [RQ_APPDELEGATE.services pushViewModel:viewModel animated:YES];
- }else{//绑定过-登录成功
- RQUserModel * user = obj.gzptUserInfo;
- myDelegate.isLogin = YES;
- /// 存储用户数据
- [RQ_USER_MANAGER loginUser:user];
- [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) {}];
- [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {}];
- [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectOne] subscribeNext:^(NSArray *examResultModelArr) {
- RQ_COMMON_MANAGER.myExamOneCount = examResultModelArr.count;
- RQ_COMMON_MANAGER.examResultOneListArr = examResultModelArr;
- }];
-
- [[RQ_HTTP_Service getMyScoreWithSubject:RQHomePageSubjectType_SubjectFour] subscribeNext:^(NSArray *examResultModelArr) {
- RQ_COMMON_MANAGER.myExamFourCount = examResultModelArr.count;
- RQ_COMMON_MANAGER.examResultFourListArr = examResultModelArr;
- }];
-
- if (user.channel == RQUserLoginChannelTypePhone) {
- /// 切换更控制器
- dispatch_async(dispatch_get_main_queue(), ^{
- /// 发通知
- [[NSNotificationCenter defaultCenter] postNotificationName:RQSwitchRootViewControllerNotification object:nil userInfo:@{RQSwitchRootViewControllerUserInfoKey:@(RQSwitchRootViewControllerFromTypeLogin)}];
- });
- }
- }
- }else{
-
- }
- [MBProgressHUD rq_hideHUD];
- }];
-
- }
- - (void)loginUserNotif:(NSNotification *)notif{
- if (!myDelegate.isLogin) {
- RQUserModel *user = notif.object;
- [self loginUserdo:user];
- }
- }
- - (void)loginUserdo:(RQUserModel *)user{
- if(user==NULL){
- return;
- }
-
-
- }
- #pragma mark - 设置子控件
- - (void)rq_setupSubViews {
- @weakify(self)
- _closeBtnTopToTop.constant = RQ_APPLICATION_STATUS_BAR_HEIGHT;
-
- /// 手机号码
- RQPhoneView *phoneView = [RQPhoneView loginCodeView];
- self.phoneView = phoneView;
- [self.containerView addSubview:phoneView];
- /// 密码
- RQPasswordView *passwordView = [RQPasswordView passwordView];
- self.passwordView = passwordView;
- [self.containerView addSubview:passwordView];
-
- /// 隐私协议
- RQAgreementView *agreementView = [RQAgreementView agreementView];
- self.agreementView = agreementView;
- [self.containerView addSubview:agreementView];
-
- /// 布局
- [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
- @strongify(self)
- make.top.mas_equalTo(self.welcomeLabel.mas_bottom).mas_offset(40.f);
- make.left.mas_equalTo(self.containerView).mas_offset(16.f);
- make.right.mas_equalTo(self.containerView).mas_offset(-16.f);
- make.height.mas_offset(RQ_FIT_HORIZONTAL(44.f));
- phoneView.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
- }];
- [passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
- @strongify(self)
- make.top.mas_equalTo(self.phoneView.mas_bottom).mas_offset(16.f);
- make.left.mas_equalTo(self.containerView).mas_offset(16.f);
- make.right.mas_equalTo(self.containerView).mas_offset(-16.f);
- make.height.mas_offset(RQ_FIT_HORIZONTAL(44.f));
- passwordView.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
- }];
-
- [agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
- @strongify(self)
- make.top.mas_equalTo(self.passwordView.mas_bottom).mas_offset(16.f);
- make.left.mas_equalTo(self.containerView).mas_offset(16.f);
- make.right.mas_equalTo(self.containerView).mas_offset(-16.f);
- make.height.mas_offset(RQ_FIT_HORIZONTAL(25.f));
- }];
- [self.loginBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
- @strongify(self)
- make.top.mas_equalTo(self.agreementView.mas_bottom).mas_offset(41.5f);
- _loginBtn.layer.cornerRadius = RQ_FIT_HORIZONTAL(44.f) / 2.f;
- }];
-
- // self.weChatLoginBtn.hidden = ![WXApi isWXAppInstalled];
- self.weChatLoginBtn.hidden = NO;
- self.thirdLoginLineLeft.hidden = self.weChatLoginBtn.hidden;
- self.thirdLoginLabel.hidden = self.weChatLoginBtn.hidden;
- self.thirdLoginLineRight.hidden = self.weChatLoginBtn.hidden;
-
- [self.loginBtn setBackgroundImage:[UIImage imageWithColor:RQ_MAIN_COLOR] forState:UIControlStateNormal];
- }
- #pragma mark - Override
- - (void)bindViewModel{
- [super bindViewModel];
-
- @weakify(self);
- /// 手机号
- RAC(self.viewModel , phone) = [[RACSignal merge:@[RACObserve(self.phoneView.loginCodeTextField, text),self.phoneView.loginCodeTextField.rac_textSignal]] takeUntil:self.rac_willDeallocSignal];
- /// 密码
- RAC(self.viewModel , password) = [[RACSignal merge:@[RACObserve(self.passwordView.passwordTextField, text),self.passwordView.passwordTextField.rac_textSignal]] takeUntil:self.rac_willDeallocSignal];
- /// 登录按钮有效性
- RAC(self.loginBtn , enabled) = [[[RACSignal
- combineLatest:@[self.viewModel.validLoginSignal,RACObserve(self.agreementView.agreeBtn, selected)]
- reduce:^(NSNumber *isValid , NSNumber *selected) {
- return @([isValid boolValue] && [selected boolValue]);
- }]
- distinctUntilChanged] takeUntil:self.rac_willDeallocSignal];
-
-
- /// show HUD
- [[[self.viewModel.loginCommand.executing
- skip:1]
- doNext:^(id x) {
- @strongify(self)
- [self.view endEditing:YES];
- }]
- subscribeNext:^(NSNumber * showHud) {
- if (showHud.boolValue) {
- [MBProgressHUD rq_showProgressHUD:@"请稍后..."];
- }
- // else if(!self.viewModel.error){
- // [MBProgressHUD rq_hideHUD];
- // }
- }];
- /// show errors
- [self.viewModel.loginCommand.errors subscribeNext:^(NSError * error) {
- [MBProgressHUD rq_hideHUD];
- [MBProgressHUD rq_showErrorTips:error];
- }];
- }
- #pragma mark - 初始化部分数据
- - (void)rq_configureData {
- /// 获取沙盒里面的用户数据
- // RQUserModel *user = RQ_USER_MANAGER.currentUser;
- if (RQStringIsNotEmpty([SAMKeychain rawLogin])) {
- self.phoneView.loginCodeTextField.text = [SAMKeychain rawLogin];
- }
- if (RQStringIsNotEmpty([SAMKeychain rawLoginPassword])) {
- self.passwordView.passwordTextField.text = [SAMKeychain rawLoginPassword];
- }
- };
- - (IBAction)registerAction:(id)sender {
- [self.viewModel.registerCommand execute:nil];
- }
- - (IBAction)changePasswordAction:(id)sender {
- [self.viewModel.editPasswordCommand execute:nil];
- }
- - (IBAction)loginBtnDidClicked:(id)sender {
- /// 密码 6~20位 且 不能含有中文
- if(self.passwordView.passwordTextField.text.length < 5
- || self.passwordView.passwordTextField.text.length > 20
- || [NSString rq_isContainChinese:self.passwordView.passwordTextField.text] ){
- [NSObject rq_showAlertViewWithTitle:@"账号或密码错误,请重新填写。" message:nil confirmTitle:@"确定"];
- return ;
- }
- ///登录
- [self.viewModel.loginCommand execute:nil];
- }
- - (IBAction)backBtnAction:(id)sender {
- [self.viewModel.closeCommand execute:nil];
- }
- - (IBAction)wechatLoginBtnAction:(id)sender {
- [RQ_WECHAT_MANAGER wechatLogin];
- }
- @end
|