// // EditPhoneOrEmailVC.m // smartRhino // // Created by armin on 2019/11/1. // Copyright © 2019 tederen. All rights reserved. // #import "EditPhoneOrEmailVC.h" #import "TabBarController.h" @interface EditPhoneOrEmailVC () @property (strong,nonatomic) IBOutlet UILabel *pageTitleLabel; //绑定手机号/邮箱背景View @property (strong,nonatomic) IBOutlet UIView *bindingBgView; //更换手机号/邮箱背景View @property (strong,nonatomic) IBOutlet UIView *changeBgView; @property (strong,nonatomic) IBOutlet UIImageView *bindingShowImgView; @property (strong,nonatomic) IBOutlet UILabel *bindingShowLabel; @property (strong,nonatomic) IBOutlet UIButton *bindingChangeBtn; @property (strong,nonatomic) IBOutlet UILabel *changeShowLabel; @property (strong,nonatomic) IBOutlet UITextField *changeOneTextField; @property (strong,nonatomic) IBOutlet UITextField *changeTwoTextField; @property (strong,nonatomic) IBOutlet UIButton *changeSendYZMBtn; @property (strong,nonatomic) IBOutlet UIButton *sureBtn; @end @implementation EditPhoneOrEmailVC +(EditPhoneOrEmailVC *)initEditPhoneOrEmailVC{ EditPhoneOrEmailVC *controller = [StoryboardManager.shared.myCenter instantiateViewControllerWithIdentifier:@"EditPhoneOrEmailVC"]; return controller; } - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.bindingChangeBtn.layer.masksToBounds = YES; self.bindingChangeBtn.layer.cornerRadius = 8.0; self.sureBtn.layer.masksToBounds = YES; self.sureBtn.layer.cornerRadius = 8.0; WS(weakSelf); if(self.pageType == BindingOrChangePhoneOrEmailPageType1){ self.pageTitleLabel.text = @"绑定手机号"; self.bindingBgView.hidden = NO; self.changeBgView.hidden = YES; self.bindingShowImgView.image = [UIImage imageNamed:@"my_edit_phoneOrEmail_icon"]; self.bindingShowLabel.text = [NSString stringWithFormat:@"绑定的手机号:%@",[AppUserModel sharedAppUserModel].Phone]; [self.bindingChangeBtn setTitle:@"更换手机号" forState:UIControlStateNormal]; [self.bindingChangeBtn setAction:^{ EditPhoneOrEmailVC *vc = [EditPhoneOrEmailVC initEditPhoneOrEmailVC]; vc.pageType = BindingOrChangePhoneOrEmailPageType3; [weakSelf.navigationController pushViewController:vc animated:YES]; }]; }else if (self.pageType == BindingOrChangePhoneOrEmailPageType2){ self.pageTitleLabel.text = @"绑定邮箱"; self.bindingBgView.hidden = NO; self.changeBgView.hidden = YES; self.bindingShowImgView.image = [UIImage imageNamed:@"my_edit_phoneOrEmail_icon"]; [self.bindingChangeBtn setTitle:@"更换邮箱" forState:UIControlStateNormal]; self.bindingShowLabel.text = [NSString stringWithFormat:@"绑定的邮箱:%@",[AppUserModel sharedAppUserModel].Email]; [self.bindingChangeBtn setAction:^{ EditPhoneOrEmailVC *vc = [EditPhoneOrEmailVC initEditPhoneOrEmailVC]; vc.pageType = BindingOrChangePhoneOrEmailPageType4; [weakSelf.navigationController pushViewController:vc animated:YES]; }]; }else if (self.pageType == BindingOrChangePhoneOrEmailPageType3){ self.pageTitleLabel.text = @"更换手机号"; self.bindingBgView.hidden = YES; self.changeBgView.hidden = NO; self.changeShowLabel.text = @"输入新手机号并验证后即可更换手机号"; self.changeOneTextField.text = @""; self.changeOneTextField.delegate = self; self.changeOneTextField.placeholder = @"请输入手机号"; self.changeOneTextField.textColor = RGB(51, 51, 51); [self.changeOneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:self.changeOneTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeOneTextField.font}]; self.changeOneTextField.attributedPlaceholder = attrStringONE; [self.changeSendYZMBtn setTitle:@"获取验证码" forState:UIControlStateNormal]; self.changeTwoTextField.text = @""; self.changeTwoTextField.delegate = self; self.changeTwoTextField.placeholder = @"请输入验证码"; self.changeTwoTextField.textColor = RGB(51, 51, 51); [self.changeTwoTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringTWO = [[NSAttributedString alloc] initWithString:self.changeTwoTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeTwoTextField.font}]; self.changeTwoTextField.attributedPlaceholder = attrStringTWO; [self.sureBtn setAction:^{ [weakSelf modiPhoneCommitData]; }]; }else if (self.pageType == BindingOrChangePhoneOrEmailPageType4){ self.pageTitleLabel.text = @"更换邮箱"; self.bindingBgView.hidden = YES; self.changeBgView.hidden = NO; self.changeShowLabel.text = @"输入新邮箱并验证后即可更换绑定邮箱"; self.changeOneTextField.text = @""; self.changeOneTextField.delegate = self; self.changeOneTextField.placeholder = @"请输入新邮箱"; self.changeOneTextField.textColor = RGB(51, 51, 51); [self.changeOneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:self.changeOneTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeOneTextField.font}]; self.changeOneTextField.attributedPlaceholder = attrStringONE; [self.changeSendYZMBtn setTitle:@"获取验证码" forState:UIControlStateNormal]; self.changeTwoTextField.text = @""; self.changeTwoTextField.delegate = self; self.changeTwoTextField.placeholder = @"请输入验证码"; self.changeTwoTextField.textColor = RGB(51, 51, 51); [self.changeTwoTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringTWO = [[NSAttributedString alloc] initWithString:self.changeTwoTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeTwoTextField.font}]; self.changeTwoTextField.attributedPlaceholder = attrStringTWO; [self.sureBtn setAction:^{ [weakSelf modiEMailCommitData]; }]; }else if (self.pageType == BindingOrChangePhoneOrEmailPageType5){ self.pageTitleLabel.text = @"绑定手机号"; self.bindingBgView.hidden = YES; self.changeBgView.hidden = NO; self.changeShowLabel.text = @"输入手机号并验证后即可绑定手机号"; self.changeOneTextField.text = @""; self.changeOneTextField.delegate = self; self.changeOneTextField.placeholder = @"请输入手机号"; self.changeOneTextField.textColor = RGB(51, 51, 51); [self.changeOneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:self.changeOneTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeOneTextField.font}]; self.changeOneTextField.attributedPlaceholder = attrStringONE; [self.changeSendYZMBtn setTitle:@"获取验证码" forState:UIControlStateNormal]; self.changeTwoTextField.text = @""; self.changeTwoTextField.delegate = self; self.changeTwoTextField.placeholder = @"请输入验证码"; self.changeTwoTextField.textColor = RGB(51, 51, 51); [self.changeTwoTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; NSAttributedString *attrStringTWO = [[NSAttributedString alloc] initWithString:self.changeTwoTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeTwoTextField.font}]; self.changeTwoTextField.attributedPlaceholder = attrStringTWO; [self.sureBtn setAction:^{ if (weakSelf.openId.length > 0) { [weakSelf modiWinXin:weakSelf.openId]; } }]; } } -(void)startTime{ __block int timeout = 60; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_timer, ^{ if(timeout <= 0){ //倒计时结束,关闭 dispatch_source_cancel(_timer); WS(weakSelf); dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示 根据自己需求设置 特别注明:UI的改变一定要在主线程中进行 //倒计时完成,恢复到原来的效果 NSString *text = @"获取验证码"; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateNormal]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateHighlighted]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateSelected]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateDisabled]; [weakSelf.changeSendYZMBtn setTitleColor:RGB(37, 138, 220) forState:UIControlStateNormal]; [weakSelf.changeSendYZMBtn.titleLabel setFont:[UIFont systemFontOfSize:13]]; [weakSelf.changeSendYZMBtn setEnabled:YES]; }); }else{ int seconds = timeout % 60; if(seconds == 0&&timeout==60){ seconds = 60; } // NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds]; WS(weakSelf); dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示 根据自己需求设置 // NSLog(@"____%@",strTime); NSString *text = [NSString stringWithFormat:@"重新获取(%lus)",(unsigned long)seconds]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateNormal]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateHighlighted]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateSelected]; [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateDisabled]; [weakSelf.changeSendYZMBtn.titleLabel setFont:[UIFont systemFontOfSize:13]]; [weakSelf.changeSendYZMBtn setTitleColor:RGB(37, 138, 220) forState:UIControlStateNormal]; }); timeout--; } }); dispatch_resume(_timer); } - (IBAction)verifyCodeAction:(UIButton *)sender { [self.view endEditing:YES]; if(self.pageType == BindingOrChangePhoneOrEmailPageType1){ }else if (self.pageType == BindingOrChangePhoneOrEmailPageType2){ }else if (self.pageType == BindingOrChangePhoneOrEmailPageType3){ [self modiPhoneNumberSendCode]; }else if (self.pageType == BindingOrChangePhoneOrEmailPageType4){ [self modiEMailberSendCode]; }else if (self.pageType == BindingOrChangePhoneOrEmailPageType5){ [self modiPhoneNumberSendCode]; } } #pragma mark - 修改手机号 /// 修改手机号发送验证码 - (void)modiPhoneNumberSendCode{ if (self.changeOneTextField.text.length == 0) { SHOWERROR(@"请输入正确手机号码"); return; } NSLog(@"⚠️ 更换手机获取验证码 %@",self.changeOneTextField.text); SHOWLOADING WS(weakSelf); [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,LoginSendCodeGet] parameters:@{@"phone":self.changeOneTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) { REMOVESHOW [weakSelf startTime]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } /// 修改手机号 提交数据 - (void)modiPhoneCommitData{ if (self.changeOneTextField.text.length != 11) { SHOWERROR(@"请输入正确手机号码"); return; } if (self.changeTwoTextField.text.length != 6) { SHOWERROR(@"请输入正确验证码"); return; } SHOWLOADING WEAKSELF [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserPhone_Post] parameters:@{@"phone":self.changeOneTextField.text,@"code":self.changeTwoTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) { [UserManager getUserInfoDetailSuccess:^(id _Nonnull responseObject) { REMOVESHOW STRONGSELF // [strongSelf.navigationController popToRootViewControllerAnimated:YES]; NSLog(@"-=-=-=-=-=-=-%@",strongSelf.navigationController.viewControllers); [strongSelf.navigationController popToViewController:strongSelf.navigationController.viewControllers[1] animated:YES]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } #pragma mark - 修改邮箱 /// 修改邮箱 发送验证码 - (void)modiEMailberSendCode{ if (![self.changeOneTextField.text isEmailAddress]) { SHOWERROR(@"请输入正确邮箱地址"); return; } NSLog(@"⚠️ 更换邮箱获取验证码 %@",self.changeOneTextField.text); SHOWLOADING [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserEMail_SendCode_Post] parameters:@{@"email":self.changeOneTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) { REMOVESHOW [self startTime]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } /// 修改邮箱 提交数据 - (void)modiEMailCommitData{ if (![self.changeOneTextField.text isEmailAddress]) { SHOWERROR(@"请输入正确邮箱地址"); return; } if (self.changeTwoTextField.text.length != 6) { SHOWERROR(@"请输入正确验证码"); return; } SHOWLOADING WEAKSELF [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserEMail_Post] parameters:@{@"email":self.changeOneTextField.text,@"code":self.changeTwoTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) { [UserManager getUserInfoDetailSuccess:^(id _Nonnull responseObject) { REMOVESHOW STRONGSELF NSLog(@"%@",strongSelf.navigationController.viewControllers); for ( int i = 0 ;i< strongSelf.navigationController.viewControllers.count;i++) { if ([strongSelf.navigationController.viewControllers[i] isKindOfClass:NSClassFromString(@"MyInfoVC")]) { [strongSelf.navigationController popToViewController:strongSelf.navigationController.viewControllers[i] animated:YES]; return; } } } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } #pragma mark - 绑定微信 - (void)modiWinXin:(NSString *)openId{ if (self.changeOneTextField.text.length != 11) { SHOWERROR(@"请输入正确手机号码"); return; } if (self.changeTwoTextField.text.length != 6) { SHOWERROR(@"请输入正确验证码"); return; } SHOWLOADING WEAKSELF [[HttpManager sharedHttpManager] POSTUrl:Host(API_App_Set_Opend_Id) parameters:@{@"Phone":self.changeOneTextField.text,@"Code":self.changeTwoTextField.text,@"OpenId":openId} responseStyle:JOSN success:^(id _Nonnull responseObject) { REMOVESHOW NSDictionary *dic = responseObject; USERDEFAULTSSET(dic[@"Token"][@"Expires"],@"tokenExpires"); NSString *token = [NSString stringWithFormat:@"%@ %@",dic[@"Token"][@"Type"],dic[@"Token"][@"Token"]]; USERDEFAULTSSET(token,@"LOGINTOKEN"); NSLog(@"登录成功之后的token%@\n%@",kToken,token); USERDEFAULTSSET(dic[@"UserId"],@"userId"); [weakSelf getUserInfoDetail]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } - (void)getUserInfoDetail{ SHOWLOADING WEAKSELF NSLog(@"%@",kUserId); [[HttpManager sharedHttpManager].manager.requestSerializer setValue:[UserManager token] forHTTPHeaderField:@"Authorization"]; [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,UserDetailGet] parameters:@{} success:^(id _Nonnull responseObject) { NSLog(@"%@",responseObject); AppUserModel * model = [[AppUserModel alloc] initWithDictionary:responseObject error:nil]; [model saveUserInfor]; REMOVESHOW [weakSelf changeToHome]; NSData * data = [NSKeyedArchiver archivedDataWithRootObject:model]; USERDEFAULTSSET(data,@"USERINFO"); //在这里登陆环信 [[EaseSDKHelper shareHelper] loginEaseWithUsername:model.ImId password:@"123456"]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } -(void)changeToHome{ UIWindow *window = [UtilsTools getWindow]; window.rootViewController = [TabBarController sharedTabBarController]; [[TabBarController sharedTabBarController] setSelectedIndex:0]; [window makeKeyAndVisible]; } /****************************************************/ #pragma mark - UITextFieldDelegate /****************************************************/ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // NSString *tempString = [textField.text stringByReplacingCharactersInRange:range withString:string]; if ([string isEqualToString:@""] || [string isEqualToString:@"\n"]) { return YES; } NSString *regex = @"[\u4e00-\u9fa5]{0,}$"; // 中文 NSPredicate *predicateRe1 = [NSPredicate predicateWithFormat:@"self matches %@", regex]; // 3、匹配字符串 BOOL resualt = [predicateRe1 evaluateWithObject:string]; if (resualt) { return NO; } return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField{ if (self.pageType == BindingOrChangePhoneOrEmailPageType3){ }else if (self.pageType == BindingOrChangePhoneOrEmailPageType4){ } } -(void)textFieldDidChange:(UITextField *)textField{ if (textField.text.length > 20) { textField.text = [textField.text substringToIndex:20]; } } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } @end