// // PBLoginViewController.m // IphoneBIMe // // Created by zjf on 2018/7/16. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBLoginViewController.h" #import "PBNavigationController.h" #import "PBAgreementViewController.h" #import "PBNetworkModel.h" #import #import "PGGCryptoDES.h" #import "PBEnterpriseCodeController.h" #define bim365Code @"300012" @interface PBLoginViewController () @property (nonatomic, weak) UILabel *codeL; @property (nonatomic, weak) UITextField *userNameTF; @property (nonatomic, weak) UITextField *passwordTF; @property (nonatomic, weak) UILabel *titleL; @property (nonatomic, weak) UIButton *loginBtn; @property (nonatomic, assign) BOOL keyBoardIsShow; @property (nonatomic, strong) NSDictionary *bim365Data; @property (nonatomic, strong) NSDictionary *customData; @property (nonatomic, assign) int logoImageVTop; @property (nonatomic, assign) int bgViewTop; @end @implementation PBLoginViewController - (void)viewDidLoad { [super viewDidLoad]; // [PBNoteCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // [PBNoteCenter addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil]; NSDictionary *dict = [NSString getApiData]; if (dict) { self.customData = dict; }else { self.customData = self.bim365Data; [self storedLocallyApiWithData:self.customData]; NSArray *arr = @[self.customData]; [self saveApiArr:arr]; } [self setupUI]; [self setApi]; } - (void)keyboardWillShow:(NSNotification *)notification { if (self.keyBoardIsShow) { return; } self.keyBoardIsShow = YES; [UIView animateWithDuration:1 animations:^{ CGFloat titleTop; if (IS_IPHONE_X) { titleTop = 12; }else { titleTop = 12 + 24; } [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(titleTop); }]; [self.loginBtn mas_updateConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.passwordTF.mas_bottom).offset(20); }]; [self.view layoutIfNeeded]; }]; } - (void)keyboardWillHidden:(NSNotification *)notification{ if (!self.keyBoardIsShow) { return; } self.keyBoardIsShow = NO; [UIView animateWithDuration:1 animations:^{ CGFloat titleTop; if (IS_IPHONE_X) { titleTop = 50; }else { titleTop = 50 + 24; } [self.titleL mas_updateConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(titleTop); }]; [self.loginBtn mas_updateConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.passwordTF.mas_bottom).offset(80); }]; [self.view layoutIfNeeded]; }]; } - (void)setupUI { self.view.backgroundColor = PBColor(244, 245, 246); UILabel *titleL = [UILabel z_labelWithText:@"欢迎登录" Color:PBColor(40, 58, 79) isBold:YES Font:26]; [self.view addSubview:titleL]; CGFloat titleTop; if (IS_IPHONE_X) { titleTop = 50; }else { titleTop = 50 + 24; } [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(titleTop); make.left.equalTo(self.view).offset(30); make.size.mas_equalTo(CGSizeMake(108, 40)); }]; UIView *codeBg = [[UIView alloc] init]; codeBg.backgroundColor = [UIColor whiteColor]; [self.view addSubview:codeBg]; [codeBg mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleL.mas_bottom).offset(30); make.left.equalTo(self.view).offset(30); make.right.equalTo(self.view).offset(-30); make.height.equalTo(@60); }]; codeBg.layer.cornerRadius = 6; codeBg.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(codeBtnAction)]; [codeBg addGestureRecognizer:tap]; UIView *userBg = [[UIView alloc] init]; userBg.backgroundColor = [UIColor whiteColor]; [self.view addSubview:userBg]; [userBg mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(codeBg.mas_bottom).offset(20); make.left.equalTo(self.view).offset(30); make.right.equalTo(self.view).offset(-30); make.height.equalTo(@60); }]; userBg.layer.cornerRadius = 6; UIView *passwordBg = [[UIView alloc] init]; passwordBg.backgroundColor = [UIColor whiteColor]; [self.view addSubview:passwordBg]; [passwordBg mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(userBg.mas_bottom).offset(20); make.left.equalTo(self.view).offset(30); make.right.equalTo(self.view).offset(-30); make.height.equalTo(@60); }]; passwordBg.layer.cornerRadius = 6; UIImageView *codeImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coding"]]; [self.view addSubview:codeImageV]; [codeImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(24, 24)); make.left.equalTo(codeBg).offset(20); make.centerY.equalTo(codeBg); }]; UILabel *codeL = [UILabel z_labelWithText:@"BIMe协作平台" Color:PBColor(40, 58, 79) isBold:YES Font:16]; [self.view addSubview:codeL]; [codeL mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(codeBg).offset(64); make.top.bottom.equalTo(codeBg); make.right.equalTo(codeBg).offset(-50); }]; UIImageView *arrow_right = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow_right"]]; [self.view addSubview:arrow_right]; [arrow_right mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(codeBg); make.size.mas_equalTo(CGSizeMake(24, 24)); make.right.equalTo(codeBg).offset(-20); }]; UIImageView *userImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"account"]]; [self.view addSubview:userImageV]; [userImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(userBg); make.size.mas_equalTo(CGSizeMake(24, 24)); make.left.equalTo(userBg).offset(20); }]; UITextField *userTF = [[UITextField alloc] init]; [self.view addSubview:userTF]; userTF.placeholder = @"请输入邮箱/手机号/账号"; userTF.textColor = PBColor(40, 58, 79); userTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation; userTF.clearButtonMode = UITextFieldViewModeAlways; userTF.delegate = self; userTF.returnKeyType = UIReturnKeyNext; userTF.font = [UIFont fontWithName:Coarse size:16]; [userTF mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(userBg).offset(64); make.top.bottom.equalTo(userBg); make.right.equalTo(userBg).offset(-20); }]; UIImageView *passwordImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"password"]]; [self.view addSubview:passwordImageV]; [passwordImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(passwordBg); make.size.mas_equalTo(CGSizeMake(24, 24)); make.left.equalTo(passwordBg).offset(20); }]; UITextField *passwordTF = [[UITextField alloc] init]; [self.view addSubview:passwordTF]; passwordTF.placeholder = @"请输入密码"; passwordTF.textColor = PBColor(40, 58, 79); passwordTF.keyboardType = UIKeyboardTypeASCIICapable; passwordTF.secureTextEntry = YES; // passwordTF.clearButtonMode = UITextFieldViewModeAlways; passwordTF.enablesReturnKeyAutomatically = YES; passwordTF.returnKeyType = UIReturnKeyDone; passwordTF.delegate = self; passwordTF.font = [UIFont fontWithName:Coarse size:16]; [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(passwordBg).offset(64); make.top.bottom.equalTo(passwordBg); make.right.equalTo(passwordBg).offset(-50); }]; UIButton *encryptionBtn = [UIButton z_bgNormalImage:[UIImage imageNamed:@"invisible"] bgSelectedImage:[UIImage imageNamed:@"visible"]]; [encryptionBtn addTarget:self action:@selector(enctyptionAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:encryptionBtn]; [encryptionBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(passwordBg); make.size.mas_equalTo(CGSizeMake(24, 24)); make.right.equalTo(passwordBg).offset(-20); }]; UIButton *loginBtn = [[UIButton alloc] init]; [loginBtn setImage:[UIImage imageNamed:@"Login_loginBtn_diss"] forState:UIControlStateDisabled]; [loginBtn setImage:[UIImage imageNamed:@"Login_loginBtn_norm"] forState:UIControlStateNormal]; loginBtn.enabled = NO; [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:loginBtn]; [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(passwordBg.mas_bottom).offset(80); make.left.equalTo(self.view).offset(30); make.right.equalTo(self.view).offset(-30); make.height.equalTo(@60); }]; // [loginBtn circleViewWithRadius:6]; UILabel *agreeL = [UILabel z_labelWithText:@"登录代表同意" Color:DescColor isBold:YES Font:12]; [self.view addSubview:agreeL]; [agreeL mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view).offset(-20); make.left.equalTo(self.view).offset(47); make.size.mas_equalTo(CGSizeMake(78, 16)); }]; UIButton *agreeBtn = [UIButton z_textButton:@"《管理平台用户协议》和《隐私政策》" boldFontSize:12 normalColor:PBColor(0, 122, 255)]; [agreeBtn addTarget:self action:@selector(agreementAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:agreeBtn]; [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.view).offset(-20); make.left.equalTo(agreeL.mas_right); make.height.equalTo(@16); }]; self.codeL = codeL; self.userNameTF = userTF; self.passwordTF = passwordTF; self.titleL = titleL; self.loginBtn = loginBtn; } - (void)enctyptionAction:(UIButton *)button { button.selected = !button.selected; if (button.selected) { self.passwordTF.secureTextEntry = NO; }else { self.passwordTF.secureTextEntry = YES; } } #pragma mark -查看协议 - (void)agreementAction { PBAgreementViewController *agreementVC = [[PBAgreementViewController alloc] init]; PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:agreementVC]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:nav animated:YES completion:nil]; } #pragma mark - 编辑企业码 - (void)codeBtnAction { PBEnterpriseCodeController *codeVC = [[PBEnterpriseCodeController alloc] init]; codeVC.SetCodeBlock = ^(NSString * _Nonnull code, NSDictionary * _Nonnull obj) { self.customData = obj; NSMutableArray *arrM = [[NSMutableArray alloc] init]; NSArray *arr = [NSString getApiArrData]; BOOL isHave = false; for (NSDictionary *dict in arr) { NSString *haveCode = [dict valueForKey:@"Code"]; if ([code isEqualToString:haveCode]) { [arrM addObject:obj]; isHave = true; } else { [dict setValue:@"0" forKey:@"Check"]; [arrM addObject:dict]; } } if (!isHave) { [arrM addObject:self.customData]; } [self saveApiArr:arrM.copy]; [self storedLocallyApiWithData:self.customData]; [self setApi]; }; codeVC.UpdataCodeBlock = ^(NSInteger index) { NSMutableArray *arrM = [[NSMutableArray alloc] init]; NSArray *arr = [NSString getApiArrData]; for (NSInteger i = 0; i < arr.count; i++) { NSDictionary *dict = arr[i]; if (i == index) { [dict setValue:@"1" forKey:@"Check"]; }else { [dict setValue:@"0" forKey:@"Check"]; } [arrM addObject:dict]; } self.customData = arrM[index]; [self saveApiArr:arrM.copy]; [self storedLocallyApiWithData:self.customData]; [self setApi]; }; codeVC.DeleteCodeBlock = ^(NSInteger index) { NSMutableArray *arrM = [[NSString getApiArrData] mutableCopy]; [arrM removeObjectAtIndex:index]; [self saveApiArr:arrM.copy]; }; PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:codeVC]; [self presentViewController:nav animated:YES completion:nil]; // UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请输入企业编码" message:@"为空即设为BIMe数据协作平台服务" preferredStyle:UIAlertControllerStyleAlert]; // UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // // [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil]; // [self getEnterpriseWithCode:alertVC.textFields.firstObject.text]; // }]; // // determineAction.enabled = NO; // [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { // // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; // textField.keyboardType = UIKeyboardTypeNumberPad; // textField.placeholder = @"请输入企业编码"; // }]; // [alertVC addAction:cancelAction]; // [alertVC addAction:determineAction]; // [self presentViewController:alertVC animated:YES completion:nil]; } //- (void)alertTextFieldDidChange:(NSNotification *)notification{ // UIAlertController *alertController = (UIAlertController *)self.presentedViewController; // if (alertController) { // UITextField *codeTF = alertController.textFields.firstObject; // UIAlertAction *determineAction = alertController.actions.lastObject; // determineAction.enabled = codeTF.text.length > 0; // } //} //#pragma mark - 更改密码 //- (void)forgotPasswordAction { // NSLog(@"更改密码"); // //} #pragma mark - 登录 - (void)loginAction:(UIButton *)button { [YJProgressHUD showProgress:@"正在登录" inView:self.view]; [[PBNetworkTools sharedTools] loginWithUserName:self.userNameTF.text andPassword:self.passwordTF.text andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"登录失败" inView:self.view]; return ; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { // [YJProgressHUD hide]; //存储本地 [self saveTokenToLocalWith:[networkModel.Data valueForKey:@"token"]]; [self savePasswordWith:self.userNameTF.text andPassword:self.passwordTF.text]; [JPUSHService setAlias:[networkModel.Data valueForKey:@"alpha"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) { NSLog(@"=============设置别名:%@",iAlias); NSLog(@"%zd",seq); } seq:1]; [self loadToken]; }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; } - (void)loadToken { [[PBNetworkTools sharedTools] getOnlyTestTokenAndCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载登录人信息失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [array lastObject]; NSString *documentPath = [documents stringByAppendingPathComponent:@"login.plist"]; NSDictionary *dict = @{ @"RealName":[networkModel.Data valueForKey:@"RealName"], @"Account":[networkModel.Data valueForKey:@"Account"], @"UserId":[networkModel.Data valueForKey:@"apisession_userid"] }; [dict writeToFile:documentPath atomically:YES]; if (self.LoginSuccessfulBlock) { self.LoginSuccessfulBlock(); } [self dismissViewControllerAnimated:YES completion:nil]; }else { [YJProgressHUD showMessage:@"加载登录人信息失败" inView:self.view]; } }]; } - (void)saveTokenToLocalWith:(NSString *)token { NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [array lastObject]; NSString *documentPath = [documents stringByAppendingPathComponent:@"token.plist"]; NSDictionary *dict = @{ @"token":token }; [dict writeToFile:documentPath atomically:YES]; } - (void)savePasswordWith:(NSString *)name andPassword:(NSString *)pass { NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [array lastObject]; NSString *documentPath = [documents stringByAppendingPathComponent:@"info.plist"]; NSDictionary *dict = @{ @"username":name, @"pass":pass }; [dict writeToFile:documentPath atomically:YES]; } - (void)setApi { self.codeL.text = [self.customData valueForKey:@"CompanyName"]; } - (void)storedLocallyApiWithData:(NSDictionary *)data { NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [array lastObject]; NSString *documentPath = [documents stringByAppendingPathComponent:@"api.plist"]; [data writeToFile:documentPath atomically:YES]; } - (void)saveApiArr:(NSArray *)arr { NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [array lastObject]; NSString *documentPath = [documents stringByAppendingPathComponent:@"apiArr.plist"]; [arr writeToFile:documentPath atomically:YES]; } #pragma mark - UITextFieldDelegate - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSInteger textlength = textField.text.length; if ([string isEqualToString:@""]) { textlength -= range.length; }else { textlength += string.length; } if (textlength == 0){ self.loginBtn.enabled = NO; return YES; } if (textField == self.userNameTF) { if (self.passwordTF.text.length == 0) { self.loginBtn.enabled = NO; }else { self.loginBtn.enabled = YES; } }else { if (self.userNameTF.text.length == 0) { self.loginBtn.enabled = NO; }else { self.loginBtn.enabled = YES; } } return YES; } - (BOOL)textFieldShouldClear:(UITextField *)textField { self.loginBtn.enabled = NO; return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField == _userNameTF) { [_passwordTF becomeFirstResponder]; }else { [self.view endEditing:NO]; } return YES; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:NO]; } - (NSDictionary *)bim365Data { if (_bim365Data == nil) { // _bim365Data = @{ // @"ModelApi" : @"https://bimcomposer.bim365.com.cn:443", // @"NewApi" : @"https://newapi.bim365.com.cn:443", // @"AppApi" : @"https://api.bim365.com.cn:443", // @"Web" : @"https://www.bim365.com.cn:443", // @"NewWeb" : @"https://www.bim365.com.cn", // @"LogoUrl" : @"https://www.bim365.com.cn:443/Content/function/Login/images/Simple_logo.png", // @"CompanyName" : @"BIMe数据协作平台", // @"Code" : bim365Code, // @"StarName" : @"STAR_bim365_com_cn", // @"Check" : @"1" // }; _bim365Data = @{ @"ModelApi" : @"http://39.129.29.216:6081", @"Panorama": @"errurl", @"NewApi" : @"http://39.129.29.216:6082", @"AppApi" : @"errurl", @"Web" : @"http://39.129.29.216:6080", @"NewWeb" : @"http://39.129.29.216:6080", @"LogoUrl" : @"http://39.129.29.216:6080/Content/function/Login/images/Simple_logo.png", @"CompanyName" : @"管理平台", @"Code" : bim365Code, @"StarName" : @"", @"Check" : @"1" }; /** ModelApi : "https://bimcomposer.bim365.com.cn:443" NewApi : "https://newapi.bim365.com.cn:443" AppApi : "https://api.bim365.com.cn:443" Web : "https://www.bim365.com.cn:443" NewWeb : "https://www.bim365.com.cn" LogoUrl : "https://www.bim365.com.cn:443/Content/function/Login/images/Simple_logo.png" CompanyName : "BIMe数据协作平台" StarName : "STAR_bim365_com_cn" @"ModelApi" : @"http://119.3.217.232:81", @"NewApi" : @"https://www.probim.cn:8080", @"AppApi" : @"http://119.3.217.232:82", @"Web" : @"https://www.probim.cn:8076", @"NewWeb" : @"http://www.probim.cn:8888", @"LogoUrl" : @"http://119.3.217.232:8075/Content/function/Login/images/Simple_logosl.png", @"CompanyName" : @"BIMe数据协作平台", @"Code" : bim365Code, @"StarName" : @"STAR_bim365_com_cn", @"Check" : @"1" */ } return _bim365Data; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end