LoginViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #import "RAModeListViewController.h"
  13. static NSString *kRememberLogin = @"RememberLogin";
  14. static NSString *kLoginUserInfo = @"UserInfo";
  15. @interface LoginViewController ()<UITableViewDelegate,UITableViewDataSource,LoginSwitchDelegate,UITextFieldDelegate>
  16. {
  17. BOOL _autoShowSetting;
  18. }
  19. @property (strong, nonatomic) IBOutlet UITableView *loginTable;
  20. @property (nonatomic,strong) NSDictionary *dataDic;
  21. @property (strong, nonatomic) IBOutlet UILabel *versionLabel;
  22. @property (strong, nonatomic) IBOutlet UIButton *loginBtn;
  23. @property (strong, nonatomic) IBOutlet UIButton *settingBtn;
  24. @property (nonatomic,copy) NSString *ServerName;
  25. @property (nonatomic,copy) NSString *user;
  26. @property (nonatomic,copy) NSString *pwd;
  27. @end
  28. @implementation LoginViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // Do any additional setup after loading the view.
  32. _autoShowSetting = NO;
  33. [self initUserData];
  34. [self loadData];
  35. [self.loginTable reloadData];
  36. [self registListenKeyboard];
  37. self.loginBtn.layer.cornerRadius = 25.0f;
  38. self.loginBtn.clipsToBounds = YES;
  39. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  40. NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
  41. NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  42. self.versionLabel.text = [NSString stringWithFormat:@"Ver: %@.%@",short_version,build];
  43. }
  44. -(void) viewWillAppear:(BOOL)animated
  45. {
  46. [self.navigationController setNavigationBarHidden:true];
  47. }
  48. -(void)viewWillDisappear:(BOOL)animated
  49. {
  50. [self.navigationController setNavigationBarHidden:false];
  51. }
  52. - (void)viewDidAppear:(BOOL)animated {
  53. [super viewDidAppear:animated];
  54. if (_autoShowSetting) {
  55. [self settingBtnClick:self.settingBtn];
  56. _autoShowSetting = NO;
  57. }
  58. }
  59. - (void)didReceiveMemoryWarning {
  60. [super didReceiveMemoryWarning];
  61. // Dispose of any resources that can be recreated.
  62. }
  63. #pragma mark - Private
  64. - (void)setServerName:(NSString *)server {
  65. _ServerName = server;
  66. if (!server.length) {
  67. _autoShowSetting = YES;
  68. } else {
  69. _autoShowSetting = NO;
  70. }
  71. }
  72. - (void)loadData {
  73. NSString *path = [[NSBundle mainBundle] pathForResource:@"loginUI.json" ofType:nil];
  74. NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil];
  75. NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  76. self.dataDic = dic;
  77. }
  78. - (void)initUserData {
  79. NSDictionary *addressDic = [self userDefaultsValue:kScanAddress];
  80. if (addressDic) {
  81. // NSUInteger selectedIndex = [[addressDic objectForKey:@"selectedIndex"] integerValue];
  82. // if (selectedIndex == 1) {
  83. // self.address = [addressDic objectForKey:@"serverAddress"];
  84. // }
  85. // if (selectedIndex == 2) {
  86. // self.address = [addressDic objectForKey:@"serverAddress"];
  87. // }
  88. self.ServerName = [addressDic objectForKey:@"serverName"];
  89. }
  90. if (!self.ServerName.length){
  91. _autoShowSetting = YES;
  92. return;
  93. }
  94. _autoShowSetting = NO;
  95. NSDictionary *userData = [self userDefaultsValue:kLoginUserInfo];
  96. if (userData) {
  97. // self.address = [userData objectForKey:@"address"];
  98. self.user = [userData objectForKey:@"user"];
  99. self.pwd = [userData objectForKey:@"pwd"];
  100. }
  101. }
  102. - (void)prepareUserData { // 登录取数据
  103. int count = [[self.dataDic objectForKey:@"count"] intValue];
  104. for (int i = 0; i < count; i++) {
  105. NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  106. if ([[itemDic objectForKey:@"type"] isEqualToString:@"text"]) {
  107. LoginTextFiledCell *cell = [self.loginTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
  108. NSString *placeOrder = cell.textBox.placeholder;
  109. if ([placeOrder isEqualToString:@"Address"]) {
  110. self.ServerName = cell.textBox.text;
  111. } else if ([placeOrder isEqualToString:@"user name"]) {
  112. self.user = cell.textBox.text;
  113. } else if ([placeOrder isEqualToString:@"password"]) {
  114. self.pwd = cell.textBox.text;
  115. }
  116. }
  117. }
  118. }
  119. #pragma mark - Orientation
  120. - (BOOL)shouldAutorotate {
  121. return YES;
  122. }
  123. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  124. return UIInterfaceOrientationPortrait;
  125. }
  126. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  127. return UIInterfaceOrientationMaskPortrait;
  128. }
  129. #pragma mark - DataSource
  130. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  131. return [[self.dataDic objectForKey:@"count"] intValue];
  132. }
  133. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. NSDictionary *itemDic = [self.dataDic objectForKey:[NSString stringWithFormat:@"item_%ld",indexPath.row]];
  135. NSString *type = [itemDic objectForKey:@"type"];
  136. BOOL rememberValue = [[self userDefaultsValue:kRememberLogin] boolValue];
  137. if ([type isEqualToString:@"text"]) {
  138. LoginTextFiledCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginTextFiledCell"];
  139. cell.textBox.placeholder = [itemDic objectForKey:@"place_order"];
  140. BOOL editable = [[itemDic objectForKey:@"edit"] boolValue];
  141. cell.textBox.enabled = editable;
  142. NSString *placeOrder = cell.textBox.placeholder;
  143. if ([placeOrder isEqualToString:@"Server"]) {
  144. cell.textBox.text = self.ServerName;
  145. } else if ([placeOrder isEqualToString:@"user name"]) {
  146. cell.textBox.text = self.user;
  147. } else if ([placeOrder isEqualToString:@"password"]) {
  148. cell.textBox.text = self.pwd;
  149. }
  150. cell.textBox.secureTextEntry = [[itemDic objectForKey:@"security"] boolValue];
  151. return cell;
  152. } else if ([type isEqualToString:@"switch"]) {
  153. LoginSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LoginSwitchCell"];
  154. cell.msgLabel.text = [itemDic objectForKey:@"tip_msg"];
  155. cell.stateSwitch.on = rememberValue;
  156. cell.delegate = self;
  157. return cell;
  158. }
  159. return nil;
  160. }
  161. #pragma mark - Delegate
  162. - (void)switchButton:(UISwitch *)switchBtn valueChange:(BOOL)value {
  163. [self setUserDefaultsValue:@(value) forKey:kRememberLogin];
  164. }
  165. #pragma mark - Button Click
  166. - (IBAction)loginBtnClick:(UIButton *)sender {
  167. [self prepareUserData];
  168. // if (!self.ServerName.length) {
  169. // [RAUtils message_alert:@"Please choose an address in setting" title:@"Warning" controller:self];
  170. // return;
  171. // }
  172. //
  173. if (!self.user.length) {
  174. [RAUtils message_alert:@"User name cann't be blank" title:@"Warning" controller:self];
  175. return;
  176. }
  177. if (!self.pwd.length) {
  178. [RAUtils message_alert:@"Password cann't be blank" title:@"Warning" controller:self];
  179. return;
  180. }
  181. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  182. // appDelegate.address = self.address;
  183. __weak typeof(self) weakself = self;
  184. __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Please wait" completion:^{
  185. [RANetwork Login:weakself.user password:weakself.pwd completionHandler:^(NSMutableDictionary *result) {
  186. NSMutableDictionary* return_json = result;
  187. [waitalert dismissViewControllerAnimated:YES completion:^{
  188. int resulti=[[return_json valueForKey:@"result"] intValue];
  189. BOOL rememberLogin = [[weakself userDefaultsValue:kRememberLogin] boolValue];
  190. if(resulti==RESULT_TRUE)
  191. {
  192. NSArray *modeList = [return_json objectForKey:@"mode_list"];
  193. if (modeList != nil) {
  194. RAModeListViewController *rootVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RARootViewController"];
  195. rootVC.title = @"RedAnt Mobile";
  196. rootVC.isroot = true;
  197. rootVC.modelist = modeList;
  198. [self.navigationController pushViewController:rootVC animated:false];
  199. // [appDelegate showNormalRootVCWithModeList:modeList];
  200. } else {
  201. [RAUtils message_alert:@"Something error" title:@"Warning" controller:weakself];
  202. }
  203. if (!rememberLogin) {
  204. [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
  205. } else
  206. {
  207. [weakself setUserDefaultsValue:@{@"user" : weakself.user,@"pwd" : weakself.pwd} forKey:kLoginUserInfo];
  208. }
  209. // appDelegate.bLogin = YES;
  210. appDelegate.user = weakself.user;
  211. appDelegate.password = weakself.pwd;
  212. }
  213. else
  214. {
  215. NSString *msg = [return_json objectForKey:@"err_msg"];
  216. [RAUtils message_alert:msg title:@"Warning" controller:weakself];
  217. }
  218. }];
  219. }];
  220. }];
  221. // __weak typeof(self) weakself = self;
  222. //#pragma clang diagnostic push
  223. //#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  224. // UIAlertView *alert = [RAUtils waiting_alert:@"Please wait" title:@"Login"];
  225. //#pragma clang diagnostic pop
  226. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  227. // if (weakself) {
  228. // __strong typeof(weakself) strongself = weakself;
  229. // NSDictionary *loginDic = [RANetwork Login:strongself.user password:strongself.pwd];
  230. // dispatch_async(dispatch_get_main_queue(), ^{
  231. // [alert dismissWithClickedButtonIndex:0 animated:YES];
  232. // int result = [[loginDic objectForKey:@"result"] intValue];
  233. // BOOL rememberLogin = [[strongself userDefaultsValue:kRememberLogin] boolValue];
  234. // if (result == RESULT_TRUE) {
  235. // NSArray *modeList = [loginDic objectForKey:@"mode_list"];
  236. // if (modeList != nil) {
  237. //
  238. // RAModeListViewController *rootVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RARootViewController"];
  239. // rootVC.title = @"RedAnt Mobile";
  240. // rootVC.isroot = true;
  241. // rootVC.modelist = modeList;
  242. //// rootVC.onRBClick = ^(RAModeListViewController* bself){
  243. ////
  244. //// UIAlertView *alert = [RAUtils waiting_alert:@"Please wait..." title:@"Logout"];
  245. ////// __weak typeof(self) weakself = self;
  246. //// dispatch_async(dispatch_get_main_queue(), ^{
  247. ////
  248. //// NSDictionary *logoutDic = [RANetwork logout];
  249. //// int result = [[logoutDic objectForKey:@"result"] intValue];
  250. //// dispatch_async(dispatch_get_main_queue(), ^{
  251. //// [alert dismissWithClickedButtonIndex:0 animated:YES];
  252. ////// AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  253. //// if (result == RESULT_TRUE) {
  254. ////
  255. //// [bself.navigationController popViewControllerAnimated:false];
  256. //// // weakself.companyIcon = [UIImage imageNamed:@"AppIcon40x40"];
  257. //// // [appDelegate logout];
  258. //// } else {
  259. //// __strong typeof(bself) strongself = bself;
  260. //// NSString *msg = [logoutDic objectForKey:@"err_msg"];
  261. //// [RAUtils message_alert:msg title:@"Warning" controller:strongself];
  262. //// }
  263. //// });
  264. ////
  265. ////
  266. //// });
  267. //// ;
  268. //// };
  269. //// rootVC.onRBName = ^NSString *{
  270. //// return @"Logout";
  271. //// };
  272. //// [rootVC setModeList:modeList];
  273. // [self.navigationController pushViewController:rootVC animated:false];
  274. //// [appDelegate showNormalRootVCWithModeList:modeList];
  275. // } else {
  276. // [RAUtils message_alert:@"Something error" title:@"Warning" controller:strongself];
  277. // }
  278. //
  279. // if (!rememberLogin) {
  280. // [self setUserDefaultsValue:nil forKey:kLoginUserInfo];
  281. // } else {
  282. // [strongself setUserDefaultsValue:@{@"user" : strongself.user,@"pwd" : strongself.pwd} forKey:kLoginUserInfo];
  283. // }
  284. //// appDelegate.bLogin = YES;
  285. // appDelegate.user = strongself.user;
  286. // appDelegate.password = strongself.pwd;
  287. //// appDelegate.companyName = [loginDic objectForKey:@"company_name"];
  288. //// [appDelegate loadCompanyIcon:[loginDic objectForKey:@"company_icon"]];
  289. //// appDelegate.modeList = [loginDic objectForKey:@"modellist"];
  290. //// [appDelegate showNormalRootVC];
  291. // } else {
  292. // NSString *msg = [loginDic objectForKey:@"err_msg"];
  293. // [RAUtils message_alert:msg title:@"Warning" controller:strongself];
  294. // }
  295. // });
  296. // }
  297. // });
  298. }
  299. - (IBAction)settingBtnClick:(UIButton *)sender {
  300. LoginSettingViewController *loginSettingVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LoginSettingViewController"];
  301. __weak typeof(self) weakself = self;
  302. loginSettingVC.returnValue = ^(NSString *address) {
  303. if (weakself) {
  304. __strong typeof(weakself) strongself = weakself;
  305. strongself.ServerName = address;
  306. [strongself.loginTable reloadData];
  307. }
  308. };
  309. // UINavigationController *settingRootNav = [[UINavigationController alloc] initWithRootViewController:loginSettingVC];
  310. // [self presentViewController:settingRootNav animated:YES completion:nil];
  311. [self.navigationController pushViewController:loginSettingVC animated:false];
  312. }
  313. - (IBAction)retrieveBtnClick:(UIButton *)sender {
  314. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Retrieve Password" message:nil preferredStyle:UIAlertControllerStyleAlert];
  315. __block UITextField *userTextField;
  316. [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  317. textField.placeholder = @"user name";
  318. userTextField = textField;
  319. }];
  320. __block UITextField *emailTextField;
  321. [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  322. textField.placeholder = @"email address";
  323. emailTextField = textField;
  324. }];
  325. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  326. }];
  327. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  328. NSLog(@"Retrieve Password\nuser: %@\nemail: %@",userTextField.text,emailTextField.text);
  329. }];
  330. [alertVC addAction:cancelAction];
  331. [alertVC addAction:okAction];
  332. [self presentViewController:alertVC animated:YES completion:nil];
  333. }
  334. #pragma mark - TextField Delegate
  335. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  336. return YES;
  337. }
  338. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  339. return YES;
  340. }
  341. - (void)textFieldDidEndEditing:(UITextField *)textField {
  342. self.currentFirstResponder = nil;
  343. }
  344. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  345. self.currentFirstResponder = textField;
  346. }
  347. @end