LoginViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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 BUILD_HMLG
  32. self.title=@"HMLG";
  33. self.checkOfflineMode.hidden=true;
  34. #endif
  35. #ifdef BUILD_NPD
  36. self.title=@"NPD";
  37. self.checkOfflineMode.hidden=false;
  38. #endif
  39. self.showList = false;
  40. self.bb_close.image=[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
  41. [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
  42. [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox_check"] forState:UIControlStateSelected];
  43. [self.checkOfflineMode setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
  44. [self.checkOfflineMode setImage:[UIImage imageNamed:@"checkbox_check"] forState:UIControlStateSelected];
  45. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  46. NSString * user = [AESCrypt decrypt:[defaults stringForKey:@"user"] password:@"usai"];
  47. NSString * password = [AESCrypt decrypt:[defaults stringForKey:@"password"] password:@"usai"];
  48. if(user.length>0&&password.length>0)
  49. {
  50. self.edituser.text=user;
  51. self.editpass.text=password;
  52. self.checkSavePassword.selected=true;
  53. }
  54. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  55. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  56. // [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  57. self.checkOfflineMode.selected=[defaults boolForKey:@"EnableOfflineMode"];
  58. [self.checkOfflineMode addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  59. [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  60. self.checkOfflineMode.tag=999;
  61. self.logindata = [iSalesDB get_saveduser];
  62. UIButton* combobutton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
  63. [combobutton setBackgroundImage:[UIImage imageNamed:@"combo_24"] forState:UIControlStateNormal];
  64. [combobutton addTarget:self action:@selector(LoginList:) forControlEvents:UIControlEventTouchUpInside];
  65. self.edituser.rightView = combobutton;
  66. self.edituser.rightViewMode=UITextFieldViewModeAlways;
  67. self.Loginlist.layer.borderColor = [UIColor lightGrayColor].CGColor;
  68. self.Loginlist.layer.borderWidth = 1.0;
  69. // self.Loginlist.layer.cornerRadius=15;
  70. self.Loginlist.layer.masksToBounds=true;
  71. // Do any additional setup after loading the view.
  72. }
  73. - (void)LoginList:(UIButton *)sender {
  74. [self.edituser endEditing:true];
  75. self.showList= !self.showList;
  76. self.Loginlist.hidden = !self.showList;
  77. // showList;
  78. }
  79. -(void)checkboxClick:(UIButton *)btn
  80. {
  81. btn.selected = !btn.selected;
  82. if(btn.tag==999)
  83. {
  84. bool haveofflineData=[OLDataProvider check_offlinedata];
  85. if(btn.selected)
  86. {
  87. if(haveofflineData)
  88. {
  89. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  90. bool forcedownload = [defaults boolForKey:@"Forcedownload"];
  91. if(forcedownload)
  92. {
  93. btn.selected = !btn.selected;
  94. [RAUtils alert_view:@"Detected incomplete sync, please finish sync first." title:@"Incomplete sync"];
  95. }
  96. else
  97. {
  98. [defaults removeObjectForKey:@"EnableOfflineMode"];
  99. [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  100. [defaults synchronize];
  101. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  102. appDelegate.offline_mode = self.checkOfflineMode.selected;
  103. }
  104. }
  105. else
  106. {
  107. btn.selected = !btn.selected;
  108. [RAUtils alert_view:@"Offline data not found, you must login online and download offline data first." title:@"Offline data not found"];
  109. }
  110. }
  111. else
  112. {
  113. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  114. [defaults removeObjectForKey:@"EnableOfflineMode"];
  115. [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  116. [defaults synchronize];
  117. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  118. appDelegate.offline_mode = self.checkOfflineMode.selected;
  119. }
  120. }
  121. else
  122. {
  123. if(!btn.selected)
  124. {
  125. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  126. [defaults removeObjectForKey:@"user"];
  127. [defaults removeObjectForKey:@"password"];
  128. [defaults synchronize];
  129. NSString* encryptu=[AESCrypt encrypt:self.edituser.text password:@"usai"];
  130. NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
  131. [iSalesDB execSql:sql];
  132. self.logindata = [iSalesDB get_saveduser];
  133. [self.Loginlist reloadData];
  134. }
  135. }
  136. }
  137. - (IBAction)CloseClick:(id)sender {
  138. [self dismissViewControllerAnimated:true completion:nil];
  139. if(self.returnValue)
  140. self.returnValue(false);
  141. }
  142. - (IBAction)onRetrievePassword:(id)sender {
  143. RetrievePassViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RetrievePassViewController" ];
  144. // dvc
  145. // UINavigationController* nv = self.navigationController;
  146. [self.navigationController pushViewController:vc animated:true];
  147. }
  148. - (IBAction)onLoginClick:(UIButton *)sender {
  149. NSString* user = self.edituser.text;
  150. NSString* password = self.editpass.text;
  151. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  152. bool forcedownload = [defaults boolForKey:@"Forcedownload"];
  153. if(forcedownload && self.checkOfflineMode.selected)
  154. {
  155. self.checkOfflineMode.selected = !self.checkOfflineMode.selected;
  156. [RAUtils alert_view:@"Detected incomplete sync, please finish sync first." title:@"Incomplete sync"];
  157. return;
  158. }
  159. if(user.length==0||password.length==0)
  160. {
  161. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Error!" message:@"user or password can not be empty." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
  162. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  163. //
  164. //
  165. // //[[UILabel appearanceWhenContainedIn:UIAlertView.class, nil] setAppearanceFont:[UIFont boldSystemFontOfSize:10]];
  166. // [alert show];
  167. [RAUtils alert_view:@"Username or Password cannot be blank." title:@"Cannot sign-in"];
  168. return;
  169. }
  170. DebugLog(@"Login... user= %@ ; password= %@",user,password);
  171. self.btnLogin.enabled = false;
  172. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Sign in"];
  173. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  174. int ret=[iSalesNetwork Authorize:user password:password];
  175. dispatch_async(dispatch_get_main_queue(), ^{
  176. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  177. self.btnLogin.enabled = true;
  178. if(ret==RESULT_TRUE)
  179. {
  180. //
  181. // NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  182. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  183. // appDelegate.contact_id=contactid;
  184. // appDelegate.contact_name =[self.content_data[indexPath.row] valueForKey:@"name"];
  185. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  186. [defaults removeObjectForKey:@"user"];
  187. [defaults removeObjectForKey:@"password"];
  188. NSString* encryptu=[AESCrypt encrypt:appDelegate.user password:@"usai"];
  189. NSString* encryptp=[AESCrypt encrypt:self.editpass.text password:@"usai"] ;
  190. if(self.checkSavePassword.selected)
  191. {
  192. [defaults setValue:encryptu forKey:@"user"];
  193. [defaults setValue:encryptp forKey:@"password"];
  194. sqlite3 *db = [iSalesDB get_db];
  195. int count =[iSalesDB get_recordcount:db table:@"login_info" where:[NSString stringWithFormat:@"name='%@'",encryptu]];
  196. if(count==0)
  197. {
  198. NSString * savelogin=[NSString stringWithFormat:@"insert into login_info(name,pwd) values('%@','%@')",encryptu,encryptp];
  199. [iSalesDB execSql:savelogin db:db];
  200. }
  201. else
  202. {
  203. NSString * savelogin=[NSString stringWithFormat:@"update login_info set pwd='%@' where name='%@'",encryptp,encryptu];
  204. [iSalesDB execSql:savelogin db:db];
  205. }
  206. [iSalesDB close_db:db];
  207. // [defaults setBool:TRUE forKey:@"autologin"];
  208. }
  209. else
  210. {
  211. NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
  212. [iSalesDB execSql:sql];
  213. }
  214. [defaults synchronize];
  215. [self dismissViewControllerAnimated:YES completion:^{
  216. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  217. bool offline_dirty=[defaults boolForKey:@"OFFLINE_DIRTY"];
  218. sqlite3* db = [iSalesDB get_db];
  219. int offline_user_exist=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
  220. [iSalesDB close_db:db];
  221. if(offline_dirty&& !appDelegate.offline_mode&& offline_user_exist>0)
  222. {
  223. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Detected new offline data" message:@"Do you want to sync with server?" preferredStyle:UIAlertControllerStyleAlert];
  224. //block代码块取代了delegate
  225. UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"Yes(Recommended)" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  226. UIApplication * app = [UIApplication sharedApplication];
  227. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  228. //UIViewController* active_vc = appDelegate.active_controller;
  229. OfflineSettingViewController * offlineVC =[ [UIStoryboard storyboardWithName:@"OLM" bundle:nil] instantiateViewControllerWithIdentifier:@"OfflineSettingViewController"];
  230. // loginvc.delegate = self;
  231. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  232. // appDelegate.active_controller = gunVC;
  233. // gunVC.onDismissVC = ^(){
  234. // appDelegate.active_controller = active_vc;
  235. // };
  236. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:offlineVC] ;
  237. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  238. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  239. [appDelegate.main_vc presentViewController:navi animated:YES completion:^{
  240. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  241. [main_vc checklogin:true];
  242. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  243. DebugLog(@"about present.........");
  244. // self.btop = false;
  245. // <#code#>
  246. }];
  247. }];
  248. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  249. if(self.returnValue)
  250. self.returnValue(true);
  251. [[NSNotificationCenter defaultCenter] postNotificationName:LoginOK_HandleUrgencyFile_Notification object:nil];
  252. }];
  253. [alertControl addAction:actionTwo];
  254. [alertControl addAction:alertthree];
  255. [appDelegate.main_vc presentViewController:alertControl animated:YES completion:nil];
  256. }
  257. else
  258. {
  259. if(self.returnValue)
  260. self.returnValue(true);
  261. [[NSNotificationCenter defaultCenter] postNotificationName:LoginOK_HandleUrgencyFile_Notification object:nil];
  262. }
  263. }];
  264. // if (self.delegate && [self.delegate respondsToSelector:@selector(LoginViewControllerDidLogin:)]) {
  265. // [self.delegate LoginViewControllerDidLogin:self];
  266. // }
  267. [[NSNotificationCenter defaultCenter] postNotificationName:User_LoginOK_Notification object:nil];
  268. }
  269. else
  270. {
  271. NSString* msg=nil;
  272. switch (ret) {
  273. case RESULT_NET_NOTAVAILABLE:
  274. msg = MSG_NET_NOTAVAILABLE;
  275. break;
  276. case RESULT_NET_ERROR:
  277. msg = MSG_NET_ERROR;
  278. break;
  279. case RESULT_FALSE:
  280. msg = MSG_USERAUTH_ERROR;
  281. break;
  282. case RESULT_VER_LOW:
  283. msg = MSG_VER_LOW;
  284. break;
  285. case RESULT_LOGIN_DEVICE:
  286. msg= MSG_LOGIN_DEVICE;
  287. break;
  288. default:
  289. break;
  290. }
  291. [RAUtils alert_view:msg title:@"Cannot sign-in"];
  292. }
  293. });
  294. });
  295. }
  296. - (void)didReceiveMemoryWarning
  297. {
  298. [super didReceiveMemoryWarning];
  299. // Dispose of any resources that can be recreated.
  300. }
  301. #pragma mark - Table view data source
  302. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  303. {
  304. return 44;
  305. }
  306. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  307. {
  308. return 1;
  309. }
  310. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  311. {
  312. return self.logindata.count;
  313. }
  314. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  315. {
  316. NSMutableDictionary* up = self.logindata[indexPath.row];
  317. NSString *CellIdentifier = @"LoginListCell";
  318. UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  319. NSString* name=up[@"name"];
  320. cell.textLabel.text = name;
  321. return cell;
  322. }
  323. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  324. {
  325. NSMutableDictionary* up = self.logindata[indexPath.row];
  326. self.edituser.text = up[@"name"];
  327. self.editpass.text = up[@"pwd"];
  328. self.showList=false;
  329. self.Loginlist.hidden=true;
  330. self.checkSavePassword.selected=true;
  331. }
  332. //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  333. //{
  334. //
  335. // return true;
  336. //}
  337. - (void)textFieldDidBeginEditing:(UITextField *)textField
  338. {
  339. self.showList=false;
  340. self.Loginlist.hidden=true;
  341. }
  342. #pragma mark - action
  343. - (IBAction)requestLoginBtnClicked:(UIButton *)sender {
  344. #ifdef BUILD_NPD
  345. WebViewController *webVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
  346. webVC.url = URL_REQUEST_LOGIN;
  347. webVC.title = @"Request Login";
  348. webVC.backItemDismiss = YES;
  349. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVC];
  350. nav.modalPresentationStyle = UIModalPresentationFormSheet;
  351. [self presentViewController:nav animated:YES completion:nil];
  352. #endif
  353. }
  354. @end