ResetPassword_VC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // ResetPassword_VC.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2018/12/30.
  6. // Copyright © 2018年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "ResetPassword_VC.h"
  9. #import "Header.h"
  10. @interface ResetPassword_VC ()<UITextFieldDelegate>
  11. @end
  12. @implementation ResetPassword_VC
  13. - (void)viewDidLoad {
  14. [super viewDidLoad];
  15. self.navigationItem.title = @"重设密码";
  16. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor colorWithRed:66/255.0 green:66/255.0 blue:66/255.0 alpha:1]}];
  17. //设置导航栏左侧按钮
  18. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  19. [backBtn setImage:[UIImage imageNamed:@"hsh_register_return"] forState:UIControlStateNormal];
  20. backBtn.frame = CGRectMake(0, 0, 44, 44);
  21. [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
  22. backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  23. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
  24. [self setupUI];
  25. }
  26. #pragma mark -Set UI
  27. -(void)setupUI{
  28. /** 设置背景 */
  29. self.view.backgroundColor = [UIColor whiteColor];
  30. /** 设置logo */
  31. [self.logoImageView setImage:[UIImage imageNamed:@"logo"]];
  32. self.logoImageView.frame = CGRectMake(WIDTH/2-55, HEIGHT/4-75, 110, 110);
  33. [self.view addSubview:self.logoImageView];
  34. /** 设置内容视图 */
  35. /**设置账户输入框 */
  36. self.accountTF.frame = CGRectMake(20, HEIGHT/2-64, WIDTH-40, 44);
  37. self.accountTF.placeholder = @"请输入注册手机号";
  38. self.accountTF.tintColor = [UIColor blueColor];
  39. self.accountTF.textColor = [UIColor blackColor];
  40. self.accountTF.font = [UIFont systemFontOfSize:18];
  41. self.accountTF.leftViewMode = UITextFieldViewModeAlways;
  42. self.accountTF.clearsOnBeginEditing = YES;
  43. self.accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  44. self.accountTF.autocorrectionType =UITextAutocorrectionTypeDefault;
  45. self.accountTF.delegate = self;
  46. //设置左边视图
  47. UIImageView *userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
  48. userImage.contentMode = UIViewContentModeLeft;
  49. //??
  50. userImage.image = [UIImage imageNamed:@"hsh_login_user"];
  51. self.accountTF.leftView = userImage;
  52. [self.view addSubview:self.accountTF];
  53. UIView *accountLine = [[UIView alloc]init];
  54. //??
  55. accountLine.frame = CGRectMake(0, 43.5, WIDTH-40, 0.5);
  56. accountLine.backgroundColor = [UIColor grayColor];
  57. self.accountLine = accountLine;
  58. [self.accountTF addSubview:accountLine];
  59. /** 设置验证码输入框 */
  60. self.smsCodeTF.frame = CGRectMake(20, HEIGHT/2-20, WIDTH-40, 44);
  61. self.smsCodeTF.placeholder = @"请输入手机验证码";
  62. self.smsCodeTF.tintColor = [UIColor blueColor];
  63. self.smsCodeTF.textColor = [UIColor blackColor];
  64. self.smsCodeTF.font = [UIFont systemFontOfSize:18];
  65. self.smsCodeTF.leftViewMode = UITextFieldViewModeAlways;
  66. // self.smsCodeTF.clearsOnBeginEditing = YES;
  67. // self.smsCodeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  68. self.smsCodeTF.autocorrectionType =UITextAutocorrectionTypeDefault;
  69. self.smsCodeTF.delegate = self;
  70. //设置左边视图
  71. UIImageView *smsImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
  72. smsImage.contentMode = UIViewContentModeLeft;
  73. smsImage.image = [UIImage imageNamed:@"hsh_register_verification"];
  74. self.smsCodeTF.leftView = smsImage;
  75. [self.view addSubview:self.smsCodeTF];
  76. UIView *smsLine = [[UIView alloc]init];
  77. //??
  78. smsLine.frame = CGRectMake(0, 43.5, WIDTH-40, 0.5);
  79. smsLine.backgroundColor = [UIColor grayColor];
  80. self.smsLine = smsLine;
  81. [self.smsCodeTF addSubview:self.smsLine];
  82. /**设置重置密码button */
  83. self.resetPasswordBtn.frame = CGRectMake(self.smsCodeTF.frame.size.width-100, 5, 100, 34);
  84. self.resetPasswordBtn.layer.cornerRadius = 12;
  85. self.resetPasswordBtn.backgroundColor = [UIColor colorWithRed:113/255.0 green:154/255.90 blue:254/255.0 alpha:1];
  86. [self.resetPasswordBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  87. [self.resetPasswordBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  88. self.resetPasswordBtn.titleLabel.textColor = [UIColor whiteColor];
  89. self.resetPasswordBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  90. self.resetPasswordBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
  91. [self.resetPasswordBtn addTarget:self action:@selector(getSMSCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  92. [self.smsCodeTF addSubview:self.resetPasswordBtn];
  93. /**设置密码输入框 */
  94. self.passwordTF.frame = CGRectMake(20, HEIGHT/2+22, WIDTH-40, 44);
  95. self.passwordTF.placeholder = @"请输入新密码";
  96. self.passwordTF.tintColor = [UIColor blueColor];
  97. self.passwordTF.textColor = [UIColor blackColor];
  98. self.passwordTF.font = [UIFont systemFontOfSize:18];
  99. self.passwordTF.leftViewMode = UITextFieldViewModeAlways;
  100. self.passwordTF.delegate = self;
  101. self.passwordTF.clearsOnBeginEditing = YES;
  102. self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  103. self.passwordTF.autocorrectionType =UITextAutocorrectionTypeDefault;
  104. self.passwordTF.secureTextEntry =YES;
  105. //设置左边视图
  106. UIImageView *passwordImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
  107. passwordImage.contentMode = UIViewContentModeLeft;
  108. passwordImage.image = [UIImage imageNamed:@"hsh_login_password"];
  109. self.passwordTF.leftView = passwordImage;
  110. [self.view addSubview:self.passwordTF];
  111. UIView *passwordLine = [[UIView alloc]init];
  112. //??
  113. passwordLine.frame = CGRectMake(0, 43.5, WIDTH-40, 0.5);
  114. passwordLine.backgroundColor = [UIColor grayColor];
  115. self.passwordLine = passwordLine;
  116. [self.passwordTF addSubview:passwordLine];
  117. /** 设置确认密码 */
  118. self.confirmPasswordTf.frame = CGRectMake(20, HEIGHT/2+66, WIDTH-40, 44);
  119. self.confirmPasswordTf.placeholder = @"请再次确认密码";
  120. self.confirmPasswordTf.tintColor = [UIColor blueColor];
  121. self.confirmPasswordTf.textColor = [UIColor blackColor];
  122. self.confirmPasswordTf.font = [UIFont systemFontOfSize:18];
  123. self.confirmPasswordTf.leftViewMode = UITextFieldViewModeAlways;
  124. self.confirmPasswordTf.delegate = self;
  125. self.confirmPasswordTf.clearsOnBeginEditing = YES;
  126. self.confirmPasswordTf.clearButtonMode = UITextFieldViewModeWhileEditing;
  127. self.confirmPasswordTf.autocorrectionType =UITextAutocorrectionTypeDefault;
  128. self.confirmPasswordTf.secureTextEntry =YES;
  129. //设置左边视图
  130. UIImageView *confirmImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
  131. confirmImage.contentMode = UIViewContentModeLeft;
  132. confirmImage.image = [UIImage imageNamed:@"hsh_login_password"];
  133. self.confirmPasswordTf.leftView = confirmImage;
  134. [self.view addSubview:self.confirmPasswordTf];
  135. UIView *confirmLine = [[UIView alloc]init];
  136. //??
  137. confirmLine.frame = CGRectMake(0, 43.5, WIDTH-40, 0.5);
  138. confirmLine.backgroundColor = [UIColor grayColor];
  139. self.confirmLine = confirmLine;
  140. [self.confirmPasswordTf addSubview:self.confirmLine];
  141. /**设置提交Button */
  142. self.registButton.frame = CGRectMake(20, HEIGHT-100, WIDTH-40, 44);
  143. self.registButton.layer.cornerRadius = 18;
  144. [self.registButton setBackgroundImage:[UIImage imageNamed:@"blue"] forState:UIControlStateNormal];
  145. [self.registButton setTitle:@"提交" forState:UIControlStateNormal];
  146. [self.registButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  147. self.registButton.titleLabel.font = [UIFont systemFontOfSize:18];
  148. self.registButton.titleLabel.textAlignment = NSTextAlignmentCenter;
  149. // self.loginButton.clipsToBounds = YES;
  150. [self.registButton addTarget:self action:@selector(comitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  151. [self.view addSubview:self.registButton];
  152. /** 布局 */
  153. [self make_layout];
  154. }
  155. -(void)make_layout{
  156. }
  157. #pragma mark - Event response
  158. -(void)backBtnClick{
  159. [self.navigationController popViewControllerAnimated:YES];
  160. }
  161. -(void)getSMSCodeBtnClick:(UIButton *)codeBtn{
  162. if (self.accountTF.text.length ==0||self.accountTF.text.length<11||self.accountTF.text.length>11) {
  163. ALERTSHOW(@"请输入正确的手机号");
  164. return;
  165. }
  166. [self startTime];
  167. }
  168. -(void)startTime{
  169. // GCD类型
  170. __block int timeout=59; //倒计时时间
  171. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  172. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  173. dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  174. dispatch_source_set_event_handler(_timer, ^{
  175. if(timeout<=0){ //倒计时结束,关闭
  176. dispatch_source_cancel(_timer);
  177. dispatch_async(dispatch_get_main_queue(), ^{
  178. //设置界面的按钮显示 根据自己需求设置(倒计时结束后调用)
  179. //设置按钮的样式
  180. [self.resetPasswordBtn setTitle:@"重新发送" forState:UIControlStateNormal];
  181. //设置不可点击
  182. self.resetPasswordBtn.userInteractionEnabled = YES;
  183. });
  184. }else{
  185. // int minutes = timeout / 60; //这里注释掉了,这个是用来测试多于60秒时计算分钟的。
  186. int seconds = timeout % 60;
  187. NSString *strTime = [NSString stringWithFormat:@"%ds", seconds];
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. //设置界面的按钮显示 根据自己需求设置
  190. // NSLog(@"____%@",strTime);
  191. [self.resetPasswordBtn setTitle:[NSString stringWithFormat:@"%@秒重发",strTime] forState:UIControlStateNormal];
  192. //设置可点击
  193. self.resetPasswordBtn.userInteractionEnabled = NO;
  194. });
  195. timeout--;
  196. }
  197. });
  198. dispatch_resume(_timer);
  199. //请求验证码
  200. [DataSourceManager getPhoneCodeWithPhone:self.accountTF.text completionBlock:^(NSDictionary *dic) {
  201. ALERTSHOW(@"验证码发送成功");
  202. }];
  203. }
  204. -(void)comitBtnClick:(UIButton *)comitBtn{
  205. if (self.accountTF.text.length ==0 ||self.accountTF.text.length<11||self.accountTF.text.length >11) {
  206. ALERTSHOW(@"手机号输入有误");
  207. return;
  208. }
  209. if (self.smsCodeTF.text.length ==0 ||self.smsCodeTF.text.length !=6) {
  210. ALERTSHOW(@"请输入有效的验证码");
  211. return;
  212. }
  213. if (self.passwordTF.text.length == 0||self.passwordTF.text.length<6||self.phoneTf.text.length >20){
  214. ALERTSHOW(@"密码输入有误");
  215. return;
  216. }
  217. if (![self.passwordTF.text isEqualToString:self.confirmPasswordTf.text]) {
  218. ALERTSHOW(@"两次输入密码不相同");
  219. return;
  220. }
  221. //提交修改
  222. [DataSourceManager updatePasswordWithUrlPhone:self.accountTF.text code:self.smsCodeTF.text Password1:self.passwordTF.text Password2:self.confirmPasswordTf.text completionBlock:^(NSDictionary *dic) {
  223. ALERTSHOW(@"修改密码成功");
  224. }];
  225. }
  226. #pragma mark - Delegate methods
  227. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  228. return YES;
  229. }
  230. //避免键盘遮挡,登录表单或按钮上移代码
  231. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
  232. NSTimeInterval animationDuration = 0.30f;
  233. [UIView beginAnimations:@ "ResizeForKeyboard" context:nil];
  234. [UIView setAnimationDuration:animationDuration];
  235. //将视图的Y坐标向上移动,以使下面腾出地方用于软键盘的显示
  236. self.view.frame = CGRectMake(0.0f, -50.0f, self.view.frame.size.width, self.view.frame.size.height); //64-216
  237. [UIView commitAnimations];
  238. return YES;
  239. }
  240. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  241. NSTimeInterval animationDuration = 0.30f;
  242. [UIView beginAnimations:@ "ResizeForKeyboard" context:nil];
  243. [UIView setAnimationDuration:animationDuration];
  244. //恢复屏幕
  245. self.view.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height); //64-216
  246. [UIView commitAnimations];
  247. [[UIApplication sharedApplication].keyWindow endEditing:YES];
  248. }
  249. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  250. [textField resignFirstResponder];
  251. return YES;
  252. }
  253. - (void)didReceiveMemoryWarning {
  254. [super didReceiveMemoryWarning];
  255. // Dispose of any resources that can be recreated.
  256. }
  257. /*
  258. #pragma mark - Navigation
  259. // In a storyboard-based application, you will often want to do a little preparation before navigation
  260. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  261. // Get the new view controller using [segue destinationViewController].
  262. // Pass the selected object to the new view controller.
  263. }
  264. */
  265. @end