// // MeCenterVC.m // LNManager // // Created by EchoShacolee on 2017/4/6. // Copyright © 2017年 lee. All rights reserved. // #import "MeCenterVC.h" #import "LoginVC.h" #import "NSString+ex.h" #import "InputView.h" #import "AboutVC.h" #import "MyMessageVC.h" //客服 #import "QMChatRoomViewController.h" @interface MeCenterVC () { NSArray *_contentArr; //要显示的内容 InputView *_inputVi; //照片选择 UIImageView *_headImgV; //头像 //客服 BOOL _isFirstClick; // 判断点击状态 BOOL _isPushed; // 判断跳转状态 } @end @implementation MeCenterVC - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"我的"; self.navigationController.navigationBar.translucent = NO; _contentArr = @[@[@""], @[@"我的资料",@"修改密码"],//,@"绑定手机" @[@"联系客服",@"关于我们"],//@"使用手册", @[@"退出登录"] ]; [self myInit]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; _isPushed = YES; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; _isPushed = NO; self.navigationController.navigationBar.barTintColor = nil; } -(void)myInit{ UITableView * tableview = [[UITableView alloc]initWithFrame:kFrame style:UITableViewStyleGrouped]; [tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"me_cellID"]; tableview.delegate = self; tableview.dataSource = self; tableview.showsVerticalScrollIndicator = NO; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 60)]; tableview.tableFooterView = view; [self.view addSubview:tableview]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:CUSTOM_LOGIN_SUCCEED object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginFaile:) name:CUSTOM_LOGIN_ERROR_USER object:nil]; _isFirstClick = YES; } #pragma mark 照片选择 -(void)tapClick{ InputView* vi = [[InputView alloc] initWithTitle:@"请选择头像"]; [vi setDelegate:self]; [vi show]; _inputVi = vi; } -(void)InputView:(InputView *)view didGetImage:(UIImage *)image { [self upLoadImgWithImage:image]; } -(void)InputViewWillPickImage:(InputView *)view { UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { pickerImage.sourceType = view.type; // pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType]; } pickerImage.delegate = self; pickerImage.allowsEditing = NO; [self presentViewController:pickerImage animated:YES completion:nil]; } - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [_inputVi cancelAction]; [picker dismissViewControllerAnimated:YES completion:^{ UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; [self upLoadImgWithImage:originalImage]; }]; } #pragma mark 头像上传 -(void)upLoadImgWithImage:(UIImage *)image { if (!image) { return; } NSData * data; if (UIImageJPEGRepresentation(image, .5)) { //图片质量 压缩成多少 data = UIImageJPEGRepresentation(image, .5); }else{ data = UIImagePNGRepresentation(image); } NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; NSMutableDictionary * mDic = [NSMutableDictionary new]; [mDic setValue:@"1" forKey:@"type"]; [mDic setValue:MYAPPDELEGATE.userDic[@"id"] forKey:@"keyword"]; [mDic setValue:encodedImageStr forKey:@"content"]; [self getDataWithDic:mDic method:@"upload-1" block:^(NSDictionary *successDic) { [self showMsgByMBWithString:@"修改头像成功"]; [_headImgV sd_setImageWithURL:[NSURL URLWithString:successDic[@"body"]] placeholderImage:[UIImage imageNamed:@"me_DefaultHead"]]; [MYAPPDELEGATE.userDic setValue:successDic[@"body"] forKey:@"photo"]; }]; } #pragma mark tableview代理相关 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _contentArr.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [_contentArr[section] count]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { return 120; } return 50; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"me_cellID" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.section == 0) { // if (!_headImgV) { // _headImgV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 90, 90)]; _headImgV.layer.masksToBounds = YES; _headImgV.layer.cornerRadius = 45; NSString *imgStr = MYAPPDELEGATE.userDic[@"photo"]; if (!imgStr) { imgStr = @""; } [_headImgV sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"me_DefaultHead"]]; [cell.contentView addSubview:_headImgV]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)]; tap.numberOfTapsRequired = 1; _headImgV.userInteractionEnabled = YES; [_headImgV addGestureRecognizer:tap]; // UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(120, 35, 200, 50)]; label.text = MYAPPDELEGATE.userDic[@"realName"]; [cell.contentView addSubview:label]; cell.accessoryType = UITableViewCellAccessoryNone; } }else{ cell.textLabel.text = _contentArr[indexPath.section][indexPath.row]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 1){ if (indexPath.row == 0) { //我的资料 MyMessageVC *vc = [[MyMessageVC alloc]init]; [self.navigationController pushViewController:vc animated:YES]; return; return; } if (indexPath.row == 1) { //修改密码 [self fixKey]; return; } }else if (indexPath.section == 2){ if (indexPath.row == 0) { //联系客服 if (_isFirstClick == NO) { return; } _isFirstClick = NO; [MBProgressHUD showHUDAddedTo:self.view animated:NO]; // userId 只能使用 数字 字母(包括大小写) 下划线 NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSString *name = [NSString stringWithFormat:@"%@[辽I监管_%@]",MYAPPDELEGATE.userDic[@"realName"],version]; [QMConnect registerSDKWithAppKey:@"32958350-4f40-11e7-a416-f12e96c638e0" userName:name userId:[NSString stringWithFormat:@"%@",MYAPPDELEGATE.userDic[@"id"]]]; return; } if (indexPath.row == 1) { AboutVC *vc = [[AboutVC alloc]init]; [self.navigationController pushViewController:vc animated:YES]; return; } }else if (indexPath.section == 3 && indexPath.row == 0){ //退出登录 UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:nil message:@"您确定要退出登录吗" preferredStyle:UIAlertControllerStyleAlert]; [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [alertFind addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [self loginAciton]; }]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tabBarController presentViewController:alertFind animated:true completion:nil]; }); } } #pragma mark 修改密码 -(void)fixKey { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证原密码" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { textField.placeholder = @"请输入原密码"; }]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSString *text = [[alert.textFields firstObject] text]; NSString *key = MYAPPDELEGATE.userDic[@"password"]; if (![[text md5Encrypt] isEqualToString:key]) { [self showMsgByMBWithString:@"原密码错误!"]; return; } [self willModifyPwd]; }]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tabBarController presentViewController:alert animated:true completion:nil]; }); } -(void)willModifyPwd{ UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:@"修改密码" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"请输入新密码"; textField.secureTextEntry = YES; }]; [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"请确认密码"; textField.secureTextEntry = YES; }]; [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alertFind addAction:[UIAlertAction actionWithTitle:@"确定修改" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { // 读取文本框的值显示出来 UITextField *newtf1 = alertFind.textFields[0]; UITextField *newtf = alertFind.textFields[1]; if (newtf1.text.length < 6 || newtf.text.length < 6) { [self showMsgByMBWithString:@"密码长度不能少于6个字符"]; return; } if (![newtf1.text isEqualToString:newtf.text]) { UIAlertController *noname = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不一致,请您核查" preferredStyle:UIAlertControllerStyleAlert]; [noname addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tabBarController presentViewController:noname animated:true completion:nil]; }); return; } [self chanagePasswordWithOldStr:MYAPPDELEGATE.userDic[@"password"] newStr:[newtf.text md5Encrypt]]; }]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tabBarController presentViewController:alertFind animated:true completion:nil]; }); } -(void)chanagePasswordWithOldStr:(NSString *)oldpwd newStr:(NSString *)newpwd{ NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:MYAPPDELEGATE.userDic[@"id"] forKey:@"id"]; [mdic setValue:oldpwd forKey:@"oldPwd"]; [mdic setValue:newpwd forKey:@"newPwd"]; [self getDataWithDic:mdic method:@"modifyPwd" block:^(NSDictionary *successDic) { //修改成功,返回登录界面,重新登录 UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:@"修改成功,请重新登录" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; [ud setObject:@"" forKey:@"userPassWord"]; [ud synchronize];//先这样吧。如果非要清除显示占用的密码再做处理 [self.tabBarController dismissViewControllerAnimated:YES completion:nil]; }]]; [self.tabBarController presentViewController:alert animated:YES completion:nil]; }]; } -(void)loginAciton{ MYAPPDELEGATE.userDic = nil; [LoginVC saveAppIosImei]; NSUserDefaults *defatluts = [NSUserDefaults standardUserDefaults]; [defatluts removeObjectForKey:@"userPassWord"]; [defatluts synchronize]; LoginVC * loginvc = [[LoginVC alloc]init]; MYAPPDELEGATE.window.rootViewController = loginvc; } #pragma mark 客服 // - 登陆成功状态 - (void)loginSuccess:(NSNotification *)sender { NSLog(@"连接成功"); if (_isPushed == YES) { return; } [MBProgressHUD hideHUDForView:self.view animated:YES]; [QMConnect sdkGetPeers:^(NSArray * _Nonnull peerArray) { dispatch_async(dispatch_get_main_queue(), ^{ NSArray *peers = peerArray; if (peers.count == 1 && peers.count != 0) { [self showChatRoomViewController:[peers.firstObject objectForKey:@"id"]]; }else { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择您咨询的类型或业务部门(对应技能组)" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:cancelAction]; for (NSDictionary *index in peers) { UIAlertAction *surelAction = [UIAlertAction actionWithTitle:[index objectForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self showChatRoomViewController:[index objectForKey:@"id"]]; }]; [alertController addAction:surelAction]; } [self presentViewController:alertController animated:YES completion:nil]; } _isFirstClick = YES; }); } failureBlock:^{ _isFirstClick = YES; }]; } // - 登陆失败状态 - (void)loginFaile:(NSNotification *)sender { NSLog(@"客服登录失败---->%@",sender.object); _isFirstClick = YES; [MBProgressHUD hideHUDForView:self.view animated:YES]; } // - 跳转聊天界面 - (void)showChatRoomViewController:(NSString *)peerId { QMChatRoomViewController *_chatRoomViewController = [[QMChatRoomViewController alloc] init]; _chatRoomViewController.peerId = peerId; _chatRoomViewController.isPush = NO; NSString *imgStr = MYAPPDELEGATE.userDic[@"photo"]; if (!imgStr) { imgStr = @""; } _chatRoomViewController.avaterStr = imgStr; [self.navigationController pushViewController:_chatRoomViewController animated:YES]; } //- (BOOL)canBecomeFirstResponder { // return NO; //} - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end