RDSLoginVerifyCodeVC.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //
  2. // RDSLoginVerifyCodeVC.m
  3. // SmartLightForBigFish
  4. //
  5. // Created by coderYK on 2017/11/17.
  6. // Copyright © 2017年 RD. All rights reserved.
  7. //
  8. #import "RDSLoginVerifyCodeVC.h"
  9. //#import "WLCaptcheButton.h"
  10. #import "RDSUserProtocolViewController.h"
  11. #import "NSString+verify.h"
  12. #import "UIView+RDSClip.h"
  13. #import "UIImage+Extension.h"
  14. #import "RDSRootControl.h"
  15. #import "RDSLoginPwdCodeVC.h"
  16. #import "RDCheckAppVersion.h"
  17. #import "YXAlertView.h"
  18. #import "RDSControlWebVC.h"
  19. @interface RDSLoginVerifyCodeVC ()<UITextFieldDelegate>
  20. @property (weak, nonatomic) IBOutlet UIButton *backBtn;
  21. @property (weak, nonatomic) IBOutlet UIButton *loginBtn;
  22. @property (weak, nonatomic) IBOutlet UIView *phoneBgView;
  23. @property (weak, nonatomic) IBOutlet UIView *codeBgView;
  24. @property (weak, nonatomic) IBOutlet UITextField *phoneTF;
  25. @property (weak, nonatomic) IBOutlet UITextField *verifyCodeTF;
  26. @property (weak, nonatomic) IBOutlet UIButton *verifyCodeBtn;
  27. @property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
  28. @property (weak, nonatomic) IBOutlet UIButton *userAgreement;
  29. @property (weak, nonatomic) IBOutlet UIButton *privacyPolicyBtn;
  30. @end
  31. @implementation RDSLoginVerifyCodeVC
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // 隐藏导航栏
  35. self.fd_prefersNavigationBarHidden = YES;
  36. self.view.backgroundColor = [UIColor whiteColor];
  37. [self.backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
  38. self.phoneBgView.layer.cornerRadius = 22.5;
  39. self.phoneBgView.layer.borderWidth = 1.0;
  40. self.phoneBgView.layer.borderColor = [UIColor whiteColor].CGColor;
  41. self.codeBgView.layer.cornerRadius = 22.5;
  42. self.codeBgView.layer.borderWidth = 1.0;
  43. self.codeBgView.layer.borderColor = [UIColor whiteColor].CGColor;
  44. NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes: @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
  45. self.phoneTF.attributedPlaceholder = attrString;
  46. self.phoneTF.text = TheDataManager.phone;
  47. NSAttributedString *attrString2 = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes: @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
  48. self.verifyCodeTF.attributedPlaceholder = attrString2;
  49. [self p_setupTF];
  50. // 检查更新
  51. // [TheCheckAppVersion checkUpdate];
  52. // 这是rootControl 自动登录时会先进入这个页面,所有先隐藏
  53. if(!self.isPushVc){
  54. self.view.alpha = 0;
  55. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  56. self.view.alpha = 1;
  57. });
  58. }
  59. }
  60. - (void)viewDidLayoutSubviews{
  61. [super viewDidLayoutSubviews];
  62. _verifyCodeBtn.layer.cornerRadius = _verifyCodeBtn.rds_height/2;
  63. }
  64. - (void)viewWillAppear:(BOOL)animated{
  65. [super viewWillAppear:animated];
  66. _agreeBtn.selected = NO;
  67. [self addAlertView];
  68. }
  69. -(void)addAlertView
  70. {
  71. NSString *state = [[NSUserDefaults standardUserDefaults] objectForKey:@"isShowAgreement"];
  72. if ([state intValue] == 0) {
  73. YXAlertView *alertView = [[YXAlertView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  74. // UIWindow *window = [UIApplication sharedApplication].keyWindow;
  75. [self.view addSubview:alertView];
  76. alertView.cancleBtnClick = ^(YXAlertView * _Nonnull alertView) {
  77. [alertView removeFromSuperview];
  78. };
  79. alertView.sureBtnClick = ^(YXAlertView * _Nonnull alertView) {
  80. [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isShowAgreement"];
  81. [alertView removeFromSuperview];
  82. };
  83. alertView.contentClick = ^(YXAlertView * _Nonnull view, NSString * _Nonnull urlStr) {
  84. RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init];
  85. webVc.requestURL = urlStr;
  86. webVc.hidesBottomBarWhenPushed = YES;
  87. [self.navigationController pushViewController:webVc animated:YES];
  88. };
  89. }
  90. }
  91. -(void)backAction:(UIButton *)btn
  92. {
  93. [self.navigationController popViewControllerAnimated:YES];
  94. }
  95. #pragma mark - Action
  96. - (IBAction)p_onPwdLogin {
  97. if(_isPushVc){
  98. [self popViewControllerAnimated:YES];
  99. }else{
  100. RDSLoginPwdCodeVC *vc = [[RDSLoginPwdCodeVC alloc] init];
  101. vc.isPushVc = YES;
  102. [self pushViewController:vc animated:YES];
  103. }
  104. }
  105. // 登录
  106. - (IBAction)p_onLogin {
  107. if (![self.phoneTF.text isPhoneNum]) {
  108. [RDSHudShower showBottomToast:@"请输入正确的手机号码"];
  109. return;
  110. }
  111. else if (self.verifyCodeTF.text.length < 1){
  112. [RDSHudShower showBottomToast:@"请输入验证码"];
  113. return;
  114. }
  115. else if (!self.agreeBtn.isSelected){
  116. [RDSHudShower showBottomToast:@"请先阅读并同意用户协议"];
  117. return;
  118. }
  119. [RDSHudShower showWithStatus:@"正在登录账户…" autoDismiss:NO];
  120. RDS_WEAKSELF(weakSelf)
  121. [RDSDemoApiHelper rds_loginWithVerifyCodePhone:self.phoneTF.text code:self.verifyCodeTF.text success:^(id responseObject) {
  122. if ([responseObject[@"code"] intValue] != 0) {
  123. [RDSHudShower showBottomToast:responseObject[@"message"]];
  124. }else{
  125. NSDictionary *data = responseObject[@"data"];
  126. NSString *access_token = data[@"access_token"];
  127. NSString *token_type = data[@"token_type"];
  128. if (access_token && token_type) {
  129. TheDataManager.token = [NSString stringWithFormat:@"%@ %@",token_type,access_token];
  130. }else{
  131. [RDSHudShower showBottomToast:@"系统错误"];
  132. return;
  133. }
  134. TheDataManager.isFirstLogin = [data[@"app_first_login"] boolValue];
  135. [weakSelf p_appInit];
  136. [RDSHudShower dismissHUD];
  137. [RDSRootControl shareControl].isLoginSuccess = YES;
  138. }
  139. } failure:^(NSError *error) {
  140. [RDSHudShower showBottomToast:@"连接服务器失败"];
  141. }];
  142. }
  143. -(void)p_appInit
  144. {
  145. [RDSDemoApiHelper rds_appInitWithType:@2 success:^(id responseObject) {
  146. } failure:^(NSError *error) {
  147. }];
  148. }
  149. - (IBAction)getVerifyCode:(UIButton *)verifyCodeBtn {
  150. if (![self.phoneTF.text isPhoneNum]) {
  151. [RDSHudShower showCenterToast:@"请输入正确的手机号码"];
  152. return;
  153. }
  154. //TheDataManager.phone = self.phoneTF.text;
  155. _verifyCodeTF.text = @"";
  156. [_verifyCodeTF becomeFirstResponder];
  157. [RDSHudShower showWithStatus:@"正在获取验证码…" autoDismiss:YES];
  158. RDS_WEAKSELF(weakSelf)
  159. [RDSDemoApiHelper rds_getVerifyCodePhone:self.phoneTF.text type:1 success:^(id responseObject) {
  160. [weakSelf timerButton:weakSelf.verifyCodeBtn];
  161. if ([responseObject[@"code"] intValue] != 0) {
  162. [RDSHudShower showBottomToast:responseObject[@"message"]];
  163. }
  164. else{
  165. [RDSHudShower showBottomToast:@"获取验证码成功"];
  166. }
  167. } failure:^(NSError *error) {
  168. [RDSHudShower showBottomToast:@"连接服务器失败"];
  169. DDLog(@"json data error:/n%@", error.description);
  170. }];
  171. [self.verifyCodeTF becomeFirstResponder];
  172. }
  173. #pragma - mark 倒计时
  174. -(void)timerButton:(UIButton *)button
  175. {
  176. __block int timeout = 60; //倒计时时间
  177. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  178. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  179. dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  180. dispatch_source_set_event_handler(_timer, ^{
  181. if(timeout <= 0) //倒计时结束,关闭
  182. {
  183. dispatch_source_cancel(_timer);
  184. dispatch_async(dispatch_get_main_queue(), ^{
  185. //设置界面的按钮显示 根据自己需求设置
  186. button.userInteractionEnabled = YES;
  187. [button setTitle:@"获取验证码" forState:UIControlStateNormal];
  188. });
  189. } else {
  190. NSString *strTime = [NSString stringWithFormat:@"%ds重新获取",timeout];
  191. dispatch_async(dispatch_get_main_queue(), ^{
  192. //设置界面的按钮显示 根据自己需求设置
  193. [button setTitle:strTime forState:UIControlStateNormal];
  194. button.userInteractionEnabled = NO;
  195. });
  196. timeout--;
  197. }
  198. });
  199. dispatch_resume(_timer);
  200. }
  201. // 阅读并同意
  202. - (IBAction)p_onAgreeBtnClick:(UIButton *)sender {
  203. sender.selected = !sender.selected;
  204. }
  205. // 点击用户协议
  206. - (IBAction)p_onUserAgreementClick {
  207. // RDSUserProtocolViewController *userProtocolVC = [[RDSUserProtocolViewController alloc] init];
  208. // userProtocolVC.url = kUserAgreement;
  209. // userProtocolVC.vcTitle = @"用户协议";
  210. // [self pushViewController:userProtocolVC animated:YES];
  211. RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init];
  212. webVc.requestURL = kUserAgreement;
  213. webVc.hidesBottomBarWhenPushed = YES;
  214. [self.navigationController pushViewController:webVc animated:YES];
  215. }
  216. // 点击隐私政策
  217. - (IBAction)p_onPrivacyPolicyClick {
  218. // RDSUserProtocolViewController *userProtocolVC = [[RDSUserProtocolViewController alloc] init];
  219. // userProtocolVC.url = kPrivacyPolicy;
  220. // userProtocolVC.vcTitle = @"隐私政策";
  221. // [self pushViewController:userProtocolVC animated:YES];
  222. RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init];
  223. webVc.requestURL = kPrivacyPolicy;
  224. webVc.hidesBottomBarWhenPushed = YES;
  225. [self.navigationController pushViewController:webVc animated:YES];
  226. }
  227. #pragma mark - UITextFieldDelegate
  228. - (void)p_setupTF {
  229. self.phoneTF.delegate = self;
  230. self.verifyCodeTF.delegate = self;
  231. }
  232. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  233. if (textField == self.phoneTF) {
  234. [self.verifyCodeTF becomeFirstResponder];
  235. }
  236. else if (textField == self.verifyCodeTF) {
  237. [self.verifyCodeTF becomeFirstResponder];
  238. }
  239. else {
  240. [self p_onLogin];
  241. }
  242. return YES;
  243. }
  244. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  245. [self.view endEditing:YES];
  246. }
  247. - (void)didReceiveMemoryWarning {
  248. [super didReceiveMemoryWarning];
  249. // Dispose of any resources that can be recreated.
  250. }
  251. - (void)dealloc{
  252. DDLog(@"dealloc~~");
  253. }
  254. @end