| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- //
- // LoginViewController.m
- // 千模
- //
- // Created by MUMEI on 2018/5/18.
- // Copyright © 2018年 MUMEI. All rights reserved.
- //
- #import "LoginViewController.h"
- #import "RegisterViewController.h"
- #import "RootNavigaViewController.h"
- #import "ModelRootViewController.h"
- #import "ForgetViewController.h"
- #import "AppDelegate.h"
- @interface LoginViewController () <UITextFieldDelegate>
- @property(strong, nonatomic) IBOutlet UITextField *usernameTextField;
- @property(strong, nonatomic) IBOutlet UITextField *passwordTextField;
- @property(weak, nonatomic) IBOutlet UIImageView *loginBtn;
- @property(weak, nonatomic) IBOutlet UILabel *wxLoginLabel;
- @property(weak, nonatomic) IBOutlet UILabel *forgetLabel;
- @property(weak, nonatomic) IBOutlet UIButton *wxLoginBtn;
- @property(weak, nonatomic) IBOutlet UILabel *toRegister;
- @property(weak, nonatomic) IBOutlet NSLayoutConstraint *bottom;
- @end
- @implementation LoginViewController
- - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- _passwordTextField.returnKeyType = UIReturnKeyDone;
- //检查网络情况
- [self checkNetwork];
- _loginBtn.userInteractionEnabled = YES;
- UITapGestureRecognizer *loginTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doLogin)];;
- [_loginBtn addGestureRecognizer:loginTap];
- _toRegister.userInteractionEnabled = YES;
- UITapGestureRecognizer *toRegister = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toRegisterVc:)];
- [_toRegister addGestureRecognizer:toRegister];
- _wxLoginLabel.userInteractionEnabled = YES;
- UITapGestureRecognizer *wxLogin = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(towxLogin:)];
- [_wxLoginLabel addGestureRecognizer:wxLogin];
- [_wxLoginBtn addTarget:self action:@selector(towxLogin:) forControlEvents:UIControlEventTouchUpInside];
- UITapGestureRecognizer *forget = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toForget:)];
- [_forgetLabel addGestureRecognizer:forget];
- _forgetLabel.userInteractionEnabled = YES;
- }
- //检查网络情况
- - (void)checkNetwork {
- // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
- [[AFNetworkReachabilityManager sharedManager] startMonitoring];
- __block BOOL network = network; //
- __block BOOL change = change; //
- change = NO;
- network = NO;
- // 检测网络连接的单例,网络变化时的回调方法
- [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
- switch (status) {
- case AFNetworkReachabilityStatusNotReachable: {
- NSLog(@"无网络");
- network = NO;
- change = YES;
- [self initAlertView];
- break;
- }
- case AFNetworkReachabilityStatusReachableViaWiFi: {
- NSLog(@"WiFi网络");
- network = YES;
- change = YES;
- break;
- }
- case AFNetworkReachabilityStatusReachableViaWWAN: {
- NSLog(@"无线网络");
- network = YES;
- change = YES;
- break;
- }
- default:
- break;
- }
- }];
- }
- - (void)initAlertView {
- NSString *info;
- info = @"网络异常!\n网络不给力,请检查网络";
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:info preferredStyle:UIAlertControllerStyleAlert];
- NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:info];
- int num = (int) hogan.length;
- [hogan addAttribute:NSFontAttributeName
- value:[UIFont systemFontOfSize:16.0]
- range:NSMakeRange(num - 11, 11)];
- [hogan addAttribute:NSFontAttributeName
- value:[UIFont systemFontOfSize:18.0]
- range:NSMakeRange(0, num - 11)];
- [alertController setValue:hogan forKey:@"attributedMessage"];
- UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- [self checkNetwork];
- }];
- [alertController addAction:confirmAction];
- [self presentViewController:alertController animated:YES completion:^{
- }];
- }
- - (void)toForget:(UITapGestureRecognizer *)recognizer {
- AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
- ForgetViewController *rVc = [[ForgetViewController alloc] init];
- RootNavigaViewController *nvc = [[RootNavigaViewController alloc] initWithRootViewController:rVc];
- delegate.window.rootViewController = nvc;
- }
- - (void)doLogin {
- [MBProgressHUD showHUDAddedTo:self.view animated:YES];
- NSDictionary *dic = @{@"mobile": self.usernameTextField.text, @"pass": self.passwordTextField.text};
- [YanCNetWorkManager requestPostWithURLStr:Url_doLogin(PublicUrl) parameters:dic finish:^(id dataDic) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- NSString *issuccess = dataDic[@"msg"];
- if ([issuccess isEqualToString:@"success"]) {
- [[[NIMSDK sharedSDK] loginManager] login:dataDic[@"data"][@"pk"]
- token:dataDic[@"data"][@"token"]
- completion:^(NSError *error) {
- if (error == nil) {
- NSString *token = dataDic[@"data"][@"token"];
- NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
- [userDefaults setObject:self.usernameTextField.text forKey:@"account"];
- [userDefaults setObject:self.passwordTextField.text forKey:@"pwd"];
- [userDefaults setObject:dataDic[@"data"][@"pk"] forKey:@"accid"];
- [userDefaults setObject:token forKey:@"token"];
- [userDefaults setObject:dataDic[@"data"][@"qrcode"] forKey:@"qrcode"];
- [userDefaults synchronize];
- ModelUser *user = [ModelUser modelUser];
- [user setValuesForKeysWithDictionary:dataDic[@"data"]];
- user.modelpk = dataDic[@"data"][@"modelpk"];
- user.coin_a = dataDic[@"data"][@"coin_a"];
- user.coin_ir = dataDic[@"data"][@"coin_ir"];
- user.pk = dataDic[@"data"][@"pk"];
- user.token = token;
- NSLog(@"----%@", user.pet);
- [ModelUser save:user];
- AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
- delegate.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
- // 创建并初始化UITabBarController
- ModelRootViewController *mineVc = [[ModelRootViewController alloc] init];
- delegate.window.rootViewController = mineVc;
- delegate.window.backgroundColor = [UIColor whiteColor];
- [delegate.window makeKeyAndVisible];
- }
- }
- ];
- } else if ([dataDic[@"msg"] isEqualToString:@"fail"]) {
- NSString *str = dataDic[@"desc"];
- [MBProgressHUD showTextHUD:str inView:self.view hideAfterDelay:1];
- } else {
- [MBProgressHUD showTextHUD:@"登陆失败" inView:self.view hideAfterDelay:1];
- }
- } enError:^(NSError *error) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- }];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.passwordTextField.secureTextEntry = YES;
- self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
- self.passwordTextField.delegate = self;
- self.usernameTextField.delegate = self;
- if (self.registerPwd.length != 0 && self.registerAccount.length != 0) {
- self.passwordTextField.text = [NSString stringWithFormat:@"%@", self.registerPwd];
- self.usernameTextField.text = [NSString stringWithFormat:@"%@", self.registerAccount];
- }
- [self.navigationController setNavigationBarHidden:YES animated:YES];
- }
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- if (![self.view isExclusiveTouch]) {
- [_usernameTextField resignFirstResponder];
- [_passwordTextField resignFirstResponder];
- }
- }
- - (void)toRegisterVc:(UITapGestureRecognizer *)recognizer {
- AppDelegate *delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
- RegisterViewController *rVc = [[RegisterViewController alloc] init];
- RootNavigaViewController *nvc = [[RootNavigaViewController alloc] initWithRootViewController:rVc];
- delegate.window.rootViewController = nvc;
- }
- - (void)towxLogin:(UITapGestureRecognizer *)recognizer {
- NSLog(@"wx");
- }
- #pragma mark - Notification
- - (void)keyboardWillShow:(NSNotification *)notification {
- NSDictionary *userInfo = [notification userInfo];
- NSTimeInterval animationDuration;
- UIViewAnimationCurve animationCurve;
- CGRect keyboardFrame;
- [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
- [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
- [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:animationDuration];
- [UIView setAnimationCurve:animationCurve];
- [UIView commitAnimations];
- }
- #pragma mark - UITextFieldDelegate
- - (void)textFieldDidChange:(NSNotification *)notification {
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField {
- NSTimeInterval animationDuration = 0.30f;
- [UIView beginAnimations:@ "ResizeForKeyboard" context:nil];
- [UIView setAnimationDuration:animationDuration];
- //将视图的Y坐标向上移动,以使下面腾出地方用于软键盘的显示
- if (textField == self.passwordTextField) {
- self.view.frame = CGRectMake(0.0f, -140.0f/*屏幕上移的高度,可以自己定*/, self.view.frame.size.width, self.view.frame.size.height);
- } else {
- self.view.frame = CGRectMake(0.0f, -100.0f/*屏幕上移的高度,可以自己定*/, self.view.frame.size.width, self.view.frame.size.height);
- }
- [UIView commitAnimations];
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField {
- //滑动效果
- NSTimeInterval animationDuration = 0.30f;
- [UIView beginAnimations:@ "ResizeForKeyboard" context:nil];
- [UIView setAnimationDuration:animationDuration];
- //恢复屏幕
- self.view.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
- [UIView commitAnimations];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|