// // XPLoginVerifBindPhoneViewController.m // xplan-ios // // Created by 冯硕 on 2021/9/18. // #import "XPLoginVerifBindPhoneViewController.h" ///Third #import #import ///Tool #import "UIImage+Utils.h" #import "CountDownHelper.h" ///Model #import "UserInfoModel.h" ///P #import "XPLoginVerifBindPhoneProtocol.h" #import "XPLoginVerifBindPhonePresenter.h" ///VC #import "LoginBindPhoneViewController.h" #import "XPMineSettingViewController.h" ///View #import "XPLoginBindSuccessView.h" #import "LoginForgetEditView.h" #import "XPLoginAraeViewController.h" #import "XPMineLoginPasswordViewController.h" @interface XPLoginVerifBindPhoneViewController () ///手机号 @property (nonatomic,strong) LoginForgetEditView *phoneView; ///验证码 @property (nonatomic,strong) LoginForgetEditView *codeView; ///手机区号 @property (nonatomic,copy)NSString *pi_phoneAreaCode; // 确认按钮 @property (nonatomic, strong) UIButton *confirmBtn; // 重新绑定手机 @property (nonatomic, assign) BOOL isResetPhone; @end @implementation XPLoginVerifBindPhoneViewController - (void)dealloc { [[CountDownHelper shareHelper] stopCountDown]; } - (XPLoginVerifBindPhonePresenter *)createPresenter { return [[XPLoginVerifBindPhonePresenter alloc] init]; } - (void)viewDidLoad { [super viewDidLoad]; NSString *code = [NSString getCountryCode]; self.pi_phoneAreaCode = [code stringByReplacingOccurrencesOfString:@"+" withString:@""]; [self initSubViews]; [self initSubViewConstraints]; [self setConfigs]; [self setEvents]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [[CountDownHelper shareHelper] stopCountDown]; self.codeView.authCodeButton.enabled= YES; [self.codeView.authCodeButton setTitle:YMLocalizedString(@"XPLoginVerifBindPhoneViewController11") forState:UIControlStateNormal]; } #pragma mark - Private Method - (void)initSubViews { [CountDownHelper shareHelper].delegate = self; self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.codeView]; [self.view addSubview:self.phoneView]; [self.view addSubview:self.confirmBtn]; } - (void)initSubViewConstraints { [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(kGetScaleWidth(30)); make.leading.trailing.equalTo(self.view).inset(kGetScaleWidth(15)); make.height.mas_equalTo(kGetScaleWidth(48)); }]; [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.height.equalTo(self.phoneView); make.top.equalTo(self.phoneView.mas_bottom).mas_offset(kGetScaleWidth(25)); }]; [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.equalTo(self.view).inset(kGetScaleWidth(15)); make.height.mas_equalTo(kGetScaleWidth(46)); make.top.mas_equalTo(kGetScaleWidth(275)); }]; } #pragma mark -配置 - (void)setConfigs { switch (self.bindingPhoneNumType) { case XPBindingPhoneNumTypeEdit:{ self.navigationItem.title = YMLocalizedString(@"XPLoginVerifBindPhoneViewController0"); } break; case XPBindingPhoneNumTypeNormal:{ self.navigationItem.title = YMLocalizedString(@"XPLoginVerifBindPhoneViewController1"); } break; case XPBindingPhoneNumTypeConfirm:{ self.navigationItem.title = YMLocalizedString(@"XPLoginVerifBindPhoneViewController2"); [self.confirmBtn setTitle:YMLocalizedString(@"XPLoginVerifBindPhoneViewController3") forState:UIControlStateNormal]; if (self.userInfo.isBindPhone) { // self.phoneNumTextField.enabled = NO; // self.phoneNumTextField.text = self.userInfo.phone; } } break; default: break; } RAC(self.confirmBtn,enabled) = [RACSignal combineLatest:@[self.phoneView.textField.rac_textSignal,self.codeView.textField.rac_textSignal] reduce:^(NSString *phone,NSString *code){ return @(phone.length > 0 && code.length == 5); }]; } #pragma mark - XPChooseAreaCodeVCDelegate -(void)chooseAreaCodeSuccess:(NSString *)code{ self.pi_phoneAreaCode = code; self.phoneView.areaCode = [NSString stringWithFormat:@"+%@",code]; } -(void)chooseAreaCodeAction{ XPLoginAraeViewController *codeVC = [XPLoginAraeViewController new]; codeVC.delegate = self; [self.navigationController pushViewController:codeVC animated:YES]; } #pragma mark 交互事件 - (void)setEvents { @weakify(self) // 获取验证码点击事件 [[[self.codeView.authCodeButton rac_signalForControlEvents:UIControlEventTouchUpInside] filter:^BOOL(__kindof UIControl * _Nullable value) { // 获取验证码 if (self.phoneView.textField.text.length == 0) { [self showErrorToast:YMLocalizedString(@"XPLoginVerifBindPhoneViewController4")]; return NO; } return YES; }] subscribeNext:^(id _Nullable x) { @strongify(self); [self.presenter phoneSmsCode:[NSString stringWithFormat:@"%@%@",self.pi_phoneAreaCode,self.phoneView.textField.text] type:self.bindingPhoneNumType == XPBindingPhoneNumTypeConfirm ? GetSmsType_Unbind_Phone : GetSmsType_Bind_Phone phoneAreaCode:self.pi_phoneAreaCode]; }]; // // 绑定和验证 [[[self.confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] filter:^BOOL(__kindof UIControl * _Nullable value) { @strongify(self) // 确认绑定手机号 if (self.phoneView.textField.text.length == 0) { [self showErrorToast:YMLocalizedString(@"XPLoginVerifBindPhoneViewController6")]; return NO; }else if (self.codeView.textField.text.length == 0) { [self showErrorToast:YMLocalizedString(@"XPLoginVerifBindPhoneViewController7")]; return NO; }else { return YES; } }] subscribeNext:^(id _Nullable x) { @strongify(self) [self.view endEditing:YES]; if (self.bindingPhoneNumType == XPBindingPhoneNumTypeConfirm) { [self.presenter checkMoblieCodeWithMoblie:[NSString stringWithFormat:@"%@%@",self.pi_phoneAreaCode,self.phoneView.textField.text] code:self.codeView.textField.text phoneAreaCode:self.pi_phoneAreaCode]; } else { [self.presenter bindkMoblieCodeWithMoblie:[NSString stringWithFormat:@"%@%@",self.pi_phoneAreaCode,self.phoneView.textField.text] code:self.codeView.textField.text phoneAreaCode:self.pi_phoneAreaCode]; } } error:^(NSError * _Nullable error) { }]; } #pragma mark - XPLoginVerifBindPhoneProtocol - (void)phoneSmsCodeSuccess { self.codeView.authCodeButton.enabled = NO; [self showSuccessToast:YMLocalizedString(@"XPLoginVerifBindPhoneViewController8")]; [[CountDownHelper shareHelper] openCountdownWithTime:60]; } - (void)checkMoblieCodeWithMoblieSuccess { [self showSuccessToast:YMLocalizedString(@"XPLoginVerifBindPhoneViewController9")]; [[CountDownHelper shareHelper] stopCountDown]; XPLoginVerifBindPhoneViewController *vc = [[XPLoginVerifBindPhoneViewController alloc] init]; vc.bindingPhoneNumType = XPBindingPhoneNumTypeEdit; vc.userInfo = self.userInfo; [self.navigationController pushViewController:vc animated:YES]; } - (void)bindMoblieCodeWithMoblieSuccess { [[CountDownHelper shareHelper] stopCountDown]; // 修改绑定信息 if (self.bindingPhoneNumType == XPBindingPhoneNumTypeEdit) { @weakify(self); [XPLoginBindSuccessView showBindSuccessViewWithHandler:^{ @strongify(self); // 返回设置页面 __block __kindof UIViewController *vc; [self.navigationController.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[XPMineSettingViewController class]]) { vc = obj; // 导航控制器中有设置VC } }]; if ([self.navigationController.childViewControllers containsObject:vc]) { [self.navigationController popToViewController:vc animated:YES]; } else { [self.navigationController popViewControllerAnimated:YES]; } }]; } else if (self.bindingPhoneNumType == XPBindingPhoneNumTypeNormal) { @weakify(self); if(self.isLogout == YES){ XPMineLoginPasswordViewController *vc = [[XPMineLoginPasswordViewController alloc] init]; vc.userInfo = self.userInfo; vc.isModifiPwd = NO; vc.isLogout = YES; [self.navigationController pushViewController:vc animated:YES]; return; } [XPLoginBindSuccessView showBindSuccessViewWithHandler:^{ @strongify(self); [self.navigationController popViewControllerAnimated:YES]; }]; } self.confirmBtn.enabled = YES; // 打开交互 } #pragma mark - CountDownHelperDelegate ///倒计时进行中 - (void)onCountdownOpen:(int)time { [self.codeView.authCodeButton setTitle:[NSString stringWithFormat:YMLocalizedString(@"XPLoginVerifBindPhoneViewController10"), time] forState:UIControlStateDisabled]; } ///倒计时结束 - (void)onCountdownFinish { self.codeView.authCodeButton.enabled= YES; [self.codeView.authCodeButton setTitle:YMLocalizedString(@"XPLoginVerifBindPhoneViewController11") forState:UIControlStateNormal]; } #pragma mark - Getters And Setters - (UIButton *)confirmBtn { if (!_confirmBtn) { _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_confirmBtn setTitle:YMLocalizedString(@"XPLoginVerifBindPhoneViewController14") forState:UIControlStateNormal]; [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled]; [_confirmBtn.titleLabel setFont:kFontRegular(16)]; _confirmBtn.layer.cornerRadius = kGetScaleWidth(23); _confirmBtn.layer.masksToBounds = YES; _confirmBtn.enabled = NO; [_confirmBtn setBackgroundImage:[UIImage gradientColorImageFromColors:@[[DJDKMIMOMColor confirmButtonGradientStartColor], [DJDKMIMOMColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal]; } return _confirmBtn; } - (LoginForgetEditView *)phoneView { if (!_phoneView) { _phoneView = [[LoginForgetEditView alloc] init]; // _phoneView.placeholder = YMLocalizedString(@"LoginForgetPasswordViewController7"); _phoneView.type = LoginForgetEditViewTypeNormal; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseAreaCodeAction)]; [_phoneView.codeView addGestureRecognizer:tap]; } return _phoneView; } - (LoginForgetEditView *)codeView { if (!_codeView) { _codeView = [[LoginForgetEditView alloc] init]; _codeView.placeholder = YMLocalizedString(@"XPLoginVerifBindPhoneViewController17"); _codeView.type = LoginForgetEditViewTypeSms; } return _codeView; } @end