RDSLoginPwdCodeVC.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. [self pushViewController:userProtocolVC animated:YES];
  141. }
  142. - (IBAction)p_onPrivacyPolicyClick:(id)sender {
  143. RDSUserProtocolViewController *userProtocolVC = [[RDSUserProtocolViewController alloc] init];
  144. userProtocolVC.url = kPrivacyPolicy;
  145. [self pushViewController:userProtocolVC animated:YES];
  146. }
  147. // 按钮选中时 显示密码
  148. - (IBAction)p_onPwdShowBtnClick:(UIButton *)sender {
  149. _pwdTF.secureTextEntry = sender.selected;
  150. sender.selected = !sender.selected;
  151. }
  152. // 继续登录操作
  153. - (void)p_onLoginSuccess {
  154. // 处理登录成功
  155. [RDSHudShower dismissHUD];
  156. //[Bugly setUserIdentifier:TheDataManager.user.telphone];
  157. [self p_handleUserInfo];
  158. [self p_changRootViewController];
  159. }
  160. - (void)p_handleUserInfo {
  161. // [kUserDefaults setObject:self.accountTF.text forKey:RDSUserNameUserDefaultKey];
  162. // NSString *pwdKey = [NSString stringWithFormat:@"%@%@",RDSPasswordUserDefaultKey, self.accountTF.text];
  163. // [RDSCacheHelper rds_cacheStringWithEncrypt:self.pwdTF.text forKey:pwdKey];
  164. }
  165. - (void)p_changRootViewController {
  166. [RDSRootControl shareControl].isLoginSuccess = YES; // 设置状态自动切换根控制器
  167. }
  168. - (void)p_alertError:(NSString *)error {
  169. [RDSHudShower dismissHUD];
  170. [self rds_alertMessage:error];
  171. }
  172. #pragma mark - UITextFieldDelegate
  173. - (void)p_setupTF {
  174. self.accountTF.delegate = self;
  175. self.pwdTF.delegate = self;
  176. }
  177. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  178. if (textField == self.accountTF) {
  179. [self.pwdTF becomeFirstResponder];
  180. }
  181. else {
  182. [self.pwdTF resignFirstResponder];
  183. [self p_onLogin];
  184. }
  185. return YES;
  186. }
  187. // 密文输入时,点击光标不清空
  188. //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  189. //
  190. // NSString *updatedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  191. //
  192. // textField.text = updatedString;
  193. //
  194. // return NO;
  195. //}
  196. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  197. [self.view endEditing:YES];
  198. }
  199. #pragma mark - UI
  200. - (void)p_initUI {
  201. [self p_setupTF];
  202. // [self p_configServerType];
  203. _accountTF.placeholder = @"请输入手机号码";
  204. _pwdTF.placeholder = @"请输入您的密码";
  205. [_registerBtn setTitle:@"注册账户" forState:UIControlStateNormal];
  206. [_resetPwdBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
  207. [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
  208. }
  209. - (void)p_textFill {
  210. _accountTF.text = TheDataManager.phone;
  211. if(!kNULLString(TheDataManager.pwd)){
  212. _pwdTF.text = TheDataManager.pwd;
  213. }
  214. }
  215. - (void)p_addObserver{
  216. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  217. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  218. }
  219. - (void)keyboardWillShow:(NSNotification *)notification
  220. {
  221. //获取处于焦点中的view
  222. NSArray *textFields = @[self.accountTF,self.pwdTF];
  223. UIView *focusView = nil;
  224. for (UITextField *view in textFields) {
  225. if ([view isFirstResponder]) {
  226. focusView = view;
  227. break;
  228. }
  229. }
  230. RDS_WEAKSELF(weakSelf)
  231. if (focusView) {
  232. //获取键盘弹出的时间
  233. double duration = [notification.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  234. //获取键盘上端Y坐标
  235. CGFloat keyboardY = [notification.userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
  236. //获取输入框下端相对于window的Y坐标
  237. CGRect rect = [focusView convertRect:focusView.bounds toView:[[[UIApplication sharedApplication] delegate] window]];
  238. CGPoint tmp = rect.origin;
  239. CGFloat inputBoxY = tmp.y + focusView.frame.size.height;
  240. //计算二者差值
  241. CGFloat ty = keyboardY - inputBoxY;
  242. NSLog(@"position keyboard: %f, inputbox: %f, ty: %f", keyboardY, inputBoxY, ty);
  243. //差值小于0,做平移变换
  244. [UIView animateWithDuration:duration animations:^{
  245. if (ty < 0) {
  246. weakSelf.view.transform = CGAffineTransformMakeTranslation(0, ty);
  247. }
  248. }];
  249. }
  250. }
  251. - (void)keyboardWillHide:(NSNotification *)notification
  252. {
  253. //获取键盘弹出的时间
  254. double duration = [notification.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  255. //还原
  256. RDS_WEAKSELF(weakSelf)
  257. [UIView animateWithDuration:duration animations:^{
  258. weakSelf.view.transform = CGAffineTransformMakeTranslation(0, 0);
  259. }];
  260. }
  261. - (void)dealloc {
  262. DDLog(@"dealloc");
  263. [kNotificationCenter removeObserver:self];
  264. }
  265. - (void)didReceiveMemoryWarning {
  266. [super didReceiveMemoryWarning];
  267. // Dispose of any resources that can be recreated.
  268. }
  269. @end