RDSLoginPwdCodeVC.m 9.8 KB

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