MessageViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. BOOL handled = [super onTapCell:event];
  200. NSString *eventName = event.eventName;
  201. if ([eventName isEqualToString:NIMKitEventNameTapContent]) {
  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. } else if ([eventName isEqualToString:NIMKitEventNameTapLabelLink]) {
  213. NSString *link = event.data;
  214. [self openSafari:link];
  215. handled = YES;
  216. } else if ([eventName isEqualToString:NIMDemoEventNameOpenSnapPicture]) {
  217. // NIMCustomObject *object = event.messageModel.message.messageObject;
  218. // NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment;
  219. // if(attachment.isFired){
  220. // return handled;
  221. // }
  222. // UIView *sender = event.data;
  223. // self.currentSingleSnapView = [NTESGalleryViewController alertSingleSnapViewWithMessage:object.message baseView:sender];
  224. // handled = YES;
  225. } else if ([eventName isEqualToString:NIMDemoEventNameCloseSnapPicture]) {
  226. // //点击很快的时候可能会触发两次查看,所以这里不管有没有查看过 先强直销毁掉
  227. // NIMCustomObject *object = event.messageModel.message.messageObject;
  228. // UIView *senderView = event.data;
  229. // [senderView dismissPresentedView:YES complete:nil];
  230. //
  231. // NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment;
  232. // if(attachment.isFired){
  233. // return handled;
  234. // }
  235. // attachment.isFired = YES;
  236. // NIMMessage *message = object.message;
  237. // if ([NTESBundleSetting sharedConfig].autoRemoveSnapMessage) {
  238. // [[NIMSDK sharedSDK].conversationManager deleteMessage:message];
  239. // [self uiDeleteMessage:message];
  240. // }else{
  241. // [[NIMSDK sharedSDK].conversationManager updateMessage:message forSession:message.session completion:nil];
  242. // [self uiUpdateMessage:message];
  243. // }
  244. // [[NSFileManager defaultManager] removeItemAtPath:attachment.filepath error:nil];
  245. // self.currentSingleSnapView = nil;
  246. // handled = YES;
  247. } else if ([eventName isEqualToString:NIMKitEventNameTapRobotLink]) {
  248. NSString *link = event.data;
  249. [self openSafari:link];
  250. handled = YES;
  251. }
  252. if (!handled) {
  253. NSAssert(0, @"invalid event");
  254. }
  255. return handled;
  256. }
  257. #pragma mark - Cell Actions
  258. - (void)showImage:(NIMMessage *)message {
  259. NIMImageObject *object = message.messageObject;
  260. NTESGalleryItem *item = [[NTESGalleryItem alloc] init];
  261. item.thumbPath = [object thumbPath];
  262. item.imageURL = [object url];
  263. item.name = [object displayName];
  264. item.itemId = [message messageId];
  265. item.size = [object size];
  266. NIMSession *session = [self isMemberOfClass:[MessageViewController class]] ? self.session : nil;
  267. NTESGalleryViewController *vc = [[NTESGalleryViewController alloc] initWithItem:item session:session];
  268. [self.navigationController pushViewController:vc animated:YES];
  269. if (![[NSFileManager defaultManager] fileExistsAtPath:object.thumbPath]) {
  270. //如果缩略图下跪了,点进看大图的时候再去下一把缩略图
  271. __weak typeof(self) wself = self;
  272. [[NIMSDK sharedSDK].resourceManager download:object.thumbUrl filepath:object.thumbPath progress:nil completion:^(NSError *error) {
  273. if (!error) {
  274. [wself uiUpdateMessage:message];
  275. }
  276. }];
  277. }
  278. }
  279. - (void)showVideo:(NIMMessage *)message {
  280. NIMVideoObject *object = message.messageObject;
  281. NIMSession *session = [self isMemberOfClass:[MessageViewController class]] ? self.session : nil;
  282. NTESVideoViewItem *item = [[NTESVideoViewItem alloc] init];
  283. item.path = object.path;
  284. item.url = object.url;
  285. item.session = session;
  286. item.itemId = object.message.messageId;
  287. NTESVideoViewController *playerViewController = [[NTESVideoViewController alloc] initWithVideoViewItem:item];
  288. [self.navigationController pushViewController:playerViewController animated:YES];
  289. if (![[NSFileManager defaultManager] fileExistsAtPath:object.coverPath]) {
  290. //如果封面图下跪了,点进视频的时候再去下一把封面图
  291. __weak typeof(self) wself = self;
  292. [[NIMSDK sharedSDK].resourceManager download:object.coverUrl filepath:object.coverPath progress:nil completion:^(NSError *error) {
  293. if (!error) {
  294. [wself uiUpdateMessage:message];
  295. }
  296. }];
  297. }
  298. }
  299. - (void)showLocation:(NIMMessage *)message {
  300. NIMLocationObject *object = message.messageObject;
  301. NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithLocationObject:object];
  302. NIMLocationViewController *vc = [[NIMLocationViewController alloc] initWithLocationPoint:locationPoint];
  303. [self.navigationController pushViewController:vc animated:YES];
  304. }
  305. //- (void)showFile:(NIMMessage *)message
  306. //{
  307. // NIMFileObject *object = message.messageObject;
  308. // NTESFilePreViewController *vc = [[NTESFilePreViewController alloc] initWithFileObject:object];
  309. // [self.navigationController pushViewController:vc animated:YES];
  310. //}
  311. - (void)showCustom:(NIMMessage *)message {
  312. //普通的自定义消息点击事件可以在这里做哦~
  313. }
  314. - (void)openSafari:(NSString *)link {
  315. NSURLComponents *components = [[NSURLComponents alloc] initWithString:link];
  316. if (components) {
  317. if (!components.scheme) {
  318. //默认添加 http
  319. components.scheme = @"http";
  320. }
  321. [[UIApplication sharedApplication] openURL:[components URL]];
  322. }
  323. }
  324. - (NSDictionary *)cellActions {
  325. static NSDictionary *actions = nil;
  326. static dispatch_once_t onceToken;
  327. dispatch_once(&onceToken, ^{
  328. actions = @{@(NIMMessageTypeImage): @"showImage:",
  329. @(NIMMessageTypeVideo): @"showVideo:",
  330. @(NIMMessageTypeLocation): @"showLocation:",
  331. @(NIMMessageTypeFile): @"showFile:",
  332. @(NIMMessageTypeCustom): @"showCustom:"};
  333. });
  334. return actions;
  335. }
  336. - (void)backClick {
  337. [self.navigationController popViewControllerAnimated:YES];
  338. }
  339. - (id <NIMSessionConfig>)sessionConfig {
  340. if (_sessionConfig == nil) {
  341. _sessionConfig = [[NTESSessionConfig alloc] init];
  342. _sessionConfig.session = self.session;
  343. }
  344. return _sessionConfig;
  345. }
  346. - (BOOL)checkRTSCondition {
  347. BOOL result = YES;
  348. if (![[Reachability reachabilityForInternetConnection] isReachable]) {
  349. [self.view makeToast:@"请检查网络" duration:2.0 position:CSToastPositionCenter];
  350. result = NO;
  351. }
  352. NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
  353. if (self.session.sessionType == NIMSessionTypeP2P && [currentAccount isEqualToString:self.session.sessionId]) {
  354. [self.view makeToast:@"不能和自己通话哦" duration:2.0 position:CSToastPositionCenter];
  355. result = NO;
  356. }
  357. if (self.session.sessionType == NIMSessionTypeTeam) {
  358. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:self.session.sessionId];
  359. NSInteger memberNumber = team.memberNumber;
  360. if (memberNumber < 2) {
  361. [self.view makeToast:@"无法发起,群人数少于2人" duration:2.0 position:CSToastPositionCenter];
  362. result = NO;
  363. }
  364. }
  365. return result;
  366. }
  367. #pragma mark - 视频聊天
  368. - (void)onTapMediaItemVideoChat:(NIMMediaItem *)item {
  369. if ([self checkRTSCondition]) {
  370. //由于音视频聊天里头有音频和视频聊天界面的切换,直接用present的话页面过渡会不太自然,这里还是用push,然后做出present的效果
  371. NTESVideoChatViewController *vc = [[NTESVideoChatViewController alloc] initWithCallee:self.session.sessionId];
  372. [self.navigationController presentViewController:vc animated:YES completion:nil];
  373. }
  374. }
  375. - (void)viewWillAppear:(BOOL)animated {
  376. self.hideTabBarWhenPop = self.tabBarController.tabBar.isHidden;
  377. self.tabBarController.tabBar.hidden = YES;
  378. }
  379. - (void)viewWillDisappear:(BOOL)animated {
  380. self.tabBarController.tabBar.hidden = self.hideTabBarWhenPop;
  381. }
  382. - (void)didReceiveMemoryWarning {
  383. [super didReceiveMemoryWarning];
  384. // Dispose of any resources that can be recreated.
  385. }
  386. @end