| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- //
- // 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 ()<NIMMessageObject>
- @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)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<NIMSessionConfig>)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];
- vc.hidesBottomBarWhenPushed = YES;
- CATransition *transition = [CATransition animation];
- transition.duration = 0.25;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
- transition.type = kCATransitionPush;
- transition.subtype = kCATransitionFromTop;
- [self.navigationController.view.layer addAnimation:transition forKey:nil];
- self.navigationController.navigationBarHidden = YES;
- [self.navigationController pushViewController:vc animated:NO];
- }
- }
- - (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
|