RDSLoginPwdCodeVC.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //
  2. // RDSLoginPwdCodeVC.m
  3. // SmartLightForBigFish
  4. //
  5. // Created by coderYK on 2017/11/17.
  6. // Copyright © 2017年 RD. All rights reserved.
  7. //
  8. #import "RDSLoginPwdCodeVC.h"
  9. #import "RDSLoginVerifyCodeVC.h"
  10. #import "RDSResetPwdVC.h"
  11. #import "RDSRootControl.h"
  12. #import "YJJTextField.h"
  13. #import "UIView+RDSClip.h"
  14. #import "NSString+Hash.h"// md5
  15. #import "NSString+verify.h"
  16. #import "RDSSetPwdVC.h"
  17. #import "RDSResetPwdVC.h"
  18. #import "RDSUserProtocolViewController.h"
  19. #import <AuthenticationServices/AuthenticationServices.h>
  20. @interface RDSLoginPwdCodeVC ()<UITextFieldDelegate,ASAuthorizationControllerDelegate,ASAuthorizationControllerPresentationContextProviding>
  21. @property (weak, nonatomic) IBOutlet UILabel *welcomeLab;
  22. @property (weak, nonatomic) IBOutlet UIButton *logoBtn;
  23. @property (weak, nonatomic) IBOutlet UIButton *loginBtn;
  24. @property (weak, nonatomic) IBOutlet UIButton *registerBtn;
  25. @property (weak, nonatomic) IBOutlet UIButton *resetPwdBtn;
  26. @property (weak, nonatomic) IBOutlet UITextField *accountTF;
  27. @property (weak, nonatomic) IBOutlet UITextField *pwdTF;
  28. @property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
  29. @property (weak, nonatomic) IBOutlet UIView *bottomView;
  30. @property (weak, nonatomic) IBOutlet UIView *accountView;
  31. @property (weak, nonatomic) IBOutlet UIView *pwdView;
  32. @property (nonatomic, assign) int pwdClickCount;
  33. @end
  34. @implementation RDSLoginPwdCodeVC
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. self.fd_prefersNavigationBarHidden = YES;
  39. // 设置导航栏字体颜色 从这里跳转的VC
  40. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:RDSThemeColor,
  41. NSFontAttributeName:[UIFont systemFontOfSize:17]}];
  42. [self p_initUI];
  43. [self p_addObserver];
  44. // 这是rootControl 自动登录时会先进入这个页面,所有先隐藏
  45. if(!self.isPushVc){
  46. self.view.alpha = 0;
  47. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  48. self.view.alpha = 1;
  49. });
  50. }
  51. }
  52. - (void)viewWillAppear:(BOOL)animated{
  53. [super viewWillAppear:animated];
  54. [self p_textFill];
  55. _agreeBtn.selected = NO;
  56. _pwdClickCount = 0;
  57. }
  58. - (void)viewDidLayoutSubviews{
  59. [super viewDidLayoutSubviews];
  60. _bottomView.layer.cornerRadius = SCREEN_WIDTH*40;
  61. _accountView.layer.cornerRadius = _accountView.rds_height/2;
  62. _pwdView.layer.cornerRadius = _pwdView.rds_height/2;
  63. _loginBtn.layer.cornerRadius = _loginBtn.rds_height/2;
  64. // _loginBtn.layer.masksToBounds = YES;
  65. // [_loginBtn rds_addShadowAndCornerRadius:_loginBtn.rds_height/2 shadowOpacity:0.5f shadowRadius:2 shadowColor:RDSThemeColor];
  66. }
  67. #pragma mark - Action
  68. - (IBAction)p_onPwdBtnClick:(UIButton *)sender {
  69. _pwdClickCount ++;
  70. if(_pwdClickCount == 10){
  71. TheDataManager.isTest = !TheDataManager.isTest;
  72. NSString *msg;
  73. if(TheDataManager.isTest){
  74. msg = @"切换开发环境";
  75. }else{
  76. msg = @"切换生产环境";
  77. }
  78. [RDSHudShower showBottomToast:msg];
  79. DDLog(@"%@", msg);
  80. _pwdClickCount = 0;
  81. }
  82. }
  83. // 阅读同意
  84. - (IBAction)p_onAgreeBtnClick:(UIButton *)sender {
  85. sender.selected = !sender.isSelected;
  86. }
  87. // 跳转验证码登录
  88. - (IBAction)p_onVerifyCodeLogin {
  89. if(_isPushVc){
  90. [self popViewControllerAnimated:YES];
  91. }else{
  92. RDSLoginVerifyCodeVC *vc = [[RDSLoginVerifyCodeVC alloc] init];
  93. vc.isPushVc = YES;
  94. [self pushViewController:vc animated:YES];
  95. }
  96. }
  97. // 忘记密码
  98. - (IBAction)p_toForgetPwd {
  99. RDSResetPwdVC *resetPwdVC = [[RDSResetPwdVC alloc] init];
  100. [self pushViewController:resetPwdVC animated:YES];
  101. }
  102. // 登录
  103. - (IBAction)p_onLogin {
  104. NSString* userName = self.accountTF.text;
  105. NSString* password = self.pwdTF.text;
  106. if (![userName isPhoneNum]) {
  107. [RDSHudShower showBottomToast:@"请输入正确的手机号码"];
  108. return;
  109. }
  110. else if (password.length == 0){
  111. [RDSHudShower showBottomToast:@"请输入密码"];
  112. return;
  113. }
  114. else if (!self.agreeBtn.isSelected){
  115. [RDSHudShower showBottomToast:@"请先阅读并同意用户协议"];
  116. return;
  117. }
  118. // md5加密
  119. NSString *pwdMd5 = [password md5String];
  120. [RDSHudShower showWithStatus:@"正在登录中..." autoDismiss:NO];
  121. [RDSDemoApiHelper rds_loginWithPwdPhone:userName password:pwdMd5 success:^(id responseObject) {
  122. if ([responseObject[@"code"] intValue] != 0) {
  123. [RDSHudShower showBottomToast:responseObject[@"message"]];
  124. }
  125. else{
  126. [RDSHudShower dismissHUD];
  127. [RDSRootControl shareControl].isLoginSuccess = YES;
  128. NSDictionary *data = responseObject[@"data"];
  129. TheDataManager.token = [NSString stringWithFormat:@"%@ %@",data[@"token_type"], data[@"access_token"]];
  130. TheDataManager.isFirstLogin = [data[@"app_first_login"] boolValue];
  131. TheDataManager.pwd = password;
  132. }
  133. } failure:^(NSError *error) {
  134. [RDSHudShower showBottomToast:@"连接服务器失败"];
  135. }];
  136. }
  137. - (IBAction)p_onUserAgreementClick:(id)sender {
  138. RDSUserProtocolViewController *userProtocolVC = [[RDSUserProtocolViewController alloc] init];
  139. userProtocolVC.url = kUserAgreement;
  140. userProtocolVC.vcTitle = @"用户协议";
  141. [self pushViewController:userProtocolVC animated:YES];
  142. }
  143. - (IBAction)p_onPrivacyPolicyClick:(id)sender {
  144. RDSUserProtocolViewController *userProtocolVC = [[RDSUserProtocolViewController alloc] init];
  145. userProtocolVC.url = kPrivacyPolicy;
  146. userProtocolVC.vcTitle = @"隐私政策";
  147. [self pushViewController:userProtocolVC animated:YES];
  148. }
  149. // 按钮选中时 显示密码
  150. - (IBAction)p_onPwdShowBtnClick:(UIButton *)sender {
  151. _pwdTF.secureTextEntry = sender.selected;
  152. sender.selected = !sender.selected;
  153. }
  154. // 继续登录操作
  155. - (void)p_onLoginSuccess {
  156. // 处理登录成功
  157. [RDSHudShower dismissHUD];
  158. //[Bugly setUserIdentifier:TheDataManager.user.telphone];
  159. [self p_handleUserInfo];
  160. [self p_changRootViewController];
  161. }
  162. - (void)p_handleUserInfo {
  163. // [kUserDefaults setObject:self.accountTF.text forKey:RDSUserNameUserDefaultKey];
  164. // NSString *pwdKey = [NSString stringWithFormat:@"%@%@",RDSPasswordUserDefaultKey, self.accountTF.text];
  165. // [RDSCacheHelper rds_cacheStringWithEncrypt:self.pwdTF.text forKey:pwdKey];
  166. }
  167. - (void)p_changRootViewController {
  168. [RDSRootControl shareControl].isLoginSuccess = YES; // 设置状态自动切换根控制器
  169. }
  170. - (void)p_alertError:(NSString *)error {
  171. [RDSHudShower dismissHUD];
  172. [self rds_alertMessage:error];
  173. }
  174. #pragma mark - UITextFieldDelegate
  175. - (void)p_setupTF {
  176. self.accountTF.delegate = self;
  177. self.pwdTF.delegate = self;
  178. }
  179. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  180. if (textField == self.accountTF) {
  181. [self.pwdTF becomeFirstResponder];
  182. }
  183. else {
  184. [self.pwdTF resignFirstResponder];
  185. [self p_onLogin];
  186. }
  187. return YES;
  188. }
  189. // 密文输入时,点击光标不清空
  190. //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  191. //
  192. // NSString *updatedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  193. //
  194. // textField.text = updatedString;
  195. //
  196. // return NO;
  197. //}
  198. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  199. [self.view endEditing:YES];
  200. }
  201. #pragma mark - UI
  202. - (void)p_initUI {
  203. [self p_setupTF];
  204. // [self p_configServerType];
  205. _accountTF.placeholder = @"请输入手机号码";
  206. _pwdTF.placeholder = @"请输入您的密码";
  207. [_registerBtn setTitle:@"注册账户" forState:UIControlStateNormal];
  208. [_resetPwdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
  209. [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
  210. }
  211. - (void)p_textFill {
  212. _accountTF.text = TheDataManager.phone;
  213. if(!kNULLString(TheDataManager.pwd)){
  214. _pwdTF.text = TheDataManager.pwd;
  215. }
  216. }
  217. - (void)p_addObserver{
  218. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  219. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  220. }
  221. - (void)keyboardWillShow:(NSNotification *)notification
  222. {
  223. //获取处于焦点中的view
  224. NSArray *textFields = @[self.accountTF,self.pwdTF];
  225. UIView *focusView = nil;
  226. for (UITextField *view in textFields) {
  227. if ([view isFirstResponder]) {
  228. focusView = view;
  229. break;
  230. }
  231. }
  232. RDS_WEAKSELF(weakSelf)
  233. if (focusView) {
  234. //获取键盘弹出的时间
  235. double duration = [notification.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  236. //获取键盘上端Y坐标
  237. CGFloat keyboardY = [notification.userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
  238. //获取输入框下端相对于window的Y坐标
  239. CGRect rect = [focusView convertRect:focusView.bounds toView:[[[UIApplication sharedApplication] delegate] window]];
  240. CGPoint tmp = rect.origin;
  241. CGFloat inputBoxY = tmp.y + focusView.frame.size.height;
  242. //计算二者差值
  243. CGFloat ty = keyboardY - inputBoxY;
  244. NSLog(@"position keyboard: %f, inputbox: %f, ty: %f", keyboardY, inputBoxY, ty);
  245. //差值小于0,做平移变换
  246. [UIView animateWithDuration:duration animations:^{
  247. if (ty < 0) {
  248. weakSelf.view.transform = CGAffineTransformMakeTranslation(0, ty);
  249. }
  250. }];
  251. }
  252. }
  253. - (void)keyboardWillHide:(NSNotification *)notification
  254. {
  255. //获取键盘弹出的时间
  256. double duration = [notification.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  257. //还原
  258. RDS_WEAKSELF(weakSelf)
  259. [UIView animateWithDuration:duration animations:^{
  260. weakSelf.view.transform = CGAffineTransformMakeTranslation(0, 0);
  261. }];
  262. }
  263. - (void)dealloc {
  264. DDLog(@"dealloc");
  265. [kNotificationCenter removeObserver:self];
  266. }
  267. - (void)didReceiveMemoryWarning {
  268. [super didReceiveMemoryWarning];
  269. // Dispose of any resources that can be recreated.
  270. }
  271. @end