LoginViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // LoginViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 25/01/2018.
  6. // Copyright © 2018 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "RetrievePasswordViewController.h"
  10. #import <CoreLocation/CoreLocation.h>
  11. #import "SignatureViewController.h"
  12. @interface LoginViewController ()
  13. @property (strong, nonatomic) IBOutlet UILabel *verLabel;
  14. @property (nonatomic,strong) CLLocationManager *locationManager;
  15. @end
  16. @implementation LoginViewController
  17. + (instancetype)viewControllerFromStoryboard {
  18. LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:[LoginViewController storyboardID]];
  19. return loginVC;
  20. }
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. [self.checkSavePassword setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
  25. [self.checkSavePassword setImage:[UIImage imageNamed:@"check"] forState:UIControlStateSelected];
  26. self.editUser.delegate = self;
  27. self.editPassword.delegate = self;
  28. self.resize = false;
  29. self.ioffset = 0;
  30. self.title=NSLocalizedString(@"Login", nil);
  31. [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  32. // [self.view addSubview:checkSavePassword];
  33. // Do any additional setup after loading the view, typically from a nib.
  34. self.mum.backgroundColor = [UIColor clearColor];
  35. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  36. NSString* shortNum =[infoDict objectForKey:@"CFBundleShortVersionString"];
  37. NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
  38. versionNum = [NSString localizedStringWithFormat:NSLocalizedString(@"ver:%@.A%@", nil),shortNum,versionNum];
  39. self.verLabel.text = versionNum;
  40. // 请求定位授权
  41. [self requestLocationAuthorization];
  42. }
  43. - (void)viewWillAppear:(BOOL)animated {
  44. [super viewWillAppear:animated];
  45. NSString * user = RASingleton.sharedInstance.savedUser;
  46. NSString *password = RASingleton.sharedInstance.savedPassword;
  47. if(user.length>0&&password.length>0)
  48. {
  49. self.editUser.text=user;
  50. self.editPassword.text=password;
  51. }
  52. self.checkSavePassword.selected = RASingleton.sharedInstance.savePassword;
  53. [self clearNavigationbar];
  54. [self configureNavigationBar];
  55. }
  56. - (void)viewDidAppear:(BOOL)animated {
  57. [super viewDidAppear:animated];
  58. if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeNone) {
  59. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:[NSString localizedStringWithFormat:NSLocalizedString(@"Do you want to response %@ location request?", nil),NSLocalizedStringFromTable(@"CFBundleDisplayName", @"InfoPlist", nil)] preferredStyle:UIAlertControllerStyleAlert];
  60. UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Reject", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  61. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeReject;
  62. }];
  63. UIAlertAction *alwaysAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Always ask", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  64. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlwaysAsk;
  65. }];
  66. UIAlertAction *allowAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Allow", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  67. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAllow;
  68. }];
  69. [alertVC addAction:rejectAction];
  70. [alertVC addAction:alwaysAction];
  71. [alertVC addAction:allowAction];
  72. [self presentViewController:alertVC animated:YES completion:nil];
  73. }
  74. }
  75. - (void)didReceiveMemoryWarning {
  76. [super didReceiveMemoryWarning];
  77. // Dispose of any resources that can be recreated.
  78. }
  79. #pragma mark - Location
  80. - (void)requestLocationAuthorization {
  81. if (self.locationManager) {
  82. return;
  83. }
  84. self.locationManager = [[CLLocationManager alloc] init];
  85. self.locationManager.allowsBackgroundLocationUpdates = YES;
  86. [self.locationManager requestAlwaysAuthorization];
  87. }
  88. #pragma mark - Config
  89. - (void)clearNavigationbar {
  90. self.tabBarController.navigationItem.leftBarButtonItem = nil;
  91. self.tabBarController.navigationItem.leftBarButtonItems = nil;
  92. self.tabBarController.navigationItem.titleView = nil;
  93. self.tabBarController.navigationItem.title = nil;
  94. self.tabBarController.navigationItem.rightBarButtonItem = nil;
  95. self.tabBarController.navigationItem.rightBarButtonItems = nil;
  96. }
  97. - (void)configureNavigationBar {
  98. if (self.navigationController && !self.navigationController.isNavigationBarHidden) {
  99. UIImage *logo = [[UIImage imageNamed:@"apex_logo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  100. UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:nil action:nil];
  101. // logoItem.enabled = NO;
  102. self.navigationItem.leftBarButtonItem = logoItem;
  103. }
  104. }
  105. - (void)clear {
  106. self.editPassword.text = nil;
  107. self.editUser.text = nil;
  108. }
  109. #pragma mark - Action
  110. - (IBAction)RetrieveButtonClick:(UIButton *)sender {
  111. // [self performSegueWithIdentifier:@"RETRIEVE" sender:self];
  112. RetrievePasswordViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RetrievePasswordViewController"];
  113. vc.preferredContentSize = CGSizeMake(330, 160);
  114. [self presentViewController:vc animated:YES completion:nil];
  115. }
  116. -(void) test_signature
  117. {
  118. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature"
  119. bundle:[NSBundle mainBundle]]
  120. instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  121. vc.onReturnImg = ^(UIImage* img)
  122. {
  123. // if(img!=nil) {
  124. // model.signature = img;
  125. // if (model.expand) {
  126. //
  127. // if ([model.expand isKindOfClass:[NSDictionary class]]) {
  128. //
  129. // NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  130. // [self expandIndexPath:indexPath withJsonItem:model.expand];
  131. //
  132. // } else if ([model.expand isKindOfClass:[RAEditBaseModel class]]) {
  133. //
  134. // if (((RAEditBaseModel *)model.expand).autofill) {
  135. // [((RAEditBaseModel *)model.expand) updateDefaultValue];
  136. // }
  137. // }
  138. //
  139. // }
  140. // }
  141. };
  142. [self.navigationController pushViewController:vc animated:NO];
  143. }
  144. - (IBAction)LoginButtonClick:(UIButton *)sender {
  145. // return [self test_signature];
  146. NSString*password= self.editPassword.text;
  147. NSString*user = self.editUser.text;
  148. if(user.length==0||password.length==0)
  149. {
  150. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:NSLocalizedString(@"alert_msg_upcanotempty", @"User&Password can not be empty!") preferredStyle:UIAlertControllerStyleAlert];
  151. UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  152. }];
  153. [alertVC addAction:action];
  154. [self presentViewController:alertVC animated:YES completion:nil];
  155. return;
  156. }
  157. DebugLog(@"Login... user= %@ ; password= %@",self.editUser.text,self.editPassword.text);
  158. self.loginButton.enabled = false;
  159. self.mum.hidden=false;
  160. NSString *request_user = self.editUser.text.lowercaseString;
  161. NSString *request_password = self.editPassword.text;
  162. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  163. NSDictionary *loginInfo =[RADataProvider requestLogin:request_user password:request_password];
  164. int ret = [[loginInfo objectForKey:@"result"] intValue];
  165. NSLog(@"login return %d",ret);
  166. dispatch_async(dispatch_get_main_queue(), ^{
  167. self.mum.hidden=true;
  168. self.loginButton.enabled = true;
  169. if (ret==RESULT_TRUE)
  170. {
  171. NSString *firstName = [loginInfo objectForKey:@"firstName"];
  172. NSString *driver = [loginInfo objectForKey:@"driver"];
  173. if(self.loginSuccessful)
  174. self.loginSuccessful(driver,request_password, firstName);
  175. }
  176. else
  177. {
  178. NSString* message = nil;
  179. switch (ret) {
  180. case RESULT_NET_NOTAVAILABLE:
  181. message = NSLocalizedString(@"net_not_available", @"Network not available, please check.");
  182. break;
  183. case RESULT_NET_ERROR:
  184. message = NSLocalizedString(@"net_error", @"Network error, can not access server.");
  185. break;
  186. case RESULT_FALSE:
  187. message = NSLocalizedString(@"auth_error", @"Can not login, user or password may not correct.");
  188. break;
  189. case RESULT_VER_LOW:
  190. message = NSLocalizedString(@"ver_low", @"Current App version is too low, please update.");
  191. break;
  192. default:{
  193. message = [loginInfo objectForKey:@"err_msg"];
  194. if (message.length == 0) {
  195. message=[NSString stringWithFormat:@"Failed to login code %d",ret];
  196. }
  197. }
  198. break;
  199. }
  200. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:message preferredStyle:UIAlertControllerStyleAlert];
  201. UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  202. }];
  203. [alertVC addAction:action];
  204. [self presentViewController:alertVC animated:YES completion:nil];
  205. }
  206. });
  207. });
  208. }
  209. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  210. [[self view] endEditing:YES];
  211. }
  212. -(void)checkboxClick:(UIButton *)btn
  213. {
  214. btn.selected = !btn.selected;
  215. RASingleton.sharedInstance.savePassword = btn.selected;
  216. }
  217. //- (IBAction)onRetrievePassword:(UIButton *)sender {
  218. //}
  219. #pragma mark - TextFied Delegate
  220. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  221. [textField resignFirstResponder];
  222. return YES;
  223. }
  224. -(void) textFieldDidBeginEditing:(UITextField *)textField
  225. {
  226. // CGRect textFrame = self.loginButton.frame;
  227. int loginpos = self.loginButton.frame.origin.y+self.loginButton.frame.size.height;
  228. self.ioffset = 216 -(self.view.frame.size.height-loginpos);
  229. if(self.ioffset>0)
  230. {
  231. self.resize = true;
  232. NSTimeInterval animationDuration = 0.30f;
  233. CGRect frame = self.view.frame;
  234. frame.origin.y -=self.ioffset;//view的Y轴上移
  235. frame.size.height +=self.ioffset; //View的高度增加
  236. self.view.frame = frame;
  237. [UIView beginAnimations:@"ResizeView" context:nil];
  238. [UIView setAnimationDuration:animationDuration];
  239. self.view.frame = frame;
  240. [UIView commitAnimations];//设置调整界面的动画效果
  241. }
  242. }
  243. /**
  244. 结束编辑UITextField的方法,让原来的界面还原高度
  245. */
  246. -(void) textFieldDidEndEditing:(UITextField *)textField
  247. {
  248. // if(prewTag == -1) //当编辑的View不是需要移动的View
  249. // {
  250. // return;
  251. // }
  252. // float moveY ;
  253. if(self.resize)
  254. {
  255. NSTimeInterval animationDuration = 0.30f;
  256. CGRect frame = self.view.frame;
  257. // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
  258. // { //还原界面
  259. // moveY = prewMoveY;
  260. frame.origin.y +=self.ioffset;
  261. frame.size. height -=self.ioffset;
  262. self.view.frame = frame;
  263. // }
  264. //self.view移回原位置
  265. [UIView beginAnimations:@"ResizeView" context:nil];
  266. [UIView setAnimationDuration:animationDuration];
  267. self.view.frame = frame;
  268. [UIView commitAnimations];
  269. [textField resignFirstResponder];
  270. self.ioffset=0;
  271. }
  272. }
  273. - (void)alertTitle:(NSString *)title withMessage:(NSString *)msg {
  274. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
  275. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  276. }];
  277. [alertVC addAction:okAction];
  278. [self presentViewController:alertVC animated:YES completion:nil];
  279. }
  280. @end