LoginViewController.mm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // LoginViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by XM on 2018/5/15.
  6. // Copyright © 2018年 XM. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "RegisterViewController.h"
  10. #import "UserAccountModel.h"
  11. @interface LoginViewController () <UserAccountModelDelegate>
  12. {
  13. UserAccountModel *accountModel; //账号相关功能接口管理器
  14. }
  15. @end
  16. @implementation LoginViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. //账号相关功能接口管理器
  20. accountModel = [[UserAccountModel alloc] init];
  21. accountModel.delegate = self;
  22. //实时检测用户名是否已经记录过
  23. [self.userNameTf addTarget:self action:@selector(onUserNameChanged) forControlEvents:UIControlEventEditingChanged];
  24. //设置导航栏
  25. [self setNaviStyle];
  26. }
  27. - (void)viewWillAppear:(BOOL)animated
  28. {
  29. //如果登录过,显示登录过的账号和密码
  30. NSString *userName = [[LoginShowControl getInstance] getLoginUserName];
  31. if (![userName isEqualToString:@""]) {
  32. self.userNameTf.text = userName;
  33. self.passwordTf.text = [[LoginShowControl getInstance] getLoginPassword];
  34. }
  35. // temp
  36. self.userNameTf.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"DemoUser"];
  37. self.passwordTf.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"DemoPassword"];
  38. }
  39. - (void)setNaviStyle {
  40. self.navigationItem.title = TS("Login_User");
  41. [self.navigationItem setHidesBackButton:YES];
  42. self.navigationItem.leftBarButtonItem = nil;
  43. }
  44. #pragma mark - 用户名变化
  45. -(void)onUserNameChanged{
  46. //获取本地保存的账户信息,如果用户名存在,则显示密码
  47. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  48. NSMutableDictionary *userInfoDic = [[defaults objectForKey:@"DemoUserInfo"] mutableCopy];
  49. if (!userInfoDic) {
  50. return;
  51. }
  52. if (![[userInfoDic allKeys] containsObject:@"DemoUserInfo"]) {
  53. return;
  54. }
  55. self.passwordTf.text = [userInfoDic objectForKey:self.userNameTf.text];
  56. }
  57. //云登陆 账号登录
  58. - (IBAction)cloudLogin {
  59. [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
  60. [accountModel loginWithName:self.userNameTf.text andPassword:self.passwordTf.text];
  61. [[NSUserDefaults standardUserDefaults] setObject:self.userNameTf.text forKey:@"DemoUser"];
  62. [[NSUserDefaults standardUserDefaults] setObject:self.passwordTf.text forKey:@"DemoPassword"];
  63. [[NSUserDefaults standardUserDefaults] synchronize];
  64. }
  65. //本地登陆
  66. - (IBAction)localLogin {
  67. [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
  68. [accountModel loginWithTypeLocal];
  69. }
  70. //ap直连
  71. - (IBAction)apLogin:(id)sender {
  72. //判断当前网络是否为ap直连
  73. if (![NSString checkSSID:[NSString getCurrent_SSID]]) {
  74. UIAlertController *alert = [UIAlertController alertControllerWithTitle:TS("Error_Prompt") message:TS("check_wifi_setting") preferredStyle:UIAlertControllerStyleAlert];
  75. UIAlertAction *okAction = [UIAlertAction actionWithTitle:TS("OK") style:UIAlertActionStyleCancel handler:nil];
  76. [alert addAction:okAction];
  77. [self presentViewController:alert animated:YES completion:nil];
  78. return;
  79. }
  80. [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
  81. [accountModel loginWithTypeAP];
  82. }
  83. //注册
  84. - (IBAction)registerUser {
  85. RegisterViewController *registerVC = [[RegisterViewController alloc] init];
  86. [self.navigationController pushViewController:registerVC animated:YES];
  87. }
  88. #pragma mark 登录结果回调,result 结果信息,一般<0是失败,>=0是成功
  89. - (void)loginWithNameDelegate:(long)reslut {
  90. [SVProgressHUD dismiss];
  91. //如果成功,dismiss,回到主界面
  92. if (reslut >= 0) {
  93. //[self dismissViewControllerAnimated:NO completion:nil];
  94. [self.navigationController popViewControllerAnimated:YES];
  95. //保存当前登录的用户名和密码
  96. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  97. NSMutableDictionary *dic = [[defaults objectForKey:@"DemoUserInfo"] mutableCopy];
  98. if (dic == nil) {
  99. dic = [[NSMutableDictionary alloc] init];
  100. }
  101. [dic setObject:self.passwordTf.text forKey:self.userNameTf.text];
  102. [defaults setObject:dic forKey:@"DemoUserInfo"];
  103. return;
  104. }
  105. //如果失败,不做操作,留在登陆界面
  106. [MessageUI ShowErrorInt:(int)reslut];
  107. }
  108. - (void)didReceiveMemoryWarning {
  109. [super didReceiveMemoryWarning];
  110. // Dispose of any resources that can be recreated.
  111. }
  112. @end