| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- //
- // LoginViewController.m
- // RA Image
- //
- // Created by Ray on 27/04/2017.
- // Copyright © 2017 USAI. All rights reserved.
- //
- #import "LoginViewController.h"
- #import "LoginTextFiledCell.h"
- #import "LoginSwitchCell.h"
- #import "LoginSettingViewController.h"
- static NSString *kRememberLogin = @"RememberLogin";
- static NSString *kLoginUserInfo = @"UserInfo";
- @interface LoginViewController ()<UITableViewDelegate,UITableViewDataSource,LoginSwitchDelegate,UITextFieldDelegate>
- {
- BOOL _autoShowSetting;
- }
- @property (strong, nonatomic) IBOutlet UITableView *loginTable;
- @property (nonatomic,strong) NSDictionary *dataDic;
- @property (strong, nonatomic) IBOutlet UILabel *versionLabel;
- @property (strong, nonatomic) IBOutlet UIButton *loginBtn;
- @property (strong, nonatomic) IBOutlet UIButton *settingBtn;
- @property (nonatomic,copy) NSString *address;
- @property (nonatomic,copy) NSString *user;
- @property (nonatomic,copy) NSString *pwd;
- @end
- @implementation LoginViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- _autoShowSetting = NO;
- [self initUserData];
- [self loadData];
- [self.loginTable reloadData];
- [self registListenKeyboard];
-
- self.loginBtn.layer.cornerRadius = 25.0f;
- self.loginBtn.clipsToBounds = YES;
-
- NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
- NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
- NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
- self.versionLabel.text = [NSString stringWithFormat:@"Ver: %@.%@",short_version,build];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- if (_autoShowSetting) {
- [self settingBtnClick:self.settingBtn];
- _autoShowSetting = NO;
- }
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Private
- - (void)setAddress:(NSString *)address {
- _address = address;
- if (!address.length) {
- _autoShowSetting = YES;
- } else {
- _autoShowSetting = NO;
- }
- }
- - (void)loadData {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"login.json" ofType:nil];
- NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- self.dataDic = dic;
- }
- - (void)initUserData {
- NSDictionary *addressDic = [self userDefaultsValue:kScanAddress];
- if (addressDic) {
- // NSUInteger selectedIndex = [[addressDic objectForKey:@"selectedIndex"] integerValue];
- // if (selectedIndex == 1) {
- // self.address = [addressDic objectForKey:@"serverAddress"];
- // }
- // if (selectedIndex == 2) {
- // self.address = [addressDic objectForKey:@"serverAddress"];
- // }
- self.address = [addressDic objectForKey:@"serverAddress"];
- }
-
- if (!self.address.length){
- _autoShowSetting = YES;
- return;
- }
- _autoShowSetting = NO;
-
- NSDictionary *userData = [self userDefaultsValue:kLoginUserInfo];
- if (userData) {
- // self.address = [userData objectForKey:@"address"];
- self.user = [userData objectForKey:@"user"];
- self.pwd = [userData objectForKey:@"pwd"];
- }
- }
- - (void)prepareUserData { // 登录取数据
-
- int count = [[self.dataDic objectForKey:@"count"] intValue];
- for (int i = 0; i < count; i++) {
- NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%d",i]];
- if ([[itemDic objectForKey:@"type"] isEqualToString:@"text"]) {
- LoginTextFiledCell *cell = [self.loginTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
- NSString *placeOrder = cell.textBox.placeholder;
- if ([placeOrder isEqualToString:@"Address"]) {
- self.address = cell.textBox.text;
- } else if ([placeOrder isEqualToString:@"user name"]) {
- self.user = cell.textBox.text;
- } else if ([placeOrder isEqualToString:@"password"]) {
- self.pwd = cell.textBox.text;
- }
- }
- }
-
- }
- #pragma mark - Orientation
- - (BOOL)shouldAutorotate {
- return YES;
- }
- - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
- return UIInterfaceOrientationPortrait;
- }
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
- return UIInterfaceOrientationMaskPortrait;
- }
- #pragma mark - DataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [[self.dataDic objectForKey:@"count"] intValue];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%ld",indexPath.row]];
-
- NSString *type = [itemDic objectForKey:@"type"];
- BOOL rememberValue = [[self userDefaultsValue:kRememberLogin] boolValue];
-
- if ([type isEqualToString:@"text"]) {
-
- LoginTextFiledCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginTextFiledCell"];
- cell.textBox.placeholder = [itemDic objectForKey:@"place_order"];
- BOOL editable = [[itemDic objectForKey:@"edit"] boolValue];
- cell.textBox.enabled = editable;
- NSString *placeOrder = cell.textBox.placeholder;
- if ([placeOrder isEqualToString:@"Address"]) {
- cell.textBox.text = self.address;
- } else if ([placeOrder isEqualToString:@"user name"]) {
- cell.textBox.text = self.user;
- } else if ([placeOrder isEqualToString:@"password"]) {
- cell.textBox.text = self.pwd;
- }
- cell.textBox.secureTextEntry = [[itemDic objectForKey:@"security"] boolValue];
- return cell;
-
- } else if ([type isEqualToString:@"switch"]) {
-
- LoginSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginSwitchCell"];
- cell.msgLabel.text = [itemDic objectForKey:@"tip_msg"];
- cell.stateSwitch.on = rememberValue;
- cell.delegate = self;
- return cell;
- }
-
- return nil;
- }
- #pragma mark - Delegate
- - (void)switchButton:(UISwitch *)switchBtn valueChange:(BOOL)value {
-
- [self setUserDefaultsValue:@(value) forKey:kRememberLogin];
- }
- #pragma mark - Button Click
- - (IBAction)loginBtnClick:(UIButton *)sender {
- [self prepareUserData];
- if (!self.address.length) {
- [RAUtils message_alert:@"Please choose an address in setting" title:@"Warning" controller:self];
- return;
- }
-
- if (!self.user.length) {
- [RAUtils message_alert:@"User name cannot be blank" title:@"Warning" controller:self];
- return;
- }
-
- if (!self.pwd.length) {
- [RAUtils message_alert:@"Password cannot be blank" title:@"Warning" controller:self];
- return;
- }
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- appDelegate.address = self.address;
- // __weak typeof(self) weakself = self;
-
- // UIAlertView *alert = [RAUtils waiting_alert:@"Please wait" title:@"Login"];
- __block UIAlertController* alert = [RAUtils waiting_alert:self title:@"Login" completion:^{
-
- [RANetwork request_login:self.user password:self.pwd completionHandler:^(NSMutableDictionary *result) {
- // [alert dismissWithClickedButtonIndex:0 animated:YES];
-
- [alert dismissViewControllerAnimated:YES completion:^{
- int result_code = [[result objectForKey:@"result"] intValue];
- BOOL rememberLogin = [[self userDefaultsValue:kRememberLogin] boolValue];
- if (result_code == RESULT_TRUE) {
- if (!rememberLogin) {
- [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
- } else {
- [self setUserDefaultsValue:@{@"address" : self.address,@"user" : self.user,@"pwd" : self.pwd} forKey:kLoginUserInfo];
- }
- appDelegate.bLogin = YES;
- appDelegate.user = self.user;
- appDelegate.password = self.pwd;
- appDelegate.companyName = [result objectForKey:@"company_name"];
- [appDelegate loadCompanyIcon:[result objectForKey:@"company_icon"]];
- appDelegate.modeList = [result objectForKey:@"modellist"];
- [appDelegate showNormalRootVC];
- } else {
- NSString *msg = [result objectForKey:@"err_msg"];
- [RAUtils message_alert:msg title:@"Warning" controller:self];
- }
- }];
-
-
- }];
-
-
- // dispatch_async(dispatch_get_global_queue(0, 0), ^{
- // if (weakself) {
- // __strong typeof(weakself) strongself = weakself;
- // NSDictionary *loginDic = [RANetwork Login:strongself.user password:strongself.pwd];
- // dispatch_async(dispatch_get_main_queue(), ^{
- //// [alert dismissWithClickedButtonIndex:0 animated:YES];
- //
- // [alert dismissViewControllerAnimated:YES completion:^{
- // int result = [[loginDic objectForKey:@"result"] intValue];
- // BOOL rememberLogin = [[strongself userDefaultsValue:kRememberLogin] boolValue];
- // if (result == RESULT_TRUE) {
- // if (!rememberLogin) {
- // [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
- // } else {
- // [strongself setUserDefaultsValue:@{@"address" : strongself.address,@"user" : strongself.user,@"pwd" : strongself.pwd} forKey:kLoginUserInfo];
- // }
- // appDelegate.bLogin = YES;
- // appDelegate.user = strongself.user;
- // appDelegate.password = strongself.pwd;
- // appDelegate.companyName = [loginDic objectForKey:@"company_name"];
- // [appDelegate loadCompanyIcon:[loginDic objectForKey:@"company_icon"]];
- // appDelegate.modeList = [loginDic objectForKey:@"modellist"];
- // [appDelegate showNormalRootVC];
- // } else {
- // NSString *msg = [loginDic objectForKey:@"err_msg"];
- // [RAUtils message_alert:msg title:@"Warning" controller:strongself];
- // }
- // }];
- //
- //
- // });
- //
- // }
- // });
- }];
-
- }
- - (IBAction)settingBtnClick:(UIButton *)sender {
-
- LoginSettingViewController *loginSettingVC = [[UIStoryboard storyboardWithName:@"RAImage" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginSettingViewController"];
- __weak typeof(self) weakself = self;
- loginSettingVC.returnValue = ^(NSString *address) {
- if (weakself) {
- __strong typeof(weakself) strongself = weakself;
- strongself.address = address;
- [strongself.loginTable reloadData];
- }
- };
- UINavigationController *settingRootNav = [[UINavigationController alloc] initWithRootViewController:loginSettingVC];
- [self presentViewController:settingRootNav animated:YES completion:nil];
-
- }
- #pragma mark - TextField Delegate
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
-
- return YES;
- }
- - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
-
- return YES;
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField {
- self.currentFirstResponder = nil;
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField {
- self.currentFirstResponder = textField;
- }
- @end
|