NTESSessionViewController.m 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. //
  2. // NTESSessionViewController.m
  3. // NIM
  4. //
  5. // Created by amao on 8/11/15.
  6. // Copyright (c) 2015 Netease. All rights reserved.
  7. //
  8. #import "NTESSessionViewController.h"
  9. @import MobileCoreServices;
  10. @import AVFoundation;
  11. #import "Reachability.h"
  12. #import "UIActionSheet+NTESBlock.h"
  13. #import "NTESCustomSysNotificationSender.h"
  14. #import "NTESSessionConfig.h"
  15. #import "NIMMediaItem.h"
  16. #import "NTESSessionMsgConverter.h"
  17. #import "NTESFileLocationHelper.h"
  18. #import "NTESSessionMsgConverter.h"
  19. #import "UIView+Toast.h"
  20. #import "NTESSnapchatAttachment.h"
  21. #import "NTESJanKenPonAttachment.h"
  22. #import "NTESFileTransSelectViewController.h"
  23. #import "NTESAudioChatViewController.h"
  24. #import "NTESWhiteboardViewController.h"
  25. #import "NTESVideoChatViewController.h"
  26. #import "NTESChartletAttachment.h"
  27. #import "NTESGalleryViewController.h"
  28. #import "NTESVideoViewController.h"
  29. #import "NTESFilePreViewController.h"
  30. #import "NTESAudio2TextViewController.h"
  31. #import "NSDictionary+NTESJson.h"
  32. #import "NIMAdvancedTeamCardViewController.h"
  33. #import "NTESSessionRemoteHistoryViewController.h"
  34. #import "NIMNormalTeamCardViewController.h"
  35. #import "UIView+NTES.h"
  36. #import "NTESBundleSetting.h"
  37. #import "NTESPersonalCardViewController.h"
  38. #import "NTESSessionSnapchatContentView.h"
  39. #import "NTESSessionLocalHistoryViewController.h"
  40. #import "NIMContactSelectViewController.h"
  41. #import "SVProgressHUD.h"
  42. #import "NTESSessionCardViewController.h"
  43. #import "NTESFPSLabel.h"
  44. #import "UIAlertView+NTESBlock.h"
  45. #import "NIMKit.h"
  46. #import "NTESSessionUtil.h"
  47. #import "NIMKitMediaFetcher.h"
  48. #import "NIMKitLocationPoint.h"
  49. #import "NIMLocationViewController.h"
  50. #import "NIMKitInfoFetchOption.h"
  51. #import "NTESSubscribeManager.h"
  52. #import "User.h"
  53. #import "GiftInputContainer.h"
  54. #import "GiftTopBarItem.h"
  55. #import "UIImageView+WebCache.h"
  56. #import "AFHTTPSessionManager.h"
  57. #import "TradePublishManager.h"
  58. #import "TradeInputContainer.h"
  59. #import "NIMMessageMaker.h"
  60. #import "GroupInfoViewController.h"
  61. #import "NIMInputView.h"
  62. #import "CityManager.h"
  63. @interface NTESSessionViewController ()
  64. <UIImagePickerControllerDelegate,
  65. UINavigationControllerDelegate,
  66. NIMSystemNotificationManagerDelegate,
  67. NIMMediaManagerDelegate,
  68. NTESTimerHolderDelegate,
  69. NIMContactSelectDelegate,
  70. NIMEventSubscribeManagerDelegate,
  71. GiftDataSourceDelegate,
  72. TradeInputDelegate,
  73. NIMInputDelegate>
  74. @property (nonatomic,strong) NTESCustomSysNotificationSender *notificaionSender;
  75. @property (nonatomic,strong) NTESSessionConfig *sessionConfig;
  76. @property (nonatomic,strong) UIImagePickerController *imagePicker;
  77. @property (nonatomic,strong) NTESTimerHolder *titleTimer;
  78. @property (nonatomic,strong) UIView *currentSingleSnapView;
  79. @property (nonatomic,strong) NTESFPSLabel *fpsLabel;
  80. @property (nonatomic,strong) NIMKitMediaFetcher *mediaFetcher;
  81. @property (nonatomic,strong) GiftTopBarItem *giftItem1;
  82. @property (nonatomic,strong) GiftTopBarItem *giftItem2;
  83. @property (nonatomic,strong) UIView *giftMovieContainer;
  84. @property (nonatomic,strong) UIImageView *giftMovie;
  85. @property (nonatomic,strong) NSTimer *timer;
  86. @property (nonatomic,assign) BOOL displayed;
  87. @end
  88. @implementation NTESSessionViewController
  89. - (void)viewDidLoad {
  90. [super viewDidLoad];
  91. DDLogInfo(@"enter session, id = %@",self.session.sessionId);
  92. _notificaionSender = [[NTESCustomSysNotificationSender alloc] init];
  93. [self setUpNav];
  94. BOOL disableCommandTyping = self.disableCommandTyping || (self.session.sessionType == NIMSessionTypeP2P &&[[NIMSDK sharedSDK].userManager isUserInBlackList:self.session.sessionId]);
  95. if (!disableCommandTyping) {
  96. _titleTimer = [[NTESTimerHolder alloc] init];
  97. [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
  98. }
  99. if ([[NTESBundleSetting sharedConfig] showFps])
  100. {
  101. self.fpsLabel = [[NTESFPSLabel alloc] initWithFrame:CGRectZero];
  102. [self.view addSubview:self.fpsLabel];
  103. self.fpsLabel.right = self.view.width;
  104. self.fpsLabel.top = self.tableView.top + self.tableView.contentInset.top;
  105. }
  106. if (self.session.sessionType == NIMSessionTypeP2P && !self.disableOnlineState)
  107. {
  108. //临时订阅这个人的在线状态
  109. [[NTESSubscribeManager sharedInstance] subscribeTempUserOnlineState:self.session.sessionId];
  110. [[NIMSDK sharedSDK].subscribeManager addDelegate:self];
  111. }
  112. //删除最近会话列表中有人@你的标记
  113. [NTESSessionUtil removeRecentSessionAtMark:self.session];
  114. //record chat room id
  115. if(self.session.sessionType == NIMSessionTypeTeam)
  116. {
  117. [User sharedInfo].currentChatRoomId = self.session.sessionId;
  118. }
  119. else
  120. {
  121. [User sharedInfo].currentChatRoomId = nil;
  122. }
  123. self.sessionInputView.giftContainer.delegate = self;
  124. _giftItem1 = [[GiftTopBarItem alloc] initWithFrame:CGRectMake(0, 64, self.view.width, 40)];
  125. _giftItem1.hidden = YES;
  126. [self.view addSubview:_giftItem1];
  127. _giftItem2 = [[GiftTopBarItem alloc] initWithFrame:CGRectMake(0, _giftItem1.bottom-5, self.view.width, 40)];
  128. _giftItem2.hidden = YES;
  129. [self.view addSubview:_giftItem2];
  130. _giftMovieContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
  131. _giftMovieContainer.backgroundColor = [UIColor colorWithRed:(0) green:(0) blue:(0) alpha:0.7];
  132. _giftMovieContainer.hidden = YES;
  133. _giftMovieContainer.userInteractionEnabled = YES;
  134. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideGiftMovie:)];
  135. [_giftMovieContainer addGestureRecognizer:singleTap];
  136. [self.view addSubview:_giftMovieContainer];
  137. _giftMovie = [[UIImageView alloc] initWithFrame:CGRectMake(0, _giftItem2.bottom + 5, self.view.width, 300)];
  138. [_giftMovieContainer addSubview:_giftMovie];
  139. [self.view bringSubviewToFront:self.sessionInputView];
  140. [self.view bringSubviewToFront:self.sessionInputView.tabContainer];
  141. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(updateGiftTopItem) userInfo:nil repeats:YES];
  142. self.sessionInputView.giftContainer.tradeContainer.delegate = self;
  143. }
  144. - (void)dealloc
  145. {
  146. [[NIMSDK sharedSDK].systemNotificationManager removeDelegate:self];
  147. if (self.session.sessionType == NIMSessionTypeP2P && !self.disableOnlineState)
  148. {
  149. [[NIMSDK sharedSDK].subscribeManager removeDelegate:self];
  150. [[NTESSubscribeManager sharedInstance] unsubscribeTempUserOnlineState:self.session.sessionId];
  151. }
  152. [_fpsLabel invalidate];
  153. [_timer invalidate];
  154. }
  155. - (void)viewDidLayoutSubviews{
  156. [super viewDidLayoutSubviews];
  157. self.fpsLabel.right = self.view.width;
  158. self.fpsLabel.top = self.tableView.top + self.tableView.contentInset.top;
  159. }
  160. - (void)viewWillAppear:(BOOL)animated
  161. {
  162. User *user = [User sharedInfo];
  163. GiftInputContainer *giftInput = [self.sessionInputView giftContainer];
  164. [giftInput setBalance:user.coin];
  165. if(self.session.sessionType == NIMSessionTypeTeam)
  166. {
  167. [giftInput setTeamId:self.session.sessionId];
  168. BOOL isSystemTeam = [[User sharedInfo] isSystemTeam:self.session.sessionId];
  169. giftInput.supplyBtn.hidden = !isSystemTeam;
  170. giftInput.demandBtn.hidden = !isSystemTeam;
  171. if(isSystemTeam)
  172. {
  173. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:self.session.sessionId];
  174. NSString *teamName = team.teamName;
  175. TradePriorityType priorityType = [[CityManager shared] tradePriorityTypeByTeamName:teamName];
  176. NSMutableArray *priorityItems = [[NSMutableArray alloc] init];
  177. for(int i=0; i<user.tradePriorityItemArr.count; i++)
  178. {
  179. TradePriorityItemData *priorityItem = [user.tradePriorityItemArr objectAtIndex:i];
  180. if(priorityItem.type <= (int)priorityType)
  181. {
  182. [priorityItems addObject:priorityItem];
  183. }
  184. }
  185. [giftInput.tradeContainer fillItemData:priorityItems];
  186. }
  187. }
  188. else if(self.session.sessionType == NIMSessionTypeP2P)
  189. {
  190. [giftInput setTeamId:nil];
  191. giftInput.supplyBtn.hidden = YES;
  192. giftInput.demandBtn.hidden = YES;
  193. }
  194. }
  195. - (void)viewDidAppear:(BOOL)animated{
  196. [super viewDidAppear:animated];
  197. self.displayed = YES;
  198. [_timer setFireDate:[NSDate distantPast]];
  199. }
  200. - (void)viewWillDisappear:(BOOL)animated{
  201. [super viewWillDisappear:animated];
  202. [[NIMSDK sharedSDK].mediaManager stopRecord];
  203. [[NIMSDK sharedSDK].mediaManager stopPlay];
  204. [_timer setFireDate:[NSDate distantFuture]];
  205. }
  206. - (void)viewDidDisappear:(BOOL)animated{
  207. [super viewDidDisappear:animated];
  208. self.displayed = NO;
  209. }
  210. - (id<NIMSessionConfig>)sessionConfig
  211. {
  212. if (_sessionConfig == nil) {
  213. _sessionConfig = [[NTESSessionConfig alloc] init];
  214. _sessionConfig.session = self.session;
  215. }
  216. return _sessionConfig;
  217. }
  218. #pragma mark - NIMEventSubscribeManagerDelegate
  219. - (void)onRecvSubscribeEvents:(NSArray *)events
  220. {
  221. for (NIMSubscribeEvent *event in events) {
  222. if ([event.from isEqualToString:self.session.sessionId]) {
  223. [self refreshSessionSubTitle:[NTESSessionUtil onlineState:self.session.sessionId detail:YES]];
  224. }
  225. }
  226. }
  227. #pragma mark - NIMSystemNotificationManagerDelegate
  228. - (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification
  229. {
  230. if (!notification.sendToOnlineUsersOnly) {
  231. return;
  232. }
  233. NSData *data = [[notification content] dataUsingEncoding:NSUTF8StringEncoding];
  234. if (data) {
  235. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
  236. options:0
  237. error:nil];
  238. if ([dict jsonInteger:NTESNotifyID] == NTESCommandTyping && self.session.sessionType == NIMSessionTypeP2P && [notification.sender isEqualToString:self.session.sessionId])
  239. {
  240. [self refreshSessionTitle:@"正在输入..."];
  241. [_titleTimer startTimer:5
  242. delegate:self
  243. repeats:NO];
  244. }
  245. }
  246. }
  247. - (void)onNTESTimerFired:(NTESTimerHolder *)holder
  248. {
  249. [self refreshSessionTitle:self.sessionTitle];
  250. }
  251. - (NSString *)sessionTitle
  252. {
  253. if ([self.session.sessionId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount]) {
  254. return @"我的电脑";
  255. }
  256. return [super sessionTitle];
  257. }
  258. - (NSString *)sessionSubTitle
  259. {
  260. if (self.session.sessionType == NIMSessionTypeP2P && ![self.session.sessionId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount]) {
  261. return [NTESSessionUtil onlineState:self.session.sessionId detail:YES];
  262. }
  263. return @"";
  264. }
  265. - (void)onTextChanged:(id)sender
  266. {
  267. [_notificaionSender sendTypingState:self.session];
  268. }
  269. - (void)onSelectChartlet:(NSString *)chartletId
  270. catalog:(NSString *)catalogId
  271. {
  272. NTESChartletAttachment *attachment = [[NTESChartletAttachment alloc] init];
  273. attachment.chartletId = chartletId;
  274. attachment.chartletCatalog = catalogId;
  275. [self sendMessage:[NTESSessionMsgConverter msgWithChartletAttachment:attachment]];
  276. }
  277. #pragma mark - 石头剪子布
  278. - (void)onTapMediaItemJanKenPon:(NIMMediaItem *)item
  279. {
  280. NTESJanKenPonAttachment *attachment = [[NTESJanKenPonAttachment alloc] init];
  281. attachment.value = arc4random() % 3 + 1;
  282. [self sendMessage:[NTESSessionMsgConverter msgWithJenKenPon:attachment]];
  283. }
  284. #pragma mark - 实时语音
  285. - (void)onTapMediaItemAudioChat:(NIMMediaItem *)item
  286. {
  287. if ([self checkCondition]) {
  288. //由于音视频聊天里头有音频和视频聊天界面的切换,直接用present的话页面过渡会不太自然,这里还是用push,然后做出present的效果
  289. NTESAudioChatViewController *vc = [[NTESAudioChatViewController alloc] initWithCallee:self.session.sessionId];
  290. CATransition *transition = [CATransition animation];
  291. transition.duration = 0.25;
  292. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
  293. transition.type = kCATransitionPush;
  294. transition.subtype = kCATransitionFromTop;
  295. [self.navigationController.view.layer addAnimation:transition forKey:nil];
  296. self.navigationController.navigationBarHidden = YES;
  297. [self.navigationController pushViewController:vc animated:NO];
  298. }
  299. }
  300. #pragma mark - 视频聊天
  301. - (void)onTapMediaItemVideoChat:(NIMMediaItem *)item
  302. {
  303. if ([self checkCondition]) {
  304. //由于音视频聊天里头有音频和视频聊天界面的切换,直接用present的话页面过渡会不太自然,这里还是用push,然后做出present的效果
  305. NTESVideoChatViewController *vc = [[NTESVideoChatViewController alloc] initWithCallee:self.session.sessionId];
  306. CATransition *transition = [CATransition animation];
  307. transition.duration = 0.25;
  308. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
  309. transition.type = kCATransitionPush;
  310. transition.subtype = kCATransitionFromTop;
  311. [self.navigationController.view.layer addAnimation:transition forKey:nil];
  312. self.navigationController.navigationBarHidden = YES;
  313. [self.navigationController pushViewController:vc animated:NO];
  314. }
  315. }
  316. #pragma mark - 文件传输
  317. - (void)onTapMediaItemFileTrans:(NIMMediaItem *)item
  318. {
  319. NTESFileTransSelectViewController *vc = [[NTESFileTransSelectViewController alloc]
  320. initWithNibName:nil bundle:nil];
  321. __weak typeof(self) wself = self;
  322. vc.completionBlock = ^void(id sender,NSString *ext){
  323. if ([sender isKindOfClass:[NSString class]]) {
  324. [wself sendMessage:[NTESSessionMsgConverter msgWithFilePath:sender]];
  325. }else if ([sender isKindOfClass:[NSData class]]){
  326. [wself sendMessage:[NTESSessionMsgConverter msgWithFileData:sender extension:ext]];
  327. }
  328. };
  329. [self.navigationController pushViewController:vc animated:YES];
  330. }
  331. #pragma mark - 阅后即焚
  332. - (void)onTapMediaItemSnapChat:(NIMMediaItem *)item
  333. {
  334. UIActionSheet *sheet;
  335. BOOL isCamraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
  336. if (isCamraAvailable) {
  337. sheet = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选取",@"拍照",nil];
  338. }else{
  339. sheet = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选取",nil];
  340. }
  341. __weak typeof(self) wself = self;
  342. [sheet showInView:self.view completionHandler:^(NSInteger index) {
  343. switch (index) {
  344. case 0:{
  345. //相册
  346. [wself.mediaFetcher fetchPhotoFromLibrary:^(NSArray *images, NSString *path, PHAssetMediaType type){
  347. if (images.count) {
  348. [wself sendSnapchatMessage:images.firstObject];
  349. }
  350. if (path) {
  351. [wself sendSnapchatMessagePath:path];
  352. }
  353. }];
  354. }
  355. break;
  356. case 1:{
  357. //相机
  358. [wself.mediaFetcher fetchMediaFromCamera:^(NSString *path, UIImage *image) {
  359. if (image) {
  360. [wself sendSnapchatMessage:image];
  361. }
  362. }];
  363. }
  364. break;
  365. default:
  366. return;
  367. }
  368. }];
  369. }
  370. - (void)sendSnapchatMessagePath:(NSString *)path
  371. {
  372. NTESSnapchatAttachment *attachment = [[NTESSnapchatAttachment alloc] init];
  373. [attachment setImageFilePath:path];
  374. [self sendMessage:[NTESSessionMsgConverter msgWithSnapchatAttachment:attachment]];
  375. }
  376. - (void)sendSnapchatMessage:(UIImage *)image
  377. {
  378. NTESSnapchatAttachment *attachment = [[NTESSnapchatAttachment alloc] init];
  379. [attachment setImage:image];
  380. [self sendMessage:[NTESSessionMsgConverter msgWithSnapchatAttachment:attachment]];
  381. }
  382. #pragma mark - 白板
  383. - (void)onTapMediaItemWhiteBoard:(NIMMediaItem *)item
  384. {
  385. NTESWhiteboardViewController *vc = [[NTESWhiteboardViewController alloc] initWithSessionID:nil
  386. peerID:self.session.sessionId
  387. types:NIMRTSServiceReliableTransfer | NIMRTSServiceAudio
  388. info:@"白板演示"];
  389. [self presentViewController:vc animated:NO completion:nil];
  390. }
  391. #pragma mark - 提醒消息
  392. - (void)onTapMediaItemTip:(NIMMediaItem *)item
  393. {
  394. UIAlertView *alert =[[UIAlertView alloc] initWithTitle:nil message:@"输入提醒" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  395. alert.alertViewStyle = UIAlertViewStylePlainTextInput;
  396. [alert showAlertWithCompletionHandler:^(NSInteger index) {
  397. switch (index) {
  398. case 1:{
  399. UITextField *textField = [alert textFieldAtIndex:0];
  400. NIMMessage *message = [NTESSessionMsgConverter msgWithTip:textField.text];
  401. [self sendMessage:message];
  402. }
  403. break;
  404. default:
  405. break;
  406. }
  407. }];
  408. }
  409. #pragma mark - 录音事件
  410. - (void)onRecordFailed:(NSError *)error
  411. {
  412. [self.view makeToast:@"录音失败" duration:2 position:CSToastPositionCenter];
  413. }
  414. - (BOOL)recordFileCanBeSend:(NSString *)filepath
  415. {
  416. NSURL *URL = [NSURL fileURLWithPath:filepath];
  417. AVURLAsset *urlAsset = [[AVURLAsset alloc]initWithURL:URL options:nil];
  418. CMTime time = urlAsset.duration;
  419. CGFloat mediaLength = CMTimeGetSeconds(time);
  420. return mediaLength > 2;
  421. }
  422. - (void)showRecordFileNotSendReason
  423. {
  424. [self.view makeToast:@"录音时间太短" duration:0.2f position:CSToastPositionCenter];
  425. }
  426. #pragma mark - Cell事件
  427. - (BOOL)onTapCell:(NIMKitEvent *)event
  428. {
  429. BOOL handled = [super onTapCell:event];
  430. NSString *eventName = event.eventName;
  431. if ([eventName isEqualToString:NIMKitEventNameTapContent])
  432. {
  433. NIMMessage *message = event.messageModel.message;
  434. NSDictionary *actions = [self cellActions];
  435. NSString *value = actions[@(message.messageType)];
  436. if (value) {
  437. SEL selector = NSSelectorFromString(value);
  438. if (selector && [self respondsToSelector:selector]) {
  439. SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:message]);
  440. handled = YES;
  441. }
  442. }
  443. }
  444. else if([eventName isEqualToString:NIMKitEventNameTapLabelLink])
  445. {
  446. NSString *link = event.data;
  447. [self.view makeToast:[NSString stringWithFormat:@"tap link : %@",link]
  448. duration:2
  449. position:CSToastPositionCenter];
  450. handled = YES;
  451. }
  452. else if([eventName isEqualToString:NIMDemoEventNameOpenSnapPicture])
  453. {
  454. NIMCustomObject *object = event.messageModel.message.messageObject;
  455. NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment;
  456. if(attachment.isFired){
  457. return handled;
  458. }
  459. UIView *sender = event.data;
  460. self.currentSingleSnapView = [NTESGalleryViewController alertSingleSnapViewWithMessage:object.message baseView:sender];
  461. handled = YES;
  462. }
  463. else if([eventName isEqualToString:NIMDemoEventNameCloseSnapPicture])
  464. {
  465. //点击很快的时候可能会触发两次查看,所以这里不管有没有查看过 先强直销毁掉
  466. NIMCustomObject *object = event.messageModel.message.messageObject;
  467. UIView *senderView = event.data;
  468. [senderView dismissPresentedView:YES complete:nil];
  469. NTESSnapchatAttachment *attachment = (NTESSnapchatAttachment *)object.attachment;
  470. if(attachment.isFired){
  471. return handled;
  472. }
  473. attachment.isFired = YES;
  474. NIMMessage *message = object.message;
  475. if ([NTESBundleSetting sharedConfig].autoRemoveSnapMessage) {
  476. [[NIMSDK sharedSDK].conversationManager deleteMessage:message];
  477. [self uiDeleteMessage:message];
  478. }else{
  479. [[NIMSDK sharedSDK].conversationManager updateMessage:message forSession:message.session completion:nil];
  480. [self uiUpdateMessage:message];
  481. }
  482. [[NSFileManager defaultManager] removeItemAtPath:attachment.filepath error:nil];
  483. handled = YES;
  484. self.currentSingleSnapView = nil;
  485. }
  486. if (!handled) {
  487. NSAssert(0, @"invalid event");
  488. }
  489. return handled;
  490. }
  491. - (BOOL)onTapAvatar:(NSString *)userId{
  492. UIViewController *vc = [[NTESPersonalCardViewController alloc] initWithUserId:userId];
  493. [self.navigationController pushViewController:vc animated:YES];
  494. return YES;
  495. }
  496. - (BOOL)onLongPressAvatar:(NSString *)userId{
  497. NSLog(@"onLongPressAvatar : %@", userId);
  498. [self.sessionInputView addAtItem:userId];
  499. return YES;
  500. }
  501. #pragma mark - Cell Actions
  502. - (void)showImage:(NIMMessage *)message
  503. {
  504. NIMImageObject *object = message.messageObject;
  505. NTESGalleryItem *item = [[NTESGalleryItem alloc] init];
  506. item.thumbPath = [object thumbPath];
  507. item.imageURL = [object url];
  508. item.name = [object displayName];
  509. NTESGalleryViewController *vc = [[NTESGalleryViewController alloc] initWithItem:item];
  510. [self.navigationController pushViewController:vc animated:YES];
  511. if(![[NSFileManager defaultManager] fileExistsAtPath:object.thumbPath]){
  512. //如果缩略图下跪了,点进看大图的时候再去下一把缩略图
  513. __weak typeof(self) wself = self;
  514. [[NIMSDK sharedSDK].resourceManager download:object.thumbUrl filepath:object.thumbPath progress:nil completion:^(NSError *error) {
  515. if (!error) {
  516. [wself uiUpdateMessage:message];
  517. }
  518. }];
  519. }
  520. }
  521. - (void)showVideo:(NIMMessage *)message
  522. {
  523. NIMVideoObject *object = message.messageObject;
  524. NTESVideoViewController *playerViewController = [[NTESVideoViewController alloc] initWithVideoObject:object];
  525. [self.navigationController pushViewController:playerViewController animated:YES];
  526. if(![[NSFileManager defaultManager] fileExistsAtPath:object.coverPath]){
  527. //如果封面图下跪了,点进视频的时候再去下一把封面图
  528. __weak typeof(self) wself = self;
  529. [[NIMSDK sharedSDK].resourceManager download:object.coverUrl filepath:object.coverPath progress:nil completion:^(NSError *error) {
  530. if (!error) {
  531. [wself uiUpdateMessage:message];
  532. }
  533. }];
  534. }
  535. }
  536. - (void)showLocation:(NIMMessage *)message
  537. {
  538. NIMLocationObject *object = message.messageObject;
  539. NIMKitLocationPoint *locationPoint = [[NIMKitLocationPoint alloc] initWithLocationObject:object];
  540. NIMLocationViewController *vc = [[NIMLocationViewController alloc] initWithLocationPoint:locationPoint];
  541. [self.navigationController pushViewController:vc animated:YES];
  542. }
  543. - (void)showFile:(NIMMessage *)message
  544. {
  545. NIMFileObject *object = message.messageObject;
  546. NTESFilePreViewController *vc = [[NTESFilePreViewController alloc] initWithFileObject:object];
  547. [self.navigationController pushViewController:vc animated:YES];
  548. }
  549. - (void)showCustom:(NIMMessage *)message
  550. {
  551. //普通的自定义消息点击事件可以在这里做哦~
  552. NSLog(@"show custom message");
  553. }
  554. #pragma mark - 导航按钮
  555. - (void)onTouchUpInfoBtn:(id)sender{
  556. NTESSessionCardViewController *vc = [[NTESSessionCardViewController alloc] initWithSession:self.session];
  557. [self.navigationController pushViewController:vc animated:YES];
  558. }
  559. - (void)enterHistory:(id)sender{
  560. [self.view endEditing:YES];
  561. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择操作" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"云消息记录",@"搜索本地消息记录",@"清空本地聊天记录", nil];
  562. [sheet showInView:self.view completionHandler:^(NSInteger index) {
  563. switch (index) {
  564. case 0:{ //查看云端消息
  565. NTESSessionRemoteHistoryViewController *vc = [[NTESSessionRemoteHistoryViewController alloc] initWithSession:self.session];
  566. [self.navigationController pushViewController:vc animated:YES];
  567. break;
  568. }
  569. case 1:{ //搜索本地消息
  570. NTESSessionLocalHistoryViewController *vc = [[NTESSessionLocalHistoryViewController alloc] initWithSession:self.session];
  571. [self.navigationController pushViewController:vc animated:YES];
  572. break;
  573. }
  574. case 2:{ //清空聊天记录
  575. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定清空聊天记录?" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
  576. __weak UIActionSheet *wSheet;
  577. [sheet showInView:self.view completionHandler:^(NSInteger index) {
  578. if (index == wSheet.destructiveButtonIndex) {
  579. BOOL removeRecentSession = [NTESBundleSetting sharedConfig].removeSessionWheDeleteMessages;
  580. [[NIMSDK sharedSDK].conversationManager deleteAllmessagesInSession:self.session removeRecentSession:removeRecentSession];
  581. }
  582. }];
  583. break;
  584. }
  585. default:
  586. break;
  587. }
  588. }];
  589. }
  590. - (void)enterTeamCard:(id)sender{
  591. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:self.session.sessionId];
  592. UIViewController *vc;
  593. if (team.type == NIMTeamTypeNormal) {
  594. vc = [[NIMNormalTeamCardViewController alloc] initWithTeam:team];
  595. }else if(team.type == NIMTeamTypeAdvanced){
  596. //vc = [[NIMAdvancedTeamCardViewController alloc] initWithTeam:team];
  597. vc = [[GroupInfoViewController alloc] initWithTeam:team];
  598. }
  599. [self.navigationController pushViewController:vc animated:YES];
  600. }
  601. #pragma mark - 菜单
  602. - (NSArray *)menusItems:(NIMMessage *)message
  603. {
  604. NSMutableArray *items = [NSMutableArray array];
  605. NSArray *defaultItems = [super menusItems:message];
  606. if (defaultItems) {
  607. [items addObjectsFromArray:defaultItems];
  608. }
  609. if ([NTESSessionUtil canMessageBeForwarded:message]) {
  610. [items addObject:[[UIMenuItem alloc] initWithTitle:@"转发" action:@selector(forwardMessage:)]];
  611. }
  612. if ([NTESSessionUtil canMessageBeRevoked:message]) {
  613. [items addObject:[[UIMenuItem alloc] initWithTitle:@"撤回" action:@selector(revokeMessage:)]];
  614. }
  615. if (message.messageType == NIMMessageTypeAudio) {
  616. [items addObject:[[UIMenuItem alloc] initWithTitle:@"转文字" action:@selector(audio2Text:)]];
  617. }
  618. return items;
  619. }
  620. - (void)audio2Text:(id)sender
  621. {
  622. NIMMessage *message = [self messageForMenu];
  623. __weak typeof(self) wself = self;
  624. NTESAudio2TextViewController *vc = [[NTESAudio2TextViewController alloc] initWithMessage:message];
  625. vc.completeHandler = ^(void){
  626. [wself uiUpdateMessage:message];
  627. };
  628. [self presentViewController:vc
  629. animated:YES
  630. completion:nil];
  631. }
  632. - (void)forwardMessage:(id)sender
  633. {
  634. NIMMessage *message = [self messageForMenu];
  635. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择会话类型" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"个人",@"群组", nil];
  636. __weak typeof(self) weakSelf = self;
  637. [sheet showInView:self.view completionHandler:^(NSInteger index) {
  638. switch (index) {
  639. case 0:{
  640. NIMContactFriendSelectConfig *config = [[NIMContactFriendSelectConfig alloc] init];
  641. config.needMutiSelected = NO;
  642. NIMContactSelectViewController *vc = [[NIMContactSelectViewController alloc] initWithConfig:config];
  643. vc.finshBlock = ^(NSArray *array){
  644. NSString *userId = array.firstObject;
  645. NIMSession *session = [NIMSession session:userId type:NIMSessionTypeP2P];
  646. [weakSelf forwardMessage:message toSession:session];
  647. };
  648. [vc show];
  649. }
  650. break;
  651. case 1:{
  652. NIMContactTeamSelectConfig *config = [[NIMContactTeamSelectConfig alloc] init];
  653. NIMContactSelectViewController *vc = [[NIMContactSelectViewController alloc] initWithConfig:config];
  654. vc.finshBlock = ^(NSArray *array){
  655. NSString *teamId = array.firstObject;
  656. NIMSession *session = [NIMSession session:teamId type:NIMSessionTypeTeam];
  657. [weakSelf forwardMessage:message toSession:session];
  658. };
  659. [vc show];
  660. }
  661. break;
  662. case 2:
  663. break;
  664. default:
  665. break;
  666. }
  667. }];
  668. }
  669. - (void)revokeMessage:(id)sender
  670. {
  671. NIMMessage *message = [self messageForMenu];
  672. __weak typeof(self) weakSelf = self;
  673. [[NIMSDK sharedSDK].chatManager revokeMessage:message completion:^(NSError * _Nullable error) {
  674. if (error) {
  675. if (error.code == NIMRemoteErrorCodeDomainExpireOld) {
  676. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"发送时间超过2分钟的消息,不能被撤回" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  677. [alert show];
  678. }else{
  679. DDLogError(@"revoke message eror code %zd",error.code);
  680. [weakSelf.view makeToast:@"消息撤回失败,请重试" duration:2.0 position:CSToastPositionCenter];
  681. }
  682. }
  683. else
  684. {
  685. NIMMessageModel *model = [self uiDeleteMessage:message];
  686. NIMMessage *tip = [NTESSessionMsgConverter msgWithTip:[NTESSessionUtil tipOnMessageRevoked:message]];
  687. tip.timestamp = model.messageTime;
  688. [self uiInsertMessages:@[tip]];
  689. tip.timestamp = message.timestamp;
  690. // saveMessage 方法执行成功后会触发 onRecvMessages: 回调,但是这个回调上来的 NIMMessage 时间为服务器时间,和界面上的时间有一定出入,所以要提前先在界面上插入一个和被删消息的界面时间相符的 Tip, 当触发 onRecvMessages: 回调时,组件判断这条消息已经被插入过了,就会忽略掉。
  691. [[NIMSDK sharedSDK].conversationManager saveMessage:tip forSession:message.session completion:nil];
  692. }
  693. }];
  694. }
  695. - (void)forwardMessage:(NIMMessage *)message toSession:(NIMSession *)session
  696. {
  697. NSString *name;
  698. if (session.sessionType == NIMSessionTypeP2P)
  699. {
  700. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  701. option.session = session;
  702. name = [[NIMKit sharedKit] infoByUser:session.sessionId option:option].showName;
  703. }
  704. else
  705. {
  706. name = [[NIMKit sharedKit] infoByTeam:session.sessionId option:nil].showName;
  707. }
  708. NSString *tip = [NSString stringWithFormat:@"确认转发给 %@ ?",name];
  709. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确认转发" message:tip delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
  710. __weak typeof(self) weakSelf = self;
  711. [alert showAlertWithCompletionHandler:^(NSInteger index) {
  712. if(index == 1){
  713. [[NIMSDK sharedSDK].chatManager forwardMessage:message toSession:session error:nil];
  714. [weakSelf.view makeToast:@"已发送" duration:2.0 position:CSToastPositionCenter];
  715. }
  716. }];
  717. }
  718. #pragma mark - 辅助方法
  719. - (void)sendImageMessagePath:(NSString *)path
  720. {
  721. [self sendSnapchatMessagePath:path];
  722. }
  723. - (BOOL)checkCondition
  724. {
  725. BOOL result = YES;
  726. if (![[Reachability reachabilityForInternetConnection] isReachable]) {
  727. [self.view makeToast:@"请检查网络" duration:2.0 position:CSToastPositionCenter];
  728. result = NO;
  729. }
  730. NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
  731. if ([currentAccount isEqualToString:self.session.sessionId]) {
  732. [self.view makeToast:@"不能和自己通话哦" duration:2.0 position:CSToastPositionCenter];
  733. result = NO;
  734. }
  735. return result;
  736. }
  737. - (NSDictionary *)cellActions
  738. {
  739. static NSDictionary *actions = nil;
  740. static dispatch_once_t onceToken;
  741. dispatch_once(&onceToken, ^{
  742. actions = @{@(NIMMessageTypeImage) : @"showImage:",
  743. @(NIMMessageTypeVideo) : @"showVideo:",
  744. @(NIMMessageTypeLocation) : @"showLocation:",
  745. @(NIMMessageTypeFile) : @"showFile:",
  746. @(NIMMessageTypeCustom): @"showCustom:"};
  747. });
  748. return actions;
  749. }
  750. - (NIMKitMediaFetcher *)mediaFetcher
  751. {
  752. if (!_mediaFetcher) {
  753. _mediaFetcher = [[NIMKitMediaFetcher alloc] init];
  754. _mediaFetcher.limit = 1;
  755. _mediaFetcher.mediaTypes = @[(NSString *)kUTTypeImage];;;
  756. }
  757. return _mediaFetcher;
  758. }
  759. - (void)setUpNav{
  760. // UIButton *enterTeamCard = [UIButton buttonWithType:UIButtonTypeCustom];
  761. // [enterTeamCard addTarget:self action:@selector(enterTeamCard:) forControlEvents:UIControlEventTouchUpInside];
  762. // [enterTeamCard setImage:[UIImage imageNamed:@"icon_session_info_normal"] forState:UIControlStateNormal];
  763. // [enterTeamCard setImage:[UIImage imageNamed:@"icon_session_info_pressed"] forState:UIControlStateHighlighted];
  764. // [enterTeamCard sizeToFit];
  765. // UIBarButtonItem *enterTeamCardItem = [[UIBarButtonItem alloc] initWithCustomView:enterTeamCard];
  766. //
  767. // UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  768. // [infoBtn addTarget:self action:@selector(onTouchUpInfoBtn:) forControlEvents:UIControlEventTouchUpInside];
  769. // [infoBtn setImage:[UIImage imageNamed:@"icon_session_info_normal"] forState:UIControlStateNormal];
  770. // [infoBtn setImage:[UIImage imageNamed:@"icon_session_info_pressed"] forState:UIControlStateHighlighted];
  771. // [infoBtn sizeToFit];
  772. // UIBarButtonItem *enterUInfoItem = [[UIBarButtonItem alloc] initWithCustomView:infoBtn];
  773. //
  774. // UIButton *historyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  775. // [historyBtn addTarget:self action:@selector(enterHistory:) forControlEvents:UIControlEventTouchUpInside];
  776. // [historyBtn setImage:[UIImage imageNamed:@"icon_history_normal"] forState:UIControlStateNormal];
  777. // [historyBtn setImage:[UIImage imageNamed:@"icon_history_pressed"] forState:UIControlStateHighlighted];
  778. // [historyBtn sizeToFit];
  779. // UIBarButtonItem *historyButtonItem = [[UIBarButtonItem alloc] initWithCustomView:historyBtn];
  780. //
  781. //
  782. //
  783. // if (self.session.sessionType == NIMSessionTypeTeam) {
  784. // self.navigationItem.rightBarButtonItems = @[enterTeamCardItem,historyButtonItem];
  785. // }else if(self.session.sessionType == NIMSessionTypeP2P){
  786. // if ([self.session.sessionId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]]) {
  787. // self.navigationItem.rightBarButtonItems = @[historyButtonItem];
  788. // }else{
  789. // self.navigationItem.rightBarButtonItems = @[enterUInfoItem,historyButtonItem];
  790. // }
  791. // }
  792. UIButton *enterTeamCard = [UIButton buttonWithType:UIButtonTypeCustom];
  793. [enterTeamCard addTarget:self action:@selector(enterTeamCard:) forControlEvents:UIControlEventTouchUpInside];
  794. [enterTeamCard setImage:[UIImage imageNamed:@"设置图标"] forState:UIControlStateNormal];
  795. [enterTeamCard setFrame:CGRectMake(0, 0, 30, 30)];
  796. UIBarButtonItem *enterTeamCardItem = [[UIBarButtonItem alloc] initWithCustomView:enterTeamCard];
  797. UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  798. [searchBtn addTarget:self action:@selector(searchChatContent:) forControlEvents:UIControlEventTouchUpInside];
  799. [searchBtn setImage:[UIImage imageNamed:@"查找聊天内容_03"] forState:UIControlStateNormal];
  800. [searchBtn sizeToFit];
  801. UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchChatContent:)];
  802. UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteChatCache:)];
  803. UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  804. [deleteBtn addTarget:self action:@selector(onTouchUpInfoBtn:) forControlEvents:UIControlEventTouchUpInside];
  805. [deleteBtn setImage:[UIImage imageNamed:@"icon_session_info_normal"] forState:UIControlStateNormal];
  806. [deleteBtn sizeToFit];
  807. //UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
  808. //UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithCustomView:deleteBtn];
  809. if(self.session.sessionType == NIMSessionTypeTeam)
  810. {
  811. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:self.session.sessionId];
  812. if(team && team.owner && [team.owner isEqualToString:@"10000"])
  813. {
  814. [self.navigationItem.titleView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterTeamCard:)]];
  815. self.navigationItem.rightBarButtonItems = @[deleteItem, searchItem];
  816. }
  817. else
  818. {
  819. self.navigationItem.rightBarButtonItems = @[enterTeamCardItem];
  820. }
  821. }
  822. else
  823. {
  824. self.navigationItem.rightBarButtonItems = @[deleteItem, searchItem];
  825. }
  826. }
  827. - (BOOL)shouldAutorotate{
  828. return !self.currentSingleSnapView;
  829. }
  830. - (void)requestGiftDataList
  831. {
  832. User *user = [User sharedInfo];
  833. [user requestGiftList:^(NSMutableArray * _Nullable list) {
  834. [self.sessionInputView.giftContainer initGiftList:list];
  835. } failure:^(NSError * _Nullable error) {
  836. NSLog(@"获取礼物列表失败");
  837. }];
  838. }
  839. - (void)sendGift:(GiftData *)giftData count:(int)count msg:(NSString *)msg userId:(NSString *)userId
  840. {
  841. NSString *teamId = @"";
  842. NSString *menberId = userId != nil ? userId : @"";
  843. if(self.session.sessionType == NIMSessionTypeTeam)
  844. {
  845. teamId = self.session.sessionId;
  846. }
  847. else if(self.session.sessionType == NIMSessionTypeP2P)
  848. {
  849. menberId = self.session.sessionId;
  850. }
  851. __weak typeof(self) wself = self;
  852. User *user = [User sharedInfo];
  853. [user sendGif:giftData count:count teamId:teamId targetId:menberId msg:msg success:^(NSString * _Nullable giftId, NSString * _Nullable info){
  854. [wself.sessionInputView.giftContainer setBalance:user.coin];
  855. NSArray *atUsers = nil;
  856. if(wself.session.sessionType == NIMSessionTypeTeam && userId != nil && userId.length > 0)
  857. {
  858. atUsers = @[userId];
  859. }
  860. else if(wself.session.sessionType == NIMSessionTypeP2P)
  861. {
  862. atUsers = @[];
  863. }
  864. if(atUsers.count)
  865. {
  866. [[NIMSDK sharedSDK].userManager fetchUserInfos:atUsers completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
  867. if(users.count)
  868. {
  869. NIMUser *u = [users objectAtIndex:0];
  870. NSString *content = [NSString stringWithFormat:@"@%@ %@", u.userInfo.nickName ? u.userInfo.nickName : u.userId, msg];
  871. [wself onSendGiftText:content atUsers:atUsers giftData:giftData giftId:giftId count:count teamId:teamId targetId:menberId];
  872. }
  873. }];
  874. }
  875. else
  876. {
  877. NSString *content = [NSString stringWithFormat:@"%@%@", @"", msg];
  878. [wself onSendGiftText:content atUsers:atUsers giftData:giftData giftId:giftId count:count teamId:teamId targetId:menberId];
  879. }
  880. //[wself requestGift];
  881. } failure:^(NSError * _Nullable error) {
  882. //
  883. }];
  884. [self.sessionInputView endEditing:YES];
  885. [self.sessionInputView unselectedTabBarItem];
  886. }
  887. - (void)onSendGiftText:(NSString *)text atUsers:(NSArray *)atUsers giftData:(GiftData *)giftData giftId:(NSString *)giftId count:(int)count teamId:(NSString *)teamId targetId:(NSString *)targetId
  888. {
  889. NTESGiftAttachment *attachment = [[NTESGiftAttachment alloc] init];
  890. attachment.giftId = giftId;
  891. attachment.senderId = [NSString stringWithFormat:@"%d", [User sharedInfo].userId];
  892. attachment.teamId = teamId;
  893. attachment.targetId = targetId;
  894. attachment.count = [NSString stringWithFormat:@"%d", count];
  895. attachment.msg = text;
  896. attachment.movieURL = giftData.movieURL;
  897. attachment.iconURL = giftData.iconURL;
  898. NIMMessage *message = [NTESSessionMsgConverter msgWithGift:attachment];
  899. if (atUsers.count) {
  900. NIMMessageApnsMemberOption *apnsOption = [[NIMMessageApnsMemberOption alloc] init];
  901. apnsOption.userIds = atUsers;
  902. apnsOption.forcePush = YES;
  903. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  904. option.session = self.session;
  905. NSString *me = [[NIMKit sharedKit].provider infoByUser:[NIMSDK sharedSDK].loginManager.currentAccount option:option].showName;
  906. apnsOption.apnsContent = [NSString stringWithFormat:@"%@在群里@了你",me];
  907. message.apnsMemberOption = apnsOption;
  908. }
  909. [self sendMessage:message];
  910. [self handleGiftMessage:attachment];
  911. }
  912. - (void)handleGiftMessage:(NTESGiftAttachment *)giftAttachment{
  913. NSString *giftId = giftAttachment.giftId;
  914. NSString *senderId = giftAttachment.senderId;
  915. NSString *teamId = giftAttachment.teamId;
  916. NSString *targetId = giftAttachment.targetId;
  917. NSString *key = teamId ? teamId : targetId;
  918. if(!self.displayed)
  919. {
  920. [[User sharedInfo] initGiftGot:key dataId:giftId];
  921. return;
  922. }
  923. if([[User sharedInfo] isNewGiftGot:key dataId:giftId])
  924. {
  925. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  926. [dict setValue:senderId forKey:@"sender"];
  927. [dict setValue:giftAttachment.iconURL forKey:@"gift"];
  928. [dict setValue:teamId forKey:@"team"];
  929. [dict setValue:targetId forKey:@"target"];
  930. [dict setValue:giftAttachment.count forKey:@"count"];
  931. if(!_giftItem1.isHidden && !_giftItem2.isHidden)
  932. {
  933. [_giftItem1 setInfo:_giftItem2.infoDict];
  934. _giftItem1.timeCount = _giftItem2.timeCount;
  935. [_giftItem2 setInfo:dict];
  936. _giftItem2.timeCount = 0;
  937. }
  938. else if(!_giftItem1.isHidden && _giftItem2.isHidden)
  939. {
  940. [_giftItem2 setInfo:dict];
  941. [_giftItem2 setHidden:NO];
  942. _giftItem2.timeCount = 0;
  943. }
  944. else
  945. {
  946. [_giftItem1 setInfo:dict];
  947. [_giftItem1 setHidden:NO];
  948. _giftItem1.timeCount = 0;
  949. }
  950. NSURL *url = [NSURL URLWithString:giftAttachment.movieURL];
  951. [_giftMovie sd_setImageWithURL:url placeholderImage:nil];
  952. [_giftMovieContainer setHidden:NO];
  953. }
  954. }
  955. - (void)updateGiftTopItem{
  956. NSLog(@"updateGiftTopItem");
  957. int hideTimeCount = 2;
  958. if(!_giftItem1.isHidden){
  959. _giftItem1.timeCount++;
  960. if(_giftItem1.timeCount > hideTimeCount)
  961. {
  962. [_giftItem1 setHidden:YES];
  963. }
  964. }
  965. if(!_giftItem2.isHidden){
  966. _giftItem2.timeCount++;
  967. if(_giftItem2.timeCount > hideTimeCount)
  968. {
  969. [_giftItem2 setHidden:YES];
  970. }
  971. }
  972. if(!_giftItem2.isHidden && _giftItem1.isHidden)
  973. {
  974. [_giftItem1 setInfo:_giftItem2.infoDict];
  975. _giftItem1.timeCount = _giftItem2.timeCount;
  976. [_giftItem1 setHidden:NO];
  977. [_giftItem2 setHidden:YES];
  978. }
  979. }
  980. - (void)hideGiftMovie:(id)sender
  981. {
  982. _giftMovieContainer.hidden = YES;
  983. }
  984. - (void)inputTabSelected{
  985. [self hideGiftMovie:nil];
  986. }
  987. - (void)deleteChatCache:(int)sender
  988. {
  989. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定清空聊天记录?" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
  990. __weak UIActionSheet *wSheet;
  991. [sheet showInView:self.view completionHandler:^(NSInteger index) {
  992. if (index == wSheet.destructiveButtonIndex) {
  993. BOOL removeRecentSession = [NTESBundleSetting sharedConfig].removeSessionWheDeleteMessages;
  994. [[NIMSDK sharedSDK].conversationManager deleteAllmessagesInSession:self.session removeRecentSession:removeRecentSession];
  995. }
  996. }];
  997. }
  998. - (void)searchChatContent:(int)sender
  999. {
  1000. NTESSessionLocalHistoryViewController *vc = [[NTESSessionLocalHistoryViewController alloc] initWithSession:self.session];
  1001. [self.navigationController pushViewController:vc animated:YES];
  1002. }
  1003. - (void)sendTrade:(NSString *)msg picArr:(NSMutableArray *)picArr priority:(int)priority priorityType:(int)priorityType type:(int)type{
  1004. if((!msg || msg.length == 0) && picArr.count == 0)
  1005. {
  1006. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"你还什么都没说呢" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  1007. [alert show];
  1008. return;
  1009. }
  1010. TradePublishManager *tradePublishManager = [[TradePublishManager alloc] init];
  1011. tradePublishManager.inputView = self.sessionInputView;
  1012. [tradePublishManager tradePublish:msg picArr:picArr priority:priority priorityType:priorityType tradeType:type];
  1013. }
  1014. @end