MessageViewController.m 19 KB

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