LoginViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // LoginViewController.m
  3. // 千模
  4. //
  5. // Created by MUMEI on 2018/5/18.
  6. // Copyright © 2018年 MUMEI. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "RegisterViewController.h"
  10. #import "RootNavigaViewController.h"
  11. #import "ModelRootViewController.h"
  12. #import "ForgetViewController.h"
  13. #import "AppDelegate.h"
  14. @interface LoginViewController () <UITextFieldDelegate>
  15. @property(strong, nonatomic) IBOutlet UITextField *usernameTextField;
  16. @property(strong, nonatomic) IBOutlet UITextField *passwordTextField;
  17. @property(weak, nonatomic) IBOutlet UIImageView *loginBtn;
  18. @property(weak, nonatomic) IBOutlet UILabel *wxLoginLabel;
  19. @property(weak, nonatomic) IBOutlet UILabel *forgetLabel;
  20. @property(weak, nonatomic) IBOutlet UIButton *wxLoginBtn;
  21. @property(weak, nonatomic) IBOutlet UILabel *toRegister;
  22. @property(weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;
  23. @end
  24. @implementation LoginViewController
  25. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  26. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  27. if (self) {
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
  30. }
  31. return self;
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _passwordTextField.returnKeyType = UIReturnKeyDone;
  36. //检查网络情况
  37. [self checkNetwork];
  38. _loginBtn.userInteractionEnabled = YES;
  39. UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doLogin)];;
  40. [_loginBtn addGestureRecognizer:loginTap];
  41. _toRegister.userInteractionEnabled = YES;
  42. UITapGestureRecognizer *toRegister = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toRegisterVc:)];
  43. [_toRegister addGestureRecognizer:toRegister];
  44. _wxLoginLabel.userInteractionEnabled = YES;
  45. UITapGestureRecognizer *wxLogin = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(towxLogin:)];
  46. [_wxLoginLabel addGestureRecognizer:wxLogin];
  47. [_wxLoginBtn addTarget:self action:@selector(towxLogin:) forControlEvents:UIControlEventTouchUpInside];
  48. UITapGestureRecognizer *forget = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toForget:)];
  49. [_forgetLabel addGestureRecognizer:forget];
  50. _forgetLabel.userInteractionEnabled = YES;
  51. }
  52. //检查网络情况
  53. - (void)checkNetwork {
  54. // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
  55. [[AFNetworkReachabilityManager sharedManager] startMonitoring];
  56. __block BOOL network = network; //
  57. __block BOOL change = change; //
  58. change = NO;
  59. network = NO;
  60. // 检测网络连接的单例,网络变化时的回调方法
  61. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  62. switch (status) {
  63. case AFNetworkReachabilityStatusNotReachable: {
  64. NSLog(@"无网络");
  65. network = NO;
  66. change = YES;
  67. [self initAlertView];
  68. break;
  69. }
  70. case AFNetworkReachabilityStatusReachableViaWiFi: {
  71. NSLog(@"WiFi网络");
  72. network = YES;
  73. change = YES;
  74. break;
  75. }
  76. case AFNetworkReachabilityStatusReachableViaWWAN: {
  77. NSLog(@"无线网络");
  78. network = YES;
  79. change = YES;
  80. break;
  81. }
  82. default:
  83. break;
  84. }
  85. }];
  86. }
  87. - (void)initAlertView {
  88. NSString *info;
  89. info = @"网络异常!\n网络不给力,请检查网络";
  90. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:info preferredStyle:UIAlertControllerStyleAlert];
  91. NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:info];
  92. int num = (int) hogan.length;
  93. [hogan addAttribute:NSFontAttributeName
  94. value:[UIFont systemFontOfSize:16.0]
  95. range:NSMakeRange(num - 11, 11)];
  96. [hogan addAttribute:NSFontAttributeName
  97. value:[UIFont systemFontOfSize:18.0]
  98. range:NSMakeRange(0, num - 11)];
  99. [alertController setValue:hogan forKey:@"attributedMessage"];
  100. UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  101. [self checkNetwork];
  102. }];
  103. [alertController addAction:confirmAction];
  104. [self presentViewController:alertController animated:YES completion:^{
  105. }];
  106. }
  107. - (void)toForget:(UITapGestureRecognizer *)recognizer {
  108. AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
  109. ForgetViewController *rVc = [[ForgetViewController alloc] init];
  110. RootNavigaViewController *nvc = [[RootNavigaViewController alloc] initWithRootViewController:rVc];
  111. delegate.window.rootViewController = nvc;
  112. }
  113. - (void)doLogin {
  114. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  115. NSDictionary *dic = @{@"mobile": self.usernameTextField.text, @"pass": self.passwordTextField.text};
  116. [YanCNetWorkManager requestPostWithURLStr:Url_doLogin(PublicUrl) parameters:dic finish:^(id dataDic) {
  117. [MBProgressHUD hideHUDForView:self.view animated:YES];
  118. NSString *issuccess = dataDic[@"msg"];
  119. if ([issuccess isEqualToString:@"success"]) {
  120. [[[NIMSDK sharedSDK] loginManager] login:dataDic[@"data"][@"pk"]
  121. token:dataDic[@"data"][@"token"]
  122. completion:^(NSError *error) {
  123. if (error == nil) {
  124. NSString *token = dataDic[@"data"][@"token"];
  125. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  126. [userDefaults setObject:self.usernameTextField.text forKey:@"account"];
  127. [userDefaults setObject:self.passwordTextField.text forKey:@"pwd"];
  128. [userDefaults setObject:dataDic[@"data"][@"pk"] forKey:@"accid"];
  129. [userDefaults setObject:token forKey:@"token"];
  130. [userDefaults setObject:dataDic[@"data"][@"qrcode"] forKey:@"qrcode"];
  131. [userDefaults synchronize];
  132. ModelUser *user = [ModelUser modelUser];
  133. [user setValuesForKeysWithDictionary:dataDic[@"data"]];
  134. user.modelpk = dataDic[@"data"][@"modelpk"];
  135. user.coin_a = dataDic[@"data"][@"coin_a"];
  136. user.coin_ir = dataDic[@"data"][@"coin_ir"];
  137. user.pk = dataDic[@"data"][@"pk"];
  138. user.token = token;
  139. NSLog(@"----%@", user.pet);
  140. [ModelUser save:user];
  141. AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
  142. delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  143. // 创建并初始化UITabBarController
  144. ModelRootViewController *mineVc = [[ModelRootViewController alloc] init];
  145. delegate.window.rootViewController = mineVc;
  146. delegate.window.backgroundColor = [UIColor whiteColor];
  147. [delegate.window makeKeyAndVisible];
  148. }
  149. }
  150. ];
  151. } else if ([dataDic[@"msg"] isEqualToString:@"fail"]) {
  152. NSString *str = dataDic[@"desc"];
  153. [MBProgressHUD showTextHUD:str inView:self.view hideAfterDelay:1];
  154. } else {
  155. [MBProgressHUD showTextHUD:@"登陆失败" inView:self.view hideAfterDelay:1];
  156. }
  157. } enError:^(NSError *error) {
  158. [MBProgressHUD hideHUDForView:self.view animated:YES];
  159. }];
  160. }
  161. - (void)viewWillAppear:(BOOL)animated {
  162. [super viewWillAppear:animated];
  163. self.passwordTextField.secureTextEntry = YES;
  164. self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
  165. self.passwordTextField.delegate = self;
  166. self.usernameTextField.delegate = self;
  167. if (self.registerPwd.length != 0 && self.registerAccount.length != 0) {
  168. self.passwordTextField.text = [NSString stringWithFormat:@"%@", self.registerPwd];
  169. self.usernameTextField.text = [NSString stringWithFormat:@"%@", self.registerAccount];
  170. }
  171. [self.navigationController setNavigationBarHidden:YES animated:YES];
  172. }
  173. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  174. if (![self.view isExclusiveTouch]) {
  175. [_usernameTextField resignFirstResponder];
  176. [_passwordTextField resignFirstResponder];
  177. }
  178. }
  179. - (void)toRegisterVc:(UITapGestureRecognizer *)recognizer {
  180. AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
  181. RegisterViewController *rVc = [[RegisterViewController alloc] init];
  182. RootNavigaViewController *nvc = [[RootNavigaViewController alloc] initWithRootViewController:rVc];
  183. delegate.window.rootViewController = nvc;
  184. }
  185. - (void)towxLogin:(UITapGestureRecognizer *)recognizer {
  186. NSLog(@"wx");
  187. }
  188. #pragma mark - Notification
  189. - (void)keyboardWillShow:(NSNotification *)notification {
  190. NSDictionary *userInfo = [notification userInfo];
  191. NSTimeInterval animationDuration;
  192. UIViewAnimationCurve animationCurve;
  193. CGRect keyboardFrame;
  194. [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
  195. [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
  196. [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
  197. [UIView beginAnimations:nil context:nil];
  198. [UIView setAnimationDuration:animationDuration];
  199. [UIView setAnimationCurve:animationCurve];
  200. [UIView commitAnimations];
  201. }
  202. #pragma mark - UITextFieldDelegate
  203. - (void)textFieldDidChange:(NSNotification *)notification {
  204. }
  205. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  206. NSTimeInterval animationDuration = 0.30f;
  207. [UIView beginAnimations:@ "ResizeForKeyboard" context:nil];
  208. [UIView setAnimationDuration:animationDuration];
  209. //将视图的Y坐标向上移动,以使下面腾出地方用于软键盘的显示
  210. if (textField == self.passwordTextField) {
  211. self.view.frame = CGRectMake(0.0f, -140.0f/*屏幕上移的高度,可以自己定*/, self.view.frame.size.width, self.view.frame.size.height);
  212. } else {
  213. self.view.frame = CGRectMake(0.0f, -100.0f/*屏幕上移的高度,可以自己定*/, self.view.frame.size.width, self.view.frame.size.height);
  214. }
  215. [UIView commitAnimations];
  216. }
  217. - (void)textFieldDidEndEditing:(UITextField *)textField {
  218. //滑动效果
  219. NSTimeInterval animationDuration = 0.30f;
  220. [UIView beginAnimations:@ "ResizeForKeyboard" context:nil];
  221. [UIView setAnimationDuration:animationDuration];
  222. //恢复屏幕
  223. self.view.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
  224. [UIView commitAnimations];
  225. }
  226. - (void)didReceiveMemoryWarning {
  227. [super didReceiveMemoryWarning];
  228. // Dispose of any resources that can be recreated.
  229. }
  230. @end