YOUPAIUCIMP2PSessionVC.m 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. //
  2. // YOUPAIUCIMP2PSessionVC.m
  3. // wolfman
  4. //
  5. // Created by 张灿 on 2017/6/3.
  6. // Copyright © 2017年 shareSmile. All rights reserved.
  7. //
  8. #import "YOUPAIUCIMP2PSessionVC.h"
  9. #import "NIMSessionConfigurateProtocol.h"
  10. #import "NIMKit.h"
  11. #import "NIMMessageCellProtocol.h"
  12. #import "NIMMessageModel.h"
  13. #import "NIMKitUtil.h"
  14. #import "NIMCustomLeftBarView.h"
  15. #import "NIMBadgeView.h"
  16. #import "UITableView+NIMScrollToBottom.h"
  17. #import "NIMMessageMaker.h"
  18. #import "UIView+NIM.h"
  19. #import "NIMKitTitleView.h"
  20. #import "NIMKitKeyboardInfo.h"
  21. #import "NIMSessionConfigurator.h"
  22. #import "NIMKitInfoFetchOption.h"
  23. #import "YOUPAIUCIMP2PSessionConfig.h"
  24. #import "NTESTimerHolder.h"
  25. #import "NSDictionary+NTESJson.h"
  26. #import "NTESCustomSysNotificationSender.h"
  27. #import "UIViewController+TFPresent.h"
  28. #import "YOUPAIZYIMSessionDetailWindow.h"
  29. #import "YOUPAIZYUpdateRemarkWindow.h"
  30. #import "YOUPAILCReportVC.h"
  31. #import "YOUPAILZUserShowVC.h"
  32. #import "YOUPAILZChatRoomPasswordWindow.h"
  33. #import "LZAlertWindow.h"
  34. #import "YOUPAIZhaPianTextAttachment.h"
  35. #import "YOUPAIZhaPianTextAttachment2.h"
  36. #import "LOTAnimationView.h"
  37. #import "YOUPAILZChatRoomVC.h"
  38. #import "YOUPAILZSessionDetailVC.h"
  39. @interface YOUPAIUCIMP2PSessionVC ()<NIMMediaManagerDelegate,UCBaseTouchTableViewDelegate,NIMSystemNotificationManagerDelegate,NTESTimerHolderDelegate,ZYIMSessionDetailWindowDelegate>
  40. @property (nonatomic,readwrite) NIMMessage *messageForMenu;
  41. @property (nonatomic,strong) UILabel *titleLabel;
  42. @property (nonatomic,strong) UILabel *subTitleLabel;
  43. @property (nonatomic,strong) NSIndexPath *lastVisibleIndexPathBeforeRotation;
  44. @property (nonatomic,strong) YOUPAIUCIMP2PSessionConfig* sessionConfig;
  45. @property (nonatomic,strong) NIMCustomLeftBarView* badgeBarView;
  46. @property (nonatomic,strong) NTESCustomSysNotificationSender *notificaionSender;
  47. @property (nonatomic,strong) NTESTimerHolder *titleTimer;
  48. @property(nonatomic,strong)UIView *bgView;
  49. @property(nonatomic,copy)NSString *youpaiptalk_room_id;
  50. @end
  51. @implementation YOUPAIUCIMP2PSessionVC
  52. #pragma mark - 子类实现
  53. - (void)vqf_youpaifprofileClick{
  54. NSLog(@"头像点击啦");
  55. }
  56. - (instancetype)initWithSession:(NIMSession *)session{
  57. self = [super initWithNibName:nil bundle:nil];
  58. if (self) {
  59. _session = session;
  60. }
  61. return self;
  62. }
  63. - (void)dealloc
  64. {
  65. [self removeListener];
  66. [[NIMKit sharedKit].robotTemplateParser clean];
  67. _tableView.delegate = nil;
  68. _tableView.dataSource = nil;
  69. }
  70. - (void)viewDidAppear:(BOOL)animated{
  71. [super viewDidAppear:animated];
  72. //判断是否有本地消息记录 用来判断是否是第一次聊天
  73. [self youpaifcheckLocalMessage];
  74. }
  75. - (void)viewDidLoad {
  76. [super viewDidLoad];
  77. self.view.backgroundColor = [UIColor whiteColor];
  78. _notificaionSender = [[NTESCustomSysNotificationSender alloc] init];
  79. BOOL disableCommandTyping = self.disableCommandTyping || (self.session.sessionType == NIMSessionTypeP2P &&[[NIMSDK sharedSDK].userManager isUserInBlackList:self.session.sessionId]);
  80. if (!disableCommandTyping) {
  81. _titleTimer = [[NTESTimerHolder alloc] init];
  82. [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self];
  83. }
  84. //导航栏
  85. [self youpaifsetupNav];
  86. //消息 tableView
  87. [self youpaifsetupTableView];
  88. //输入框 inputView
  89. [self youpaifsetupInputView];
  90. //会话相关逻辑配置器安装
  91. [self youpaifsetupConfigurator];
  92. //添加监听
  93. [self addListener];
  94. //进入会话时,标记所有消息已读,并发送已读回执
  95. [self markRead];
  96. //更新已读位置
  97. [self uiCheckReceipts:nil];
  98. [self loadShowChatRoomHeaderViewData];
  99. }
  100. - (void)youpaifsetupNav
  101. {
  102. //子类实现
  103. [self setUpTitleView];
  104. //导航栏设置
  105. NIMCustomLeftBarView *leftBarView = [[NIMCustomLeftBarView alloc] initWithFrame:CGRectMake(0, 0, 70, 44)];
  106. leftBarView.badgeView.badgeTextColor = LZ273145Color;
  107. leftBarView.backImgV.image = [UIImage imageNamed:@"vqu_images_navigation_back_black"];
  108. [leftBarView addTarget:self action:@selector(youpaifbackClick) forControlEvents:UIControlEventTouchUpInside];
  109. // self.navigationController.navigationBar.barTintColor = LZ273145Color;
  110. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarView];
  111. // if (@available(iOS 11.0, *)) {
  112. // leftBarView.translatesAutoresizingMaskIntoConstraints = NO;
  113. // }
  114. self.badgeBarView = leftBarView;
  115. self.navigationItem.leftBarButtonItem = leftItem;
  116. // //设置不进入聊天详情的页面号
  117. // NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  118. // textAttrs[NSForegroundColorAttributeName] = LZ273145Color;
  119. // self.navigationController.navigationBar.titleTextAttributes = textAttrs;
  120. // if (@available(iOS 15.0, *)) {
  121. // UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
  122. // appearance.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleRegular];
  123. // [appearance setBackgroundColor:[UIColor whiteColor]];
  124. // appearance.backgroundEffect = nil;
  125. // appearance.shadowColor = [UIColor clearColor];
  126. // appearance.titleTextAttributes = textAttrs;
  127. // self.navigationController.navigationBar.standardAppearance = appearance;
  128. // self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
  129. // }
  130. // if (![self.session.sessionId isEqualToString:[LCSaveData getSysId]] && (![self.session.sessionId isEqualToString:[LCSaveData getServerId]])) {
  131. // self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifuserSetClick) image:[UIImage imageNamed:@"vqu_images_IM_im_details"]];
  132. // }
  133. [LCHttpHelper requestWithURLString:AppConfig parameters:nil needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  134. NSDictionary* dict = (NSDictionary*)responseObject;
  135. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  136. if (code==0) {//成功
  137. NSArray *array = [[[dict objectForKey:@"data"]objectForKey:@"config"] objectForKey:@"no_info"];
  138. if (![array containsObject:self.session.sessionId]) {
  139. self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(youpaifuserSetClick) image:[UIImage imageNamed:@"vqu_images_IM_im_details"]] ;
  140. }
  141. }
  142. } failure:^(NSError *error) {}];
  143. }
  144. - (void)youpaifbackClick{
  145. [self.navigationController popViewControllerAnimated:YES];
  146. }
  147. #pragma mark-更多
  148. - (void)youpaifuserSetClick{
  149. YOUPAILZSessionDetailVC *vc = [[YOUPAILZSessionDetailVC alloc] init];
  150. vc.youpaipsession = self.session;
  151. [self.navigationController pushViewController:vc animated:YES];
  152. // YOUPAIZYIMSessionDetailWindow *vc= [[YOUPAIZYIMSessionDetailWindow alloc]init];
  153. // vc.vWidth = KScreenWidth -68;
  154. // vc.vHeight = 315+25+30;
  155. // vc.youpaipsession = self.session;
  156. // vc.youpaipdelegate = self;
  157. // vc.isTouchDismiss = YES;
  158. // [self TFPresentVC:vc completion:^{}];
  159. }
  160. -(void)loadShowChatRoomHeaderViewData{
  161. [LCHttpHelper requestWithURLString:ChatRoom_getUserTalk parameters:@{@"user_id":self.session.sessionId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  162. NSDictionary* dict = (NSDictionary*)responseObject;
  163. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  164. if (code == 0) {//成功
  165. NSString *roomID =dict[@"data"][@"room_id"];
  166. if (roomID.length>0) {
  167. [self loadShowChatRoomHeaderView:dict[@"data"][@"avatar"]:dict[@"data"][@"room_id"]];
  168. self.youpaiptalk_room_id =dict[@"data"][@"room_id"];
  169. }
  170. }else{
  171. [ZCHUDHelper showTitle:dict[@"message"]];
  172. }
  173. } failure:^(NSError *error) {
  174. }];
  175. }
  176. //头部语音房显示
  177. -(void)loadShowChatRoomHeaderView:(NSString*)avatar :(NSString*)roomId{
  178. UIView *bgView = [UIView new];
  179. self.bgView = bgView;
  180. [self.view addSubview:bgView];
  181. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  182. make.top.mas_equalTo(20);
  183. make.centerX.mas_equalTo(0);
  184. make.height.mas_equalTo(40);
  185. make.width.mas_equalTo(240);
  186. }];
  187. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(vqf_handlePan:)];
  188. [bgView addGestureRecognizer:panGestureRecognizer];
  189. // bgView.backgroundColor = [UIColor yellowColor];
  190. UIImageView *youpaipcoverImgV = [[UIImageView alloc] init];
  191. youpaipcoverImgV.layer.cornerRadius = 35.0f/2;
  192. youpaipcoverImgV.clipsToBounds = YES;
  193. youpaipcoverImgV.contentMode = UIViewContentModeScaleAspectFill;
  194. [bgView addSubview:youpaipcoverImgV];
  195. [youpaipcoverImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.centerY.equalTo(0);
  197. make.left.mas_equalTo(0);
  198. make.size.mas_offset(CGSizeMake(35, 35));
  199. }];
  200. youpaipcoverImgV.backgroundColor = [UIColor clearColor];
  201. [youpaipcoverImgV sd_setImageWithURL:[LCTools getImageUrlWithAddress:avatar] placeholderImage:nil];
  202. LOTAnimationView *youpaiptalkingAnimationView = [LOTAnimationView animationWithFilePath:[[NSBundle mainBundle] pathForResource:@"ic_chatroom_seat_animation" ofType:@"json"]];
  203. youpaiptalkingAnimationView.frame = CGRectMake(0.0f, 0.0f, 35.0f * 1.7f, 35.0f * 1.7f);
  204. youpaiptalkingAnimationView.loopAnimation = YES;
  205. youpaiptalkingAnimationView.contentMode = UIViewContentModeScaleAspectFill;
  206. [youpaiptalkingAnimationView play];
  207. [bgView addSubview:youpaiptalkingAnimationView];
  208. [youpaiptalkingAnimationView mas_makeConstraints:^(MASConstraintMaker *make) {
  209. make.center.equalTo(youpaipcoverImgV);
  210. make.size.mas_offset(CGSizeMake(35 * 1.7f, 35 * 1.7f));
  211. }];
  212. //
  213. UIButton *contentImage = [UIButton new];
  214. [bgView addSubview:contentImage];
  215. [contentImage mas_makeConstraints:^(MASConstraintMaker *make) {
  216. make.left.mas_equalTo(youpaipcoverImgV.mas_right).offset(-5);
  217. make.width.mas_equalTo(166);
  218. make.height.mas_equalTo(26);
  219. make.centerY.mas_equalTo(youpaipcoverImgV);
  220. }];
  221. [contentImage setBackgroundImage:[UIImage imageNamed:@"vqu_chatroom_bg_HeaderView_bg"] forState:UIControlStateNormal];
  222. [bgView sendSubviewToBack:contentImage];
  223. [contentImage addTarget:self action:@selector(contentImageClickToChatRoom) forControlEvents:UIControlEventTouchUpInside];
  224. //
  225. UILabel *contentLabel = [UILabel new];
  226. [contentImage addSubview:contentLabel];
  227. [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  228. make.left.mas_equalTo(10);
  229. make.centerY.mas_equalTo(0);
  230. }];
  231. contentLabel.text = @"我在语聊厅内表演";
  232. contentLabel.textColor = [UIColor whiteColor];
  233. contentLabel.font = [UIFont systemFontOfSize:12];
  234. //
  235. UIImageView *contentRightImage = [UIImageView new];
  236. [bgView addSubview:contentRightImage];
  237. [contentRightImage mas_makeConstraints:^(MASConstraintMaker *make) {
  238. make.left.mas_equalTo(contentLabel.mas_right).offset(3);
  239. make.centerY.mas_equalTo(0);
  240. make.size.mas_equalTo(CGSizeMake(51, 20));
  241. }];
  242. contentRightImage.image = [UIImage imageNamed:@"vqu_chatroom_bg_HeaderView_right"];
  243. //取消
  244. UIButton *cancelBtn = [UIButton new];
  245. [bgView addSubview:cancelBtn];
  246. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  247. make.left.mas_equalTo(contentRightImage.mas_right);
  248. make.centerY.mas_equalTo(0);
  249. make.size.mas_equalTo(CGSizeMake(30, 20));
  250. }];
  251. UIImageView *im = [UIImageView new];
  252. [cancelBtn addSubview:im];
  253. [im mas_makeConstraints:^(MASConstraintMaker *make) {
  254. make.center.mas_equalTo(0);
  255. make.size.mas_equalTo(CGSizeMake(9, 9));
  256. }];
  257. im.image = [UIImage imageNamed:@"vqu_chatroom_bg_HeaderView_cancel"];
  258. // cancelBtn.backgroundColor= [UIColor redColor];
  259. [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  260. }
  261. - (void)vqf_handlePan:(UIPanGestureRecognizer*) recognizer{
  262. CGPoint translation = [recognizer translationInView:self.view];
  263. CGFloat centerX=recognizer.view.center.x+ translation.x;
  264. CGFloat thecenter=0;
  265. recognizer.view.center = CGPointMake(centerX,recognizer.view.center.y+ translation.y);
  266. [recognizer setTranslation:CGPointZero inView:self.view];
  267. if(recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
  268. if(centerX > KScreenWidth/2) {
  269. thecenter = KScreenWidth-240.0f/2 - 10.0f;
  270. }else{
  271. thecenter = 240.0f / 2 + 10.0f;
  272. }
  273. [UIView animateWithDuration:0.3 animations:^{
  274. CGFloat y = recognizer.view.center.y;
  275. if (recognizer.view.center.y < NavBarHeight) {
  276. y = 21.0f+15;
  277. }else if (recognizer.view.center.y > KScreenHeight - TabbarHeight - 21.0f - 240.0f / 2.0f){
  278. y = KScreenHeight - TabbarHeight - 21.0f - 240.0f / 2.0f;
  279. }
  280. recognizer.view.center=CGPointMake(thecenter, y + translation.y);
  281. }];
  282. }
  283. }
  284. -(void)cancelBtnClick{
  285. [UIView animateWithDuration:0.01 animations:^{
  286. self.bgView.frame = CGRectMake(0,-NavBarHeight, 240, 40);
  287. }];
  288. }
  289. -(void)contentImageClickToChatRoom{
  290. if ([[YOUPAILZChatRoomManager shareManager].chatroomModel.youpaiproom_id isEqual:self.youpaiptalk_room_id] && [YOUPAILZChatRoomManager shareManager].chatroomController != nil) {
  291. [[YOUPAILZChatRoomManager shareManager] youpaifshowChatRoom];
  292. return;
  293. }
  294. [self youpaifshowPasswordWindowWithRoomId:self.youpaiptalk_room_id youpaipis_comein_room_hide:0];
  295. }
  296. - (void)youpaifshowPasswordWindowWithRoomId:(NSString *)roomId youpaipis_comein_room_hide:(NSInteger)youpaipis_comein_room_hide{
  297. @weakify(self);
  298. [LCHttpHelper requestWithURLString:GetRoomStatus parameters:@{@"room_id":roomId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  299. @strongify(self);
  300. NSDictionary* dict = (NSDictionary*)responseObject;
  301. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  302. if (code==0) {
  303. NSDictionary *data = [dict objectForKey:@"data"];
  304. if ([[data objectForKey:@"pwd"] integerValue] == 1) { // 有密码
  305. YOUPAILZChatRoomPasswordWindow *window = [[YOUPAILZChatRoomPasswordWindow alloc] init];
  306. [window setYoupaippasswordBlock:^(NSString * _Nonnull password, YOUPAILZChatRoomPasswordWindow * _Nonnull window) {
  307. @strongify(self);
  308. [self youpaifgoChatRoomVCWithPwd:password RoomId:roomId youpaipis_comein_room_hide:youpaipis_comein_room_hide window:window];
  309. }];
  310. [self TFPresentVC:window completion:^{}];
  311. }else{
  312. [self youpaifgoChatRoomVCWithPwd:@"" RoomId:roomId youpaipis_comein_room_hide:youpaipis_comein_room_hide window:nil];
  313. }
  314. }
  315. } failure:^(NSError *error) {
  316. }];
  317. }
  318. /// 加入语聊房
  319. /// @param pwd 密码
  320. /// @param roomId 房间号
  321. /// @param youpaipis_comein_room_hide 是否悄悄进房
  322. /// @param window 密码框
  323. - (void)youpaifgoChatRoomVCWithPwd:(NSString *)pwd RoomId:(NSString *)roomId youpaipis_comein_room_hide:(NSInteger)youpaipis_comein_room_hide window:(YOUPAILZChatRoomPasswordWindow *)window{
  324. [ZCHUDHelper show];
  325. @weakify(self);
  326. [LCHttpHelper requestWithURLString:JoinTalk parameters:@{@"room_id":roomId,@"pwd":pwd,@"is_comein_room_hide":@(youpaipis_comein_room_hide)} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  327. [ZCHUDHelper dismiss];
  328. @strongify(self);
  329. NSDictionary* dict = (NSDictionary*)responseObject;
  330. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  331. if (code==0) {
  332. if (window != nil) {
  333. [window dismissViewControllerAnimated:YES completion:^{}];
  334. }
  335. [[YOUPAILZChatRoomManager shareManager] youpaifleaveChannel];
  336. YOUPAILZChatRoomModel *youpaipchatroomModel = [YOUPAILZChatRoomModel mj_objectWithKeyValues:dict[@"data"]];
  337. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  338. YOUPAILZChatRoomVC *vc = [[YOUPAILZChatRoomVC alloc] init];
  339. vc.youpaipchatroomModel = youpaipchatroomModel;
  340. [[LCTools getContainNavigationControllerCurrentVC].navigationController pushViewController:vc animated:YES];
  341. [YOUPAILZChatRoomManager shareManager].chatroomModel = vc.youpaipchatroomModel;
  342. [YOUPAILZChatRoomManager shareManager].chatroomController = vc;
  343. // });
  344. }else{
  345. if (window != nil) {
  346. [window youpaifshowHUDWithTitle:[dict objectForKey:@"message"]];
  347. }else{
  348. [ZCHUDHelper showTitle:[dict objectForKey:@"message"]];
  349. }
  350. }
  351. } failure:^(NSError *error) {
  352. [ZCHUDHelper dismiss];
  353. }];
  354. }
  355. - (void)youpaifsetupTableView
  356. {
  357. self.view.backgroundColor = [UIColor whiteColor];
  358. self.tableView = [[YOUPAIUCBaseTouchTableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, KScreenWidth, KScreenHeight-NavBarHeight) style:UITableViewStylePlain];
  359. self.tableView.touchDelegate = self;
  360. self.tableView.backgroundColor = LZFAFAFCColor;
  361. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  362. self.tableView.estimatedRowHeight = 0;
  363. self.tableView.estimatedSectionHeaderHeight = 0;
  364. self.tableView.estimatedSectionFooterHeight = 0;
  365. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  366. if ([self.sessionConfig respondsToSelector:@selector(sessionBackgroundImage)] && [self.sessionConfig sessionBackgroundImage]) {
  367. UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  368. imgView.image = [self.sessionConfig sessionBackgroundImage];
  369. imgView.contentMode = UIViewContentModeScaleAspectFill;
  370. self.tableView.backgroundView = imgView;
  371. }
  372. if (@available(iOS 15.0, *)) {
  373. self.tableView.sectionHeaderTopPadding = 0;
  374. }
  375. [self.view addSubview:self.tableView];
  376. }
  377. - (void)youpaifsetupInputView
  378. {
  379. if ([self shouldShowInputView])
  380. {
  381. self.sessionInputView = [[YOUPAINIMInputView alloc] initWithFrame:CGRectMake(0, 0, self.view.nim_width,0) config:self.sessionConfig];
  382. self.sessionInputView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
  383. [self.sessionInputView setSession:self.session];
  384. [self.sessionInputView setInputDelegate:self];
  385. [self.sessionInputView setInputActionDelegate:self];
  386. [self.sessionInputView refreshStatus:NIMInputStatusText];
  387. [self.view addSubview:_sessionInputView];
  388. // self.sessionInputView
  389. self.tableView.nim_height -= self.sessionInputView.toolBar.nim_height;
  390. }
  391. }
  392. - (void)youpaifsetupConfigurator
  393. {
  394. _configurator = [[NIMSessionConfigurator alloc] init];
  395. [_configurator youpaifuc_setup:self];
  396. BOOL needProximityMonitor = [self needProximityMonitor];
  397. [[NIMSDK sharedSDK].mediaManager setNeedProximityMonitor:needProximityMonitor];
  398. }
  399. - (void)viewWillAppear:(BOOL)animated
  400. {
  401. [super viewWillAppear:animated];
  402. [self.interactor onViewWillAppear];
  403. // [self.navigationController.navigationBar setBackgroundColor:LCNavColor];
  404. // if (@available(iOS 15.0, *)) {
  405. // [self.navigationController.navigationBar.scrollEdgeAppearance setBackgroundColor:LCNavColor];
  406. // [self.navigationController.navigationBar.standardAppearance setBackgroundColor:LCNavColor];
  407. // }
  408. }
  409. - (void)viewWillDisappear:(BOOL)animated
  410. {
  411. [super viewWillDisappear:animated];
  412. [self.sessionInputView endEditing:YES];
  413. // [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
  414. // if (@available(iOS 15.0, *)) {
  415. // [self.navigationController.navigationBar.scrollEdgeAppearance setBackgroundColor:[UIColor clearColor]];
  416. // [self.navigationController.navigationBar.standardAppearance setBackgroundColor:[UIColor clearColor]];
  417. // }
  418. }
  419. - (void)viewDidDisappear:(BOOL)animated
  420. {
  421. [super viewDidDisappear:animated];
  422. [self.interactor onViewDidDisappear];
  423. }
  424. - (void)viewDidLayoutSubviews
  425. {
  426. [self changeLeftBarBadge:self.conversationManager.allUnreadCount];
  427. [self.interactor resetLayout];
  428. }
  429. #pragma mark - 第一次聊天的提示信息
  430. - (void)youpaifcheckLocalMessage{
  431. NIMMessageSearchOption* option = [[NIMMessageSearchOption alloc]init];
  432. option.allMessageTypes = YES;
  433. option.order = NIMMessageSearchOrderDesc;
  434. [[[NIMSDK sharedSDK] conversationManager] searchMessages:self.session option:option result:^(NSError * _Nullable error, NSArray<NIMMessage *> * _Nullable messages) {
  435. if (messages.count==0 && [LCSaveModel getUserModel].youpaipuserinfo.youpaipgender==2) {
  436. //不是系统和客服才请求
  437. if (![self.session.sessionId isEqualToString:[LCSaveData getSysId]] && ![self.session.sessionId isEqualToString:[LCSaveData getServerId]]) {//请求提示信息
  438. }
  439. }
  440. }];
  441. }
  442. #pragma mark - 对方输入状态
  443. - (void)onReceiveCustomSystemNotification:(NIMCustomSystemNotification *)notification
  444. {
  445. if (!notification.sendToOnlineUsersOnly) {
  446. return;
  447. }
  448. NSData *data = [[notification content] dataUsingEncoding:NSUTF8StringEncoding];
  449. if (data) {
  450. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
  451. options:0
  452. error:nil];
  453. if ([dict jsonInteger:NTESNotifyID] == NTESCommandTyping && self.session.sessionType == NIMSessionTypeP2P && [notification.sender isEqualToString:self.session.sessionId])
  454. {
  455. [self refreshSessionTitle:@"对方正在输入..."];
  456. [_titleTimer startTimer:5
  457. delegate:self
  458. repeats:NO];
  459. }
  460. }
  461. }
  462. - (void)onNTESTimerFired:(NTESTimerHolder *)holder
  463. {
  464. [self refreshSessionTitle:self.youpaifsessionTitle];
  465. }
  466. #pragma mark - 消息收发接口
  467. - (void)sendMessage:(NIMMessage *)message
  468. {
  469. // // 构造自定义消息附件
  470. // NIMCustomObject *object = [[NIMCustomObject alloc] init];
  471. // YOUPAIZhaPianTextAttachment *attachment = [[YOUPAIZhaPianTextAttachment alloc] init];
  472. // object.attachment = attachment;
  473. //
  474. // // 构造出具体消息并注入附件
  475. // NIMMessage *message1 = [[NIMMessage alloc] init];
  476. // message1.messageObject = object;
  477. //
  478. //
  479. // NIMCustomObject *object1 = [[NIMCustomObject alloc] init];
  480. // YOUPAIZhaPianTextAttachment2 *attachment1 = [[YOUPAIZhaPianTextAttachment2 alloc] init];
  481. // object1.attachment = attachment1;
  482. //
  483. // // 构造出具体消息并注入附件
  484. // NIMMessage *message2 = [[NIMMessage alloc] init];
  485. // message2.messageObject = object1;
  486. //
  487. //
  488. // // 错误反馈对象
  489. // NSError *error = nil;
  490. // if ([message.text isEqualToString:@"63"]) {
  491. //
  492. // [[NIMSDK sharedSDK].chatManager sendMessage:message1 toSession:self.session error:&error];
  493. // }else if ([message.text isEqualToString:@"64"]){
  494. // [[NIMSDK sharedSDK].chatManager sendMessage:message2 toSession:self.session error:&error];
  495. // }else{
  496. // [self.interactor sendMessage:message];
  497. // }
  498. [UMengRecordTool umengEventCountWithId:ClickSendMessage];
  499. // 发送消息
  500. // [self.interactor sendMessageReceipt:@[message]];
  501. [self.interactor sendMessage:message];
  502. }
  503. #pragma mark - Touch Event
  504. - (void)youpaifdidTouchesBegan:(YOUPAIUCBaseTouchTableView *)tableView{
  505. [_sessionInputView endEditing:YES];
  506. }
  507. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  508. {
  509. [super touchesBegan:touches withEvent:event];
  510. [_sessionInputView endEditing:YES];
  511. }
  512. #pragma mark - NIMSessionConfiguratorDelegate
  513. - (void)didFetchMessageData
  514. {
  515. [self uiCheckReceipts:nil];
  516. [self.tableView reloadData];
  517. [self.tableView nim_scrollToBottom:NO];
  518. }
  519. - (void)didRefreshMessageData
  520. {
  521. [self refreshSessionTitle:self.youpaifsessionTitle];
  522. [self refreshSessionSubTitle:self.sessionSubTitle];
  523. [self.tableView reloadData];
  524. }
  525. - (void)didPullUpMessageData {}
  526. #pragma mark - 会话title
  527. - (NSString *)youpaifsessionTitle
  528. {
  529. NSString *title = @"";
  530. NIMSessionType type = self.session.sessionType;
  531. switch (type) {
  532. case NIMSessionTypeTeam:{
  533. NIMTeam *team = [[[NIMSDK sharedSDK] teamManager] teamById:self.session.sessionId];
  534. title = [NSString stringWithFormat:@"%@(%zd)",[team teamName],[team memberNumber]];
  535. }
  536. break;
  537. case NIMSessionTypeP2P:{
  538. title = [NIMKitUtil showNick:self.session.sessionId inSession:self.session];
  539. }
  540. break;
  541. default:
  542. break;
  543. }
  544. return title;
  545. }
  546. - (NSString *)sessionSubTitle{return @"";};
  547. #pragma mark - NIMChatManagerDelegate
  548. //开始发送
  549. - (void)willSendMessage:(NIMMessage *)message
  550. {
  551. id<NIMSessionInteractor> interactor = self.interactor;
  552. if ([message.session isEqual:self.session]) {
  553. if ([interactor findMessageModel:message]) {
  554. [interactor updateMessage:message];
  555. }else{
  556. [interactor addMessages:@[message]];
  557. }
  558. }
  559. }
  560. //上传资源文件成功
  561. - (void)uploadAttachmentSuccess:(NSString *)urlString
  562. forMessage:(NIMMessage *)message
  563. {
  564. //如果需要使用富文本推送,可以在这里进行 message apns payload 的设置
  565. }
  566. //发送结果
  567. - (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error
  568. {
  569. if ([message.session isEqual:_session])
  570. {
  571. [self.interactor updateMessage:message];
  572. if (message.session.sessionType == NIMSessionTypeTeam)
  573. {
  574. //如果是群的话需要检查一下回执显示情况
  575. NIMMessageReceipt *receipt = [[NIMMessageReceipt alloc] initWithMessage:message];
  576. [self.interactor checkReceipts:@[receipt]];
  577. }else{
  578. if (error == nil && [LCSaveModel getUserModel].youpaipuserinfo.youpaipgender == 2) {
  579. NIMMessageSearchOption *searchOption = [[NIMMessageSearchOption alloc] init];
  580. searchOption.messageTypes = @[@(NIMMessageTypeText),@(NIMMessageTypeImage),@(NIMMessageTypeAudio),@(NIMMessageTypeVideo)];
  581. searchOption.fromIds = @[[LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id];
  582. [[NIMSDK sharedSDK].conversationManager searchMessages:self.session option:searchOption result:^(NSError * _Nullable error, NSArray<NIMMessage *> * _Nullable messages) {
  583. if (messages.count == 1) {
  584. [LCHttpHelper requestWithURLString:SendTipMsg parameters:@{@"user_id":self.session.sessionId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  585. NSDictionary* dict = (NSDictionary*)responseObject;
  586. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  587. if (code==0) {
  588. }
  589. } failure:^(NSError *error) {
  590. }];
  591. }
  592. }];
  593. }
  594. }
  595. }
  596. }
  597. //发送进度
  598. -(void)sendMessage:(NIMMessage *)message progress:(float)progress
  599. {
  600. if ([message.session isEqual:_session]) {
  601. [self.interactor updateMessage:message];
  602. }
  603. }
  604. //接收消息
  605. - (void)onRecvMessages:(NSArray *)messages
  606. {
  607. if ([self shouldAddListenerForNewMsg])
  608. {
  609. NIMMessage *message = messages.firstObject;
  610. NIMSession *session = message.session;
  611. if (![session isEqual:self.session] || !messages.count)
  612. {
  613. return;
  614. }
  615. [self youpaifuiAddMessages:messages];
  616. [self.interactor markRead];
  617. }
  618. }
  619. - (void)fetchMessageAttachment:(NIMMessage *)message progress:(float)progress
  620. {
  621. if ([message.session isEqual:_session])
  622. {
  623. [self.interactor updateMessage:message];
  624. }
  625. }
  626. - (void)fetchMessageAttachment:(NIMMessage *)message didCompleteWithError:(NSError *)error
  627. {
  628. if ([message.session isEqual:_session])
  629. {
  630. NIMMessageModel *model = [self.interactor findMessageModel:message];
  631. //下完缩略图之后,因为比例有变化,重新刷下宽高。
  632. [model cleanCache];
  633. [self.interactor updateMessage:message];
  634. }
  635. }
  636. - (void)onRecvMessageReceipts:(NSArray<NIMMessageReceipt *> *)receipts
  637. {
  638. if ([self shouldAddListenerForNewMsg])
  639. {
  640. NSMutableArray *handledReceipts = [[NSMutableArray alloc] init];
  641. for (NIMMessageReceipt *receipt in receipts) {
  642. if ([receipt.session isEqual:self.session])
  643. {
  644. [handledReceipts addObject:receipt];
  645. }
  646. }
  647. if (handledReceipts.count)
  648. {
  649. [self uiCheckReceipts:handledReceipts];
  650. }
  651. }
  652. }
  653. #pragma mark - NIMConversationManagerDelegate
  654. - (void)messagesDeletedInSession:(NIMSession *)session{
  655. [self.interactor resetMessages:nil];
  656. [self.tableView reloadData];
  657. }
  658. - (void)didAddRecentSession:(NIMRecentSession *)recentSession
  659. totalUnreadCount:(NSInteger)totalUnreadCount{
  660. [self changeUnreadCount:recentSession totalUnreadCount:totalUnreadCount];
  661. }
  662. - (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
  663. totalUnreadCount:(NSInteger)totalUnreadCount{
  664. [self changeUnreadCount:recentSession totalUnreadCount:totalUnreadCount];
  665. }
  666. - (void)didRemoveRecentSession:(NIMRecentSession *)recentSession
  667. totalUnreadCount:(NSInteger)totalUnreadCount{
  668. [self changeUnreadCount:recentSession totalUnreadCount:totalUnreadCount];
  669. }
  670. - (void)changeUnreadCount:(NIMRecentSession *)recentSession
  671. totalUnreadCount:(NSInteger)totalUnreadCount{
  672. if ([recentSession.session isEqual:self.session]) {
  673. return;
  674. }
  675. [self changeLeftBarBadge:totalUnreadCount];
  676. }
  677. #pragma mark - NIMMediaManagerDelegate
  678. - (void)recordAudio:(NSString *)filePath didBeganWithError:(NSError *)error {
  679. if (!filePath || error) {
  680. _sessionInputView.recording = NO;
  681. [self youpaifonRecordFailed:error];
  682. }
  683. }
  684. - (void)recordAudio:(NSString *)filePath didCompletedWithError:(NSError *)error {
  685. if(!error) {
  686. if ([self youpaifrecordFileCanBeSend:filePath]) {
  687. [self sendMessage:[NIMMessageMaker msgWithAudio:filePath]];
  688. }else{
  689. [self showRecordFileNotSendReason];
  690. }
  691. } else {
  692. [self youpaifonRecordFailed:error];
  693. }
  694. _sessionInputView.recording = NO;
  695. }
  696. - (void)recordAudioDidCancelled {
  697. _sessionInputView.recording = NO;
  698. }
  699. - (void)recordAudioProgress:(NSTimeInterval)currentTime {
  700. [_sessionInputView updateAudioRecordTime:currentTime];
  701. }
  702. - (void)recordAudioInterruptionBegin {
  703. [[NIMSDK sharedSDK].mediaManager cancelRecord];
  704. }
  705. #pragma mark - 录音相关接口
  706. - (void)youpaifonRecordFailed:(NSError *)error{}
  707. - (BOOL)youpaifrecordFileCanBeSend:(NSString *)filepath
  708. {
  709. return YES;
  710. }
  711. - (void)showRecordFileNotSendReason{}
  712. #pragma mark - NIMInputDelegate
  713. - (void)youpaifdidChangeInputHeight:(CGFloat)inputHeight
  714. {
  715. [self.interactor changeLayout:inputHeight];
  716. }
  717. #pragma mark - NIMInputActionDelegate
  718. - (BOOL)onTapMediaItem:(NIMMediaItem *)item{
  719. SEL sel = item.selctor;
  720. BOOL handled = sel && [self respondsToSelector:sel];
  721. if (handled) {
  722. NIMKit_SuppressPerformSelectorLeakWarning([self performSelector:sel withObject:item]);
  723. handled = YES;
  724. }
  725. return handled;
  726. }
  727. - (void)onTextChanged:(id)sender{
  728. [_notificaionSender sendTypingState:self.session];
  729. }
  730. - (void)onSendText:(NSString *)text atUsers:(NSArray *)atUsers
  731. {
  732. NSMutableArray *users = [NSMutableArray arrayWithArray:atUsers];
  733. if (self.session.sessionType == NIMSessionTypeP2P)
  734. {
  735. [users addObject:self.session.sessionId];
  736. }
  737. NSString *robotsToSend = [self robotsToSend:users];
  738. NIMMessage *message = nil;
  739. if (robotsToSend.length)
  740. {
  741. message = [NIMMessageMaker msgWithRobotQuery:text toRobot:robotsToSend];
  742. }
  743. else
  744. {
  745. message = [NIMMessageMaker msgWithText:text];
  746. }
  747. if (atUsers.count)
  748. {
  749. NIMMessageApnsMemberOption *apnsOption = [[NIMMessageApnsMemberOption alloc] init];
  750. apnsOption.userIds = atUsers;
  751. apnsOption.forcePush = YES;
  752. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  753. option.session = self.session;
  754. NSString *me = [[NIMKit sharedKit].provider infoByUser:[NIMSDK sharedSDK].loginManager.currentAccount option:option].showName;
  755. apnsOption.apnsContent = [NSString stringWithFormat:@"%@在群里@了你",me];
  756. message.apnsMemberOption = apnsOption;
  757. }
  758. [self sendMessage:message];
  759. }
  760. - (NSString *)robotsToSend:(NSArray *)atUsers
  761. {
  762. for (NSString *userId in atUsers)
  763. {
  764. if ([[NIMSDK sharedSDK].robotManager isValidRobot:userId])
  765. {
  766. return userId;
  767. }
  768. }
  769. return nil;
  770. }
  771. - (void)onSelectChartlet:(NSString *)chartletId
  772. catalog:(NSString *)catalogId{}
  773. - (void)onCancelRecording
  774. {
  775. [[NIMSDK sharedSDK].mediaManager cancelRecord];
  776. }
  777. - (void)onStopRecording
  778. {
  779. [[NIMSDK sharedSDK].mediaManager stopRecord];
  780. }
  781. - (void)onStartRecording
  782. {
  783. _sessionInputView.recording = YES;
  784. NIMAudioType type = [self recordAudioType];
  785. NSTimeInterval duration = [NIMKit sharedKit].config.recordMaxDuration;
  786. [[NIMSDK sharedSDK].mediaManager addDelegate:self];
  787. [[NIMSDK sharedSDK].mediaManager record:type
  788. duration:duration];
  789. }
  790. #pragma mark - NIMMessageCellDelegate
  791. - (BOOL)onTapCell:(NIMKitEvent *)event{
  792. BOOL handle = NO;
  793. NSString *eventName = event.eventName;
  794. if ([eventName isEqualToString:NIMKitEventNameTapAudio])
  795. {
  796. [self.interactor mediaAudioPressed:event.messageModel];
  797. handle = YES;
  798. }
  799. if ([eventName isEqualToString:NIMKitEventNameTapRobotBlock]) {
  800. NSDictionary *param = event.data;
  801. NIMMessage *message = [NIMMessageMaker msgWithRobotSelect:param[@"text"] target:param[@"target"] params:param[@"param"] toRobot:param[@"robotId"]];
  802. [self sendMessage:message];
  803. handle = YES;
  804. }
  805. if ([eventName isEqualToString:NIMKitEventNameTapRobotContinueSession]) {
  806. NIMRobotObject *robotObject = (NIMRobotObject *)event.messageModel.message.messageObject;
  807. NIMRobot *robot = [[NIMSDK sharedSDK].robotManager robotInfo:robotObject.robotId];
  808. NSString *text = [NSString stringWithFormat:@"%@%@%@",NIMInputAtStartChar,robot.nickname,NIMInputAtEndChar];
  809. NIMInputAtItem *item = [[NIMInputAtItem alloc] init];
  810. item.uid = robot.userId;
  811. item.name = robot.nickname;
  812. [self.sessionInputView.atCache addAtItem:item];
  813. [self.sessionInputView.toolBar insertText:text];
  814. handle = YES;
  815. }
  816. return handle;
  817. }
  818. - (void)onRetryMessage:(NIMMessage *)message
  819. {
  820. if (message.isReceivedMsg) {
  821. [[[NIMSDK sharedSDK] chatManager] fetchMessageAttachment:message
  822. error:nil];
  823. }else{
  824. [[[NIMSDK sharedSDK] chatManager] resendMessage:message
  825. error:nil];
  826. }
  827. }
  828. - (BOOL)onLongPressCell:(NIMMessage *)message
  829. inView:(UIView *)view
  830. {
  831. BOOL handle = NO;
  832. NSArray *items = [self youpaifmenusItems:message];
  833. if ([items count] && [self becomeFirstResponder]) {
  834. UIMenuController *controller = [UIMenuController sharedMenuController];
  835. controller.menuItems = items;
  836. _messageForMenu = message;
  837. [controller setTargetRect:view.bounds inView:view];
  838. [controller setMenuVisible:YES animated:YES];
  839. handle = YES;
  840. }
  841. return handle;
  842. }
  843. - (BOOL)disableAudioPlayedStatusIcon:(NIMMessage *)message
  844. {
  845. BOOL disable = NO;
  846. if ([self.sessionConfig respondsToSelector:@selector(disableAudioPlayedStatusIcon)])
  847. {
  848. disable = [self.sessionConfig disableAudioPlayedStatusIcon];
  849. }
  850. return disable;
  851. }
  852. #pragma mark - 配置项
  853. - (id<NIMSessionConfig>)sessionConfig
  854. {
  855. return [[YOUPAIUCIMP2PSessionConfig alloc] init]; //使用默认配置
  856. }
  857. #pragma mark - 配置项列表
  858. //是否需要监听新消息通知 : 某些场景不需要监听新消息,如浏览服务器消息历史界面
  859. - (BOOL)shouldAddListenerForNewMsg
  860. {
  861. BOOL should = YES;
  862. if ([self.sessionConfig respondsToSelector:@selector(disableReceiveNewMessages)]) {
  863. should = ![self.sessionConfig disableReceiveNewMessages];
  864. }
  865. return should;
  866. }
  867. //是否需要显示输入框 : 某些场景不需要显示输入框,如使用 3D touch 的场景预览会话界面内容
  868. - (BOOL)shouldShowInputView
  869. {
  870. BOOL should = YES;
  871. if ([self.sessionConfig respondsToSelector:@selector(disableInputView)]) {
  872. should = ![self.sessionConfig disableInputView];
  873. }
  874. if ([[LCSaveData getSysId] isEqualToString:self.session.sessionId] || [self.session.sessionId isEqualToString:@"11"]) {
  875. should = NO;
  876. }
  877. return should;
  878. }
  879. //当前录音格式 : NIMSDK 支持 aac 和 amr 两种格式
  880. - (NIMAudioType)recordAudioType
  881. {
  882. NIMAudioType type = NIMAudioTypeAAC;
  883. if ([self.sessionConfig respondsToSelector:@selector(recordType)]) {
  884. type = [self.sessionConfig recordType];
  885. }
  886. return type;
  887. }
  888. //是否需要监听感应器事件
  889. - (BOOL)needProximityMonitor
  890. {
  891. BOOL needProximityMonitor = YES;
  892. if ([self.sessionConfig respondsToSelector:@selector(disableProximityMonitor)]) {
  893. needProximityMonitor = !self.sessionConfig.disableProximityMonitor;
  894. }
  895. return needProximityMonitor;
  896. }
  897. #pragma mark - 菜单
  898. - (NSArray *)youpaifmenusItems:(NIMMessage *)message
  899. {
  900. NSMutableArray *items = [NSMutableArray array];
  901. BOOL copyText = NO;
  902. if (message.messageType == NIMMessageTypeText)
  903. {
  904. copyText = YES;
  905. }
  906. if (message.messageType == NIMMessageTypeRobot)
  907. {
  908. NIMRobotObject *robotObject = (NIMRobotObject *)message.messageObject;
  909. copyText = !robotObject.isFromRobot;
  910. }
  911. if (copyText) {
  912. [items addObject:[[UIMenuItem alloc] initWithTitle:@"复制"
  913. action:@selector(copyText:)]];
  914. }
  915. [items addObject:[[UIMenuItem alloc] initWithTitle:@"删除"
  916. action:@selector(deleteMsg:)]];
  917. return items;
  918. }
  919. - (NIMMessage *)messageForMenu
  920. {
  921. return _messageForMenu;
  922. }
  923. - (BOOL)canBecomeFirstResponder
  924. {
  925. return YES;
  926. }
  927. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  928. {
  929. NSArray *items = [[UIMenuController sharedMenuController] menuItems];
  930. for (UIMenuItem *item in items) {
  931. if (action == [item action]){
  932. return YES;
  933. }
  934. }
  935. return NO;
  936. }
  937. - (void)copyText:(id)sender
  938. {
  939. NIMMessage *message = [self messageForMenu];
  940. if (message.text.length) {
  941. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  942. [pasteboard setString:message.text];
  943. }
  944. }
  945. - (void)deleteMsg:(id)sender
  946. {
  947. NIMMessage *message = [self messageForMenu];
  948. [self youpaifuiDeleteMessage:message];
  949. [self.conversationManager deleteMessage:message];
  950. }
  951. - (void)menuDidHide:(NSNotification *)notification
  952. {
  953. [UIMenuController sharedMenuController].menuItems = nil;
  954. }
  955. #pragma mark - 操作接口
  956. - (void)youpaifuiAddMessages:(NSArray *)messages
  957. {
  958. [self.interactor addMessages:messages];
  959. }
  960. - (void)youpaifuiInsertMessages:(NSArray *)messages
  961. {
  962. [self.interactor insertMessages:messages];
  963. }
  964. - (NIMMessageModel *)youpaifuiDeleteMessage:(NIMMessage *)message{
  965. NIMMessageModel *model = [self.interactor deleteMessage:message];
  966. if (model.shouldShowReadLabel && model.message.session.sessionType == NIMSessionTypeP2P)
  967. {
  968. [self uiCheckReceipts:nil];
  969. }
  970. return model;
  971. }
  972. - (void)youpaifuiUpdateMessage:(NIMMessage *)message{
  973. [self.interactor updateMessage:message];
  974. }
  975. - (void)uiCheckReceipts:(NSArray<NIMMessageReceipt *> *)receipts
  976. {
  977. [self.interactor checkReceipts:receipts];
  978. }
  979. #pragma mark - NIMMeidaButton
  980. - (void)onTapMediaItemPicture:(NIMMediaItem *)item
  981. {
  982. [self.interactor mediaPicturePressed];
  983. }
  984. - (void)onTapMediaItemShoot:(NIMMediaItem *)item
  985. {
  986. [self.interactor mediaShootPressed];
  987. }
  988. - (void)onTapMediaItemLocation:(NIMMediaItem *)item
  989. {
  990. [self.interactor mediaLocationPressed];
  991. }
  992. #pragma mark - 旋转处理 (iOS8 or above)
  993. - (void)viewWillTransitionToSize:(CGSize)size
  994. withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  995. {
  996. self.lastVisibleIndexPathBeforeRotation = [self.tableView indexPathsForVisibleRows].lastObject;
  997. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  998. if (self.view.window) {
  999. __weak typeof(self) wself = self;
  1000. [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context)
  1001. {
  1002. [[NIMSDK sharedSDK].mediaManager cancelRecord];
  1003. [wself.interactor cleanCache];
  1004. [wself.sessionInputView reset];
  1005. [wself.tableView reloadData];
  1006. [wself.tableView scrollToRowAtIndexPath:wself.lastVisibleIndexPathBeforeRotation atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  1007. } completion:nil];
  1008. }
  1009. }
  1010. #pragma mark - 标记已读
  1011. - (void)markRead
  1012. {
  1013. [self.interactor markRead];
  1014. }
  1015. #pragma mark - Private
  1016. - (void)addListener
  1017. {
  1018. [[NIMSDK sharedSDK].chatManager addDelegate:self];
  1019. [[NIMSDK sharedSDK].conversationManager addDelegate:self];
  1020. }
  1021. - (void)removeListener
  1022. {
  1023. [[NIMSDK sharedSDK].chatManager removeDelegate:self];
  1024. [[NIMSDK sharedSDK].conversationManager removeDelegate:self];
  1025. }
  1026. - (void)changeLeftBarBadge:(NSInteger)unreadCount
  1027. {
  1028. // NIMCustomLeftBarView *leftBarView = (NIMCustomLeftBarView *)self.navigationItem.leftBarButtonItem.customView;
  1029. self.badgeBarView.badgeView.badgeValue = [NSString stringWithFormat:@"%ld", unreadCount];
  1030. // self.badgeBarView.badgeView.backgroundColor = [UIColor yellowColor];
  1031. self.badgeBarView.badgeView.hidden = !unreadCount;
  1032. }
  1033. - (id<NIMConversationManager>)conversationManager{
  1034. switch (self.session.sessionType) {
  1035. case NIMSessionTypeChatroom:
  1036. return nil;
  1037. break;
  1038. case NIMSessionTypeP2P:
  1039. case NIMSessionTypeTeam:
  1040. default:
  1041. return [NIMSDK sharedSDK].conversationManager;
  1042. }
  1043. }
  1044. - (void)setUpTitleView
  1045. {
  1046. NIMKitTitleView *titleView = (NIMKitTitleView *)self.navigationItem.titleView;
  1047. if (!titleView || ![titleView isKindOfClass:[NIMKitTitleView class]])
  1048. {
  1049. titleView = [[NIMKitTitleView alloc] initWithFrame:CGRectZero];
  1050. self.navigationItem.titleView = titleView;
  1051. titleView.titleLabel.text = self.youpaifsessionTitle;
  1052. titleView.subtitleLabel.text = self.sessionSubTitle;
  1053. titleView.titleLabel.textColor = LZ273145Color;
  1054. titleView.titleLabel.font = LCFont16;
  1055. self.titleLabel = titleView.titleLabel;
  1056. self.subTitleLabel = titleView.subtitleLabel;
  1057. }
  1058. [titleView sizeToFit];
  1059. }
  1060. - (void)refreshSessionTitle:(NSString *)title
  1061. {
  1062. self.titleLabel.text = title;
  1063. [self setUpTitleView];
  1064. }
  1065. - (void)refreshSessionSubTitle:(NSString *)title
  1066. {
  1067. self.subTitleLabel.text = title;
  1068. [self setUpTitleView];
  1069. }
  1070. #pragma mark ZYIMSessionDetailWindowDelegate
  1071. -(void)youpaifimSessionDetailWindowAction:(NSString *)type andSession:(NIMSession *)session
  1072. {
  1073. if ([type isEqualToString:@"updateRemark"]) {//设置备注名
  1074. YOUPAIZYUpdateRemarkWindow *VC= [[YOUPAIZYUpdateRemarkWindow alloc]init];
  1075. VC.vWidth = KScreenWidth -68;
  1076. VC.vHeight = ScaleHeight(187)+10;
  1077. VC.youpaipsession = session;
  1078. VC.isTouchDismiss = NO;
  1079. [kAppDelegate.window.rootViewController TFPresentVC:VC completion:^{
  1080. }];
  1081. }
  1082. if ([type isEqualToString:@"clearChat"]) {//清除聊天记录
  1083. WeakSelf;
  1084. LZAlertAction *cancelAction = [LZAlertAction actionWithTitle:@"取消" handler:^(LZAlertAction *action) {
  1085. }];
  1086. cancelAction.cornerRadius = 24.0f;
  1087. cancelAction.color = LZ273145Color;
  1088. cancelAction.bgColor = LZF5F4F7Color;
  1089. LZAlertAction *confimAction = [LZAlertAction actionWithTitle:@"删除" handler:^(LZAlertAction *action) {
  1090. [weakSelf clearChatSession:session];
  1091. }];
  1092. confimAction.cornerRadius = 24.0f;
  1093. confimAction.color = [UIColor whiteColor];
  1094. confimAction.bgColor = [UIColor colorWithPatternImage:[LCTools ColorImage:CGSizeMake((KScreenWidth - 105.0f) / 2.0f, 48.0f) FromColors:@[LZBFB6FFColor,LZ7C69FEColor] ByGradientType:GradientLeftToRight]];
  1095. LZAlertWindow *alert = [LZAlertWindow alertWithTitle:@"提示" content:@"确定删除该聊天?" action:@[cancelAction,confimAction]];
  1096. alert.contentTextAlignment = NSTextAlignmentCenter;
  1097. [self TFPresentVC:alert completion:^{}];
  1098. }
  1099. if ([type isEqualToString:@"report"]) {//举报
  1100. YOUPAILCReportVC* report = [[YOUPAILCReportVC alloc]init];
  1101. report.youpaipreport_uid = session.sessionId;
  1102. report.youpaiptype = 4;
  1103. [self.navigationController pushViewController:report animated:YES];
  1104. }
  1105. }
  1106. -(void)showUserVCWithSession:(NIMSession *)session
  1107. {
  1108. YOUPAILZUserShowVC *vc = [[YOUPAILZUserShowVC alloc] init];
  1109. vc.youpaipuserId = session.sessionId;
  1110. [self.navigationController pushViewController:vc animated:YES];
  1111. }
  1112. -(void)clearChatSession:(NIMSession *)session
  1113. {
  1114. id<NIMConversationManager> manager = [[NIMSDK sharedSDK] conversationManager];
  1115. NIMDeleteMessagesOption* deleteOption = [[NIMDeleteMessagesOption alloc]init];
  1116. deleteOption.removeSession = NO;
  1117. deleteOption.removeTable = YES;
  1118. [manager deleteAllmessagesInSession:session option:deleteOption];
  1119. [ZCHUDHelper showTitle:@"已清空"];
  1120. }
  1121. @end