EditPhoneOrEmailVC.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. //
  2. // EditPhoneOrEmailVC.m
  3. // smartRhino
  4. //
  5. // Created by armin on 2019/11/1.
  6. // Copyright © 2019 tederen. All rights reserved.
  7. //
  8. #import "EditPhoneOrEmailVC.h"
  9. #import "TabBarController.h"
  10. @interface EditPhoneOrEmailVC ()<UITextFieldDelegate>
  11. @property (strong,nonatomic) IBOutlet UILabel *pageTitleLabel;
  12. //绑定手机号/邮箱背景View
  13. @property (strong,nonatomic) IBOutlet UIView *bindingBgView;
  14. //更换手机号/邮箱背景View
  15. @property (strong,nonatomic) IBOutlet UIView *changeBgView;
  16. @property (strong,nonatomic) IBOutlet UIImageView *bindingShowImgView;
  17. @property (strong,nonatomic) IBOutlet UILabel *bindingShowLabel;
  18. @property (strong,nonatomic) IBOutlet UIButton *bindingChangeBtn;
  19. @property (strong,nonatomic) IBOutlet UILabel *changeShowLabel;
  20. @property (strong,nonatomic) IBOutlet UITextField *changeOneTextField;
  21. @property (strong,nonatomic) IBOutlet UITextField *changeTwoTextField;
  22. @property (strong,nonatomic) IBOutlet UIButton *changeSendYZMBtn;
  23. @property (strong,nonatomic) IBOutlet UIButton *sureBtn;
  24. @end
  25. @implementation EditPhoneOrEmailVC
  26. +(EditPhoneOrEmailVC *)initEditPhoneOrEmailVC{
  27. EditPhoneOrEmailVC *controller = [StoryboardManager.shared.myCenter instantiateViewControllerWithIdentifier:@"EditPhoneOrEmailVC"];
  28. return controller;
  29. }
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. self.fd_prefersNavigationBarHidden = YES;
  33. self.bindingChangeBtn.layer.masksToBounds = YES;
  34. self.bindingChangeBtn.layer.cornerRadius = 8.0;
  35. self.sureBtn.layer.masksToBounds = YES;
  36. self.sureBtn.layer.cornerRadius = 8.0;
  37. WS(weakSelf);
  38. if(self.pageType == BindingOrChangePhoneOrEmailPageType1){
  39. self.pageTitleLabel.text = @"绑定手机号";
  40. self.bindingBgView.hidden = NO;
  41. self.changeBgView.hidden = YES;
  42. self.bindingShowImgView.image = [UIImage imageNamed:@"my_edit_phoneOrEmail_icon"];
  43. self.bindingShowLabel.text = [NSString stringWithFormat:@"绑定的手机号:%@",[AppUserModel sharedAppUserModel].Phone];
  44. [self.bindingChangeBtn setTitle:@"更换手机号" forState:UIControlStateNormal];
  45. [self.bindingChangeBtn setAction:^{
  46. EditPhoneOrEmailVC *vc = [EditPhoneOrEmailVC initEditPhoneOrEmailVC];
  47. vc.pageType = BindingOrChangePhoneOrEmailPageType3;
  48. [weakSelf.navigationController pushViewController:vc animated:YES];
  49. }];
  50. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType2){
  51. self.pageTitleLabel.text = @"绑定邮箱";
  52. self.bindingBgView.hidden = NO;
  53. self.changeBgView.hidden = YES;
  54. self.bindingShowImgView.image = [UIImage imageNamed:@"my_edit_phoneOrEmail_icon"];
  55. [self.bindingChangeBtn setTitle:@"更换邮箱" forState:UIControlStateNormal];
  56. self.bindingShowLabel.text = [NSString stringWithFormat:@"绑定的邮箱:%@",[AppUserModel sharedAppUserModel].Email];
  57. [self.bindingChangeBtn setAction:^{
  58. EditPhoneOrEmailVC *vc = [EditPhoneOrEmailVC initEditPhoneOrEmailVC];
  59. vc.pageType = BindingOrChangePhoneOrEmailPageType4;
  60. [weakSelf.navigationController pushViewController:vc animated:YES];
  61. }];
  62. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType3){
  63. self.pageTitleLabel.text = @"更换手机号";
  64. self.bindingBgView.hidden = YES;
  65. self.changeBgView.hidden = NO;
  66. self.changeShowLabel.text = @"输入新手机号并验证后即可更换手机号";
  67. self.changeOneTextField.text = @"";
  68. self.changeOneTextField.delegate = self;
  69. self.changeOneTextField.placeholder = @"请输入手机号";
  70. self.changeOneTextField.textColor = RGB(51, 51, 51);
  71. [self.changeOneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  72. NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:self.changeOneTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeOneTextField.font}];
  73. self.changeOneTextField.attributedPlaceholder = attrStringONE;
  74. [self.changeSendYZMBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  75. self.changeTwoTextField.text = @"";
  76. self.changeTwoTextField.delegate = self;
  77. self.changeTwoTextField.placeholder = @"请输入验证码";
  78. self.changeTwoTextField.textColor = RGB(51, 51, 51);
  79. [self.changeTwoTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  80. NSAttributedString *attrStringTWO = [[NSAttributedString alloc] initWithString:self.changeTwoTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeTwoTextField.font}];
  81. self.changeTwoTextField.attributedPlaceholder = attrStringTWO;
  82. [self.sureBtn setAction:^{
  83. [weakSelf modiPhoneCommitData];
  84. }];
  85. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType4){
  86. self.pageTitleLabel.text = @"更换邮箱";
  87. self.bindingBgView.hidden = YES;
  88. self.changeBgView.hidden = NO;
  89. self.changeShowLabel.text = @"输入新邮箱并验证后即可更换绑定邮箱";
  90. self.changeOneTextField.text = @"";
  91. self.changeOneTextField.delegate = self;
  92. self.changeOneTextField.placeholder = @"请输入新邮箱";
  93. self.changeOneTextField.textColor = RGB(51, 51, 51);
  94. [self.changeOneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  95. NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:self.changeOneTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeOneTextField.font}];
  96. self.changeOneTextField.attributedPlaceholder = attrStringONE;
  97. [self.changeSendYZMBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  98. self.changeTwoTextField.text = @"";
  99. self.changeTwoTextField.delegate = self;
  100. self.changeTwoTextField.placeholder = @"请输入验证码";
  101. self.changeTwoTextField.textColor = RGB(51, 51, 51);
  102. [self.changeTwoTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  103. NSAttributedString *attrStringTWO = [[NSAttributedString alloc] initWithString:self.changeTwoTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeTwoTextField.font}];
  104. self.changeTwoTextField.attributedPlaceholder = attrStringTWO;
  105. [self.sureBtn setAction:^{
  106. [weakSelf modiEMailCommitData];
  107. }];
  108. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType5){
  109. self.pageTitleLabel.text = @"绑定手机号";
  110. self.bindingBgView.hidden = YES;
  111. self.changeBgView.hidden = NO;
  112. self.changeShowLabel.text = @"输入手机号并验证后即可绑定手机号";
  113. self.changeOneTextField.text = @"";
  114. self.changeOneTextField.delegate = self;
  115. self.changeOneTextField.placeholder = @"请输入手机号";
  116. self.changeOneTextField.textColor = RGB(51, 51, 51);
  117. [self.changeOneTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  118. NSAttributedString *attrStringONE = [[NSAttributedString alloc] initWithString:self.changeOneTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeOneTextField.font}];
  119. self.changeOneTextField.attributedPlaceholder = attrStringONE;
  120. [self.changeSendYZMBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  121. self.changeTwoTextField.text = @"";
  122. self.changeTwoTextField.delegate = self;
  123. self.changeTwoTextField.placeholder = @"请输入验证码";
  124. self.changeTwoTextField.textColor = RGB(51, 51, 51);
  125. [self.changeTwoTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  126. NSAttributedString *attrStringTWO = [[NSAttributedString alloc] initWithString:self.changeTwoTextField.placeholder attributes: @{NSForegroundColorAttributeName:RGB(153, 153, 153), NSFontAttributeName:self.changeTwoTextField.font}];
  127. self.changeTwoTextField.attributedPlaceholder = attrStringTWO;
  128. [self.sureBtn setAction:^{
  129. if (weakSelf.openId.length > 0) {
  130. [weakSelf modiWinXin:weakSelf.openId];
  131. }
  132. }];
  133. }
  134. }
  135. -(void)startTime{
  136. __block int timeout = 60; //倒计时时间
  137. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  138. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  139. dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  140. dispatch_source_set_event_handler(_timer, ^{
  141. if(timeout <= 0){ //倒计时结束,关闭
  142. dispatch_source_cancel(_timer);
  143. WS(weakSelf);
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. //设置界面的按钮显示 根据自己需求设置 特别注明:UI的改变一定要在主线程中进行
  146. //倒计时完成,恢复到原来的效果
  147. NSString *text = @"获取验证码";
  148. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateNormal];
  149. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateHighlighted];
  150. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateSelected];
  151. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateDisabled];
  152. [weakSelf.changeSendYZMBtn setTitleColor:RGB(37, 138, 220) forState:UIControlStateNormal];
  153. [weakSelf.changeSendYZMBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
  154. [weakSelf.changeSendYZMBtn setEnabled:YES];
  155. });
  156. }else{
  157. int seconds = timeout % 60;
  158. if(seconds == 0&&timeout==60){
  159. seconds = 60;
  160. }
  161. // NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
  162. WS(weakSelf);
  163. dispatch_async(dispatch_get_main_queue(), ^{
  164. //设置界面的按钮显示 根据自己需求设置
  165. // NSLog(@"____%@",strTime);
  166. NSString *text = [NSString stringWithFormat:@"重新获取(%lus)",(unsigned long)seconds];
  167. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateNormal];
  168. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateHighlighted];
  169. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateSelected];
  170. [weakSelf.changeSendYZMBtn setTitle:text forState:UIControlStateDisabled];
  171. [weakSelf.changeSendYZMBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
  172. [weakSelf.changeSendYZMBtn setTitleColor:RGB(37, 138, 220) forState:UIControlStateNormal];
  173. });
  174. timeout--;
  175. }
  176. });
  177. dispatch_resume(_timer);
  178. }
  179. - (IBAction)verifyCodeAction:(UIButton *)sender {
  180. [self.view endEditing:YES];
  181. if(self.pageType == BindingOrChangePhoneOrEmailPageType1){
  182. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType2){
  183. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType3){
  184. [self modiPhoneNumberSendCode];
  185. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType4){
  186. [self modiEMailberSendCode];
  187. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType5){
  188. [self modiPhoneNumberSendCode];
  189. }
  190. }
  191. #pragma mark - 修改手机号
  192. /// 修改手机号发送验证码
  193. - (void)modiPhoneNumberSendCode{
  194. if (self.changeOneTextField.text.length == 0) {
  195. SHOWERROR(@"请输入正确手机号码");
  196. return;
  197. }
  198. NSLog(@"⚠️ 更换手机获取验证码 %@",self.changeOneTextField.text);
  199. SHOWLOADING
  200. WS(weakSelf);
  201. [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,LoginSendCodeGet] parameters:@{@"phone":self.changeOneTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) {
  202. REMOVESHOW
  203. [weakSelf startTime];
  204. } failure:^(NSError * _Nonnull error) {
  205. REMOVESHOW
  206. SHOWERROR([ZYCTool handerResultData:error])
  207. }];
  208. }
  209. /// 修改手机号 提交数据
  210. - (void)modiPhoneCommitData{
  211. if (self.changeOneTextField.text.length != 11) {
  212. SHOWERROR(@"请输入正确手机号码");
  213. return;
  214. }
  215. if (self.changeTwoTextField.text.length != 6) {
  216. SHOWERROR(@"请输入正确验证码");
  217. return;
  218. }
  219. SHOWLOADING
  220. WEAKSELF
  221. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserPhone_Post] parameters:@{@"phone":self.changeOneTextField.text,@"code":self.changeTwoTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) {
  222. [UserManager getUserInfoDetailSuccess:^(id _Nonnull responseObject) {
  223. REMOVESHOW
  224. STRONGSELF
  225. // [strongSelf.navigationController popToRootViewControllerAnimated:YES];
  226. NSLog(@"-=-=-=-=-=-=-%@",strongSelf.navigationController.viewControllers);
  227. [strongSelf.navigationController popToViewController:strongSelf.navigationController.viewControllers[1] animated:YES];
  228. } failure:^(NSError * _Nonnull error) {
  229. REMOVESHOW
  230. SHOWERROR([ZYCTool handerResultData:error])
  231. }];
  232. } failure:^(NSError * _Nonnull error) {
  233. REMOVESHOW
  234. SHOWERROR([ZYCTool handerResultData:error])
  235. }];
  236. }
  237. #pragma mark - 修改邮箱
  238. /// 修改邮箱 发送验证码
  239. - (void)modiEMailberSendCode{
  240. if (![self.changeOneTextField.text isEmailAddress]) {
  241. SHOWERROR(@"请输入正确邮箱地址");
  242. return;
  243. }
  244. NSLog(@"⚠️ 更换邮箱获取验证码 %@",self.changeOneTextField.text);
  245. SHOWLOADING
  246. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserEMail_SendCode_Post] parameters:@{@"email":self.changeOneTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) {
  247. REMOVESHOW
  248. [self startTime];
  249. } failure:^(NSError * _Nonnull error) {
  250. REMOVESHOW
  251. SHOWERROR([ZYCTool handerResultData:error])
  252. }];
  253. }
  254. /// 修改邮箱 提交数据
  255. - (void)modiEMailCommitData{
  256. if (![self.changeOneTextField.text isEmailAddress]) {
  257. SHOWERROR(@"请输入正确邮箱地址");
  258. return;
  259. }
  260. if (self.changeTwoTextField.text.length != 6) {
  261. SHOWERROR(@"请输入正确验证码");
  262. return;
  263. }
  264. SHOWLOADING
  265. WEAKSELF
  266. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserEMail_Post] parameters:@{@"email":self.changeOneTextField.text,@"code":self.changeTwoTextField.text} responseStyle:DATA success:^(id _Nonnull responseObject) {
  267. [UserManager getUserInfoDetailSuccess:^(id _Nonnull responseObject) {
  268. REMOVESHOW
  269. STRONGSELF
  270. NSLog(@"%@",strongSelf.navigationController.viewControllers);
  271. for ( int i = 0 ;i< strongSelf.navigationController.viewControllers.count;i++) {
  272. if ([strongSelf.navigationController.viewControllers[i] isKindOfClass:NSClassFromString(@"MyInfoVC")]) {
  273. [strongSelf.navigationController popToViewController:strongSelf.navigationController.viewControllers[i] animated:YES];
  274. return;
  275. }
  276. }
  277. } failure:^(NSError * _Nonnull error) {
  278. REMOVESHOW
  279. SHOWERROR([ZYCTool handerResultData:error])
  280. }];
  281. } failure:^(NSError * _Nonnull error) {
  282. REMOVESHOW
  283. SHOWERROR([ZYCTool handerResultData:error])
  284. }];
  285. }
  286. #pragma mark - 绑定微信
  287. - (void)modiWinXin:(NSString *)openId{
  288. if (self.changeOneTextField.text.length != 11) {
  289. SHOWERROR(@"请输入正确手机号码");
  290. return;
  291. }
  292. if (self.changeTwoTextField.text.length != 6) {
  293. SHOWERROR(@"请输入正确验证码");
  294. return;
  295. }
  296. SHOWLOADING
  297. WEAKSELF
  298. [[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) {
  299. REMOVESHOW
  300. NSDictionary *dic = responseObject;
  301. USERDEFAULTSSET(dic[@"Token"][@"Expires"],@"tokenExpires");
  302. NSString *token = [NSString stringWithFormat:@"%@ %@",dic[@"Token"][@"Type"],dic[@"Token"][@"Token"]];
  303. USERDEFAULTSSET(token,@"LOGINTOKEN");
  304. NSLog(@"登录成功之后的token%@\n%@",kToken,token);
  305. USERDEFAULTSSET(dic[@"UserId"],@"userId");
  306. [weakSelf getUserInfoDetail];
  307. } failure:^(NSError * _Nonnull error) {
  308. REMOVESHOW
  309. SHOWERROR([ZYCTool handerResultData:error])
  310. }];
  311. }
  312. - (void)getUserInfoDetail{
  313. SHOWLOADING
  314. WEAKSELF
  315. NSLog(@"%@",kUserId);
  316. [[HttpManager sharedHttpManager].manager.requestSerializer setValue:[UserManager token] forHTTPHeaderField:@"Authorization"];
  317. [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,UserDetailGet] parameters:@{} success:^(id _Nonnull responseObject) {
  318. NSLog(@"%@",responseObject);
  319. AppUserModel * model = [[AppUserModel alloc] initWithDictionary:responseObject error:nil];
  320. [model saveUserInfor];
  321. REMOVESHOW
  322. [weakSelf changeToHome];
  323. NSData * data = [NSKeyedArchiver archivedDataWithRootObject:model];
  324. USERDEFAULTSSET(data,@"USERINFO");
  325. //在这里登陆环信
  326. [[EaseSDKHelper shareHelper] loginEaseWithUsername:model.ImId password:@"123456"];
  327. } failure:^(NSError * _Nonnull error) {
  328. REMOVESHOW
  329. SHOWERROR([ZYCTool handerResultData:error])
  330. }];
  331. }
  332. -(void)changeToHome{
  333. UIWindow *window = [UtilsTools getWindow];
  334. window.rootViewController = [TabBarController sharedTabBarController];
  335. [[TabBarController sharedTabBarController] setSelectedIndex:0];
  336. [window makeKeyAndVisible];
  337. }
  338. /****************************************************/
  339. #pragma mark - UITextFieldDelegate
  340. /****************************************************/
  341. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  342. // NSString *tempString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  343. if ([string isEqualToString:@""] || [string isEqualToString:@"\n"]) {
  344. return YES;
  345. }
  346. NSString *regex = @"[\u4e00-\u9fa5]{0,}$"; // 中文
  347. NSPredicate *predicateRe1 = [NSPredicate predicateWithFormat:@"self matches %@", regex];
  348. // 3、匹配字符串
  349. BOOL resualt = [predicateRe1 evaluateWithObject:string];
  350. if (resualt)
  351. {
  352. return NO;
  353. }
  354. return YES;
  355. }
  356. - (void)textFieldDidEndEditing:(UITextField *)textField{
  357. if (self.pageType == BindingOrChangePhoneOrEmailPageType3){
  358. }else if (self.pageType == BindingOrChangePhoneOrEmailPageType4){
  359. }
  360. }
  361. -(void)textFieldDidChange:(UITextField *)textField{
  362. if (textField.text.length > 20) {
  363. textField.text = [textField.text substringToIndex:20];
  364. }
  365. }
  366. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  367. [textField resignFirstResponder];
  368. return YES;
  369. }
  370. - (void)viewWillDisappear:(BOOL)animated
  371. {
  372. [super viewWillDisappear:animated];
  373. }
  374. @end