LoginViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. //
  2. // LoginViewController.m
  3. // IBOSSmini
  4. //
  5. // Created by guan hong hou on 2017/5/4.
  6. // Copyright © 2017年 elongtian. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "ServerSettingViewController.h"
  10. #include <sys/types.h>
  11. #include <sys/param.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include <net/if.h>
  15. #include <netinet/in.h>
  16. #include <net/if_dl.h>
  17. #include <sys/sysctl.h>
  18. @interface LoginViewController ()
  19. @end
  20. @implementation LoginViewController
  21. /**
  22. viewDidLoad
  23. */
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self initUI];
  27. [self checkAutoLogin];
  28. _txtAccount.text=kkAccountCode;
  29. _txtUserName.text=kkUserCode;
  30. _txtPassword.text=kkUserPwdOrinal;
  31. if(_autoLogin)
  32. {
  33. double delayInSeconds = 0.5;
  34. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
  35. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  36. [self gotoLogin];
  37. });
  38. }
  39. }
  40. /**
  41. viewWillDisappear
  42. @param animated <#animated description#>
  43. */
  44. -(void)viewWillDisappear:(BOOL)animated
  45. {
  46. //[self.navigationController.navigationBar setHidden:NO];
  47. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification
  48. object:nil];
  49. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification
  50. object:nil];
  51. }
  52. /**
  53. viewDidAppear
  54. @param animated <#animated description#>
  55. */
  56. -(void)viewDidAppear:(BOOL)animated
  57. {
  58. [[NSNotificationCenter defaultCenter] addObserver:self
  59. selector:@selector(keyboardWillShow)
  60. name:UIKeyboardWillShowNotification
  61. object:nil];
  62. [[NSNotificationCenter defaultCenter] addObserver:self
  63. selector:@selector(keyboardWillHide)
  64. name:UIKeyboardWillHideNotification
  65. object:nil];
  66. }
  67. /**
  68. 初始化函数
  69. @return <#return value description#>
  70. */
  71. - (void)initUI{
  72. UIView *contentView=[[UIView alloc]init];
  73. [contentView setBackgroundColor:[UIColor whiteColor]];
  74. contentView.frame=CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
  75. UIImage *pic=[UIImage imageNamed:@"icon_logo"];
  76. [self.view addSubview:contentView];
  77. UIImageView *bannerView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.width*pic.size.height/pic.size.width)];
  78. [bannerView setImage:pic];
  79. // bannerView.contentMode = UIViewContentModeScaleAspectFill;
  80. // bannerView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  81. // bannerView.clipsToBounds = YES;
  82. [contentView addSubview:bannerView];
  83. pic=[UIImage imageNamed:@"loginbg"];
  84. float inputHeight = (self.view.frame.size.width-46)*pic.size.height/pic.size.width;
  85. UIImageView *loginBgImg=[[UIImageView alloc] initWithFrame:CGRectMake(23,self.view.frame.size.height/4,self.view.frame.size.width-46,inputHeight)];
  86. [loginBgImg setImage:pic];
  87. [contentView addSubview:loginBgImg];
  88. float lbly = CGRectGetMinY(loginBgImg.frame) + inputHeight / 3 / 3;
  89. UIImageView *accountImg=[[UIImageView alloc] initWithFrame:CGRectMake(66,lbly,16,17.5)];
  90. [accountImg setImage:[UIImage imageNamed:@"icon_account"]];
  91. [contentView addSubview:accountImg];
  92. _txtAccount=[UITextField new];
  93. _txtAccount.textAlignment = NSTextAlignmentCenter;
  94. _txtAccount.font = [UIFont systemFontOfSize:TextFontOfSize];
  95. _txtAccount.frame=CGRectMake(CGRectGetMaxX(accountImg.frame)+10,lbly, self.view.frame.size.width - 2*( CGRectGetMaxX(accountImg.frame) + 10), 20);
  96. [contentView addSubview:_txtAccount];
  97. _txtAccount.placeholder=@"帐套";
  98. UIView *accountSeparatorView=[[UIView alloc]init];
  99. [accountSeparatorView setBackgroundColor:[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1]];
  100. accountSeparatorView.frame=CGRectMake(50, CGRectGetMinY(loginBgImg.frame) + inputHeight / 3-1, loginBgImg.frame.size.width-60, 1);
  101. [contentView addSubview:accountSeparatorView];
  102. lbly = CGRectGetMinY(loginBgImg.frame) + inputHeight / 3 / 3 + inputHeight / 3;
  103. UIImageView *userNameImg=[[UIImageView alloc] initWithFrame:CGRectMake(66,lbly,16,18)];
  104. [userNameImg setImage:[UIImage imageNamed:@"icon_username"]];
  105. [contentView addSubview:userNameImg];
  106. _txtUserName=[UITextField new];
  107. _txtUserName.placeholder=@"用户名";
  108. _txtUserName.font = [UIFont systemFontOfSize:TextFontOfSize];
  109. _txtUserName.textAlignment = NSTextAlignmentCenter;
  110. _txtUserName.frame=CGRectMake(CGRectGetMaxX(userNameImg.frame)+10,lbly, self.view.frame.size.width - 2*( CGRectGetMaxX(userNameImg.frame) + 10), 20);
  111. [contentView addSubview:_txtUserName];
  112. UIView *userNameSeparatorView=[[UIView alloc]init];
  113. [userNameSeparatorView setBackgroundColor:[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1]];
  114. userNameSeparatorView.frame=CGRectMake(50, CGRectGetMinY(loginBgImg.frame) + inputHeight * 2/ 3-1, loginBgImg.frame.size.width-60, 1);
  115. [contentView addSubview:userNameSeparatorView];
  116. lbly = CGRectGetMinY(loginBgImg.frame) + inputHeight / 3 / 3 + inputHeight *2 / 3;
  117. UIImageView *passwordImg=[[UIImageView alloc] initWithFrame:CGRectMake(66,lbly,15,20)];
  118. [passwordImg setImage:[UIImage imageNamed:@"icon_password"]];
  119. [contentView addSubview:passwordImg];
  120. _txtPassword=[UITextField new];
  121. _txtPassword.frame=CGRectMake(CGRectGetMaxX(passwordImg.frame) + 10,lbly, self.view.frame.size.width - 2*( CGRectGetMaxX(passwordImg.frame) + 10), 20);
  122. _txtPassword.placeholder=@"密码";
  123. _txtPassword.font = [UIFont systemFontOfSize:TextFontOfSize];
  124. _txtPassword.textAlignment = NSTextAlignmentCenter;
  125. _txtPassword.secureTextEntry = YES;
  126. [contentView addSubview:_txtPassword];
  127. _autoLoginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  128. [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
  129. _autoLoginBtn.frame=CGRectMake(self.view.frame.size.width/2-50,CGRectGetMaxY(loginBgImg.frame)+20,20,20);
  130. [_autoLoginBtn addTarget:self action:@selector(changeAutoState) forControlEvents:UIControlEventTouchUpInside];
  131. [contentView addSubview: _autoLoginBtn];
  132. UILabel *autoLoginLbl=[[UILabel alloc]init];
  133. autoLoginLbl.frame=CGRectMake(CGRectGetMaxX(_autoLoginBtn.frame)+10,CGRectGetMaxY(loginBgImg.frame)+18,80, 20);
  134. autoLoginLbl.text=@"自动登录";
  135. [contentView addSubview:autoLoginLbl];
  136. pic=[UIImage imageNamed:@"bt_login"];
  137. _btnlogin=[UIButton buttonWithType:UIButtonTypeCustom];
  138. [_btnlogin setBackgroundImage:pic forState:UIControlStateNormal];
  139. _btnlogin.frame=CGRectMake(self.view.frame.size.width/2-100,CGRectGetMaxY(_autoLoginBtn.frame)+18,200,200*pic.size.height/pic.size.width);
  140. [ _btnlogin addTarget:self action:@selector(gotoLogin) forControlEvents:UIControlEventTouchUpInside];
  141. [contentView addSubview: _btnlogin];
  142. pic=[UIImage imageNamed:@"bt_set"];
  143. _btnlset=[UIButton buttonWithType:UIButtonTypeCustom];
  144. [_btnlset setBackgroundImage:pic forState:UIControlStateNormal];
  145. _btnlset.frame=CGRectMake(self.view.frame.size.width/2-100,CGRectGetMaxY(_btnlogin.frame)+18,200,200*pic.size.height/pic.size.width);
  146. [_btnlset addTarget:self action:@selector(gotoSet) forControlEvents:UIControlEventTouchUpInside];
  147. [contentView addSubview: _btnlset];
  148. UILabel *versionLbl=[[UILabel alloc]init];
  149. versionLbl.frame=CGRectMake(contentView.frame.size.width/2-40,self.view.frame.size.height-60,100, 20);
  150. versionLbl.textAlignment = NSTextAlignmentCenter;
  151. NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary];
  152. NSString *strVer = [NSString stringWithFormat:@"Version:%@",[infoDic objectForKey:@"CFBundleShortVersionString"]];
  153. versionLbl.text = strVer;
  154. versionLbl.font = [UIFont systemFontOfSize:10];
  155. versionLbl.textColor=[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1];
  156. [contentView addSubview:versionLbl];
  157. UILabel *versionDescLbl=[[UILabel alloc]init];
  158. versionDescLbl.textAlignment = NSTextAlignmentCenter;
  159. versionDescLbl.frame=CGRectMake(contentView.frame.size.width/2-140,self.view.frame.size.height-40,280, 20);
  160. versionDescLbl.text=@"版权所有:北京东科企信软件有限公司@dongkesoft-2017";
  161. versionDescLbl.textColor=[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1];
  162. versionDescLbl.font = [UIFont systemFontOfSize:10];
  163. [contentView addSubview:versionDescLbl];
  164. }
  165. /**
  166. 键盘弹出
  167. */
  168. -(void)keyboardWillShow {
  169. // Animate the current view out of the way
  170. if(!_keyboardShow){
  171. [UIView animateWithDuration:0.3f animations:^ {
  172. self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-160, self.view.frame.size.width, self.view.frame.size.height);
  173. }];}
  174. _keyboardShow=YES;
  175. }
  176. /**
  177. 键盘隐藏
  178. */
  179. -(void)keyboardWillHide {
  180. // Animate the current view back to its original position
  181. if(_keyboardShow){
  182. [UIView animateWithDuration:0.3f animations:^ {
  183. self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+160, self.view.frame.size.width, self.view.frame.size.height);
  184. }];}
  185. _keyboardShow=NO;
  186. }
  187. /**
  188. 触摸事件
  189. @param touches <#touches description#>
  190. @param event <#event description#>
  191. */
  192. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  193. {
  194. [self.view endEditing:YES];
  195. }
  196. /**
  197. 成功回调
  198. @param sender <#sender description#>
  199. */
  200. - (void)onLoadFinish:(ASIDownManager *)sender {
  201. NSDictionary *dic = [sender.mWebStr JSONValue];
  202. [self cancel];
  203. if (dic && [dic isKindOfClass:[NSDictionary class]]) {
  204. int iStatus = [[dic objectForKey:@"Status"] intValue];
  205. NSString *message=[dic objectForKey:@"Message"];
  206. if (iStatus == 0) {
  207. //返回值正确,登陆成功
  208. [self automatic:dic];
  209. kkUserPwdOrinal=_txtPassword.text;
  210. kkUserPwd=[self md5:_txtPassword.text];
  211. kkUserCode=_txtUserName.text;
  212. kkAccountCode=_txtAccount.text;
  213. kkUserName=[dic objectForKey:@"UserName"];
  214. kkUserID=[dic objectForKey:@"UserId"];
  215. kkIsMini=[dic objectForKey:@"IsMini"];//"1为mini版" 其它的不是mini版
  216. kkLicenseCode =[dic objectForKey:@"LicenseCode"];
  217. NSInteger organizationIdValue= [[dic objectForKey:@"OrgID"]integerValue];
  218. kkOrganizationId=[NSString stringWithFormat:@"%d",organizationIdValue];
  219. kkOrganizationCode=[dic objectForKey:@"OrgCode"];
  220. kkOrganizationName=[dic objectForKey:@"OrgName"];
  221. //服务器端没有匹配,暂时注释掉
  222. NSObject *dicRights = [dic objectForKey:@"Rights"];
  223. if([dicRights isKindOfClass:[NSArray class]]){
  224. NSArray* rights= (NSArray*) dicRights;
  225. NSMutableArray *rightsArray=[[NSMutableArray alloc]init];
  226. if(rights != nil && rights.count > 0){
  227. NSString *str = @"";
  228. for (int i = 0; i < rights.count; i++) {
  229. NSDictionary *dic = rights[i];
  230. NSString *code = [dic objectForKey:@"FunctionCode"];
  231. [rightsArray addObject:code];
  232. }
  233. str = [rightsArray componentsJoinedByString:@","];
  234. kkRights = str;
  235. }
  236. }
  237. else{
  238. kkRights=@"";
  239. }
  240. [Appdelegate changeViewDidLogin];
  241. }
  242. else
  243. {
  244. if(message==nil||message.length==0)
  245. {
  246. message=@"登录失败";
  247. }
  248. [self showAlertViewText:message];
  249. }
  250. }
  251. }
  252. /**
  253. 回调失败
  254. @param sender <#sender description#>
  255. */
  256. - (void)onLoadFail:(ASIDownManager *)sender {
  257. [self cancel];
  258. [self showAlertViewText:@"登录失败"];
  259. }
  260. /**
  261. 取消进度条
  262. */
  263. - (void)cancel {
  264. [self stopLoading];
  265. }
  266. /**
  267. 用户数据
  268. @param dic
  269. */
  270. - (void)automatic:(NSDictionary *)dic
  271. {
  272. NSMutableDictionary *userinfodic = [NSMutableDictionary dictionaryWithDictionary:dic];
  273. NSMutableDictionary *dict=[NSMutableDictionary new];
  274. [dict setObject:userinfodic forKey:@"userinfo"];
  275. NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
  276. [userDef setObject:dict forKey:@"dataDic"];
  277. [userDef synchronize];
  278. }
  279. /**
  280. 状态改变
  281. */
  282. -(void)changeAutoState{
  283. if(kkAutoLogin!=nil)
  284. {
  285. if([[NSString stringWithFormat:@"%@",kkAutoLogin] isEqualToString:@"1"])
  286. {
  287. [ _autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
  288. kkAutoLogin=@"0";
  289. }
  290. else{
  291. [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
  292. kkAutoLogin=@"1";
  293. }
  294. }
  295. else
  296. {
  297. [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
  298. kkAutoLogin=@"1";
  299. }
  300. }
  301. /**
  302. 校验
  303. */
  304. -(void)checkAutoLogin
  305. {
  306. if(kkAutoLogin!=nil)
  307. {
  308. if([[NSString stringWithFormat:@"%@",kkAutoLogin] isEqualToString:@"1"])
  309. {
  310. [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal];
  311. }
  312. else{
  313. [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
  314. }
  315. }
  316. else
  317. {
  318. [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal];
  319. }
  320. }
  321. /**
  322. md5加密算法
  323. @param str <#str description#>
  324. @return <#return value description#>
  325. */
  326. - (NSString *) md5:(NSString *)str
  327. {
  328. const char *cStr = [str UTF8String];
  329. unsigned char result[16];
  330. CC_MD5( cStr,(CC_LONG) strlen(cStr), result );
  331. return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  332. result[0], result[1], result[2], result[3],
  333. result[4], result[5], result[6], result[7],
  334. result[8], result[9], result[10], result[11],
  335. result[12], result[13], result[14], result[15]
  336. ];
  337. }
  338. /**
  339. 登陆校验
  340. */
  341. -(void)gotoLogin{
  342. //检测是否为空
  343. if(_txtAccount.text.length==0)
  344. {
  345. [self showAlertViewText:@"帐套不能为空"];
  346. return;
  347. }
  348. if(_txtUserName.text.length==0)
  349. {
  350. [self showAlertViewText:@"用户名不能为空"];
  351. return;
  352. }
  353. [self goToLogin];
  354. }
  355. /**
  356. 登录
  357. */
  358. -(void)goToLogin
  359. {
  360. self.mDownManager = [[ASIDownManager alloc] init];
  361. self.mDownManager.delegate = self;
  362. self.mDownManager.OnImageDown = @selector(onLoadFinish:);
  363. self.mDownManager.OnImageFail = @selector(onLoadFail:);
  364. if(kkServerUrl==nil||kkServerUrl.length==0||kkServerPort==nil||kkServerPort.length==0)//提示输入地址和端口
  365. {
  366. [self showAlertViewText:@"您服务器地址和端口尚未配置,请配置完再登录"];
  367. return;
  368. }
  369. [self startLoading];
  370. [self showLoadingText:@"正在登录..."];
  371. NSString *urlStr = ServerURL;
  372. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  373. /* 登录 */
  374. [dict setObject:@"SMDLogin" forKey:@"Action"];
  375. [dict setObject:_txtAccount.text forKey:@"AccountCode"];
  376. [dict setObject:_txtUserName.text forKey:@"UserCode"];
  377. [dict setObject:[self md5:_txtPassword.text ] forKey:@"UserPassword"];
  378. [dict setObject:[self getIponeMAC] forKey:@"MACAddress"];
  379. [dict setObject:[self IPAddress] forKey:@"IPAddress"];
  380. [dict setObject:@"00000000" forKey:@"PhoneCode"];
  381. // [dict setObject:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"PhoneCode"];
  382. [dict setObject:PhoneType forKey:@"PhoneType"];
  383. [dict setObject:AppVersion forKey:@"AppVersion"];
  384. [dict setObject:@"IOS" forKey:@"SystemType"];
  385. [dict setObject:SystemVersion forKey:@"SystemVersion"];
  386. [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];
  387. }
  388. /**
  389. 设置按钮事件
  390. */
  391. -(void)gotoSet{
  392. ServerSettingViewController *setVC=[[ServerSettingViewController alloc] init];
  393. [self presentViewController:setVC animated:YES completion:nil];
  394. }
  395. /**
  396. 获取设备Mac地址
  397. @return return value description
  398. */
  399. - (NSString *)getIponeMAC{
  400. int mib[6];
  401. size_t len;
  402. char *buf;
  403. unsigned char *ptr;
  404. struct if_msghdr *ifm;
  405. struct sockaddr_dl *sdl;
  406. mib[0] = CTL_NET;
  407. mib[1] = AF_ROUTE;
  408. mib[2] = 0;
  409. mib[3] = AF_LINK;
  410. mib[4] = NET_RT_IFLIST;
  411. if ((mib[5] = if_nametoindex("en0")) == 0) {
  412. printf("Error: if_nametoindex error/n");
  413. return NULL;
  414. }
  415. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  416. printf("Error: sysctl, take 1/n");
  417. return NULL;
  418. }
  419. if ((buf = malloc(len)) == NULL) {
  420. printf("Could not allocate memory. error!/n");
  421. return NULL;
  422. }
  423. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  424. printf("Error: sysctl, take 2");
  425. return NULL;
  426. }
  427. ifm = (struct if_msghdr *)buf;
  428. sdl = (struct sockaddr_dl *)(ifm + 1);
  429. ptr = (unsigned char *)LLADDR(sdl);
  430. NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  431. // NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  432. free(buf);
  433. NSLog(@"-------mac %@", outstring);
  434. return [outstring uppercaseString];
  435. }
  436. @end