// // MessageViewController.m // 千模 // // Created by MUMEI on 2018/6/28. // Copyright © 2018年 MUMEI. All rights reserved. // #import "MessageViewController.h" #import "NTESSessionConfig.h" #import "NTESVideoChatViewController.h" #import "NTESVideoViewController.h" #import "NTESNetChatViewController.h" #import "NTESGalleryViewController.h" #import "NIMMessageMaker.h" #import "NIMKitLocationPoint.h" #import "NIMLocationViewController.h" NSString *const NIMDemoEventNameOpenSnapPicture = @"NIMDemoEventNameOpenSnapPicture"; NSString *const NIMDemoEventNameCloseSnapPicture = @"NIMDemoEventNameCloseSnapPicture"; @interface MessageViewController () @property(nonatomic, strong) NTESSessionConfig *sessionConfig; @property(nonatomic, strong) UIView *bgblackView;//黑色背景 @property(nonatomic, strong) UIView *bgwhiteView;//下拉选项背景 @property(nonatomic, assign) BOOL hideTabBarWhenPop; @end @implementation MessageViewController - (void)viewDidLoad { [super viewDidLoad]; self.extendedLayoutIncludesOpaqueBars = YES; // [self setupNav]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; if ([@"system" isEqualToString:self.session.sessionId]) { self.sessionInputView.hidden = YES; [self.tableView setFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, ScreenWidth, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height)]; } } - (void)setupNav { [super setUpTitleView]; if ([UIScreen spt_currentScreenMode] == LESScreenModeIPhoneX) { self.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight + 34); } UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 40, 40); [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal]; btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0); [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整 self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem]; UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; rightBtn.frame = CGRectMake(0, 0, 40, 40); [rightBtn setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal]; rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -20); [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn]; UIBarButtonItem *nagetiveRightSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整 self.navigationItem.rightBarButtonItems = @[rightItem, nagetiveRightSpacer]; } - (void)rightBtnClick { if (!_bgblackView) { _bgblackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]; _bgblackView.backgroundColor = RGBA(0, 0, 0, 0.6); _bgblackView.hidden = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickHiddenSiftView)]; [_bgblackView addGestureRecognizer:tap]; [self.view addSubview:_bgblackView]; self.bgwhiteView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth - 100, -100, 100, 100)]; self.bgwhiteView.backgroundColor = [UIColor whiteColor]; self.bgwhiteView.layer.shadowColor = RGB(200, 200, 200).CGColor; self.bgwhiteView.layer.shadowOffset = CGSizeMake(0, 1); self.bgwhiteView.layer.shadowOpacity = 0.5; [_bgblackView addSubview:self.bgwhiteView]; UIButton *reportButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; reportButton.backgroundColor = [UIColor whiteColor]; [reportButton setImage:[UIImage imageNamed:@"report"] forState:UIControlStateNormal]; [reportButton setTitle:@"举报" forState:UIControlStateNormal]; [reportButton setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal]; reportButton.titleLabel.font = [UIFont systemFontOfSize:14]; reportButton.titleLabel.textAlignment = NSTextAlignmentLeft; [reportButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 60)]; [reportButton setTitleEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)]; [reportButton addTarget:self action:@selector(clickReport) forControlEvents:UIControlEventTouchUpInside]; [self.bgwhiteView addSubview:reportButton]; UIButton *screenButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 100, 50)]; screenButton.backgroundColor = [UIColor whiteColor]; [screenButton setImage:[UIImage imageNamed:@"screen"] forState:UIControlStateNormal]; [screenButton setTitle:@"屏蔽" forState:UIControlStateNormal]; [screenButton setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal]; screenButton.titleLabel.font = [UIFont systemFontOfSize:14]; screenButton.titleLabel.textAlignment = NSTextAlignmentLeft; [screenButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 60)]; [screenButton setTitleEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)]; [screenButton addTarget:self action:@selector(clickScreen) forControlEvents:UIControlEventTouchUpInside]; [self.bgwhiteView addSubview:screenButton]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 100, 0.5)]; line.backgroundColor = RGB(151, 151, 151); [self.bgwhiteView addSubview:line]; } _bgblackView.hidden = NO; [UIView animateWithDuration:0.25 animations:^{ self.bgwhiteView.frame = CGRectMake(ScreenWidth - 100, 64, 100, 100); } completion:^(BOOL finished) { }]; } //点击隐藏 - (void)clickHiddenSiftView { [UIView animateWithDuration:0.25 animations:^{ self.bgwhiteView.frame = CGRectMake(ScreenWidth - 100, -100, 100, 100); } completion:^(BOOL finished) { _bgblackView.hidden = YES; }]; } //举报 - (void)clickReport { UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定举报该用户" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) { [self postReport]; }]; [alertVc addAction:cancelAction]; [alertVc addAction:sureAction]; [self presentViewController:alertVc animated:YES completion:nil]; [self clickHiddenSiftView]; } - (void)postReport { NSString *str = [NSString stringWithFormat:@"%@/memberInfo?action=report&accuserpk=%@&appelleepk=%@&reason=%@", PublicUrl, [ModelUser user].pk, self.session.sessionId, @""]; [[AHHttpManager sharedManager] POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) { if ([[responseObject objectForKey:@"msg"] isEqualToString:@"success"]) { UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"举报成功,我们将会在24小时之内给出回复" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) { }]; [alertVc addAction:sureAction]; [self presentViewController:alertVc animated:YES completion:nil]; } else { [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1]; } } failure:^(NSError *error) { NSLog(@"error = %@", error); [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1]; }]; } //屏蔽 - (void)clickScreen { UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定屏蔽该用户" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) { [[NIMSDK sharedSDK].userManager addToBlackList:self.session.sessionId completion:^(NSError *_Nullable error) { if (!error) { [MBProgressHUD showTextHUD:@"已屏蔽该用户" inView:self.view hideAfterDelay:1]; } }]; }]; [alertVc addAction:cancelAction]; [alertVc addAction:sureAction]; [self presentViewController:alertVc animated:YES completion:nil]; [self clickHiddenSiftView]; } //- (void)onSendText:(NSString *)text atUsers:(NSArray *)atUsers //{ // NSMutableArray *users = [NSMutableArray arrayWithArray:atUsers]; // if (self.session.sessionType == NIMSessionTypeP2P) // { // [users addObject:self.session.sessionId]; // } //// NSString *robotsToSend = [self robotsToSend:users]; //// // NIMMessage *message = nil; //// if (robotsToSend.length) //// { //// message = [NIMMessageMaker msgWithRobotQuery:text toRobot:robotsToSend]; //// } //// else //// { //// message = [NIMMessageMaker msgWithText:text]; //// } // message = [NIMMessageMaker msgWithText:text]; // // if (atUsers.count) // { // NIMMessageApnsMemberOption *apnsOption = [[NIMMessageApnsMemberOption alloc] init]; // apnsOption.userIds = atUsers; // apnsOption.forcePush = YES; // // NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init]; // option.session = self.session; // // NSString *me = [[NIMKit sharedKit].provider infoByUser:[NIMSDK sharedSDK].loginManager.currentAccount option:option].showName; // apnsOption.apnsContent = [NSString stringWithFormat:@"%@在群里@了你",me]; // message.apnsMemberOption = apnsOption; // } //// message.apnsPayload = @{@"session":self.session.sessionId}; // [self sendMessage:message]; //} #pragma mark - Cell事件 - (BOOL)onTapCell:(NIMKitEvent *)event { BOOL handled = [super onTapCell:event]; NSString *eventName = event.eventName; if ([eventName isEqualToString:NIMKitEventNameTapContent]) { NIMMessage *message = event.messageModel.message; NSDictionary *actions = [self cellActions]; NSString *value = actions[@(message.messageType)]; if (value) { SEL selector = NSSelectorFromString(value); if (selector && [self respondsToSelector:selector]) { SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:message]); handled = YES; } } } else if ([eventName isEqualToString:NIMKitEventNameTapLabelLink]) { NSString *link = event.data; [self openSafari:link]; handled = YES; } else if ([eventName isEqualToString:NIMDemoEventNameOpenSnapPicture]) { // NIMCustomObject *object = event.messageModel.message.messageObject; // NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment; // if(attachment.isFired){ // return handled; // } // UIView *sender = event.data; // self.currentSingleSnapView = [NTESGalleryViewController alertSingleSnapViewWithMessage:object.message baseView:sender]; // handled = YES; } else if ([eventName isEqualToString:NIMDemoEventNameCloseSnapPicture]) { // //点击很快的时候可能会触发两次查看,所以这里不管有没有查看过 先强直销毁掉 // NIMCustomObject *object = event.messageModel.message.messageObject; // UIView *senderView = event.data; // [senderView dismissPresentedView:YES complete:nil]; // // NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment; // if(attachment.isFired){ // return handled; // } // attachment.isFired = YES; // NIMMessage *message = object.message; // if ([NTESBundleSetting sharedConfig].autoRemoveSnapMessage) { // [[NIMSDK sharedSDK].conversationManager deleteMessage:message]; // [self uiDeleteMessage:message]; // }else{ // [[NIMSDK sharedSDK].conversationManager updateMessage:message forSession:message.session completion:nil]; // [self uiUpdateMessage:message]; // } // [[NSFileManager defaultManager] removeItemAtPath:attachment.filepath error:nil]; // self.currentSingleSnapView = nil; // handled = YES; } else if ([eventName isEqualToString:NIMKitEventNameTapRobotLink]) { NSString *link = event.data; [self openSafari:link]; handled = YES; } if (!handled) { NSAssert(0, @"invalid event"); } return handled; } #pragma mark - Cell Actions - (void)showImage:(NIMMessage *)message { NIMImageObject *object = message.messageObject; NTESGalleryItem *item = [[NTESGalleryItem alloc] init]; item.thumbPath = [object thumbPath]; item.imageURL = [object url]; item.name = [object displayName]; item.itemId = [message messageId]; item.size = [object size]; NIMSession *session = [self isMemberOfClass:[MessageViewController class]] ? self.session : nil; NTESGalleryViewController *vc = [[NTESGalleryViewController alloc] initWithItem:item session:session]; [self.navigationController pushViewController:vc animated:YES]; if (![[NSFileManager defaultManager] fileExistsAtPath:object.thumbPath]) { //如果缩略图下跪了,点进看大图的时候再去下一把缩略图 __weak typeof(self) wself = self; [[NIMSDK sharedSDK].resourceManager download:object.thumbUrl filepath:object.thumbPath progress:nil completion:^(NSError *error) { if (!error) { [wself uiUpdateMessage:message]; } }]; } } - (void)showVideo:(NIMMessage *)message { NIMVideoObject *object = message.messageObject; NIMSession *session = [self isMemberOfClass:[MessageViewController class]] ? self.session : nil; NTESVideoViewItem *item = [[NTESVideoViewItem alloc] init]; item.path = object.path; item.url = object.url; item.session = session; item.itemId = object.message.messageId; NTESVideoViewController *playerViewController = [[NTESVideoViewController alloc] initWithVideoViewItem:item]; [self.navigationController pushViewController:playerViewController animated:YES]; if (![[NSFileManager defaultManager] fileExistsAtPath:object.coverPath]) { //如果封面图下跪了,点进视频的时候再去下一把封面图 __weak typeof(self) wself = self; [[NIMSDK sharedSDK].resourceManager download:object.coverUrl filepath:object.coverPath progress:nil completion:^(NSError *error) { if (!error) { [wself uiUpdateMessage:message]; } }]; } } - (void)showLocation:(NIMMessage *)message { NIMLocationObject *object = message.messageObject; NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithLocationObject:object]; NIMLocationViewController *vc = [[NIMLocationViewController alloc] initWithLocationPoint:locationPoint]; [self.navigationController pushViewController:vc animated:YES]; } //- (void)showFile:(NIMMessage *)message //{ // NIMFileObject *object = message.messageObject; // NTESFilePreViewController *vc = [[NTESFilePreViewController alloc] initWithFileObject:object]; // [self.navigationController pushViewController:vc animated:YES]; //} - (void)showCustom:(NIMMessage *)message { //普通的自定义消息点击事件可以在这里做哦~ } - (void)openSafari:(NSString *)link { NSURLComponents *components = [[NSURLComponents alloc] initWithString:link]; if (components) { if (!components.scheme) { //默认添加 http components.scheme = @"http"; } [[UIApplication sharedApplication] openURL:[components URL]]; } } - (NSDictionary *)cellActions { static NSDictionary *actions = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ actions = @{@(NIMMessageTypeImage): @"showImage:", @(NIMMessageTypeVideo): @"showVideo:", @(NIMMessageTypeLocation): @"showLocation:", @(NIMMessageTypeFile): @"showFile:", @(NIMMessageTypeCustom): @"showCustom:"}; }); return actions; } - (void)backClick { [self.navigationController popViewControllerAnimated:YES]; } - (id )sessionConfig { if (_sessionConfig == nil) { _sessionConfig = [[NTESSessionConfig alloc] init]; _sessionConfig.session = self.session; } return _sessionConfig; } - (BOOL)checkRTSCondition { BOOL result = YES; if (![[Reachability reachabilityForInternetConnection] isReachable]) { [self.view makeToast:@"请检查网络" duration:2.0 position:CSToastPositionCenter]; result = NO; } NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount]; if (self.session.sessionType == NIMSessionTypeP2P && [currentAccount isEqualToString:self.session.sessionId]) { [self.view makeToast:@"不能和自己通话哦" duration:2.0 position:CSToastPositionCenter]; result = NO; } if (self.session.sessionType == NIMSessionTypeTeam) { NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:self.session.sessionId]; NSInteger memberNumber = team.memberNumber; if (memberNumber < 2) { [self.view makeToast:@"无法发起,群人数少于2人" duration:2.0 position:CSToastPositionCenter]; result = NO; } } return result; } #pragma mark - 视频聊天 - (void)onTapMediaItemVideoChat:(NIMMediaItem *)item { if ([self checkRTSCondition]) { //由于音视频聊天里头有音频和视频聊天界面的切换,直接用present的话页面过渡会不太自然,这里还是用push,然后做出present的效果 NTESVideoChatViewController *vc = [[NTESVideoChatViewController alloc] initWithCallee:self.session.sessionId]; [self.navigationController presentViewController:vc animated:YES completion:nil]; } } - (void)viewWillAppear:(BOOL)animated { self.hideTabBarWhenPop = self.tabBarController.tabBar.isHidden; self.tabBarController.tabBar.hidden = YES; } - (void)viewWillDisappear:(BOOL)animated { self.tabBarController.tabBar.hidden = self.hideTabBarWhenPop; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end