LoginViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "RAUtils.h"
  10. #import "ACNetwork.h"
  11. #import "AppDelegate.h"
  12. //#import "RetrievePassViewController.h"
  13. //#import "UILabel+FontAppearance.h"
  14. //#import "OLDataProvider.h"
  15. //#import "NotificationNameCenter.h"
  16. //#import "OfflineSettingViewController.h"
  17. //#import "MainViewController.h"
  18. //#import "WebViewController.h"
  19. @interface LoginViewController ()
  20. @property (strong, nonatomic) IBOutlet UIButton *requestLoginBtn;
  21. @end
  22. @implementation LoginViewController
  23. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  24. {
  25. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  26. if (self) {
  27. // Custom initialization
  28. }
  29. return self;
  30. }
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. self.title=@"Sign in";//@"Granite Expo";
  35. self.showList = false;
  36. self.bb_close.image=[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
  37. [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox"] forState:UIControlStateNormal];
  38. [self.checkSavePassword setImage:[UIImage imageNamed:@"checkbox_check"] forState:UIControlStateSelected];
  39. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  40. NSString * user = [AESCrypt decrypt:[defaults stringForKey:@"user"] password:@"usai"];
  41. NSString * password = [AESCrypt decrypt:[defaults stringForKey:@"password"] password:@"usai"];
  42. if(user.length>0&&password.length>0)
  43. {
  44. self.edituser.text=user;
  45. self.editpass.text=password;
  46. self.checkSavePassword.selected=true;
  47. }
  48. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  49. // [defaults removeObjectForKey:@"EnableOfflineMode"];
  50. // [defaults setBool:self.checkOfflineMode.selected forKey:@"EnableOfflineMode"];
  51. [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  52. // self.checkOfflineMode.tag=999;
  53. // self.logindata = [iSalesDB get_saveduser];
  54. UIButton* combobutton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
  55. [combobutton setBackgroundImage:[UIImage imageNamed:@"combo_24"] forState:UIControlStateNormal];
  56. [combobutton addTarget:self action:@selector(LoginList:) forControlEvents:UIControlEventTouchUpInside];
  57. self.edituser.rightView = combobutton;
  58. self.edituser.rightViewMode=UITextFieldViewModeAlways;
  59. //
  60. // self.Loginlist.layer.borderColor = [UIColor lightGrayColor].CGColor;
  61. // self.Loginlist.layer.borderWidth = 1.0;
  62. // // self.Loginlist.layer.cornerRadius=15;
  63. // self.Loginlist.layer.masksToBounds=true;
  64. // Do any additional setup after loading the view.
  65. }
  66. - (void)LoginList:(UIButton *)sender {
  67. [self.edituser endEditing:true];
  68. self.showList= !self.showList;
  69. // self.Loginlist.hidden = !self.showList;
  70. // showList;
  71. }
  72. -(void)checkboxClick:(UIButton *)btn
  73. {
  74. btn.selected = !btn.selected;
  75. {
  76. if(!btn.selected)
  77. {
  78. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  79. [defaults removeObjectForKey:@"user"];
  80. [defaults removeObjectForKey:@"password"];
  81. [defaults synchronize];
  82. // NSString* encryptu=[AESCrypt encrypt:self.edituser.text password:@"usai"];
  83. // NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
  84. // [iSalesDB execSql:sql];
  85. // self.logindata = [iSalesDB get_saveduser];
  86. // [self.Loginlist reloadData];
  87. }
  88. }
  89. }
  90. - (IBAction)CloseClick:(id)sender {
  91. [self dismissViewControllerAnimated:true completion:nil];
  92. if(self.returnValue)
  93. self.returnValue(false);
  94. }
  95. - (IBAction)onRetrievePassword:(id)sender {
  96. // RetrievePassViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RetrievePassViewController" ];
  97. // // dvc
  98. // // UINavigationController* nv = self.navigationController;
  99. //
  100. // [self.navigationController pushViewController:vc animated:true];
  101. }
  102. - (IBAction)onLoginClick:(UIButton *)sender {
  103. NSString* user = self.edituser.text;
  104. NSString* password = self.editpass.text;
  105. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  106. //
  107. // bool forcedownload = [defaults boolForKey:@"Forcedownload"];
  108. // if(forcedownload && self.checkOfflineMode.selected)
  109. // {
  110. // self.checkOfflineMode.selected = !self.checkOfflineMode.selected;
  111. //
  112. // [RAUtils alert_view:@"Detected incomplete sync, please finish sync first." title:@"Incomplete sync"];
  113. // return;
  114. // }
  115. if(user.length==0||password.length==0)
  116. {
  117. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @"Error!" message:@"user or password can not be empty." delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
  118. // // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  119. //
  120. //
  121. // //[[UILabel appearanceWhenContainedIn:UIAlertView.class, nil] setAppearanceFont:[UIFont boldSystemFontOfSize:10]];
  122. // [alert show];
  123. [RAUtils alert_view:@"Username or Password cannot be blank." title:@"Cannot sign-in"];
  124. return;
  125. }
  126. DebugLog(@"Login... user= %@ ; password= %@",user,password);
  127. self.btnLogin.enabled = false;
  128. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Sign in"];
  129. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  130. int ret=[ACNetwork Authorize:user password:password];
  131. dispatch_async(dispatch_get_main_queue(), ^{
  132. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  133. self.btnLogin.enabled = true;
  134. if(ret==RESULT_TRUE)
  135. {
  136. //
  137. // NSString* contactid=[self.content_data[indexPath.row] valueForKey:@"contact_id"] ;
  138. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  139. // appDelegate.contact_id=contactid;
  140. // appDelegate.contact_name =[self.content_data[indexPath.row] valueForKey:@"name"];
  141. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  142. [defaults removeObjectForKey:@"user"];
  143. [defaults removeObjectForKey:@"password"];
  144. NSString* encryptu=[AESCrypt encrypt:appDelegate.user password:@"usai"];
  145. NSString* encryptp=[AESCrypt encrypt:self.editpass.text password:@"usai"] ;
  146. if(self.checkSavePassword.selected)
  147. {
  148. [defaults setValue:encryptu forKey:@"user"];
  149. [defaults setValue:encryptp forKey:@"password"];
  150. // sqlite3 *db = [iSalesDB get_db];
  151. //
  152. // int count =[iSalesDB get_recordcount:db table:@"login_info" where:[NSString stringWithFormat:@"name='%@'",encryptu]];
  153. //
  154. // if(count==0)
  155. // {
  156. //
  157. // NSString * savelogin=[NSString stringWithFormat:@"insert into login_info(name,pwd) values('%@','%@')",encryptu,encryptp];
  158. // [iSalesDB execSql:savelogin db:db];
  159. // }
  160. // else
  161. // {
  162. // NSString * savelogin=[NSString stringWithFormat:@"update login_info set pwd='%@' where name='%@'",encryptp,encryptu];
  163. // [iSalesDB execSql:savelogin db:db];
  164. // }
  165. //
  166. // [iSalesDB close_db:db];
  167. // [defaults setBool:TRUE forKey:@"autologin"];
  168. }
  169. // else
  170. // {
  171. // NSString* sql=[NSString stringWithFormat:@"delete from login_info where name='%@'",encryptu];
  172. // [iSalesDB execSql:sql];
  173. // }
  174. [defaults synchronize];
  175. [self dismissViewControllerAnimated:YES completion:^{
  176. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  177. // bool offline_dirty=[defaults boolForKey:@"OFFLINE_DIRTY"];
  178. // sqlite3* db = [iSalesDB get_db];
  179. // int offline_user_exist=[iSalesDB get_recordcount:db table:@"offline_login" where:[NSString stringWithFormat:@"username='%@'",appDelegate.user ]];
  180. //
  181. // [iSalesDB close_db:db];
  182. // else
  183. {
  184. if(self.returnValue)
  185. self.returnValue(true);
  186. // [[NSNotificationCenter defaultCenter] postNotificationName:LoginOK_HandleUrgencyFile_Notification object:nil];
  187. }
  188. }];
  189. // if (self.delegate && [self.delegate respondsToSelector:@selector(LoginViewControllerDidLogin:)]) {
  190. // [self.delegate LoginViewControllerDidLogin:self];
  191. // }
  192. // [[NSNotificationCenter defaultCenter] postNotificationName:User_LoginOK_Notification object:nil];
  193. }
  194. else
  195. {
  196. NSString* msg=nil;
  197. switch (ret) {
  198. case RESULT_NET_NOTAVAILABLE:
  199. msg = MSG_NET_NOTAVAILABLE;
  200. break;
  201. case RESULT_NET_ERROR:
  202. msg = MSG_NET_ERROR;
  203. break;
  204. case RESULT_FALSE:
  205. msg = MSG_USERAUTH_ERROR;
  206. break;
  207. case RESULT_VER_LOW:
  208. msg = MSG_VER_LOW;
  209. break;
  210. case RESULT_LOGIN_DEVICE:
  211. msg= MSG_LOGIN_DEVICE;
  212. break;
  213. default:
  214. break;
  215. }
  216. [RAUtils alert_view:msg title:@"Cannot sign-in"];
  217. }
  218. });
  219. });
  220. }
  221. - (void)didReceiveMemoryWarning
  222. {
  223. [super didReceiveMemoryWarning];
  224. // Dispose of any resources that can be recreated.
  225. }
  226. #pragma mark - Table view data source
  227. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  228. {
  229. return 44;
  230. }
  231. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  232. {
  233. return 1;
  234. }
  235. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  236. {
  237. return self.logindata.count;
  238. }
  239. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  240. {
  241. NSMutableDictionary* up = self.logindata[indexPath.row];
  242. NSString *CellIdentifier = @"LoginListCell";
  243. UITableViewCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  244. NSString* name=up[@"name"];
  245. cell.textLabel.text = name;
  246. return cell;
  247. }
  248. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  249. {
  250. // NSMutableDictionary* up = self.logindata[indexPath.row];
  251. // self.edituser.text = up[@"name"];
  252. // self.editpass.text = up[@"pwd"];
  253. //
  254. // self.showList=false;
  255. // self.Loginlist.hidden=true;
  256. // self.checkSavePassword.selected=true;
  257. }
  258. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  259. {
  260. return true;
  261. }
  262. - (void)textFieldDidBeginEditing:(UITextField *)textField
  263. {
  264. self.showList=false;
  265. // self.Loginlist.hidden=true;
  266. }
  267. #pragma mark - action
  268. //- (IBAction)requestLoginBtnClicked:(UIButton *)sender {
  269. //
  270. // WebViewController *webVC = [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];
  271. // webVC.url = URL_REQUEST_LOGIN;
  272. // webVC.title = @"Request Login";
  273. // webVC.backItemDismiss = YES;
  274. //
  275. //
  276. // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webVC];
  277. // nav.modalPresentationStyle = UIModalPresentationFormSheet;
  278. //
  279. // [self presentViewController:nav animated:YES completion:nil];
  280. //
  281. //}
  282. @end