// // SettingViewController.m // IBOSS // // Created by guan hong hou on 2018/6/25. // Copyright © 2018年 elongtian. All rights reserved. // #import "SettingNewViewController.h" @interface SettingNewViewController (){ /** 缓存文本 */ UILabel *lbSize; } @end @implementation SettingNewViewController - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; } /** viewWillAppear @param animated <#animated description#> */ - (void)viewWillAppear:(BOOL)animated{ // 修改密码消失 if(!_clickFlag){ [self cancelPW]; } [self updateCacheSize:lbSize]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)initUI{ self.navigationItem.title = @"设置"; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0,45,22); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; _clickFlag = true; CGFloat titleHeight = 50; CGFloat lblx = 20; CGFloat fontsize = 14; CGFloat valuey = 13; CGFloat valueheight = 25; CGFloat heightLine =1; //头布局 UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 0)]; [self.view addSubview: headView]; float margin = 5; UIView *viewBackgroud; CGRect cg = CGRectMake(Screen_Width - lblx - 9,valuey+4,9,15 ); // //切换用户 ————————— UIView *vchangeUser = [UIView new]; vchangeUser.frame=CGRectMake(0,0, Screen_Width, titleHeight); [headView addSubview:vchangeUser]; UIImageView *imgview = [UIImageView new]; imgview.frame=CGRectMake(lblx, valuey+margin, 14, 19); [imgview setImage:[UIImage imageNamed:@"setting_change_user"]]; [vchangeUser addSubview:imgview]; UIButton *btnChangeUser = [UIButton buttonWithType:UIButtonTypeCustom]; btnChangeUser.frame=CGRectMake(CGRectGetMaxX(imgview.frame)+2*margin, valuey, Screen_Width - CGRectGetMaxX(imgview.frame), valueheight); btnChangeUser.titleLabel.font = [UIFont systemFontOfSize:fontsize]; btnChangeUser.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [btnChangeUser setTitle:@"切换用户" forState:UIControlStateNormal]; [btnChangeUser setTitleColor:NavTitleColor forState:UIControlStateNormal]; [btnChangeUser addTarget:self action:@selector(changeUser) forControlEvents:UIControlEventTouchUpInside]; [vchangeUser addSubview:btnChangeUser]; UIImageView *bannerView = [[UIImageView alloc] initWithFrame:cg]; [bannerView setImage:[UIImage imageNamed:@"setting_right_arrow"]]; [vchangeUser addSubview:bannerView]; viewBackgroud = [UIView new]; viewBackgroud.frame = CGRectMake(0, CGRectGetMaxY(vchangeUser.frame), Screen_Width, heightLine); viewBackgroud.backgroundColor = LineBackgroundColor; [headView addSubview:viewBackgroud]; // 修改密码 UIView *vchangePw = [UIView new]; vchangePw.frame=CGRectMake(0, CGRectGetMaxY(viewBackgroud.frame), Screen_Width, titleHeight); [headView addSubview:vchangePw]; imgview = [UIImageView new]; imgview.frame=CGRectMake(lblx, valuey+margin, 14, 16); [imgview setImage:[UIImage imageNamed:@"setting_change_pw"]]; [vchangePw addSubview:imgview]; UIButton *btnChangePw = [UIButton buttonWithType:UIButtonTypeCustom]; btnChangePw.frame=CGRectMake(CGRectGetMaxX(imgview.frame)+2*margin, valuey, Screen_Width - CGRectGetMaxX(imgview.frame), valueheight); btnChangePw.titleLabel.font = [UIFont systemFontOfSize:fontsize]; btnChangePw.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [btnChangePw setTitle:@"修改密码" forState:UIControlStateNormal]; [btnChangePw setTitleColor:NavTitleColor forState:UIControlStateNormal]; [btnChangePw addTarget:self action:@selector(changePw) forControlEvents:UIControlEventTouchUpInside]; [vchangePw addSubview:btnChangePw]; bannerView = [[UIImageView alloc] initWithFrame:cg]; [bannerView setImage:[UIImage imageNamed:@"setting_right_arrow"]]; [vchangePw addSubview:bannerView]; viewBackgroud = [UIView new]; viewBackgroud.frame = CGRectMake(0, CGRectGetMaxY(vchangePw.frame), Screen_Width,heightLine); viewBackgroud.backgroundColor = LineBackgroundColor; [headView addSubview:viewBackgroud]; //清除缓存 UIView *vClear = [UIView new]; vClear.frame=CGRectMake(0, CGRectGetMaxY(viewBackgroud.frame), Screen_Width, titleHeight); [headView addSubview:vClear]; imgview = [UIImageView new]; imgview.frame=CGRectMake(lblx-3, valuey+2, 19, 19); [imgview setImage:[UIImage imageNamed:@"icon_clear"]]; [vClear addSubview:imgview]; UIButton *btnClear = [UIButton buttonWithType:UIButtonTypeCustom]; btnClear.frame=CGRectMake(CGRectGetMaxX(imgview.frame)+2*margin-2, valuey, Screen_Width - CGRectGetMaxX(imgview.frame), valueheight); btnClear.titleLabel.font = [UIFont systemFontOfSize:fontsize]; btnClear.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [btnClear setTitle:@"清除缓存" forState:UIControlStateNormal]; [btnClear setTitleColor:NavTitleColor forState:UIControlStateNormal]; [btnClear addTarget:self action:@selector(clearData) forControlEvents:UIControlEventTouchUpInside]; [vClear addSubview:btnClear]; bannerView = [[UIImageView alloc] initWithFrame:cg]; [bannerView setImage:[UIImage imageNamed:@"setting_right_arrow"]]; [vClear addSubview:bannerView]; UILabel *lbSize = [[UILabel alloc] init]; [self updateCacheSize:lbSize]; lbSize.font = [UIFont systemFontOfSize:13]; lbSize.textColor = [UIColor grayColor]; [lbSize sizeToFit]; lbSize.frame = CGRectMake(CGRectGetMaxX(bannerView.frame)- CGRectGetWidth(bannerView.frame)-CGRectGetWidth(lbSize.frame)-10, valuey+4, CGRectGetWidth(lbSize.frame), CGRectGetHeight(lbSize.frame)); [vClear addSubview:lbSize]; viewBackgroud = [UIView new]; viewBackgroud.frame = CGRectMake(0, CGRectGetMaxY(vClear.frame), Screen_Width,heightLine); viewBackgroud.backgroundColor = LineBackgroundColor; [headView addSubview:viewBackgroud]; // 退出登录 UIView *vexit = [UIView new]; vexit.frame=CGRectMake(0, CGRectGetMaxY(viewBackgroud.frame), Screen_Width, titleHeight); [headView addSubview:vexit]; imgview = [UIImageView new]; imgview.frame=CGRectMake(lblx, valuey + margin, 15, 16); [imgview setImage:[UIImage imageNamed:@"setting_exit"]]; [vexit addSubview:imgview]; UIButton *btnExit= [UIButton buttonWithType:UIButtonTypeCustom]; btnExit.frame=CGRectMake(CGRectGetMaxX(imgview.frame)+2*margin, valuey, Screen_Width - CGRectGetMaxX(imgview.frame), valueheight); btnExit.titleLabel.font = [UIFont systemFontOfSize:fontsize]; btnExit.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [btnExit setTitle:@"退出登录" forState:UIControlStateNormal]; [btnExit setTitleColor:NavTitleColor forState:UIControlStateNormal]; [btnExit addTarget:self action:@selector(exitApp) forControlEvents:UIControlEventTouchUpInside]; [vexit addSubview:btnExit]; bannerView = [[UIImageView alloc] initWithFrame:cg]; [bannerView setImage:[UIImage imageNamed:@"setting_right_arrow"]]; [vexit addSubview:bannerView]; viewBackgroud = [UIView new]; viewBackgroud.frame = CGRectMake(0, CGRectGetMaxY(vexit.frame), Screen_Width,heightLine); viewBackgroud.backgroundColor = LineBackgroundColor; [headView addSubview:viewBackgroud]; headView.frame = CGRectMake(0,0, Screen_Width, CGRectGetMaxY(viewBackgroud.frame)); } #pragma mark - 回调函数 /** 密码修改成功回调成功 @param sender <#sender description#> */ - (void)OnPasswordLoadFinish:(ASIDownManager *)sender{ // 取消进度条 [self stopLoading]; // 服务器返回数据 RequestResultModel *resultModel = [RequestResultModel dk_modelWithJSON:sender.mWebStr]; // 服务器返回数据状态值 int iStatus = resultModel.status; // 服务器返回数据消息 NSString *message = resultModel.message; // 服务器返回数据状态值正确 if (iStatus == 0) { _clickFlag = true; [self showAlertViewText:@"密码修改成功"]; [_passwordModify removeFromSuperview]; return; } else{ _clickFlag = false; [self showAlertViewText:message]; return; } } /** 回调失败 @param sender <#sender description#> */ - (void)OnPasswordLoadFail:(ASIDownManager *)sender{ [self stopLoading]; _clickFlag = false; [self showAlertViewText:@"密码修改失败"]; return; } /** 切换用户回调成功 @param sender <#sender description#> */ - (void)onLoadFinish:(ASIDownManager *)sender { [self stopLoading]; // 服务器返回数据转换model RequestResultModel *resultModel = [RequestResultModel dk_modelWithJSON:sender.mWebStr]; // 服务器返回数据状态值 int iStatus = resultModel.status; // 服务器返回数据消息 NSString *message = resultModel.message; // 服务器返回数据状态值正确 if (iStatus == 0) { //个人数据统统滴清除 kkUserPwdOrinal = @""; kkUserPwd = @""; kkUserCode = @""; kkAccountCode = @""; [[UserInfoManager Share]ClearUserData ]; //退回登录界面 [Appdelegate changeViewDidLogout]; } else if(iStatus == ActionResultStatusAuthError ||iStatus == ActionResultStatusNoLogin ||iStatus == ActionResultStatusLogined||iStatus==ActionResultSessionOverdue){ [self showReLoginDialog:message]; } else { [self showAlertViewText:message]; // 退回登录界面 [Appdelegate changeViewDidLogout]; } } /** 切换用户回调失败 @param sender <#sender description#> */ - (void)onLoadFail:(ASIDownManager *)sender { [self stopLoading]; [self showAlertViewText:@"切换用户失败"]; } /** 切换用户 */ - (void)changeUser{ __weak typeof (self)weakself = self; // 初始化-> UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"切换用户" message:@"是否要切换用户吗?" preferredStyle:UIAlertControllerStyleAlert]; // addAction-> UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ weakself.downManager = [[ASIDownManager alloc] init]; weakself.downManager.delegate = self; weakself.downManager.onRequestSuccess = @selector(onLoadFinish:); weakself.downManager.onRequestFail = @selector(onLoadFail:); [weakself startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"SMDLogout" forKey:@"Action"]; [dict setObject:kkAccountCode forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [weakself.downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; }]; // addAction-> UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ }]; UIColor *cancelColor = [UIColor blackColor]; UIColor *sureColor = [UIColor redColor]; [cancelAction setValue:cancelColor forKey:@"titleTextColor"]; [okAction setValue:sureColor forKey:@"titleTextColor"]; [alert addAction:okAction]; [alert addAction:cancelAction]; // 展示-> [self presentViewController:alert animated:YES completion:nil]; } /** 修改密码 */ - (void)changePw{ if(!_clickFlag){ return; } _clickFlag = false; _passwordModify = [DKPWModifyAlterView alterViewWithTitle:@"密码修改" cancel:@"取消" sure:@"确定"]; [self.view addSubview:_passwordModify]; [[_passwordModify sureBt] addTarget:self action:@selector(surePW) forControlEvents:UIControlEventTouchUpInside]; [[_passwordModify cancelBt] addTarget:self action:@selector(cancelPW) forControlEvents:UIControlEventTouchUpInside]; } /** 退出app程序 */ - (void)exitApp{ // NSMutableArray *arr = [NSMutableArray array]; // NSLog(@"%@",arr[10]); if(!_clickFlag){ return; } //初始化提示框; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出程序" message:@"是否要退出登录吗?" preferredStyle:UIAlertControllerStyleAlert ]; // addAction-> UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ [self exit]; }]; // addAction-> UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ }]; UIColor *cancelColor = [UIColor blackColor]; UIColor *sureColor = [UIColor redColor]; [cancelAction setValue:cancelColor forKey:@"titleTextColor"]; [okAction setValue:sureColor forKey:@"titleTextColor"]; [alert addAction:okAction]; [alert addAction:cancelAction]; // 3.显示alertController:presentViewController [self presentViewController:alert animated:YES completion:nil]; } /** 退出登录 */ - (void) exit{ if([[Util getNetWorkStates] isEqualToString:@"无网络"]){ exit(0); return; } [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"SMDLogout" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; self.downManager = [[ASIDownManager alloc] init]; _downManager.delegate = self; _downManager.onRequestSuccess = @selector(onExitFinish:); _downManager.onRequestFail = @selector(onExitFail:); [_downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } - (void) onExitFinish:(ASIDownManager *) sender { exit(0); } - (void) onExitFail:(ASIDownManager *) sender { exit(0); } /** 确定修改密码 */ - (void)surePW{ _originalPassword= _passwordModify.originalPassword.text; if(_originalPassword == nil||[_originalPassword isEqualToString:@""]){ [self showAlertViewText:@"原密码不能为空"]; return; } _newsPassword=_passwordModify.nePassword.text; if(_newsPassword == nil||[_newsPassword isEqualToString:@""]){ [self showAlertViewText:@"新密码不能为空"]; return; } _confirmPassword=_passwordModify.confirmPassword.text; if(_confirmPassword == nil||[_confirmPassword isEqualToString:@""]){ [self showAlertViewText:@"确认密码不能为空"]; return; } if(![_newsPassword isEqualToString:_confirmPassword]){ [self showAlertViewText:@"新密码与确认密码不一致"]; return; } self.downManager = [[ASIDownManager alloc] init]; [self startLoading]; self.downManager.delegate = self; self.downManager.onRequestSuccess = @selector(OnPasswordLoadFinish:); self.downManager.onRequestFail = @selector(OnPasswordLoadFail:); NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"SaveUserPassWordIphone" forKey:@"Action"]; [dict setObject:kkAccountCode forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [dict setObject:_originalPassword forKey:@"OldUserPassWord"]; [dict setObject:_newsPassword forKey:@"NewUserPassword"]; [self.downManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; } /** 取消修改密码 */ - (void)cancelPW{ _clickFlag = true; [_passwordModify removeFromSuperview]; } -(void)clearData{ // // 找到Documents文件夹路径 // NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // NSString *dataFilePath = [documentsPath stringByAppendingPathComponent:@"IBOSSERROR"]; // // NSFileManager *fileManager = [NSFileManager defaultManager]; // // BOOL isDir = NO; // // // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录 // BOOL existed = [fileManager fileExistsAtPath:dataFilePath isDirectory:&isDir]; // NSError *error = nil; // if ( (isDir == YES && existed == YES) ) { // NSArray *fileList = [[NSArray alloc] init]; // fileList = [fileManager contentsOfDirectoryAtPath:dataFilePath error:&error]; // NSString* str = fileList.firstObject; // NSString *resultStr = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",dataFilePath,str] encoding:NSUTF8StringEncoding error:nil]; // NSLog(@"%@",resultStr); // } UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:@"确定要清除所有缓存吗?" preferredStyle:UIAlertControllerStyleAlert ]; // addAction-> UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ [self deleteFile:IBOSSERROR]; [self deleteFile:IBOSSIMAGE]; [self deleteFile:IBOSSSOUND]; [self updateCacheSize:lbSize]; }]; // addAction-> UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ }]; UIColor *cancelColor = [UIColor blackColor]; UIColor *sureColor = [UIColor redColor]; [cancelAction setValue:cancelColor forKey:@"titleTextColor"]; [okAction setValue:sureColor forKey:@"titleTextColor"]; [alert addAction:okAction]; [alert addAction:cancelAction]; // 3.显示alertController:presentViewController [self presentViewController:alert animated:YES completion:nil]; } /** 更新缓存大小 @param lbSize <#lbSize description#> */ - (void)updateCacheSize:(UILabel *) lbSize{ CGFloat sizeIBOSSERROR = [self folderSizeAtPath: IBOSSERROR]; CGFloat sizeIBOSSIMAGE = [self folderSizeAtPath: IBOSSIMAGE]; CGFloat sizeIBOSSSOUND = [self folderSizeAtPath: IBOSSSOUND]; CGFloat sum = sizeIBOSSERROR+sizeIBOSSIMAGE+sizeIBOSSSOUND; if(sum == 0){ lbSize.hidden = YES; }else{ lbSize.hidden = NO; } lbSize.text = [NSString stringWithFormat:@"约%.2fM",sum]; [lbSize sizeToFit]; } /** 删除Document下文件夹 @param fileName <#fileName description#> */ - (void)deleteFile:(NSString *) fileName{ NSFileManager* fileManager=[NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); //文件名 NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:fileName]; BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath]; if (!blHave) { NSLog(@"no have"); return ; }else { NSLog(@" have"); BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil]; if (blDele) { NSLog(@"dele success"); }else { NSLog(@"dele fail"); } } } //遍历文件夹获得文件夹大小,返回多少M - (float )folderSizeAtPath:(NSString*) folderPath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); //文件名 folderPath=[[paths objectAtIndex:0] stringByAppendingPathComponent:folderPath]; NSFileManager* manager = [NSFileManager defaultManager]; if (![manager fileExistsAtPath:folderPath]) return 0; NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator]; NSString* fileName; long long folderSize = 0; while ((fileName = [childFilesEnumerator nextObject]) != nil){ NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName]; folderSize += [self fileSizeAtPath:fileAbsolutePath]; } return folderSize/(1024.0*1024.0); } //单个文件的大小 - (long long) fileSizeAtPath:(NSString*) filePath{ NSFileManager* manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:filePath]){ return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]; } return 0; } @end