LoginViewController.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. //
  2. // LoginViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-6-20.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "RetrievePassViewController.h"
  10. #import "UILabel+FontAppearance.h"
  11. #import "OLDataProvider.h"
  12. #import "NotificationNameCenter.h"
  13. #import "OfflineSettingViewController.h"
  14. #import "MainViewController.h"
  15. #import "WebViewController.h"
  16. @interface LoginViewController ()
  17. @property (strong, nonatomic) IBOutlet UIButton *requestLoginBtn;
  18. @end
  19. @implementation LoginViewController
  20. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  21. {
  22. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  23. if (self) {
  24. // Custom initialization
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. #ifdef OFFLINE_MODE
  32. self.checkOfflineMode.hidden=false;
  33. #else
  34. self.checkOfflineMode.hidden=true;
  35. #endif
  36. #ifdef BUILD_HMLG
  37. self.title=COMPANY_SHORT_NAME;
  38. self.requestLoginBtn.hidden = YES;
  39. #endif
  40. #if defined(BUILD_HOMER) || defined(BUILD_GATIT) || defined(BUILD_UWAVER)
  41. self.title=COMPANY_SHORT_NAME;
  42. // self.checkOfflineMode.hidden = YES;
  43. self.requestLoginBtn.hidden = YES;
  44. #endif
  45. #if defined(BUILD_NPD) || defined(BUILD_USAI)|| defined (BUILD_CONTRAST)
  46. self.title=COMPANY_SHORT_NAME;
  47. // self.checkOfflineMode.hidden=false;
  48. #endif
  49. self.showList = false;
  50. self.bb_close.image=[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
  51. [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
  52. [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox_check"] forState:UIControlStateSelected];
  53. [self.checkOfflineMode setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
  54. [self.checkOfflineMode setImage:[UIImage imageNamed:@"checkbox_check"] forState:UIControlStateSelected];
  55. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  56. NSString * user = [AESCrypt decrypt:[defaults stringForKey:@"user"] password:@"usai"];
  57. NSString * password = [AESCrypt decrypt:[defaults stringForKey:@"password"] password:@"usai"];
  58. if(user.length>0&&password.length>0)
  59. {
  60. self.edituser.text=user;
  61. self.editpass.text=password;
  62. self.checkSavePassword.selected=true;
  63. }
  64. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  65. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  66. // [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  67. self.checkOfflineMode.selected=[defaults boolForKey:@"EnableOfflineMode"];
  68. [self.checkOfflineMode addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  69. [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  70. self.checkOfflineMode.tag=999;
  71. self.logindata = [iSalesDB get_saveduser];
  72. UIButton* combobutton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
  73. [combobutton setBackgroundImage:[UIImage imageNamed:@"combo_24"] forState:UIControlStateNormal];
  74. [combobutton addTarget:self action:@selector(LoginList:) forControlEvents:UIControlEventTouchUpInside];
  75. self.edituser.rightView = combobutton;
  76. self.edituser.rightViewMode=UITextFieldViewModeAlways;
  77. self.Loginlist.layer.borderColor = [UIColor lightGrayColor].CGColor;
  78. self.Loginlist.layer.borderWidth = 1.0;
  79. // self.Loginlist.layer.cornerRadius=15;
  80. self.Loginlist.layer.masksToBounds=true;
  81. // Do any additional setup after loading the view.
  82. #ifdef SCANNER_ORDER
  83. self.btnGuest.hidden=false;
  84. #endif
  85. }
  86. - (void)LoginList:(UIButton *)sender {
  87. [self.edituser endEditing:true];
  88. self.showList= !self.showList;
  89. self.Loginlist.hidden = !self.showList;
  90. // showList;
  91. }
  92. -(void)checkboxClick:(UIButton *)btn
  93. {
  94. btn.selected = !btn.selected;
  95. if(btn.tag==999)
  96. {
  97. bool haveofflineData=[OLDataProvider check_offlinedata];
  98. if(btn.selected)
  99. {
  100. if(haveofflineData)
  101. {
  102. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  103. bool forcedownload = [defaults boolForKey:@"Forcedownload"];
  104. if(forcedownload)
  105. {
  106. btn.selected = !btn.selected;
  107. [RAUtils message_box:@"Incomplete sync" message:@"Detected incomplete sync, please finish sync first." completion:nil];
  108. [defaults removeObjectForKey:@"EnableOfflineMode"];
  109. [defaults setBool:false forKey:@"EnableOfflineMode"];
  110. [defaults synchronize];
  111. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  112. appDelegate.offline_mode = false;
  113. // [RAUtils alert_view:@"Detected incomplete sync, please finish sync first." title:@"Incomplete sync"];
  114. }
  115. else
  116. {
  117. [defaults removeObjectForKey:@"EnableOfflineMode"];
  118. [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  119. [defaults synchronize];
  120. #ifdef OFFLINE_MODE
  121. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  122. appDelegate.offline_mode = self.checkOfflineMode.selected;
  123. #endif
  124. }
  125. }
  126. else
  127. {
  128. btn.selected = !btn.selected;
  129. [RAUtils message_box:@"Offline data not found" message:@"Offline data not found, you must login online and download offline data first." completion:nil];
  130. // [RAUtils alert_view:@"Offline data not found, you must login online and download offline data first." title:@"Offline data not found"];
  131. }
  132. }
  133. else
  134. {
  135. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  136. [defaults removeObjectForKey:@"EnableOfflineMode"];
  137. [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  138. [defaults synchronize];
  139. #ifdef OFFLINE_MODE
  140. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  141. appDelegate.offline_mode = self.checkOfflineMode.selected;
  142. #endif
  143. }
  144. }
  145. else
  146. {
  147. if(!btn.selected)
  148. {
  149. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  150. [defaults removeObjectForKey:@"user"];
  151. [defaults removeObjectForKey:@"password"];
  152. [defaults synchronize];
  153. NSString* encryptu=[AESCrypt encrypt:self.edituser.text password:@"usai"];
  154. NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
  155. [iSalesDB execSql:sql];
  156. self.logindata = [iSalesDB get_saveduser];
  157. [self.Loginlist reloadData];
  158. }
  159. }
  160. }
  161. - (IBAction)CloseClick:(id)sender {
  162. [self dismissViewControllerAnimated:true completion:^{
  163. if(self.returnValue)
  164. self.returnValue(false);
  165. }];
  166. }
  167. - (IBAction)onRetrievePassword:(id)sender {
  168. RetrievePassViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"RetrievePassViewController" ];
  169. // dvc
  170. // UINavigationController* nv = self.navigationController;
  171. [self.navigationController pushViewController:vc animated:true];
  172. }
  173. - (IBAction)onGuestClick:(id)sender {
  174. [OLDataProvider realguestLogin];
  175. [self dismissViewControllerAnimated:true completion:^{
  176. if(self.returnValue)
  177. self.returnValue(false);
  178. }];
  179. }
  180. - (IBAction)onLoginClick:(UIButton *)sender {
  181. NSString* user = self.edituser.text;
  182. NSString* password = self.editpass.text;
  183. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  184. bool forcedownload = [defaults boolForKey:@"Forcedownload"];
  185. if(forcedownload && self.checkOfflineMode.selected)
  186. {
  187. self.checkOfflineMode.selected = !self.checkOfflineMode.selected;
  188. [RAUtils message_box:@"Incomplete sync" message:@"Detected incomplete sync, please finish sync first." completion:nil];
  189. [defaults removeObjectForKey:@"EnableOfflineMode"];
  190. [defaults setBool:false forKey:@"EnableOfflineMode"];
  191. [defaults synchronize];
  192. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  193. appDelegate.offline_mode = false;
  194. // [RAUtils alert_view:@"Detected incomplete sync, please finish sync first." title:@"Incomplete sync"];
  195. return;
  196. }
  197. if(user.length==0||password.length==0)
  198. {
  199. [RAUtils message_box:@"Cannot sign-in" message:@"Username or Password cannot be blank." completion:nil];
  200. // [RAUtils alert_view:@"Username or Password cannot be blank." title:@"Cannot sign-in"];
  201. return;
  202. }
  203. DebugLog(@"Login... user= %@ ; password= %@",user,password);
  204. self.btnLogin.enabled = false;
  205. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Sign in"];
  206. // __block UIAlertController* waitalert= [RAUtils waiting_alert:self title:@"Sign in" completion:^{
  207. PopWaitAlert* pop = [RAUtils waiting_pop:@"Sign in" completion:nil];
  208. [RANetwork request_login:user password:password completionHandler:^(NSMutableDictionary *result) {
  209. [pop hide];
  210. // [waitalert dismissViewControllerAnimated:true completion:^{
  211. int ret=[result[@"result"] intValue];
  212. self.btnLogin.enabled = true;
  213. if(ret==RESULT_TRUE)
  214. {
  215. //
  216. // NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  217. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  218. // appDelegate.contact_id=contactid;
  219. // appDelegate.contact_name =[self.content_data[indexPath.row] valueForKey:@"name"];
  220. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  221. [defaults removeObjectForKey:@"user"];
  222. [defaults removeObjectForKey:@"password"];
  223. NSString* encryptu=[AESCrypt encrypt:appDelegate.user password:@"usai"];
  224. NSString* encryptp=[AESCrypt encrypt:self.editpass.text password:@"usai"] ;
  225. if(self.checkSavePassword.selected)
  226. {
  227. [defaults setValue:encryptu forKey:@"user"];
  228. [defaults setValue:encryptp forKey:@"password"];
  229. sqlite3 *db = [iSalesDB get_db];
  230. int count =[iSalesDB get_recordcount:db table:@"login_info" where:[NSString stringWithFormat:@"name='%@'",encryptu]];
  231. if(count==0)
  232. {
  233. NSString * savelogin=[NSString stringWithFormat:@"insert into login_info(name,pwd) values('%@','%@')",encryptu,encryptp];
  234. [iSalesDB execSql:savelogin db:db];
  235. }
  236. else
  237. {
  238. NSString * savelogin=[NSString stringWithFormat:@"update login_info set pwd='%@' where name='%@'",encryptp,encryptu];
  239. [iSalesDB execSql:savelogin db:db];
  240. }
  241. [iSalesDB close_db:db];
  242. // [defaults setBool:TRUE forKey:@"autologin"];
  243. }
  244. else
  245. {
  246. NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
  247. [iSalesDB execSql:sql];
  248. }
  249. [defaults synchronize];
  250. #ifdef SCANNER_ORDER
  251. [ActiveViewController Notify:@"ScanOrderListViewController" Message:RA_NOTIFICATION_RELOAD_DATA];
  252. [ActiveViewController Notify:@"ScanSearchViewController,ScanHistoryViewController,ServerSettingViewController" Message:RA_NOTIFICATION_REFRESH_UI];
  253. #endif
  254. [self dismissViewControllerAnimated:YES completion:^{
  255. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  256. bool offline_dirty=[defaults boolForKey:@"OFFLINE_DIRTY"];
  257. sqlite3* db = [iSalesDB get_db];
  258. int offline_user_exist=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
  259. [iSalesDB close_db:db];
  260. BOOL condition = offline_dirty && offline_user_exist>0;
  261. #ifdef OFFLINE_MODE
  262. condition = condition && !appDelegate.offline_mode;
  263. #endif
  264. if(condition)
  265. {
  266. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Detected new offline data" message:@"Do you want to sync with server?" preferredStyle:UIAlertControllerStyleAlert];
  267. //block代码块取代了delegate
  268. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes(Recommended)" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  269. UIApplication * app = [UIApplication sharedApplication];
  270. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  271. //UIViewController* active_vc = appDelegate.active_controller;
  272. OfflineSettingViewController * offlineVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflineSettingViewController"];
  273. // loginvc.delegate = self;
  274. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  275. // appDelegate.active_controller = gunVC;
  276. // gunVC.onDismissVC = ^(){
  277. // appDelegate.active_controller = active_vc;
  278. // };
  279. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:offlineVC] ;
  280. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  281. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  282. [appDelegate.main_vc presentViewController:navi animated:YES completion:^{
  283. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  284. [main_vc checklogin:true];
  285. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  286. DebugLog(@"about present.........");
  287. // self.btop = false;
  288. // <#code#>
  289. }];
  290. }];
  291. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  292. if(self.returnValue)
  293. self.returnValue(true);
  294. [[NSNotificationCenter defaultCenter] postNotificationName:LoginOK_HandleUrgencyFile_Notification object:nil];
  295. }];
  296. [alertControl addAction:actionTwo];
  297. [alertControl addAction:alertthree];
  298. [appDelegate.main_vc presentViewController:alertControl animated:YES completion:nil];
  299. }
  300. else
  301. {
  302. if(self.returnValue)
  303. self.returnValue(true);
  304. [[NSNotificationCenter defaultCenter] postNotificationName:LoginOK_HandleUrgencyFile_Notification object:nil];
  305. }
  306. }];
  307. // if (self.delegate && [self.delegate respondsToSelector:@selector(LoginViewControllerDidLogin:)]) {
  308. // [self.delegate LoginViewControllerDidLogin:self];
  309. // }
  310. [[NSNotificationCenter defaultCenter] postNotificationName:User_LoginOK_Notification object:nil];
  311. }
  312. else
  313. {
  314. NSString* msg=nil;
  315. switch (ret) {
  316. case RESULT_NET_NOTAVAILABLE:
  317. msg = MSG_NET_NOTAVAILABLE;
  318. break;
  319. case RESULT_NET_ERROR:
  320. msg = MSG_NET_ERROR;
  321. break;
  322. case RESULT_FALSE:
  323. msg = MSG_USERAUTH_ERROR;
  324. break;
  325. case RESULT_VER_LOW:
  326. msg = MSG_VER_LOW;
  327. break;
  328. case RESULT_LOGIN_DEVICE:
  329. msg= MSG_LOGIN_DEVICE;
  330. break;
  331. default:
  332. break;
  333. }
  334. [RAUtils message_box:@"Cannot sign-in" message:msg completion:nil];
  335. // [RAUtils alert_view:msg title:@"Cannot sign-in"];
  336. }
  337. // }];
  338. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  339. }];
  340. // }];
  341. return;
  342. }
  343. - (void)didReceiveMemoryWarning
  344. {
  345. [super didReceiveMemoryWarning];
  346. // Dispose of any resources that can be recreated.
  347. }
  348. #pragma mark - Table view data source
  349. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  350. {
  351. return 44;
  352. }
  353. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  354. {
  355. return 1;
  356. }
  357. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  358. {
  359. return self.logindata.count;
  360. }
  361. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  362. {
  363. NSMutableDictionary* up = self.logindata[indexPath.row];
  364. NSString *CellIdentifier = @"LoginListCell";
  365. UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  366. NSString* name=up[@"name"];
  367. cell.textLabel.text = name;
  368. return cell;
  369. }
  370. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  371. {
  372. NSMutableDictionary* up = self.logindata[indexPath.row];
  373. self.edituser.text = up[@"name"];
  374. self.editpass.text = up[@"pwd"];
  375. self.showList=false;
  376. self.Loginlist.hidden=true;
  377. self.checkSavePassword.selected=true;
  378. }
  379. //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  380. //{
  381. //
  382. // return true;
  383. //}
  384. - (void)textFieldDidBeginEditing:(UITextField *)textField
  385. {
  386. self.showList=false;
  387. self.Loginlist.hidden=true;
  388. }
  389. #pragma mark - action
  390. - (IBAction)requestLoginBtnClicked:(UIButton *)sender {
  391. #if defined(BUILD_NPD) || defined(BUILD_USAI) || defined(BUILD_UWAVER)|| defined (BUILD_CONTRAST)
  392. WebViewController *webVC = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"WebViewController"];
  393. webVC.url = URL_REQUEST_LOGIN;
  394. webVC.title = @"Request Login";
  395. webVC.backItemDismiss = YES;
  396. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVC];
  397. nav.modalPresentationStyle = UIModalPresentationFormSheet;
  398. [self presentViewController:nav animated:YES completion:nil];
  399. return;
  400. #endif
  401. #ifdef SCANNER_ORDER
  402. SignUpViewController * vc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"SignUpViewController"];
  403. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  404. nav.modalPresentationStyle = UIModalPresentationFormSheet;
  405. nav.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  406. [self presentViewController:nav animated:YES completion:nil];
  407. return;
  408. #endif
  409. }
  410. @end