LoginViewController.m 20 KB

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