MessageViewController.m 19 KB

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