酷店

KDPPhoneLoginViewController.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // KDPPhoneLoginViewController.m
  3. // KuDianProject
  4. //
  5. // Created by admin on 2019/7/8.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPPhoneLoginViewController.h"
  9. #import "KDPWebViewViewController.h"
  10. #import "KDPBindFastHandViewController.h"
  11. #import "KDPublicMethod.h"
  12. static int count = 0;
  13. @interface KDPPhoneLoginViewController ()
  14. @property (nonatomic, strong) UITextField *phoneTextField;
  15. @property (nonatomic, strong) UITextField *codeField;
  16. @property (nonatomic, strong) UIButton *sendCodeBtn;
  17. @property (nonatomic, strong) NSTimer *timer;
  18. // 保存的手机验证码
  19. @property (nonatomic, strong) NSString *saveCode;
  20. @end
  21. @implementation KDPPhoneLoginViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. self.view.backgroundColor = [UIColor whiteColor];
  26. self.navBar.hidden = YES;
  27. [self setSubViews];
  28. }
  29. - (void)setSubViews{
  30. // UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  31. // UIImage *image = [[UIImage imageNamed:@"close_web"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  32. // [closeBtn setImage:image];
  33. // [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
  34. // closeBtn.frame = CGRectMake(15, KDStatusHeight + 18, 16, 16);
  35. // [self.view addSubview:closeBtn];
  36. UILabel *phoneTipLabel = [[UILabel alloc] initWithFrame:CGRectMake(32, KDStatusHeight + Fitsize(87) , 217, 42)];
  37. phoneTipLabel.textColor = [UIColor colorWithHex:0x333333];
  38. phoneTipLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size: 30];
  39. phoneTipLabel.text = @"手机号快速登录";
  40. [self.view addSubview:phoneTipLabel];
  41. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: @"登录代表您已阅读并同意《酷店app隐私协议政策》"];
  42. text.yy_font = [UIFont systemFontOfSize:12];
  43. text.yy_color = [UIColor colorWithHex:0x4B4B4B];
  44. __weak typeof(self) weakself = self;
  45. [text yy_setTextHighlightRange:NSMakeRange(11, text.length-11) color:[UIColor colorWithHex:0x208EEA] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
  46. KDPWebViewViewController *webVC = [[KDPWebViewViewController alloc] init];
  47. NSString *urlStr = @"https://tbk.726p.com/agreementh5/agreement/index.html?name=酷店app";
  48. webVC.webUrl = urlStr;
  49. [weakself presentViewController:webVC animated:YES completion:nil];
  50. }];
  51. YYLabel *privacyLabel = [[YYLabel alloc] initWithFrame:CGRectMake(32, phoneTipLabel.bottom+10, SCREEN_WIDTH-32, 17)];
  52. privacyLabel.attributedText = text;
  53. [self.view addSubview:privacyLabel];
  54. UITextField *phoneField = [[UITextField alloc] initWithFrame:CGRectMake(32, privacyLabel.bottom+Fitsize(102), 200, 22)];
  55. phoneField.keyboardType = UIKeyboardTypePhonePad;
  56. phoneField.textColor = [UIColor colorWithHex:0x333333];
  57. phoneField.font = FONT_SYS(16);
  58. phoneField.placeholder = @"请输入手机号";
  59. [phoneField addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
  60. self.phoneTextField = phoneField;
  61. [self.view addSubview:self.phoneTextField];
  62. self.sendCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  63. self.sendCodeBtn.frame = CGRectMake(SCREEN_WIDTH-120-36, phoneField.top, 120, 17);
  64. self.sendCodeBtn.centerY = phoneField.centerY;
  65. self.sendCodeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  66. [self.sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
  67. [self.sendCodeBtn setTitleColor:[UIColor baseColor] forState:UIControlStateNormal];
  68. self.sendCodeBtn.titleLabel.font = FONT_SYS(12);
  69. [self.sendCodeBtn addTarget:self action:@selector(sendCodeClick:) forControlEvents:UIControlEventTouchUpInside];
  70. [self.view addSubview:self.sendCodeBtn];
  71. UIView *phoneLine = [[UIView alloc] initWithFrame:CGRectMake(32, phoneField.bottom + 12, SCREEN_WIDTH-64, 1)];
  72. phoneLine.backgroundColor = [UIColor colorWithHex:0xD1D1D1];
  73. [self.view addSubview:phoneLine];
  74. self.codeField = [[UITextField alloc] initWithFrame:CGRectMake(32, phoneLine.bottom + 27, phoneLine.width, self.phoneTextField.height)];
  75. self.codeField.keyboardType = UIKeyboardTypePhonePad;
  76. self.codeField.textColor = [UIColor colorWithHex:0x333333];
  77. self.codeField.font = FONT_SYS(16);
  78. self.codeField.placeholder = @"请输入验证码";
  79. [self.view addSubview:self.codeField];
  80. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(32, self.codeField.bottom + 12, SCREEN_WIDTH-64, 1)];
  81. lineView.backgroundColor = [UIColor colorWithHex:0xD1D1D1];
  82. [self.view addSubview:lineView];
  83. UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  84. loginBtn.frame = CGRectMake(32, lineView.bottom + Fitsize(80), SCREEN_WIDTH-64, 48);
  85. [loginBtn setGradientBackgroundWithColors:@[[UIColor colorWithHex:0xFF7676],[UIColor colorWithHex:0xFF235F]] locations:@[@0,@1] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];
  86. [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
  87. [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  88. loginBtn.titleLabel.font = FONT_SYS(17);
  89. [loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  90. loginBtn.layer.cornerRadius = 24;
  91. loginBtn.layer.masksToBounds = YES;
  92. [self.view addSubview:loginBtn];
  93. UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, loginBtn.bottom + 7, SCREEN_WIDTH, 18)];
  94. bottomLabel.textAlignment = NSTextAlignmentCenter;
  95. bottomLabel.textColor = [UIColor colorWithHex:0x5d5d5d];
  96. bottomLabel.font = FONT_SYS(13);
  97. bottomLabel.text = @"未注册的手机号码验证后自动注册";
  98. [self.view addSubview:bottomLabel];
  99. }
  100. - (void)closeAction:(UIButton *)sender{
  101. }
  102. - (void)loginBtnClick:(UIButton *)sender{
  103. if (self.phoneTextField.text.length < 11 || self.phoneTextField.text.length >13) {
  104. [MBProgressHUD showMessage:@"请输入正确的手机号"];
  105. return;
  106. }
  107. if (self.codeField.text.length) {
  108. [LoadingView show];
  109. NSDictionary *params=@{@"phone":self.phoneTextField.text,@"code":self.codeField.text};
  110. [KDPNetworkRequestHTTP postURL:LoginURL params:params success:^(id _Nonnull json) {
  111. [LoadingView dismiss];
  112. NSInteger has_kwai=[json[@"has_kwai"] integerValue];
  113. KDPAccountModel *accountModel = [KDPAccountModel yy_modelWithJSON:json];
  114. accountModel.phone = self.phoneTextField.text;
  115. [KDPAccountTool saveAccount:accountModel];
  116. //判断是否填写快手号,,
  117. if (has_kwai !=1) {//1的时候已经填写快手号
  118. KDPBindFastHandViewController *setV = [[KDPBindFastHandViewController alloc]init];
  119. [self presentViewController:[[UINavigationController alloc]initWithRootViewController:setV] animated:YES completion:nil];
  120. }else{
  121. UIViewController *vc = self;
  122. while (vc.presentingViewController) {
  123. vc = vc.presentingViewController;
  124. }
  125. [vc dismissViewControllerAnimated:YES completion:nil];
  126. }
  127. } failure:^(NSError * _Nonnull error) {
  128. [MBProgressHUD showError:@"验证码输入错误"];
  129. [LoadingView dismiss];
  130. }];
  131. } else{
  132. [MBProgressHUD showMessage:@"请输入验证码"];
  133. }
  134. }
  135. - (void)valueChanged:(UITextField *)textfield{
  136. }
  137. - (void)sendCodeClick:(UIButton *)sender{
  138. if (self.phoneTextField.text.length >= 11 && self.phoneTextField.text.length <= 13) {
  139. [LoadingView showInView:self.view];
  140. [self.phoneTextField resignFirstResponder];
  141. NSString *ttl = [KDPublicMethod getNowTimeTimestamp];
  142. NSString *sign = [NSString stringWithFormat:@"phone=%@&ttl=%@%@",self.phoneTextField.text,ttl,SignCode];
  143. NSString *md5Str = [KDPublicMethod md5:sign];
  144. NSDictionary *para = @{@"phone":self.phoneTextField.text,
  145. @"ttl":ttl,
  146. @"sign":md5Str
  147. };
  148. [KDPNetworkRequestHTTP postURL:GetCodeURL params:para success:^(id _Nonnull json) {
  149. [LoadingView dismiss];
  150. NSDictionary *dics =(NSDictionary *)json;
  151. count = [json[@"wait_time"]integerValue ];
  152. [self startTime];
  153. if ([json[@"success"] boolValue] == 1) {
  154. if ([dics.allKeys containsObject:@"code"]) {
  155. self.codeField.text=[NSString stringWithFormat:@"%@", json[@"code"]];
  156. }
  157. }
  158. } failure:^(NSError * _Nonnull error) {
  159. [LoadingView dismiss];
  160. }];
  161. } else{
  162. [MBProgressHUD showMessage:@"请输入正确的手机号"];
  163. }
  164. }
  165. - (void)startTime{
  166. [_timer invalidate];
  167. [self changeCode];
  168. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
  169. }
  170. - (void)changeCode{
  171. NSString *title = @"获取验证码";
  172. if (count > 0) {
  173. title = [NSString stringWithFormat:@"%d秒后可重新获取", count];
  174. [self.sendCodeBtn setTitleColor:[UIColor colorWithHex:0x999999] forState:UIControlStateNormal];
  175. self.sendCodeBtn.userInteractionEnabled = NO;
  176. }else{
  177. [self.sendCodeBtn setTitleColor:[UIColor baseColor] forState:UIControlStateNormal];
  178. }
  179. [self.sendCodeBtn setTitle:title forState:UIControlStateNormal];
  180. }
  181. -(void)viewWillAppear:(BOOL)animated
  182. {
  183. [super viewWillAppear:animated];
  184. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleDefault;
  185. }
  186. -(void)viewWillDisappear:(BOOL)animated
  187. {
  188. [super viewWillDisappear:animated];
  189. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
  190. }
  191. - (void)updateTime {
  192. count --;
  193. [self changeCode];
  194. if (count <= 0) {
  195. self.sendCodeBtn.userInteractionEnabled = YES;
  196. [self.timer invalidate];
  197. self.timer = nil;
  198. }
  199. }
  200. @end