BGIMMsgHandler.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. //
  2. // BGIMMsgHandler.m
  3. // BugoLive
  4. //
  5. // Created by xfg on 16/11/24.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "BGIMMsgHandler.h"
  9. #import "V2TIMMessage+CY.h"
  10. @interface BGIMMsgHandler()<V2TIMAdvancedMsgListener>
  11. @end
  12. @implementation BGIMMsgHandler
  13. - (void)dealloc
  14. {
  15. _sharedRunLoopRef = nil;
  16. }
  17. BogoSingletonM(Instance);
  18. - (id)init
  19. {
  20. @synchronized (self)
  21. {
  22. self = [super init];
  23. if (self)
  24. {
  25. _BuguLive = [GlobalVariables sharedInstance];
  26. _newMsgMArray = [NSMutableArray array];
  27. // 为了不影响视频,runloop线程优先级较低,可根据自身需要去调整
  28. _sharedRunLoopRef = [AVIMRunLoop sharedAVIMRunLoop];
  29. _msgCacheLock = OS_SPINLOCK_INIT;
  30. TIMManager *manager = [TIMManager sharedInstance];
  31. //[manager setMessageListener:self];
  32. [manager addMessageListener:self];
  33. //V2TIMSDKListener V2TIMGroupListener
  34. // [[V2TIMManager sharedInstance] addIMSDKListener:self];
  35. // [[V2TIMManager sharedInstance] addGroupListener:self];
  36. [[V2TIMManager sharedInstance] addAdvancedMsgListener:self];
  37. [[V2TIMManager sharedInstance] addGroupListener:self];
  38. }
  39. return self;
  40. }
  41. }
  42. #pragma mark - ----------------------- 接收并处理消息 -----------------------
  43. #pragma mark 从缓存中取出maxDo条数据处理
  44. //系统消息
  45. - (void)onReceiveRESTCustomData:(NSString *)groupID data:(NSData *)data
  46. {
  47. V2TIMMessage *msg = [[V2TIMManager sharedInstance] createCustomMessage:data];
  48. [msg qmui_bindObject:groupID forKey:@"groupID2"];
  49. // [msg setValue:groupID forKey:@"groupID"];
  50. // if (_liveItem) // 直播间内的群消息
  51. // {
  52. // if([groupID isEqualToString:[_liveItem liveIMChatRoomId]])
  53. // {
  54. // // 处理群聊天消息
  55. // [_newMsgMArray addObject:msg];
  56. //
  57. // }
  58. // }
  59. // else
  60. // {
  61. // [self onRecvMsg:msg msgType:V2TIM_GROUP];
  62. // }
  63. [self onRecvMsg:msg msgType:TIM_SYSTEM];
  64. }
  65. - (void)onHandleMyNewMessage:(NSInteger)maxDo
  66. {
  67. NSInteger myIndex = 0;
  68. while ([_newMsgMArray count]>0)
  69. {
  70. TIMMessage *msg = [_newMsgMArray firstObject];
  71. [_newMsgMArray removeObject:msg];
  72. [self onRecvMsg:msg msgType:TIM_GROUP];
  73. myIndex ++;
  74. if (myIndex == maxDo)
  75. {
  76. return;
  77. }
  78. }
  79. }
  80. #pragma mark 循环消息处
  81. - (void)onNewMessage:(NSArray *)msgs
  82. {
  83. // [self performSelector:@selector(onHandleNewMessage:) onThread:_sharedRunLoopRef.thread withObject:msgs waitUntilDone:NO];
  84. }
  85. - (void)onRecvNewMessage:(V2TIMMessage *)msg
  86. {
  87. [self performSelector:@selector(onHandleNewMessageV2:) onThread:_sharedRunLoopRef.thread withObject:msg waitUntilDone:NO];
  88. }
  89. #pragma mark 循环消息处理时收到消息后判断消息类型
  90. - (void)onHandleNewMessageV2:(V2TIMMessage *)msg
  91. {
  92. [SIMMsgObj maybeIMChatMsg:msg];
  93. NSString *rid = [msg userID];
  94. if(rid != nil)
  95. {
  96. [self onRecvMsg:msg msgType:V2TIM_C2C];
  97. }
  98. else
  99. {
  100. if (_liveItem) // 直播间内的群消息
  101. {
  102. if([[msg groupID] isEqualToString:[_liveItem liveIMChatRoomId]])
  103. {
  104. // 处理群聊天消息
  105. [_newMsgMArray addObject:msg];
  106. }
  107. }
  108. else // 预留其它群消息
  109. {
  110. [self onRecvMsg:msg msgType:V2TIM_GROUP];
  111. }
  112. }
  113. }
  114. #pragma mark 解析IM消息
  115. - (void)onRecvMsg:(V2TIMMessage *)msg msgType:(V2TIMConversationType)msgType
  116. {
  117. V2TIMTextElem *textElem = msg.textElem;
  118. V2TIMCustomElem *customElem = nil;
  119. V2TIMCustomElem *Elem = (V2TIMCustomElem *) textElem.nextElem;
  120. if([Elem isKindOfClass:V2TIMCustomElem.class])
  121. {
  122. customElem = Elem;
  123. }else
  124. {
  125. customElem = msg.customElem;
  126. }
  127. //
  128. // V2TIMTextElem *textElem = msg.textElem;
  129. // V2TIMCustomElem *customElem = msg.customElem;
  130. // V2TIMGroupTipsElem *groupSystemElem = msg.groupTipsElem;
  131. if (msg)
  132. {
  133. //:identifier、nickname、faceURL、customInfo
  134. TIMUserProfile *profil = [[TIMUserProfile alloc] init];
  135. profil.identifier = (msg.userID == nil)?msg.groupID:msg.userID;
  136. NSString *g2 = [msg qmui_getBoundObjectForKey:@"groupID2"];
  137. if(g2 !=nil)
  138. {
  139. profil.identifier = g2;
  140. }
  141. profil.nickname = msg.nickName;
  142. id<IMUserAble> profile = profil;
  143. // if (!profile)
  144. // {
  145. // if (msgType == TIM_C2C)
  146. // {
  147. // NSString *recv = msg.userID;
  148. // profile = [self syncGetC2CUserInfo:recv];
  149. // }
  150. // else if (msgType == TIM_GROUP)
  151. // {
  152. // profile = [msg getSenderGroupMemberProfile];
  153. // }
  154. // }
  155. // if(msg.elemType == V2TIM_ELEM_TYPE_CUSTOM)
  156. // {
  157. //
  158. // }
  159. // for(int index = 0; index < [msg elemCount]; index++)
  160. // {
  161. // TIMElem *elem = [msg getElem:index];
  162. // if([elem isKindOfClass:[TIMTextElem class]])
  163. // {
  164. // textElem = (TIMTextElem *)elem;
  165. // }
  166. // else if([elem isKindOfClass:[TIMCustomElem class]])
  167. // {
  168. // customElem = (TIMCustomElem *)elem;
  169. // }
  170. // else if([elem isKindOfClass:[TIMGroupSystemElem class]])
  171. // {
  172. // groupSystemElem = (TIMGroupSystemElem *)elem;
  173. // }
  174. // }
  175. // 脏字库过滤
  176. if (msgType == TIM_C2C && customElem) // 一对一消息
  177. {
  178. if (textElem)
  179. {
  180. [self onRecvC2CSender:profile customMsg:customElem textMsg:textElem.text];
  181. }
  182. else
  183. {
  184. [self onRecvC2CSender:profile customMsg:customElem textMsg:@""];
  185. }
  186. }
  187. else if (msgType == TIM_GROUP && customElem) // 群自定义消息
  188. {
  189. if (textElem)
  190. {
  191. [self onRecvGroupSender:profile groupId:msg.groupID customMsg:customElem textMsg:textElem.text];
  192. }
  193. else
  194. {
  195. if (profile) {
  196. }
  197. [self onRecvGroupSender:profile groupId:msg.groupID customMsg:customElem textMsg:@""];
  198. }
  199. }
  200. else if (msgType == TIM_SYSTEM ) // 群系统消息
  201. {
  202. NSString *g2 = [msg qmui_getBoundObjectForKey:@"groupID2"];
  203. NSString *sysGroupID = g2;
  204. if (![BGUtils isBlankString:sysGroupID])
  205. {
  206. if (textElem)
  207. {
  208. [self onRecvSystepGroupSender:profile groupId:sysGroupID customMsg:msg.customElem textMsg:textElem.text];
  209. }
  210. else
  211. {
  212. [self onRecvSystepGroupSender:profile groupId:sysGroupID customMsg:msg.customElem textMsg:@""];
  213. }
  214. }
  215. }
  216. }
  217. }
  218. #pragma mark - ----------------------- 发送自定义消息 -----------------------
  219. #pragma mark 组装TIMCustomElem
  220. - (TIMCustomElem *)getCustomElemWith:(CustomMessageModel *)customMessageModel
  221. {
  222. SenderModel *sender = [[SenderModel alloc]init];
  223. sender.user_id = [IMAPlatform sharedInstance].host.imUserId;
  224. sender.nick_name = [IMAPlatform sharedInstance].host.imUserName;
  225. sender.head_image = [IMAPlatform sharedInstance].host.imUserIconUrl;
  226. sender.user_level = (int)[[IMAPlatform sharedInstance].host getUserRank];
  227. sender.sort_num = [IMAPlatform sharedInstance].host.sort_num;
  228. sender.v_icon = [[IMAPlatform sharedInstance].host.customInfoDict toString:@"v_icon"];
  229. sender.v_type = [[IMAPlatform sharedInstance].host.customInfoDict toInt:@"v_type"];
  230. // sender.noble_car_name = [IMAPlatform sharedInstance].host.car_name;
  231. sender.car_svga = [IMAPlatform sharedInstance].host.car_svga;
  232. sender.has_car = [[IMAPlatform sharedInstance].host.has_car intValue];
  233. NSDictionary *dic = [IMAPlatform sharedInstance].host.customInfoDict;
  234. sender.noble_vip_type = [dic objectForKey:@"noble_vip_type"];
  235. sender.noble_stealth = [dic objectForKey:@"noble_stealth"];
  236. sender.noble_avatar = [dic objectForKey:@"noble_avatar"];
  237. sender.noble_icon = [dic objectForKey:@"noble_icon"];
  238. sender.noble_is_avatar = [dic objectForKey:@"noble_is_avatar"];
  239. sender.noble_car_url = sender.car_svga;
  240. sender.noble_car_name = [dic objectForKey:@"noble_car_name"];
  241. sender.is_guardian = customMessageModel.is_guardian;
  242. sender.guardianModel = customMessageModel.guardianModel;
  243. sender.guardian_broadcast = customMessageModel.guardianModel.guardian_broadcast;
  244. sender.guardian_remind = customMessageModel.guardianModel.guardian_remind;
  245. sender.guardian_skin = customMessageModel.guardianModel.guardian_skin;
  246. sender.guardian_gift = customMessageModel.guardianModel.guardian_gift;
  247. sender.guardian_img = customMessageModel.guardianModel.guardian_img;
  248. sender.guardian_kick = customMessageModel.guardianModel.guardian_kick;
  249. sender.guardian_icon = customMessageModel.guardianModel.guardian_icon;
  250. sender.is_vip = [dic objectForKey:@"is_vip"];
  251. NSString *is_noble_mysterious = [dic objectForKey:@"is_noble_mysterious"];
  252. //是否是神秘人
  253. if ([is_noble_mysterious integerValue] == 1){
  254. //如果是
  255. sender.head_image = [dic valueForKey:@"mysterious_picture"];
  256. sender.nick_name = [dic valueForKey:@"mysterious_name"];
  257. }
  258. sender.is_noble_mysterious = [dic objectForKey:@"is_noble_mysterious"];
  259. customMessageModel.sender = sender;
  260. NSMutableDictionary *stuDict = customMessageModel.mj_keyValues;
  261. if ([stuDict objectForKey:@"avimMsgShowSize"])
  262. {
  263. [stuDict removeObjectForKey:@"avimMsgShowSize"];
  264. }
  265. [stuDict removeObjectsForKeys:@[@"debugDescription", @"description", @"hash", @"superclass"]];
  266. if ([stuDict objectForKey:@"sender"])
  267. {
  268. [[stuDict objectForKey:@"sender"] removeObjectsForKeys:@[@"debugDescription", @"description", @"hash", @"superclass"]];
  269. }
  270. NSString *sendMessage = [BGUtils dataTOjsonString:stuDict];
  271. TIMCustomElem* messageElem = [[TIMCustomElem alloc] init];
  272. messageElem.data = [sendMessage dataUsingEncoding:NSUTF8StringEncoding];
  273. return messageElem;
  274. }
  275. #pragma mark - 发送pk消息
  276. - (void)sendCustomC2CMsg:(SendCustomMsgModel *)sCMM widthPID:(NSString *)pid succ:(TIMSucc)succ fail:(TIMFail)fail
  277. {
  278. if (sCMM.msgReceiver)
  279. {
  280. CustomMessageModel *customMessageModel = [[CustomMessageModel alloc] init];
  281. customMessageModel.pk_id = pid;
  282. customMessageModel.type = sCMM.msgType;
  283. if (sCMM.msgType == MSG_RECEIVE_MIKE)
  284. {
  285. customMessageModel.push_rtmp2 = sCMM.push_rtmp2;
  286. customMessageModel.play_rtmp_acc = sCMM.play_rtmp_acc;
  287. }
  288. else if (sCMM.msgType == MSG_REFUSE_MIKE)
  289. {
  290. customMessageModel.msg = sCMM.msg;
  291. }
  292. TIMCustomElem* messageElem = [self getCustomElemWith:customMessageModel];
  293. TIMMessage* timMsg = [[TIMMessage alloc] init];
  294. [timMsg addElem:messageElem];
  295. if (sCMM.msgType == MSG_TEXT && _BuguLive.appModel.has_dirty_words == 1)
  296. {
  297. TIMTextElem *textElem = [[TIMTextElem alloc]init];
  298. textElem.text = sCMM.msg;
  299. [timMsg addElem:textElem];
  300. }
  301. V2TIMCustomElem *messageElemV2 = [[V2TIMCustomElem alloc] init];
  302. messageElemV2.data = messageElem.data;
  303. V2TIMMessage *msg = [[V2TIMManager sharedInstance] createCustomMessage:messageElem.data];
  304. msg.isExcludedFromUnreadCount = YES;
  305. if (sCMM.msgType == MSG_TEXT && _BuguLive.appModel.has_dirty_words == 1)
  306. {
  307. V2TIMTextElem *textElem = [[V2TIMTextElem alloc]init];
  308. textElem.text = sCMM.msg;
  309. [msg.customElem appendElem:textElem];
  310. }
  311. [[V2TIMManager sharedInstance] sendMessage:msg receiver:[sCMM.msgReceiver imUserId] groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  312. } succ:^{
  313. if(succ)
  314. {
  315. succ();
  316. }
  317. } fail:^(int code, NSString *desc) {
  318. if(fail)
  319. {
  320. fail(code, desc);
  321. }
  322. }];
  323. // TIMConversation *conv = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:[sCMM.msgReceiver imUserId]];
  324. //
  325. // [conv sendMessage:timMsg succ:succ fail:^(int code, NSString *msg)
  326. // {
  327. // DebugLog(@"发送消息失败:%@", timMsg);
  328. // if (fail)
  329. // {
  330. // fail(code, msg);
  331. // }
  332. // }];
  333. }
  334. }
  335. #pragma mark 发送自定义一对一消息
  336. - (void)sendCustomC2CMsg:(SendCustomMsgModel *)sCMM succ:(TIMSucc)succ fail:(TIMFail)fail
  337. {
  338. if (sCMM.msgReceiver)
  339. {
  340. CustomMessageModel *customMessageModel = [[CustomMessageModel alloc] init];
  341. customMessageModel.type = sCMM.msgType;
  342. customMessageModel.link_mic_user_id = sCMM.link_mic_user_id;
  343. if (sCMM.msgType == MSG_RECEIVE_MIKE)
  344. {
  345. customMessageModel.push_rtmp2 = sCMM.push_rtmp2;
  346. customMessageModel.play_rtmp_acc = sCMM.play_rtmp_acc;
  347. }
  348. else if (sCMM.msgType == MSG_REFUSE_MIKE)
  349. {
  350. customMessageModel.msg = sCMM.msg;
  351. }else if (sCMM.msgType == MSG_REQEUST_PK){
  352. customMessageModel.pkid = sCMM.pkid;
  353. }
  354. TIMCustomElem* messageElem = [self getCustomElemWith:customMessageModel];
  355. TIMMessage* timMsg = [[TIMMessage alloc] init];
  356. [timMsg addElem:messageElem];
  357. if (sCMM.msgType == MSG_TEXT && _BuguLive.appModel.has_dirty_words == 1)
  358. {
  359. TIMTextElem *textElem = [[TIMTextElem alloc]init];
  360. textElem.text = sCMM.msg;
  361. [timMsg addElem:textElem];
  362. }
  363. // TIMConversation *conv = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:[sCMM.msgReceiver imUserId]];
  364. //
  365. // [conv sendMessage:timMsg succ:succ fail:^(int code, NSString *msg)
  366. // {
  367. // DebugLog(@"发送消息失败:%@", timMsg);
  368. // if (fail)
  369. // {
  370. // fail(code, msg);
  371. // }
  372. // }];
  373. V2TIMCustomElem *messageElemV2 = [[V2TIMCustomElem alloc] init];
  374. messageElemV2.data = messageElem.data;
  375. V2TIMMessage *msg = [[V2TIMManager sharedInstance] createCustomMessage:messageElem.data];
  376. msg.isExcludedFromUnreadCount = YES;
  377. if (sCMM.msgType == MSG_TEXT && _BuguLive.appModel.has_dirty_words == 1)
  378. {
  379. V2TIMTextElem *textElem = [[V2TIMTextElem alloc]init];
  380. textElem.text = sCMM.msg;
  381. [msg.customElem appendElem:textElem];
  382. }
  383. [[V2TIMManager sharedInstance] sendMessage:msg receiver:[sCMM.msgReceiver imUserId] groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  384. } succ:^{
  385. if(succ)
  386. {
  387. succ();
  388. }
  389. } fail:^(int code, NSString *desc) {
  390. if(fail)
  391. {
  392. fail(code, desc);
  393. }
  394. }];
  395. }
  396. }
  397. #pragma mark 发送自定义群消息
  398. - (void)sendCustomGroupMsg:(SendCustomMsgModel *)sCMM succ:(TIMSucc)succ fail:(TIMFail)fail
  399. {
  400. if (_chatRoomConversation)
  401. {
  402. CustomMessageModel *customMessageModel = [[CustomMessageModel alloc] init];
  403. customMessageModel.type = sCMM.msgType;
  404. customMessageModel.chatGroupID = sCMM.chatGroupID;
  405. if (sCMM.msgType == MSG_LIVING_MESSAGE)
  406. { //直播消息
  407. customMessageModel.desc = sCMM.msg;
  408. }
  409. else if(sCMM.msgType == MSG_RECEIVE_MIKE)
  410. {
  411. customMessageModel.link_mic_user_id = sCMM.link_mic_user_id;
  412. customMessageModel.push_rtmp2 = sCMM.push_rtmp2;
  413. customMessageModel.play_rtmp_acc = sCMM.play_rtmp_acc;
  414. }
  415. else if (sCMM.msgType == MSG_BREAK_MIKE)
  416. {
  417. customMessageModel.link_mic_user_id = sCMM.link_mic_user_id;
  418. }
  419. else if(sCMM.msgType == MSG_LIGHT)
  420. { //点亮
  421. customMessageModel.imageName = sCMM.msg;
  422. if (sCMM.isShowLight)
  423. {
  424. customMessageModel.showMsg = 1;
  425. }
  426. else
  427. {
  428. customMessageModel.showMsg = 0;
  429. }
  430. customMessageModel.is_guardian = sCMM.is_guardian;
  431. customMessageModel.is_vip = sCMM.is_vip;
  432. customMessageModel.guardianModel = sCMM.guardianModel;
  433. }else if (sCMM.msgType == MSG_VIEWER_JOIN || sCMM.msgType == MSG_TEXT){
  434. customMessageModel.is_guardian = sCMM.is_guardian;
  435. customMessageModel.noble_icon = sCMM.noble_icon;
  436. customMessageModel.is_vip = sCMM.is_vip;
  437. customMessageModel.text = sCMM.msg;
  438. customMessageModel.guardianModel = sCMM.guardianModel;
  439. customMessageModel.guardian_icon = sCMM.guardian_icon;
  440. customMessageModel.guardian_gift = sCMM.guardian_gift;
  441. customMessageModel.guardian_skin = sCMM.guardian_skin;
  442. customMessageModel.guardian_img = sCMM.guardian_img;
  443. customMessageModel.guardian_broadcast = sCMM.guardian_broadcast;
  444. }
  445. else if(sCMM.msgType == MSG_LianMai_Mute)
  446. {
  447. customMessageModel.to_user_id = sCMM.to_user_id;
  448. customMessageModel.mute_status = sCMM.mute_status;
  449. }
  450. else if(sCMM.msgType == MSG_SEND_EMOTION)
  451. {
  452. customMessageModel.faceUrl = sCMM.faceUrl;
  453. }
  454. else
  455. {
  456. customMessageModel.text = sCMM.msg;
  457. }
  458. if (sCMM.msgType == MSG_SHOP_SAY_TYPE) {
  459. customMessageModel.shopModel = sCMM.shopModel;
  460. }
  461. customMessageModel.guardianModel = sCMM.guardianModel;
  462. customMessageModel.guardian_icon = sCMM.guardian_icon;
  463. customMessageModel.guardian_gift = sCMM.guardian_gift;
  464. customMessageModel.guardian_skin = sCMM.guardian_skin;
  465. customMessageModel.guardian_img = sCMM.guardian_img;
  466. customMessageModel.guardian_broadcast = sCMM.guardian_broadcast;
  467. TIMCustomElem* messageElem = [self getCustomElemWith:customMessageModel];
  468. V2TIMCustomElem *messageElemV2 = [[V2TIMCustomElem alloc] init];
  469. messageElemV2.data = messageElem.data;
  470. V2TIMMessage *msg = [[V2TIMManager sharedInstance] createCustomMessage:messageElem.data];
  471. // msg.cloudCustomData = messageElem.data;
  472. // TIMMessage* timMsg = [[TIMMessage alloc] init];
  473. // [timMsg addElem:messageElem];
  474. // if (sCMM.msgType == MSG_TEXT && _BuguLive.appModel.has_dirty_words == 1)
  475. // {
  476. // TIMTextElem *textElem = [[TIMTextElem alloc]init];
  477. // textElem.text = sCMM.msg;
  478. // [timMsg addElem:textElem];
  479. // }
  480. [[V2TIMManager sharedInstance] sendMessage:msg receiver:nil groupID:sCMM.chatGroupID priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  481. } succ:^{
  482. [self onRecvGroupSender:[IMAPlatform sharedInstance].host groupId:sCMM.chatGroupID customMsg:msg.customElem textMsg:@""];
  483. } fail:^(int code, NSString *desc) {
  484. }];
  485. /*[_chatRoomConversation sendMessage:timMsg succ:^{
  486. // 防止频繁切换房间时把消息发送给了下一个直播间
  487. if ([customMessageModel.chatGroupID isEqualToString:[_liveItem liveIMChatRoomId]])
  488. {
  489. // 需要在直播间聊天列表中显示的消息就加进来
  490. [self onRecvGroupSender:[IMAPlatform sharedInstance].host groupId:sCMM.chatGroupID customMsg:messageElem textMsg:@""];
  491. }
  492. if (succ)
  493. {
  494. succ();
  495. }
  496. } fail:^(int code, NSString *msg) {
  497. if (code == 80001)
  498. {
  499. [FanweMessage alert:ASLocalizedString(@"该词被禁用")];
  500. }else if (code == 10017 || code == 20012)
  501. {
  502. [FanweMessage alert:ASLocalizedString(@"您已被禁言")];
  503. }
  504. DebugLog(@"发送消息失败:%@", timMsg);
  505. if (fail)
  506. {
  507. fail(code, msg);
  508. }
  509. }];*/
  510. }
  511. else
  512. {
  513. if (fail)
  514. {
  515. fail(-1,ASLocalizedString( @"TIMConversation 为空"));
  516. }
  517. }
  518. }
  519. #pragma mark - ----------------------- 设置是否支持缓存模式 -----------------------
  520. - (void)setIsCacheMode:(BOOL)isCacheMode
  521. {
  522. _isCacheMode = isCacheMode;
  523. if (_isCacheMode)
  524. {
  525. [self createMsgCache];
  526. }
  527. else
  528. {
  529. [self releaseMsgCache];
  530. }
  531. }
  532. @end
  533. #pragma mark - ======================= 供子类重写 =======================
  534. @implementation BGIMMsgHandler (ProtectedMethod)
  535. - (id<IMUserAble>)syncGetC2CUserInfo:(NSString *)identifier
  536. {
  537. if (_liveItem)
  538. {
  539. if ([[[_liveItem liveHost] imUserId] isEqualToString:identifier])
  540. {
  541. // 主播发过来的消息
  542. return [_liveItem liveHost];
  543. }
  544. else
  545. {
  546. TIMUserProfile *profile = [[TIMUserProfile alloc] init];
  547. profile.identifier = identifier;
  548. return profile;
  549. }
  550. }
  551. else
  552. {
  553. TIMUserProfile *profile = [[TIMUserProfile alloc] init];
  554. profile.identifier = identifier;
  555. return profile;
  556. }
  557. }
  558. #pragma mark - ----------------------- 解析消息后处理对应的代理事件 -----------------------
  559. #pragma mark 收到C2C自定义消息
  560. - (void)onRecvC2CSender:(id<IMUserAble>)sender customMsg:(V2TIMCustomElem *)msg textMsg:(NSString *)textMsg
  561. {
  562. id<AVIMMsgAble> c2cMsg = [self getCustomMsgModel:sender groupId:@"" customMsg:msg textMsg:textMsg];
  563. dispatch_async(dispatch_get_main_queue(), ^{
  564. if (c2cMsg && _liveItem)
  565. {
  566. [_iMMsgListener onIMHandler:self recvCustomC2C:c2cMsg];
  567. }
  568. });
  569. }
  570. #pragma mark 收到群自定义消息处理
  571. - (void)onRecvGroupSender:(id<IMUserAble>)sender groupId:(NSString *)groupId customMsg:(V2TIMCustomElem *)msg textMsg:(NSString *)textMsg
  572. {
  573. id<AVIMMsgAble> cachedMsg = [self getCustomMsgModel:sender groupId:groupId customMsg:msg textMsg:textMsg];
  574. NSLog(@"tttyypppee %ld",(long)[cachedMsg msgType]);
  575. if (cachedMsg && _liveItem)
  576. {
  577. if ([cachedMsg msgType] == MSG_TEXT || [cachedMsg msgType] == MSG_VIEWER_JOIN || [cachedMsg msgType] == MSG_SEND_GIFT_SUCCESS || [cachedMsg msgType] == UPDATE_PK_TICKET)
  578. {
  579. [self enCache:cachedMsg noCache:^{}];
  580. }
  581. dispatch_async(dispatch_get_main_queue(), ^{
  582. [_iMMsgListener onIMHandler:self recvCustomGroup:cachedMsg];
  583. });
  584. }
  585. // if ([cachedMsg msgType] == MSG_RECEIVE_BROADCAST) {
  586. // NSDictionary *resposeDict = [NSJSONSerialization JSONObjectWithData:msg.data options:NSJSONReadingAllowFragments error:nil];
  587. // CustomMessageModel *customMessageModel = [CustomMessageModel mj_objectWithKeyValues:resposeDict];
  588. //
  589. // [self showGlobalViewWithModel:customMessageModel];
  590. //
  591. // }
  592. //
  593. // return;
  594. // if ([cachedMsg msgType] == MSG_OPEN_VIP_TYPE) {
  595. // NSDictionary *resposeDict = [NSJSONSerialization JSONObjectWithData:msg.data options:NSJSONReadingAllowFragments error:nil];
  596. // CustomMessageModel *customMessageModel = [CustomMessageModel mj_objectWithKeyValues:resposeDict];
  597. //
  598. // customMessageModel.sender.text = [resposeDict valueForKeyPath:@"broadcash_msg_content"];
  599. // [self showGlobalViewWithModel:customMessageModel];
  600. // }
  601. }
  602. -(void)showGlobalViewWithModel:(CustomMessageModel *)customMessageModel{
  603. // self.globalView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
  604. dispatch_async(dispatch_get_main_queue(), ^{
  605. if (!self.globalView) {
  606. self.globalView = [[MGGlobalVipView alloc]initWithFrame:CGRectMake(kScreenW, FD_Top_Height + 120, 230, 40)];
  607. }
  608. // 判断是否有悬浮窗
  609. UIView *tmpView = [AppDelegate sharedAppDelegate].sus_window.rootViewController ? [AppDelegate sharedAppDelegate].sus_window : [AppDelegate sharedAppDelegate].window;
  610. UIView *inView = [AppDelegate sharedAppDelegate].sus_window.isSmallSusWindow ? [AppDelegate sharedAppDelegate].window : tmpView;
  611. // 超时自动消失
  612. // [hud hideAnimated:YES afterDelay:kRequestTimeOutTime];
  613. self.globalView.model = customMessageModel;
  614. [inView addSubview:self.globalView];
  615. [inView bringSubviewToFront:self.globalView];
  616. FWWeakify(self)
  617. [UIView animateWithDuration:10.0f animations:^{
  618. FWStrongify(self)
  619. self.globalView.left = -kScreenW;
  620. } completion:^(BOOL finished) {
  621. FWStrongify(self)
  622. self.globalView.left = kScreenW;
  623. }];
  624. });
  625. }
  626. #pragma mark 收到群系统消息
  627. - (void)onRecvSystepGroupSender:(id<IMUserAble>)sender groupId:(NSString *)groupId customMsg:(V2TIMCustomElem *)msg textMsg:(NSString *)textMsg
  628. {
  629. id<AVIMMsgAble> systepGroup = [self getSystemGroupMsgModel:sender groupId:groupId customMsg:msg textMsg:textMsg];
  630. dispatch_async(dispatch_get_main_queue(), ^{
  631. if (systepGroup && _liveItem && [groupId isEqualToString:[_liveItem liveIMChatRoomId]])
  632. {
  633. [_iMMsgListener onIMHandler:self recvCustomGroup:systepGroup];
  634. }
  635. });
  636. }
  637. #pragma mark - ----------------------- 解析消息 -----------------------
  638. #pragma mark 解析自定义消息
  639. - (id<AVIMMsgAble>)getCustomMsgModel:(id<IMUserAble>)sender groupId:(NSString *)groupId customMsg:(V2TIMCustomElem *)msg textMsg:(NSString *)textMsg
  640. {
  641. return [self getMsgModel:sender groupId:groupId customMsg:msg.data textMsg:textMsg];
  642. }
  643. #pragma mark 解析群系统消息
  644. - (id<AVIMMsgAble>)getSystemGroupMsgModel:(id<IMUserAble>)sender groupId:(NSString *)groupId customMsg:(V2TIMCustomElem *)msg textMsg:(NSString *)textMsg
  645. {
  646. #ifdef DEBUG
  647. // NSString *jsonStr = [[NSString alloc]initWithData:msg.userData encoding:NSUTF8StringEncoding];
  648. // NetHttpsManager *http = [NetHttpsManager manager];
  649. // NSMutableDictionary *dic = [NSMutableDictionary new];
  650. // [dic setObject:jsonStr forKey:@"test_system_im"];
  651. // [dic setObject:@"pushTest" forKey:@"act"];
  652. // [dic setObject:@"games" forKey:@"ctl"];
  653. // [http POSTWithParameters:dic SuccessBlock:^(NSDictionary *responseJson) {
  654. // NSLog(@"%@",[responseJson toString:@"status"]);
  655. // } FailureBlock:^(NSError *error) {
  656. // NSLog(@"%@",error);
  657. // }];
  658. #endif
  659. return [self getMsgModel:sender groupId:groupId customMsg:msg.data textMsg:textMsg];
  660. }
  661. #pragma mark 解析消息,获取消息实体
  662. - (id<AVIMMsgAble>)getMsgModel:(id<IMUserAble>)sender groupId:(NSString *)groupId customMsg:(NSData *)data textMsg:(NSString *)textMsg
  663. {
  664. CustomMessageModel *customMessageModel;
  665. NSDictionary *resposeDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  666. NSLog(@"getMsgModel %@",resposeDict);
  667. if (resposeDict && [resposeDict isKindOfClass:[NSDictionary class]])
  668. {
  669. if ([resposeDict count])
  670. {
  671. customMessageModel = [CustomMessageModel mj_objectWithKeyValues:resposeDict];
  672. customMessageModel.dicData = resposeDict;
  673. SenderModel *tmpSender = [SenderModel mj_objectWithKeyValues:[resposeDict objectForKey:@"sender"]];
  674. if (!StrValid(tmpSender.noble_stealth)) {
  675. tmpSender.noble_stealth = tmpSender.is_noble_stealth;
  676. }
  677. customMessageModel.sender = tmpSender;
  678. customMessageModel.chatGroupID = groupId;
  679. if (tmpSender.user_level > [[IMAPlatform sharedInstance].host getUserRank])
  680. {
  681. dispatch_async(dispatch_get_main_queue(), ^{
  682. [[IMAPlatform sharedInstance].host getMyInfo:^(AppBlockModel *blockModel) {
  683. // 判断当前用户的等级能否发言了
  684. [[NSNotificationCenter defaultCenter] postNotificationName:kLiveRoomCanSendMessage object:nil];
  685. }];
  686. });
  687. }
  688. if (customMessageModel.type == MSG_LIVE_STOP)
  689. {
  690. dispatch_async(dispatch_get_main_queue(), ^{
  691. // 刷新首页(主要为了删除首页已经退出的直播间)
  692. [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshHomeItem object:resposeDict];
  693. });
  694. }
  695. if (textMsg && ![textMsg isEqualToString:@""])
  696. {
  697. customMessageModel.text = textMsg;
  698. }
  699. }
  700. }
  701. [customMessageModel prepareForRender];
  702. return customMessageModel;
  703. }
  704. @end
  705. #pragma mark - ======================= 缓存相关 =======================
  706. @implementation BGIMMsgHandler (CacheMode)
  707. - (void)createMsgCache
  708. {
  709. if (_msgCache)
  710. {
  711. _msgCache = nil;
  712. }
  713. _msgCache = [NSMutableDictionary dictionary];
  714. [_msgCache setObject:[[AVIMCache alloc] initWith:1000] forKey:kRoomTableViewMsgKey];
  715. [_msgCache setObject:[[AVIMCache alloc] initWith:10] forKey:@(MSG_LIGHT)];
  716. }
  717. - (void)resetMsgCache
  718. {
  719. [self createMsgCache];
  720. }
  721. - (void)releaseMsgCache
  722. {
  723. _msgCache = nil;
  724. }
  725. - (void)enCache:(id<AVIMMsgAble>)msg noCache:(FWVoidBlock)noCacheblock;
  726. {
  727. if (!_isCacheMode)
  728. {
  729. if (noCacheblock)
  730. {
  731. noCacheblock();
  732. }
  733. }
  734. else
  735. {
  736. if (msg)
  737. {
  738. OSSpinLockLock(&_msgCacheLock);
  739. AVIMCache *cache;
  740. if ([msg msgType] == MSG_LIGHT)
  741. {
  742. cache = [_msgCache objectForKey:@([msg msgType])];
  743. }
  744. else
  745. {
  746. cache = [_msgCache objectForKey:kRoomTableViewMsgKey];
  747. }
  748. if (cache)
  749. {
  750. [cache enCache:msg];
  751. }
  752. else
  753. {
  754. if (noCacheblock)
  755. {
  756. noCacheblock();
  757. }
  758. }
  759. OSSpinLockUnlock(&_msgCacheLock);
  760. }
  761. }
  762. }
  763. - (NSDictionary *)getMsgCache
  764. {
  765. OSSpinLockLock(&_msgCacheLock);
  766. NSDictionary *dic = _msgCache;
  767. OSSpinLockUnlock(&_msgCacheLock);
  768. return dic;
  769. }
  770. @end
  771. #pragma mark - ======================= 直播间调用的方法 =======================
  772. @implementation BGIMMsgHandler (LivingRoom)
  773. #pragma mark 设置群监听
  774. - (void)setGroupChatListener:(id<FWShowLiveRoomAble>)liveItem block:(CommonBlock)block
  775. {
  776. DebugLog(@"========:setGroupChatListener");
  777. _liveItem = liveItem;
  778. [[V2TIMManager sharedInstance] getConversation:[NSString stringWithFormat:@"c2c_%d",liveItem.liveAVRoomId] succ:^(V2TIMConversation *conv) {
  779. _chatRoomConversation = conv;
  780. self.isCacheMode = kSupportIMMsgCache;
  781. block(nil);
  782. } fail:^(int code, NSString *desc) {
  783. }];
  784. }
  785. #pragma mark 移除群监听
  786. - (void)removeGroupChatListener
  787. {
  788. DebugLog(@"========:removeGroupChatListener");
  789. _liveItem = nil;
  790. _chatRoomConversation = nil;
  791. self.isCacheMode = NO;
  792. [_newMsgMArray removeAllObjects];
  793. }
  794. @end