LoginViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // LoginViewController.m
  3. // RA Image
  4. //
  5. // Created by Ray on 27/04/2017.
  6. // Copyright © 2017 USAI. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "LoginTextFiledCell.h"
  10. #import "LoginSwitchCell.h"
  11. #import "LoginSettingViewController.h"
  12. static NSString *kRememberLogin = @"RememberLogin";
  13. static NSString *kLoginUserInfo = @"UserInfo";
  14. @interface LoginViewController ()<UITableViewDelegate,UITableViewDataSource,LoginSwitchDelegate,UITextFieldDelegate>
  15. {
  16. BOOL _autoShowSetting;
  17. }
  18. @property (strong, nonatomic) IBOutlet UITableView *loginTable;
  19. @property (nonatomic,strong) NSDictionary *dataDic;
  20. @property (strong, nonatomic) IBOutlet UILabel *versionLabel;
  21. @property (strong, nonatomic) IBOutlet UIButton *loginBtn;
  22. @property (strong, nonatomic) IBOutlet UIButton *settingBtn;
  23. @property (nonatomic,copy) NSString *address;
  24. @property (nonatomic,copy) NSString *user;
  25. @property (nonatomic,copy) NSString *pwd;
  26. @end
  27. @implementation LoginViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view.
  31. _autoShowSetting = NO;
  32. [self initUserData];
  33. [self loadData];
  34. [self.loginTable reloadData];
  35. [self registListenKeyboard];
  36. self.loginBtn.layer.cornerRadius = 25.0f;
  37. self.loginBtn.clipsToBounds = YES;
  38. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  39. NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
  40. NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  41. self.versionLabel.text = [NSString stringWithFormat:@"Ver: %@.%@",short_version,build];
  42. }
  43. - (void)viewDidAppear:(BOOL)animated {
  44. [super viewDidAppear:animated];
  45. if (_autoShowSetting) {
  46. [self settingBtnClick:self.settingBtn];
  47. _autoShowSetting = NO;
  48. }
  49. }
  50. - (void)didReceiveMemoryWarning {
  51. [super didReceiveMemoryWarning];
  52. // Dispose of any resources that can be recreated.
  53. }
  54. #pragma mark - Private
  55. - (void)setAddress:(NSString *)address {
  56. _address = address;
  57. if (!address.length) {
  58. _autoShowSetting = YES;
  59. } else {
  60. _autoShowSetting = NO;
  61. }
  62. }
  63. - (void)loadData {
  64. NSString *path = [[NSBundle mainBundle] pathForResource:@"login.json" ofType:nil];
  65. NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
  66. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  67. self.dataDic = dic;
  68. }
  69. - (void)initUserData {
  70. NSDictionary *addressDic = [self userDefaultsValue:kScanAddress];
  71. if (addressDic) {
  72. // NSUInteger selectedIndex = [[addressDic objectForKey:@"selectedIndex"] integerValue];
  73. // if (selectedIndex == 1) {
  74. // self.address = [addressDic objectForKey:@"serverAddress"];
  75. // }
  76. // if (selectedIndex == 2) {
  77. // self.address = [addressDic objectForKey:@"serverAddress"];
  78. // }
  79. self.address = [addressDic objectForKey:@"serverAddress"];
  80. }
  81. if (!self.address.length){
  82. _autoShowSetting = YES;
  83. return;
  84. }
  85. _autoShowSetting = NO;
  86. NSDictionary *userData = [self userDefaultsValue:kLoginUserInfo];
  87. if (userData) {
  88. // self.address = [userData objectForKey:@"address"];
  89. self.user = [userData objectForKey:@"user"];
  90. self.pwd = [userData objectForKey:@"pwd"];
  91. }
  92. }
  93. - (void)prepareUserData { // 登录取数据
  94. int count = [[self.dataDic objectForKey:@"count"] intValue];
  95. for (int i = 0; i < count; i++) {
  96. NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  97. if ([[itemDic objectForKey:@"type"] isEqualToString:@"text"]) {
  98. LoginTextFiledCell *cell = [self.loginTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
  99. NSString *placeOrder = cell.textBox.placeholder;
  100. if ([placeOrder isEqualToString:@"Address"]) {
  101. self.address = cell.textBox.text;
  102. } else if ([placeOrder isEqualToString:@"user name"]) {
  103. self.user = cell.textBox.text;
  104. } else if ([placeOrder isEqualToString:@"password"]) {
  105. self.pwd = cell.textBox.text;
  106. }
  107. }
  108. }
  109. }
  110. #pragma mark - Orientation
  111. - (BOOL)shouldAutorotate {
  112. return YES;
  113. }
  114. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  115. return UIInterfaceOrientationPortrait;
  116. }
  117. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  118. return UIInterfaceOrientationMaskPortrait;
  119. }
  120. #pragma mark - DataSource
  121. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  122. return [[self.dataDic objectForKey:@"count"] intValue];
  123. }
  124. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  125. NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  126. NSString *type = [itemDic objectForKey:@"type"];
  127. BOOL rememberValue = [[self userDefaultsValue:kRememberLogin] boolValue];
  128. if ([type isEqualToString:@"text"]) {
  129. LoginTextFiledCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginTextFiledCell"];
  130. cell.textBox.placeholder = [itemDic objectForKey:@"place_order"];
  131. BOOL editable = [[itemDic objectForKey:@"edit"] boolValue];
  132. cell.textBox.enabled = editable;
  133. NSString *placeOrder = cell.textBox.placeholder;
  134. if ([placeOrder isEqualToString:@"Address"]) {
  135. cell.textBox.text = self.address;
  136. } else if ([placeOrder isEqualToString:@"user name"]) {
  137. cell.textBox.text = self.user;
  138. } else if ([placeOrder isEqualToString:@"password"]) {
  139. cell.textBox.text = self.pwd;
  140. }
  141. cell.textBox.secureTextEntry = [[itemDic objectForKey:@"security"] boolValue];
  142. return cell;
  143. } else if ([type isEqualToString:@"switch"]) {
  144. LoginSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginSwitchCell"];
  145. cell.msgLabel.text = [itemDic objectForKey:@"tip_msg"];
  146. cell.stateSwitch.on = rememberValue;
  147. cell.delegate = self;
  148. return cell;
  149. }
  150. return nil;
  151. }
  152. #pragma mark - Delegate
  153. - (void)switchButton:(UISwitch *)switchBtn valueChange:(BOOL)value {
  154. [self setUserDefaultsValue:@(value) forKey:kRememberLogin];
  155. }
  156. #pragma mark - Button Click
  157. - (IBAction)loginBtnClick:(UIButton *)sender {
  158. [self prepareUserData];
  159. if (!self.address.length) {
  160. [RAUtils message_alert:@"Please choose an address in setting" title:@"Warning" controller:self];
  161. return;
  162. }
  163. if (!self.user.length) {
  164. [RAUtils message_alert:@"User name cannot be blank" title:@"Warning" controller:self];
  165. return;
  166. }
  167. if (!self.pwd.length) {
  168. [RAUtils message_alert:@"Password cannot be blank" title:@"Warning" controller:self];
  169. return;
  170. }
  171. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  172. appDelegate.address = self.address;
  173. // __weak typeof(self) weakself = self;
  174. // UIAlertView *alert = [RAUtils waiting_alert:@"Please wait" title:@"Login"];
  175. __block UIAlertController* alert = [RAUtils waiting_alert:self title:@"Login" completion:^{
  176. [RANetwork request_login:self.user password:self.pwd completionHandler:^(NSMutableDictionary *result) {
  177. // [alert dismissWithClickedButtonIndex:0 animated:YES];
  178. [alert dismissViewControllerAnimated:YES completion:^{
  179. int result_code = [[result objectForKey:@"result"] intValue];
  180. BOOL rememberLogin = [[self userDefaultsValue:kRememberLogin] boolValue];
  181. if (result_code == RESULT_TRUE) {
  182. if (!rememberLogin) {
  183. [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
  184. } else {
  185. [self setUserDefaultsValue:@{@"address" : self.address,@"user" : self.user,@"pwd" : self.pwd} forKey:kLoginUserInfo];
  186. }
  187. appDelegate.bLogin = YES;
  188. appDelegate.user = self.user;
  189. appDelegate.password = self.pwd;
  190. appDelegate.companyName = [result objectForKey:@"company_name"];
  191. [appDelegate loadCompanyIcon:[result objectForKey:@"company_icon"]];
  192. appDelegate.modeList = [result objectForKey:@"modellist"];
  193. [appDelegate showNormalRootVC];
  194. } else {
  195. NSString *msg = [result objectForKey:@"err_msg"];
  196. [RAUtils message_alert:msg title:@"Warning" controller:self];
  197. }
  198. }];
  199. }];
  200. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  201. // if (weakself) {
  202. // __strong typeof(weakself) strongself = weakself;
  203. // NSDictionary *loginDic = [RANetwork Login:strongself.user password:strongself.pwd];
  204. // dispatch_async(dispatch_get_main_queue(), ^{
  205. //// [alert dismissWithClickedButtonIndex:0 animated:YES];
  206. //
  207. // [alert dismissViewControllerAnimated:YES completion:^{
  208. // int result = [[loginDic objectForKey:@"result"] intValue];
  209. // BOOL rememberLogin = [[strongself userDefaultsValue:kRememberLogin] boolValue];
  210. // if (result == RESULT_TRUE) {
  211. // if (!rememberLogin) {
  212. // [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
  213. // } else {
  214. // [strongself setUserDefaultsValue:@{@"address" : strongself.address,@"user" : strongself.user,@"pwd" : strongself.pwd} forKey:kLoginUserInfo];
  215. // }
  216. // appDelegate.bLogin = YES;
  217. // appDelegate.user = strongself.user;
  218. // appDelegate.password = strongself.pwd;
  219. // appDelegate.companyName = [loginDic objectForKey:@"company_name"];
  220. // [appDelegate loadCompanyIcon:[loginDic objectForKey:@"company_icon"]];
  221. // appDelegate.modeList = [loginDic objectForKey:@"modellist"];
  222. // [appDelegate showNormalRootVC];
  223. // } else {
  224. // NSString *msg = [loginDic objectForKey:@"err_msg"];
  225. // [RAUtils message_alert:msg title:@"Warning" controller:strongself];
  226. // }
  227. // }];
  228. //
  229. //
  230. // });
  231. //
  232. // }
  233. // });
  234. }];
  235. }
  236. - (IBAction)settingBtnClick:(UIButton *)sender {
  237. LoginSettingViewController *loginSettingVC = [[UIStoryboard storyboardWithName:@"RAImage" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginSettingViewController"];
  238. __weak typeof(self) weakself = self;
  239. loginSettingVC.returnValue = ^(NSString *address) {
  240. if (weakself) {
  241. __strong typeof(weakself) strongself = weakself;
  242. strongself.address = address;
  243. [strongself.loginTable reloadData];
  244. }
  245. };
  246. UINavigationController *settingRootNav = [[UINavigationController alloc] initWithRootViewController:loginSettingVC];
  247. [self presentViewController:settingRootNav animated:YES completion:nil];
  248. }
  249. #pragma mark - TextField Delegate
  250. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  251. return YES;
  252. }
  253. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  254. return YES;
  255. }
  256. - (void)textFieldDidEndEditing:(UITextField *)textField {
  257. self.currentFirstResponder = nil;
  258. }
  259. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  260. self.currentFirstResponder = textField;
  261. }
  262. @end