// // LoginViewController.m // IBOSSmini // // Created by guan hong hou on 2017/5/4. // Copyright © 2017年 elongtian. All rights reserved. // #import "LoginViewController.h" #import "ServerSettingViewController.h" #include #include #include #include #include #include #include #include @interface LoginViewController () @end @implementation LoginViewController /** viewDidLoad */ - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; [self checkAutoLogin]; _txtAccount.text=kkAccountCode; _txtUserName.text=kkUserCode; _txtPassword.text=kkUserPwdOrinal; if(_autoLogin) { double delayInSeconds = 0.5; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self gotoLogin]; }); } } /** viewWillDisappear @param animated <#animated description#> */ -(void)viewWillDisappear:(BOOL)animated { //[self.navigationController.navigationBar setHidden:NO]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } /** viewDidAppear @param animated <#animated description#> */ -(void)viewDidAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil]; } /** 初始化函数 @return <#return value description#> */ - (void)initUI{ UIView *contentView=[[UIView alloc]init]; [contentView setBackgroundColor:[UIColor whiteColor]]; contentView.frame=CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height); UIImage *pic=[UIImage imageNamed:@"icon_logo"]; [self.view addSubview:contentView]; UIImageView *bannerView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.width*pic.size.height/pic.size.width)]; [bannerView setImage:pic]; // bannerView.contentMode = UIViewContentModeScaleAspectFill; // bannerView.autoresizingMask = UIViewAutoresizingFlexibleHeight; // bannerView.clipsToBounds = YES; [contentView addSubview:bannerView]; pic=[UIImage imageNamed:@"loginbg"]; float inputHeight = (self.view.frame.size.width-46)*pic.size.height/pic.size.width; UIImageView *loginBgImg=[[UIImageView alloc] initWithFrame:CGRectMake(23,self.view.frame.size.height/4,self.view.frame.size.width-46,inputHeight)]; [loginBgImg setImage:pic]; [contentView addSubview:loginBgImg]; float lbly = CGRectGetMinY(loginBgImg.frame) + inputHeight / 3 / 3; UIImageView *accountImg=[[UIImageView alloc] initWithFrame:CGRectMake(66,lbly,16,17.5)]; [accountImg setImage:[UIImage imageNamed:@"icon_account"]]; [contentView addSubview:accountImg]; _txtAccount=[UITextField new]; _txtAccount.textAlignment = NSTextAlignmentCenter; _txtAccount.font = [UIFont systemFontOfSize:TextFontOfSize]; _txtAccount.frame=CGRectMake(CGRectGetMaxX(accountImg.frame)+10,lbly, self.view.frame.size.width - 2*( CGRectGetMaxX(accountImg.frame) + 10), 20); [contentView addSubview:_txtAccount]; _txtAccount.placeholder=@"帐套"; UIView *accountSeparatorView=[[UIView alloc]init]; [accountSeparatorView setBackgroundColor:[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1]]; accountSeparatorView.frame=CGRectMake(50, CGRectGetMinY(loginBgImg.frame) + inputHeight / 3-1, loginBgImg.frame.size.width-60, 1); [contentView addSubview:accountSeparatorView]; lbly = CGRectGetMinY(loginBgImg.frame) + inputHeight / 3 / 3 + inputHeight / 3; UIImageView *userNameImg=[[UIImageView alloc] initWithFrame:CGRectMake(66,lbly,16,18)]; [userNameImg setImage:[UIImage imageNamed:@"icon_username"]]; [contentView addSubview:userNameImg]; _txtUserName=[UITextField new]; _txtUserName.placeholder=@"用户名"; _txtUserName.font = [UIFont systemFontOfSize:TextFontOfSize]; _txtUserName.textAlignment = NSTextAlignmentCenter; _txtUserName.frame=CGRectMake(CGRectGetMaxX(userNameImg.frame)+10,lbly, self.view.frame.size.width - 2*( CGRectGetMaxX(userNameImg.frame) + 10), 20); [contentView addSubview:_txtUserName]; UIView *userNameSeparatorView=[[UIView alloc]init]; [userNameSeparatorView setBackgroundColor:[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1]]; userNameSeparatorView.frame=CGRectMake(50, CGRectGetMinY(loginBgImg.frame) + inputHeight * 2/ 3-1, loginBgImg.frame.size.width-60, 1); [contentView addSubview:userNameSeparatorView]; lbly = CGRectGetMinY(loginBgImg.frame) + inputHeight / 3 / 3 + inputHeight *2 / 3; UIImageView *passwordImg=[[UIImageView alloc] initWithFrame:CGRectMake(66,lbly,15,20)]; [passwordImg setImage:[UIImage imageNamed:@"icon_password"]]; [contentView addSubview:passwordImg]; _txtPassword=[UITextField new]; _txtPassword.frame=CGRectMake(CGRectGetMaxX(passwordImg.frame) + 10,lbly, self.view.frame.size.width - 2*( CGRectGetMaxX(passwordImg.frame) + 10), 20); _txtPassword.placeholder=@"密码"; _txtPassword.font = [UIFont systemFontOfSize:TextFontOfSize]; _txtPassword.textAlignment = NSTextAlignmentCenter; _txtPassword.secureTextEntry = YES; [contentView addSubview:_txtPassword]; _autoLoginBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal]; _autoLoginBtn.frame=CGRectMake(self.view.frame.size.width/2-50,CGRectGetMaxY(loginBgImg.frame)+20,20,20); [_autoLoginBtn addTarget:self action:@selector(changeAutoState) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview: _autoLoginBtn]; UILabel *autoLoginLbl=[[UILabel alloc]init]; autoLoginLbl.frame=CGRectMake(CGRectGetMaxX(_autoLoginBtn.frame)+10,CGRectGetMaxY(loginBgImg.frame)+18,80, 20); autoLoginLbl.text=@"自动登录"; [contentView addSubview:autoLoginLbl]; pic=[UIImage imageNamed:@"bt_login"]; _btnlogin=[UIButton buttonWithType:UIButtonTypeCustom]; [_btnlogin setBackgroundImage:pic forState:UIControlStateNormal]; _btnlogin.frame=CGRectMake(self.view.frame.size.width/2-100,CGRectGetMaxY(_autoLoginBtn.frame)+18,200,200*pic.size.height/pic.size.width); [ _btnlogin addTarget:self action:@selector(gotoLogin) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview: _btnlogin]; pic=[UIImage imageNamed:@"bt_set"]; _btnlset=[UIButton buttonWithType:UIButtonTypeCustom]; [_btnlset setBackgroundImage:pic forState:UIControlStateNormal]; _btnlset.frame=CGRectMake(self.view.frame.size.width/2-100,CGRectGetMaxY(_btnlogin.frame)+18,200,200*pic.size.height/pic.size.width); [_btnlset addTarget:self action:@selector(gotoSet) forControlEvents:UIControlEventTouchUpInside]; [contentView addSubview: _btnlset]; UILabel *versionLbl=[[UILabel alloc]init]; versionLbl.frame=CGRectMake(contentView.frame.size.width/2-40,self.view.frame.size.height-60,100, 20); versionLbl.textAlignment = NSTextAlignmentCenter; NSDictionary *infoDic = [[NSBundle mainBundle]infoDictionary]; NSString *strVer = [NSString stringWithFormat:@"Version:%@",[infoDic objectForKey:@"CFBundleShortVersionString"]]; versionLbl.text = strVer; versionLbl.font = [UIFont systemFontOfSize:10]; versionLbl.textColor=[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1]; [contentView addSubview:versionLbl]; UILabel *versionDescLbl=[[UILabel alloc]init]; versionDescLbl.textAlignment = NSTextAlignmentCenter; versionDescLbl.frame=CGRectMake(contentView.frame.size.width/2-140,self.view.frame.size.height-40,280, 20); versionDescLbl.text=@"版权所有:北京东科企信软件有限公司@dongkesoft-2017"; versionDescLbl.textColor=[UIColor colorWithRed:213.0/255.0 green:213.0/255.0 blue:213.0/255.0 alpha:1]; versionDescLbl.font = [UIFont systemFontOfSize:10]; [contentView addSubview:versionDescLbl]; } /** 键盘弹出 */ -(void)keyboardWillShow { // Animate the current view out of the way if(!_keyboardShow){ [UIView animateWithDuration:0.3f animations:^ { 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); }];} _keyboardShow=YES; } /** 键盘隐藏 */ -(void)keyboardWillHide { // Animate the current view back to its original position if(_keyboardShow){ [UIView animateWithDuration:0.3f animations:^ { 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); }];} _keyboardShow=NO; } /** 触摸事件 @param touches <#touches description#> @param event <#event description#> */ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } /** 成功回调 @param sender <#sender description#> */ - (void)onLoadFinish:(ASIDownManager *)sender { NSDictionary *dic = [sender.mWebStr JSONValue]; [self cancel]; if (dic && [dic isKindOfClass:[NSDictionary class]]) { int iStatus = [[dic objectForKey:@"Status"] intValue]; NSString *message=[dic objectForKey:@"Message"]; if (iStatus == 0) { //返回值正确,登陆成功 [self automatic:dic]; kkUserPwdOrinal=_txtPassword.text; kkUserPwd=[self md5:_txtPassword.text]; kkUserCode=_txtUserName.text; kkAccountCode=_txtAccount.text; kkUserName=[dic objectForKey:@"UserName"]; kkUserID=[dic objectForKey:@"UserId"]; kkIsMini=[dic objectForKey:@"IsMini"];//"1为mini版" 其它的不是mini版 kkLicenseCode =[dic objectForKey:@"LicenseCode"]; NSInteger organizationIdValue= [[dic objectForKey:@"OrgID"]integerValue]; kkOrganizationId=[NSString stringWithFormat:@"%d",organizationIdValue]; kkOrganizationCode=[dic objectForKey:@"OrgCode"]; kkOrganizationName=[dic objectForKey:@"OrgName"]; //服务器端没有匹配,暂时注释掉 NSObject *dicRights = [dic objectForKey:@"Rights"]; if([dicRights isKindOfClass:[NSArray class]]){ NSArray* rights= (NSArray*) dicRights; NSMutableArray *rightsArray=[[NSMutableArray alloc]init]; if(rights != nil && rights.count > 0){ NSString *str = @""; for (int i = 0; i < rights.count; i++) { NSDictionary *dic = rights[i]; NSString *code = [dic objectForKey:@"FunctionCode"]; [rightsArray addObject:code]; } str = [rightsArray componentsJoinedByString:@","]; kkRights = str; } } else{ kkRights=@""; } [Appdelegate changeViewDidLogin]; } else { if(message==nil||message.length==0) { message=@"登录失败"; } [self showAlertViewText:message]; } } } /** 回调失败 @param sender <#sender description#> */ - (void)onLoadFail:(ASIDownManager *)sender { [self cancel]; [self showAlertViewText:@"登录失败"]; } /** 取消进度条 */ - (void)cancel { [self stopLoading]; } /** 用户数据 @param dic */ - (void)automatic:(NSDictionary *)dic { NSMutableDictionary *userinfodic = [NSMutableDictionary dictionaryWithDictionary:dic]; NSMutableDictionary *dict=[NSMutableDictionary new]; [dict setObject:userinfodic forKey:@"userinfo"]; NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; [userDef setObject:dict forKey:@"dataDic"]; [userDef synchronize]; } /** 状态改变 */ -(void)changeAutoState{ if(kkAutoLogin!=nil) { if([[NSString stringWithFormat:@"%@",kkAutoLogin] isEqualToString:@"1"]) { [ _autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal]; kkAutoLogin=@"0"; } else{ [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal]; kkAutoLogin=@"1"; } } else { [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal]; kkAutoLogin=@"1"; } } /** 校验 */ -(void)checkAutoLogin { if(kkAutoLogin!=nil) { if([[NSString stringWithFormat:@"%@",kkAutoLogin] isEqualToString:@"1"]) { [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"checked"] forState:UIControlStateNormal]; } else{ [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal]; } } else { [_autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"unchecked"] forState:UIControlStateNormal]; } } /** md5加密算法 @param str <#str description#> @return <#return value description#> */ - (NSString *) md5:(NSString *)str { const char *cStr = [str UTF8String]; unsigned char result[16]; CC_MD5( cStr,(CC_LONG) strlen(cStr), result ); return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15] ]; } /** 登陆校验 */ -(void)gotoLogin{ //检测是否为空 if(_txtAccount.text.length==0) { [self showAlertViewText:@"帐套不能为空"]; return; } if(_txtUserName.text.length==0) { [self showAlertViewText:@"用户名不能为空"]; return; } [self goToLogin]; } /** 登录 */ -(void)goToLogin { self.mDownManager = [[ASIDownManager alloc] init]; self.mDownManager.delegate = self; self.mDownManager.OnImageDown = @selector(onLoadFinish:); self.mDownManager.OnImageFail = @selector(onLoadFail:); if(kkServerUrl==nil||kkServerUrl.length==0||kkServerPort==nil||kkServerPort.length==0)//提示输入地址和端口 { [self showAlertViewText:@"您服务器地址和端口尚未配置,请配置完再登录"]; return; } [self startLoading]; [self showLoadingText:@"正在登录..."]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; /* 登录 */ [dict setObject:@"SMDLogin" forKey:@"Action"]; [dict setObject:_txtAccount.text forKey:@"AccountCode"]; [dict setObject:_txtUserName.text forKey:@"UserCode"]; [dict setObject:[self md5:_txtPassword.text ] forKey:@"UserPassword"]; [dict setObject:[self getIponeMAC] forKey:@"MACAddress"]; [dict setObject:[self IPAddress] forKey:@"IPAddress"]; [dict setObject:@"00000000" forKey:@"PhoneCode"]; // [dict setObject:[[[UIDevice currentDevice] identifierForVendor] UUIDString] forKey:@"PhoneCode"]; [dict setObject:PhoneType forKey:@"PhoneType"]; [dict setObject:AppVersion forKey:@"AppVersion"]; [dict setObject:@"IOS" forKey:@"SystemType"]; [dict setObject:SystemVersion forKey:@"SystemVersion"]; [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } /** 设置按钮事件 */ -(void)gotoSet{ ServerSettingViewController *setVC=[[ServerSettingViewController alloc] init]; [self presentViewController:setVC animated:YES completion:nil]; } /** 获取设备Mac地址 @return return value description */ - (NSString *)getIponeMAC{ int mib[6]; size_t len; char *buf; unsigned char *ptr; struct if_msghdr *ifm; struct sockaddr_dl *sdl; mib[0] = CTL_NET; mib[1] = AF_ROUTE; mib[2] = 0; mib[3] = AF_LINK; mib[4] = NET_RT_IFLIST; if ((mib[5] = if_nametoindex("en0")) == 0) { printf("Error: if_nametoindex error/n"); return NULL; } if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { printf("Error: sysctl, take 1/n"); return NULL; } if ((buf = malloc(len)) == NULL) { printf("Could not allocate memory. error!/n"); return NULL; } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { printf("Error: sysctl, take 2"); return NULL; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned char *)LLADDR(sdl); NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)]; // NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)]; free(buf); NSLog(@"-------mac %@", outstring); return [outstring uppercaseString]; } @end