| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- //
- // 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 <sys/types.h>
- #include <sys/param.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #include <netinet/in.h>
- #include <net/if_dl.h>
- #include <sys/sysctl.h>
- @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
|