MessageViewController.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. //
  2. // MessageViewController.m
  3. // 千模
  4. //
  5. // Created by MUMEI on 2018/6/28.
  6. // Copyright © 2018年 MUMEI. All rights reserved.
  7. //
  8. #import "MessageViewController.h"
  9. #import "NTESSessionConfig.h"
  10. #import "NTESVideoChatViewController.h"
  11. #import "NTESVideoViewController.h"
  12. #import "NTESNetChatViewController.h"
  13. #import "NTESGalleryViewController.h"
  14. #import "NIMMessageMaker.h"
  15. #import "NIMKitLocationPoint.h"
  16. #import "NIMLocationViewController.h"
  17. NSString *const NIMDemoEventNameOpenSnapPicture = @"NIMDemoEventNameOpenSnapPicture";
  18. NSString *const NIMDemoEventNameCloseSnapPicture = @"NIMDemoEventNameCloseSnapPicture";
  19. @interface MessageViewController ()<NIMMessageObject>
  20. @property (nonatomic,strong) NTESSessionConfig *sessionConfig;
  21. @property (nonatomic,strong) UIView * bgblackView;//黑色背景
  22. @property (nonatomic,strong) UIView * bgwhiteView;//下拉选项背景
  23. @property(nonatomic, assign) BOOL hideTabBarWhenPop;
  24. @end
  25. @implementation MessageViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.extendedLayoutIncludesOpaqueBars = YES;
  29. // [self setupNav];
  30. }
  31. - (void)viewDidLayoutSubviews {
  32. [super viewDidLayoutSubviews];
  33. if ([@"system" isEqualToString:self.session.sessionId]) {
  34. self.sessionInputView.hidden = YES;
  35. [self.tableView setFrame: CGRectMake(0, self.navigationController.navigationBar.frame.size.height, ScreenWidth, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height)];
  36. }
  37. }
  38. - (void)setupNav{
  39. [super setUpTitleView];
  40. if ([UIScreen spt_currentScreenMode]== LESScreenModeIPhoneX){
  41. self.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight+34);
  42. }
  43. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  44. btn.frame = CGRectMake(0, 0, 40, 40);
  45. [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal];
  46. btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
  47. [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  48. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  49. UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  50. nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整
  51. self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem];
  52. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  53. rightBtn.frame = CGRectMake(0, 0, 40, 40);
  54. [rightBtn setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
  55. rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -20);
  56. [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
  57. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
  58. UIBarButtonItem *nagetiveRightSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  59. // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整
  60. self.navigationItem.rightBarButtonItems = @[rightItem, nagetiveRightSpacer];
  61. }
  62. -(void)rightBtnClick{
  63. if (!_bgblackView) {
  64. _bgblackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
  65. _bgblackView.backgroundColor = RGBA(0, 0, 0, 0.6);
  66. _bgblackView.hidden = YES;
  67. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHiddenSiftView)];
  68. [_bgblackView addGestureRecognizer:tap];
  69. [self.view addSubview:_bgblackView];
  70. self.bgwhiteView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth-100, -100, 100, 100)];
  71. self.bgwhiteView.backgroundColor = [UIColor whiteColor];
  72. self.bgwhiteView.layer.shadowColor = RGB(200, 200, 200).CGColor;
  73. self.bgwhiteView.layer.shadowOffset = CGSizeMake(0, 1);
  74. self.bgwhiteView.layer.shadowOpacity = 0.5;
  75. [_bgblackView addSubview:self.bgwhiteView];
  76. UIButton *reportButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
  77. reportButton.backgroundColor = [UIColor whiteColor];
  78. [reportButton setImage:[UIImage imageNamed:@"report"] forState:UIControlStateNormal];
  79. [reportButton setTitle:@"举报" forState:UIControlStateNormal];
  80. [reportButton setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
  81. reportButton.titleLabel.font = [UIFont systemFontOfSize:14];
  82. reportButton.titleLabel.textAlignment = NSTextAlignmentLeft;
  83. [reportButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 60)];
  84. [reportButton setTitleEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
  85. [reportButton addTarget:self action:@selector(clickReport) forControlEvents:UIControlEventTouchUpInside];
  86. [self.bgwhiteView addSubview:reportButton];
  87. UIButton *screenButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 50, 100, 50)];
  88. screenButton.backgroundColor = [UIColor whiteColor];
  89. [screenButton setImage:[UIImage imageNamed:@"screen"] forState:UIControlStateNormal];
  90. [screenButton setTitle:@"屏蔽" forState:UIControlStateNormal];
  91. [screenButton setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
  92. screenButton.titleLabel.font = [UIFont systemFontOfSize:14];
  93. screenButton.titleLabel.textAlignment = NSTextAlignmentLeft;
  94. [screenButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 60)];
  95. [screenButton setTitleEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
  96. [screenButton addTarget:self action:@selector(clickScreen) forControlEvents:UIControlEventTouchUpInside];
  97. [self.bgwhiteView addSubview:screenButton];
  98. UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 100, 0.5)];
  99. line.backgroundColor = RGB(151, 151, 151);
  100. [self.bgwhiteView addSubview:line];
  101. }
  102. _bgblackView.hidden = NO;
  103. [UIView animateWithDuration:0.25 animations:^{
  104. self.bgwhiteView.frame = CGRectMake(ScreenWidth-100, 64, 100, 100);
  105. } completion:^(BOOL finished) {
  106. }];
  107. }
  108. //点击隐藏
  109. -(void)clickHiddenSiftView{
  110. [UIView animateWithDuration:0.25 animations:^{
  111. self.bgwhiteView.frame = CGRectMake(ScreenWidth-100, -100, 100, 100);
  112. } completion:^(BOOL finished) {
  113. _bgblackView.hidden = YES;
  114. }];
  115. }
  116. //举报
  117. -(void)clickReport{
  118. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定举报该用户" preferredStyle:UIAlertControllerStyleAlert];
  119. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  120. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  121. [self postReport];
  122. }];
  123. [alertVc addAction:cancelAction];
  124. [alertVc addAction:sureAction];
  125. [self presentViewController:alertVc animated:YES completion:nil];
  126. [self clickHiddenSiftView];
  127. }
  128. - (void)postReport{
  129. NSString *str = [NSString stringWithFormat:@"%@/memberInfo?action=report&accuserpk=%@&appelleepk=%@&reason=%@",PublicUrl,[ModelUser user].pk,self.session.sessionId,@""];
  130. [[AHHttpManager sharedManager]POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) {
  131. if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) {
  132. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"举报成功,我们将会在24小时之内给出回复" preferredStyle:UIAlertControllerStyleAlert];
  133. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  134. }];
  135. [alertVc addAction:sureAction];
  136. [self presentViewController:alertVc animated:YES completion:nil];
  137. }else{
  138. [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
  139. }
  140. } failure:^(NSError *error) {
  141. NSLog(@"error = %@",error);
  142. [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
  143. }];
  144. }
  145. //屏蔽
  146. -(void)clickScreen{
  147. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定屏蔽该用户" preferredStyle:UIAlertControllerStyleAlert];
  148. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  149. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  150. [[NIMSDK sharedSDK].userManager addToBlackList:self.session.sessionId completion:^(NSError * _Nullable error) {
  151. if (!error) {
  152. [MBProgressHUD showTextHUD:@"已屏蔽该用户" inView:self.view hideAfterDelay:1];
  153. }
  154. }];
  155. }];
  156. [alertVc addAction:cancelAction];
  157. [alertVc addAction:sureAction];
  158. [self presentViewController:alertVc animated:YES completion:nil];
  159. [self clickHiddenSiftView];
  160. }
  161. //- (void)onSendText:(NSString *)text atUsers:(NSArray *)atUsers
  162. //{
  163. // NSMutableArray *users = [NSMutableArray arrayWithArray:atUsers];
  164. // if (self.session.sessionType == NIMSessionTypeP2P)
  165. // {
  166. // [users addObject:self.session.sessionId];
  167. // }
  168. //// NSString *robotsToSend = [self robotsToSend:users];
  169. ////
  170. // NIMMessage *message = nil;
  171. //// if (robotsToSend.length)
  172. //// {
  173. //// message = [NIMMessageMaker msgWithRobotQuery:text toRobot:robotsToSend];
  174. //// }
  175. //// else
  176. //// {
  177. //// message = [NIMMessageMaker msgWithText:text];
  178. //// }
  179. // message = [NIMMessageMaker msgWithText:text];
  180. //
  181. // if (atUsers.count)
  182. // {
  183. // NIMMessageApnsMemberOption *apnsOption = [[NIMMessageApnsMemberOption alloc] init];
  184. // apnsOption.userIds = atUsers;
  185. // apnsOption.forcePush = YES;
  186. //
  187. // NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  188. // option.session = self.session;
  189. //
  190. // NSString *me = [[NIMKit sharedKit].provider infoByUser:[NIMSDK sharedSDK].loginManager.currentAccount option:option].showName;
  191. // apnsOption.apnsContent = [NSString stringWithFormat:@"%@在群里@了你",me];
  192. // message.apnsMemberOption = apnsOption;
  193. // }
  194. //// message.apnsPayload = @{@"session":self.session.sessionId};
  195. // [self sendMessage:message];
  196. //}
  197. #pragma mark - Cell事件
  198. - (BOOL)onTapCell:(NIMKitEvent *)event
  199. {
  200. BOOL handled = [super onTapCell:event];
  201. NSString *eventName = event.eventName;
  202. if ([eventName isEqualToString:NIMKitEventNameTapContent])
  203. {
  204. NIMMessage *message = event.messageModel.message;
  205. NSDictionary *actions = [self cellActions];
  206. NSString *value = actions[@(message.messageType)];
  207. if (value) {
  208. SEL selector = NSSelectorFromString(value);
  209. if (selector && [self respondsToSelector:selector]) {
  210. SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:message]);
  211. handled = YES;
  212. }
  213. }
  214. }
  215. else if([eventName isEqualToString:NIMKitEventNameTapLabelLink])
  216. {
  217. NSString *link = event.data;
  218. [self openSafari:link];
  219. handled = YES;
  220. }
  221. else if([eventName isEqualToString:NIMDemoEventNameOpenSnapPicture])
  222. {
  223. // NIMCustomObject *object = event.messageModel.message.messageObject;
  224. // NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment;
  225. // if(attachment.isFired){
  226. // return handled;
  227. // }
  228. // UIView *sender = event.data;
  229. // self.currentSingleSnapView = [NTESGalleryViewController alertSingleSnapViewWithMessage:object.message baseView:sender];
  230. // handled = YES;
  231. }
  232. else if([eventName isEqualToString:NIMDemoEventNameCloseSnapPicture])
  233. {
  234. // //点击很快的时候可能会触发两次查看,所以这里不管有没有查看过 先强直销毁掉
  235. // NIMCustomObject *object = event.messageModel.message.messageObject;
  236. // UIView *senderView = event.data;
  237. // [senderView dismissPresentedView:YES complete:nil];
  238. //
  239. // NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment;
  240. // if(attachment.isFired){
  241. // return handled;
  242. // }
  243. // attachment.isFired = YES;
  244. // NIMMessage *message = object.message;
  245. // if ([NTESBundleSetting sharedConfig].autoRemoveSnapMessage) {
  246. // [[NIMSDK sharedSDK].conversationManager deleteMessage:message];
  247. // [self uiDeleteMessage:message];
  248. // }else{
  249. // [[NIMSDK sharedSDK].conversationManager updateMessage:message forSession:message.session completion:nil];
  250. // [self uiUpdateMessage:message];
  251. // }
  252. // [[NSFileManager defaultManager] removeItemAtPath:attachment.filepath error:nil];
  253. // self.currentSingleSnapView = nil;
  254. // handled = YES;
  255. }
  256. else if([eventName isEqualToString:NIMKitEventNameTapRobotLink])
  257. {
  258. NSString *link = event.data;
  259. [self openSafari:link];
  260. handled = YES;
  261. }
  262. if (!handled) {
  263. NSAssert(0, @"invalid event");
  264. }
  265. return handled;
  266. }
  267. #pragma mark - Cell Actions
  268. - (void)showImage:(NIMMessage *)message
  269. {
  270. NIMImageObject *object = message.messageObject;
  271. NTESGalleryItem *item = [[NTESGalleryItem alloc] init];
  272. item.thumbPath = [object thumbPath];
  273. item.imageURL = [object url];
  274. item.name = [object displayName];
  275. item.itemId = [message messageId];
  276. item.size = [object size];
  277. NIMSession *session = [self isMemberOfClass:[MessageViewController class]]? self.session : nil;
  278. NTESGalleryViewController *vc = [[NTESGalleryViewController alloc] initWithItem:item session:session];
  279. [self.navigationController pushViewController:vc animated:YES];
  280. if(![[NSFileManager defaultManager] fileExistsAtPath:object.thumbPath]){
  281. //如果缩略图下跪了,点进看大图的时候再去下一把缩略图
  282. __weak typeof(self) wself = self;
  283. [[NIMSDK sharedSDK].resourceManager download:object.thumbUrl filepath:object.thumbPath progress:nil completion:^(NSError *error) {
  284. if (!error) {
  285. [wself uiUpdateMessage:message];
  286. }
  287. }];
  288. }
  289. }
  290. - (void)showVideo:(NIMMessage *)message
  291. {
  292. NIMVideoObject *object = message.messageObject;
  293. NIMSession *session = [self isMemberOfClass:[MessageViewController class]]? self.session : nil;
  294. NTESVideoViewItem *item = [[NTESVideoViewItem alloc] init];
  295. item.path = object.path;
  296. item.url = object.url;
  297. item.session = session;
  298. item.itemId = object.message.messageId;
  299. NTESVideoViewController *playerViewController = [[NTESVideoViewController alloc] initWithVideoViewItem:item];
  300. [self.navigationController pushViewController:playerViewController animated:YES];
  301. if(![[NSFileManager defaultManager] fileExistsAtPath:object.coverPath]){
  302. //如果封面图下跪了,点进视频的时候再去下一把封面图
  303. __weak typeof(self) wself = self;
  304. [[NIMSDK sharedSDK].resourceManager download:object.coverUrl filepath:object.coverPath progress:nil completion:^(NSError *error) {
  305. if (!error) {
  306. [wself uiUpdateMessage:message];
  307. }
  308. }];
  309. }
  310. }
  311. - (void)showLocation:(NIMMessage *)message
  312. {
  313. NIMLocationObject *object = message.messageObject;
  314. NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithLocationObject:object];
  315. NIMLocationViewController *vc = [[NIMLocationViewController alloc] initWithLocationPoint:locationPoint];
  316. [self.navigationController pushViewController:vc animated:YES];
  317. }
  318. //- (void)showFile:(NIMMessage *)message
  319. //{
  320. // NIMFileObject *object = message.messageObject;
  321. // NTESFilePreViewController *vc = [[NTESFilePreViewController alloc] initWithFileObject:object];
  322. // [self.navigationController pushViewController:vc animated:YES];
  323. //}
  324. - (void)showCustom:(NIMMessage *)message
  325. {
  326. //普通的自定义消息点击事件可以在这里做哦~
  327. }
  328. - (void)openSafari:(NSString *)link
  329. {
  330. NSURLComponents *components = [[NSURLComponents alloc] initWithString:link];
  331. if (components)
  332. {
  333. if (!components.scheme)
  334. {
  335. //默认添加 http
  336. components.scheme = @"http";
  337. }
  338. [[UIApplication sharedApplication] openURL:[components URL]];
  339. }
  340. }
  341. - (NSDictionary *)cellActions
  342. {
  343. static NSDictionary *actions = nil;
  344. static dispatch_once_t onceToken;
  345. dispatch_once(&onceToken, ^{
  346. actions = @{@(NIMMessageTypeImage) : @"showImage:",
  347. @(NIMMessageTypeVideo) : @"showVideo:",
  348. @(NIMMessageTypeLocation) : @"showLocation:",
  349. @(NIMMessageTypeFile) : @"showFile:",
  350. @(NIMMessageTypeCustom): @"showCustom:"};
  351. });
  352. return actions;
  353. }
  354. -(void)backClick{
  355. [self.navigationController popViewControllerAnimated:YES];
  356. }
  357. - (id<NIMSessionConfig>)sessionConfig
  358. {
  359. if (_sessionConfig == nil) {
  360. _sessionConfig = [[NTESSessionConfig alloc] init];
  361. _sessionConfig.session = self.session;
  362. }
  363. return _sessionConfig;
  364. }
  365. - (BOOL)checkRTSCondition
  366. {
  367. BOOL result = YES;
  368. if (![[Reachability reachabilityForInternetConnection] isReachable])
  369. {
  370. [self.view makeToast:@"请检查网络" duration:2.0 position:CSToastPositionCenter];
  371. result = NO;
  372. }
  373. NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
  374. if (self.session.sessionType == NIMSessionTypeP2P && [currentAccount isEqualToString:self.session.sessionId])
  375. {
  376. [self.view makeToast:@"不能和自己通话哦" duration:2.0 position:CSToastPositionCenter];
  377. result = NO;
  378. }
  379. if (self.session.sessionType == NIMSessionTypeTeam)
  380. {
  381. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:self.session.sessionId];
  382. NSInteger memberNumber = team.memberNumber;
  383. if (memberNumber < 2)
  384. {
  385. [self.view makeToast:@"无法发起,群人数少于2人" duration:2.0 position:CSToastPositionCenter];
  386. result = NO;
  387. }
  388. }
  389. return result;
  390. }
  391. #pragma mark - 视频聊天
  392. - (void)onTapMediaItemVideoChat:(NIMMediaItem *)item
  393. {
  394. if ([self checkRTSCondition]) {
  395. //由于音视频聊天里头有音频和视频聊天界面的切换,直接用present的话页面过渡会不太自然,这里还是用push,然后做出present的效果
  396. NTESVideoChatViewController *vc = [[NTESVideoChatViewController alloc] initWithCallee:self.session.sessionId];
  397. vc.hidesBottomBarWhenPushed = YES;
  398. CATransition *transition = [CATransition animation];
  399. transition.duration = 0.25;
  400. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
  401. transition.type = kCATransitionPush;
  402. transition.subtype = kCATransitionFromTop;
  403. [self.navigationController.view.layer addAnimation:transition forKey:nil];
  404. self.navigationController.navigationBarHidden = YES;
  405. [self.navigationController pushViewController:vc animated:NO];
  406. }
  407. }
  408. - (void)viewWillAppear:(BOOL)animated {
  409. self.hideTabBarWhenPop = self.tabBarController.tabBar.isHidden;
  410. self.tabBarController.tabBar.hidden = YES;
  411. }
  412. - (void)viewWillDisappear:(BOOL)animated {
  413. self.tabBarController.tabBar.hidden = self.hideTabBarWhenPop;
  414. }
  415. - (void)didReceiveMemoryWarning {
  416. [super didReceiveMemoryWarning];
  417. // Dispose of any resources that can be recreated.
  418. }
  419. @end