// // personVC.m // LN_School // // Created by apple on 2017/4/5. // Copyright © 2017年 Danson. All rights reserved. // #import "personVC.h" #import "SelectHeadImgView.h" #import "LoginViewController.h" #import "UIImageView+WebCache.h" #import "AboutVC.h" //客服 #import "QMChatRoomViewController.h" #import #import "QMAlert.h" #define PersonVCTop_H kStatusHeight+20 #define PersonVCRate 1.0/(PersonVCTop_H+100-kNavOffSet)//100为透视图高度 @interface personVC () { UITableView *mainTableView; UIImageView *headImg; UILabel *nameLabel; UILabel *schoolNamelabel; SelectHeadImgView *selectImgView; NSArray *titles; NSArray *imgArray; //客服 BOOL _isFirstClick; // 判断点击状态 BOOL _isPushed; // 判断跳转状态 } @property(nonatomic,strong)UIView *topView; //客服 @property (nonatomic, assign) BOOL isPushed; // 控制跳转 @property (nonatomic, assign) BOOL isConnecting; // 控制多次注册 @property (nonatomic, copy) NSDictionary * dictionary; //客服一些配置 @end @implementation personVC - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"我的"; //个人中心的头 CGFloat x,y,w,h; x = 0; y = 0; w = kSize.width; h = PersonVCTop_H; _topView = [[UIView alloc] KSetxywh]; _topView.backgroundColor = RQMianColor; [self.view addSubview:_topView]; y+=h; h=117; UIView *headView = [[UIView alloc] KSetxywh]; headView.backgroundColor = KBackGroundColor; y -= PersonVCTop_H; h = 100; UIView *view = [[UIView alloc] KSetxywh]; view.backgroundColor = RQMianColor; [headView addSubview:view]; x = y = 15; w = h = 70; UIImageView *imgView = [[UIImageView alloc] KSetxywh]; [imgView setModeAspectFill]; [imgView setRound]; [view addSubview:imgView]; headImg = imgView; UIButton *btn = [[UIButton alloc] KSetxywh]; [btn addTarget:self action:@selector(changeHeadImg) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:btn]; x += w + 10; y -= 5; w = kSize.width - x; h = 40; UILabel *label = [[UILabel alloc] KSetxywh]; [label setText:@"欢迎您!" Font:Font18 TextColor:KTitleColor]; [view addSubview:label]; nameLabel = label; y += h; label = [[UILabel alloc] KSetxywh]; [label setText:@"" Font:Font16 TextColor:KTitleColor]; [view addSubview:label]; schoolNamelabel = label; [self setPersonData]; titles = @[@[@"修改头像",@"修改昵称"],@[@"修改密码",@"退出登录"],@[@"使用说明",@"关于",@"联系我们"]]; imgArray = @[@[@"center_img1",@"center_img2"],@[@"center_img3",@"center_img4"],@[@"center_img5",@"center_img6",@"center_img7"]]; mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, PersonVCTop_H, kSize.width, kSize.height) style:UITableViewStyleGrouped]; mainTableView.height -= kTabBarHeight+PersonVCTop_H; [mainTableView setDelegate:self]; [mainTableView setDataSource:self]; // mainTableView.contentInset = UIEdgeInsetsMake(84, 0, 0, 0); [mainTableView setTableHeaderView:headView]; [self.view addSubview:mainTableView]; [self.view insertSubview:mainTableView belowSubview:_topView]; [self setPersonData]; //客服 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerSuccess:) name:CUSTOM_LOGIN_SUCCEED object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registerFailure:) name:CUSTOM_LOGIN_ERROR_USER object:nil]; _isConnecting = NO; _isPushed = NO; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; _isPushed = NO; self.navigationController.navigationBar.translucent = YES; self.navigationController.navigationBar.alpha = 0; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; _isPushed = YES; self.navigationController.navigationBar.alpha = 1.0; } -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.tabBarController.tabBar.hidden = NO; //刷新待办事项 [self setPersonData]; } - (void)setPersonData { NSString *imagePath = defUser.userDict[@"photo"]; if (!imagePath) { imagePath = @""; } [headImg sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage imageNamed:@"headImg"]]; NSString *nameString = @"欢迎您!"; if ([defUser.userDict[@"realName"] length] > 0) { nameString = [NSString stringWithFormat:@"%@,欢迎您!",defUser.userDict[@"realName"]]; } nameLabel.text = nameString; schoolNamelabel.text = defUser.userDict[@"schoolName"]; } #pragma mark tableView -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return titles.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [titles[section] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; } cell.imageView.image = [UIImage imageNamed:[imgArray[indexPath.section] objectAtIndex:indexPath.row]]; cell.textLabel.text = [titles[indexPath.section] objectAtIndex:indexPath.row]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { if (indexPath.row == 0) { //修改头像 [self changeHeadImg]; }else if (indexPath.row == 1) { //修改昵称 ShowMsgUnOpen(); } }else if (indexPath.section == 1) { if (indexPath.row == 0) { //修改密码 [self fixKey]; }else if (indexPath.row == 1) { //退出登录 [self loginOut]; } }else{ if (indexPath.row == 0) { //使用说明 ShowMsgUnOpen(); }else if (indexPath.row == 1) { //关于 AboutVC * vc = [[AboutVC alloc]init]; [self navPushHideTabbarToVC:vc]; }else if (indexPath.row == 2) { if (_isConnecting) { return; } _isConnecting = YES; [MBProgressHUD showLoadToView:self.view]; // userId 只能使用 数字 字母(包括大小写) 下划线 NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSString *name = [NSString stringWithFormat:@"%@[闽I驾校_%@]",defUser.userDict[@"realName"],version]; [QMConnect registerSDKWithAppKey:CUSTOMERSERVICE_APPKEY userName:name userId:[NSString stringWithFormat:@"%@",defUser.userDict[@"id"]]]; } } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ //topview CGRect newFrame = self.topView.frame; CGFloat settingViewOffsetY = PersonVCTop_H - scrollView.contentOffset.y; newFrame.size.height = settingViewOffsetY; if (settingViewOffsetY < PersonVCTop_H) { newFrame.size.height = PersonVCTop_H; } self.topView.frame = newFrame; //navBar self.navigationController.navigationBar.alpha = PersonVCRate*scrollView.contentOffset.y; // NSLog(@"-->> %f", self.navigationController.navigationBar.alpha); } #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 = defUser.userDict[@"password"]; if (![[text md5Encrypt] isEqualToString:key]) { ShowMsg(@"原密码错误!"); 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) { ShowMsg(@"密码长度不能少于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:defUser.userDict[@"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:defUser.userDict[@"id"] forKey:@"userId"]; [mdic setValue:oldpwd forKey:@"oldPwd"]; [mdic setValue:newpwd forKey:@"newPwd"]; [NetManager requestAnythingWithURL:@"modifyPwd" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { if (!root) { ShowMsg(@"修改密码失败!"); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowErrorMsg(root[@"msg"]); return; } ShowMsg(@"修改成功,请重新登录"); [LoginViewController loginFromVC:self]; }]; } #pragma mark 上传头像 - (void)changeHeadImg { selectImgView = [[SelectHeadImgView alloc] initWithTitle:@"请选择头像"]; [selectImgView setDelegate:self]; [selectImgView show]; } -(void)SelectHeadImgView:(SelectHeadImgView *)view didGetImage:(UIImage*)image { [self uploadCoachHeadImg:image]; } -(void)SelectHeadImgWillPickImage:(SelectHeadImgView *)view { UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:view.type]) { pickerImage.sourceType = view.type; 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 { [selectImgView cancelAction]; [picker dismissViewControllerAnimated:YES completion:^{ UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self uploadCoachHeadImg:image]; }]; } -(void)uploadCoachHeadImg:(UIImage*)image { if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSData *data = UIImagePNGRepresentation([image scaledToWid:100]); NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:imgString forKey:@"content"]; [dic setObject:defUser.userDict[@"id"] forKey:@"keyword"]; [dic setObject:@"1" forKey:@"type"]; NSString *method = @"upload-1"; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { if (!root) { ShowMsg(@"上传头像失败!"); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowErrorMsg(root[@"msg"]); return; } NSString *imgPath = root[@"body"]; if (!imgPath) { imgPath = @""; } if ([imgPath length] > 0) { [headImg sd_setImageWithURL:[NSURL URLWithString:imgPath] placeholderImage:[UIImage imageNamed:@"headImg"]]; } //上传头像成功 [self schoolLogin]; }]; } - (void)schoolLogin { if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSString *systemName = [[UIDevice currentDevice] systemName]; NSString *systemVersion = [[UIDevice currentDevice] systemVersion]; NSString *identifier = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:defUser.userDict[@"loginCode"] forKey:@"loginCode"]; [dic setObject:defUser.userDict[@"password"] forKey:@"password"]; [dic setObject:defUser.userDict[@"dqbh"] forKey:@"dqbh"]; [dic setObject:@"2" forKey:@"appType"]; [dic setObject:[NSString stringWithFormat:@"%@%@",systemName,systemVersion] forKey:@"systemVersion"]; [dic setObject:[NSString stringWithFormat:@"%@%@",identifier,currentVersion] forKey:@"version"]; NSString *method = @"login"; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root){ return; } if ([root[@"code"] isEqualToString:@"1"]) { return; } ShowMsg(@"操作成功"); defUser.userDict = root[@"body"]; [self setPersonData]; }]; } #pragma mark - 退出登录 -(void)loginOut{ //退出登录 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) { defUser.userDict = nil; myDelegate.isLogin = NO; [LoginViewController saveIosMei]; [LoginViewController loginFromVC:self]; }]]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tabBarController presentViewController:alertFind animated:true completion:nil]; }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark 客服 - (void)registerSuccess:(NSNotification *)sender { NSLog(@"注册成功"); if (_isPushed) { [MBProgressHUD hideHUDForView:self.view]; _isConnecting = NO; return; } [QMConnect sdkGetWebchatScheduleConfig:^(NSDictionary * _Nonnull scheduleDic) { dispatch_async(dispatch_get_main_queue(), ^{ self.dictionary = scheduleDic; if ([self.dictionary[@"scheduleEnable"] intValue] == 1) { NSLog(@"日程管理"); [self starSchedule]; }else{ NSLog(@"技能组"); [self getPeers]; } }); } failBlock:^{ }]; } - (void)registerFailure:(NSNotification *)sender { NSLog(@"注册失败::%@", sender.object); self.isConnecting = NO; [MBProgressHUD hideHUDForView:self.view]; } #pragma mark - 技能组选择 - (void)getPeers { [QMConnect sdkGetPeers:^(NSArray * _Nonnull peerArray) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"%@", peerArray); NSArray *peers = peerArray; self.isConnecting = NO; [MBProgressHUD hideHUDForView:self.view]; if (peers.count == 1 && peers.count != 0) { [self showChatRoomViewController:[peers.firstObject objectForKey:@"id"] processType:@""]; }else { [self showPeersWithAlert:peers messageStr:@"选择您咨询的类型或业务部门(对应技能组)"]; } }); } failureBlock:^{ dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUDForView:self.view]; self.isConnecting = NO; }); }]; } #pragma mark - 日程管理 - (void)starSchedule { self.isConnecting = NO; [MBProgressHUD hideHUDForView:self.view]; if ([self.dictionary[@"scheduleId"] isEqual: @""] || [self.dictionary[@"processId"] isEqual: @""] || [self.dictionary objectForKey:@"entranceNode"] == nil || [self.dictionary objectForKey:@"leavemsgNodes"] == nil) { [QMAlert showMessage:@"对不起,由于在线咨询配置错误,暂时无法进行咨询"]; }else{ NSDictionary *entranceNode = self.dictionary[@"entranceNode"]; NSArray *entrances = entranceNode[@"entrances"]; NSLog(@" 获取到日程节点数组 ===== %@", entrances); // [self showPeersWithAlert: sdkEntrances]; if (entrances.count == 1 && entrances.count != 0) { [self showChatRoomViewController:[entrances.firstObject objectForKey:@"processTo"] processType:[entrances.firstObject objectForKey:@"processType"]]; }else{ [self showPeersWithAlert:entrances messageStr:@"选择您咨询的日程管理类型"]; } } } - (void)showPeersWithAlert: (NSArray *)peers messageStr: (NSString *)message { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"选择您咨询的类型或业务部门(对应技能组)" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { self.isConnecting = NO; }]; [alertController addAction:cancelAction]; for (NSDictionary *index in peers) { UIAlertAction *surelAction = [UIAlertAction actionWithTitle:[index objectForKey:@"name"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if ([self.dictionary[@"scheduleEnable"] integerValue] == 1) { [self showChatRoomViewController:[index objectForKey:@"processTo"] processType:[index objectForKey:@"processType"]]; }else{ [self showChatRoomViewController:[index objectForKey:@"id"] processType:@""]; } }]; [alertController addAction:surelAction]; } [self presentViewController:alertController animated:YES completion:nil]; } #pragma mark - 跳转聊天界面 - (void)showChatRoomViewController:(NSString *)peerId processType:(NSString *)processType { QMChatRoomViewController *chatRoomViewController = [[QMChatRoomViewController alloc] init]; chatRoomViewController.peerId = peerId; chatRoomViewController.isPush = NO; NSString *path = defUser.userDict[@"photo"]; if (!path) { path = @""; } chatRoomViewController.avaterStr = path; if ([self.dictionary[@"scheduleEnable"] intValue] == 1) { chatRoomViewController.isOpenSchedule = true; chatRoomViewController.scheduleId = self.dictionary[@"scheduleId"]; chatRoomViewController.processId = self.dictionary[@"processId"]; chatRoomViewController.currentNodeId = peerId; chatRoomViewController.processType = processType; }else{ chatRoomViewController.isOpenSchedule = false; } [self navPushHideTabbarToVC:chatRoomViewController]; } - (NSMutableAttributedString *)setSpace:(CGFloat)line kern:(NSNumber *)kern font:(UIFont *)font text:(NSString *)text { NSMutableParagraphStyle * paraStyle = [NSMutableParagraphStyle new]; paraStyle.lineBreakMode = NSLineBreakByCharWrapping; paraStyle.alignment = NSTextAlignmentCenter; paraStyle.lineSpacing = line; paraStyle.hyphenationFactor = 1.0; paraStyle.firstLineHeadIndent = 0.0; paraStyle.paragraphSpacingBefore = 0.0; paraStyle.headIndent = 0; paraStyle.tailIndent = 0; NSDictionary *attributes = @{ NSFontAttributeName: font, NSParagraphStyleAttributeName: paraStyle, NSKernAttributeName: kern }; NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes]; return attributeStr; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_SUCCEED object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:CUSTOM_LOGIN_ERROR_USER object:nil]; } @end