From 36415cca74dcbc5be3b90bf3e2f3da265c873ceb Mon Sep 17 00:00:00 2001 From: fengshuo <963787902@qq.com> Date: Wed, 8 Dec 2021 14:36:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E5=B0=8F=E5=8C=96=E6=88=BF=E9=97=B4?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E4=B8=80=E4=BA=9B=E4=B8=8B=E9=BA=A6=E4=B8=8A?= =?UTF-8?q?=E9=BA=A6=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Main/Room/View/StageView/StageView.m | 2 + xplan-ios/Main/Room/View/XPMiniRoomView.h | 4 +- xplan-ios/Main/Room/View/XPMiniRoomView.m | 199 +++++++++++++++++- .../Main/Room/View/XPRoomViewController.m | 6 +- 4 files changed, 198 insertions(+), 13 deletions(-) diff --git a/xplan-ios/Main/Room/View/StageView/StageView.m b/xplan-ios/Main/Room/View/StageView/StageView.m index 7f550657..aa9ffda4 100644 --- a/xplan-ios/Main/Room/View/StageView/StageView.m +++ b/xplan-ios/Main/Room/View/StageView/StageView.m @@ -137,6 +137,8 @@ sequence.userInfo = userInfo; } [self microQueueUpdated]; + // 最小化进房 如果当前用户在坑位上的话 + [self.hostDelegate onMicroQueueUpdate:self.micQueue]; }]; } diff --git a/xplan-ios/Main/Room/View/XPMiniRoomView.h b/xplan-ios/Main/Room/View/XPMiniRoomView.h index debd3cdd..f3f0910c 100644 --- a/xplan-ios/Main/Room/View/XPMiniRoomView.h +++ b/xplan-ios/Main/Room/View/XPMiniRoomView.h @@ -8,7 +8,7 @@ #import NS_ASSUME_NONNULL_BEGIN -@class RoomInfoModel; +@class RoomInfoModel, UserInfoModel; @interface XPMiniRoomView : UIView /* @@ -23,7 +23,7 @@ NS_ASSUME_NONNULL_BEGIN ///房间的id @property (nonatomic,strong, readonly) NSString *currentRoomId; ///房间最小化 初始化 -- (void)configRoomMiniView:(RoomInfoModel *)roomInfo; +- (void)configRoomMiniView:(RoomInfoModel *)roomInfo userInfo:(UserInfoModel *)userInfo micQueue:(NSMutableDictionary *)micQueue; ///隐藏最小化的view - (void)hiddenRoomMiniView; @end diff --git a/xplan-ios/Main/Room/View/XPMiniRoomView.m b/xplan-ios/Main/Room/View/XPMiniRoomView.m index 95dcde5f..87bb561a 100644 --- a/xplan-ios/Main/Room/View/XPMiniRoomView.m +++ b/xplan-ios/Main/Room/View/XPMiniRoomView.m @@ -15,13 +15,19 @@ #import "NetImageView.h" #import "RtcManager.h" #import "UIButton+EnlargeTouchArea.h" -#import "XPNoteView.h" +#import "TTPopup.h" +#import "XCHudTool.h" ///Model #import "RoomInfoModel.h" +#import "MicroQueueModel.h" +#import "MicroStateModel.h" +#import "UserInfoModel.h" +#import "AttachmentModel.h" ///View #import "XPRoomViewController.h" +#import "XPNoteView.h" -@interface XPMiniRoomView () +@interface XPMiniRoomView () ///底部的底图 @property (nonatomic,strong) UIImageView *backImageView; ///头像 @@ -36,10 +42,14 @@ @property (nonatomic,strong) XPNoteView *noteView; ///房间最小化 @property (nonatomic,strong) RoomInfoModel *roomInfo; +///房间最小化的时候 传入的用户信息 +@property (nonatomic,strong) UserInfoModel *userInfo; ///当前最小化的房间的uid 如果没有的话 为nil @property (nonatomic,strong) NSString *currentRoomUid; ///房间的id @property (nonatomic,strong) NSString *currentRoomId; +///麦序 +@property (nonatomic, strong) NSMutableDictionary *micQueue; @end @@ -57,6 +67,8 @@ - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { + [[NIMSDK sharedSDK].chatroomManager addDelegate:self]; + [[NIMSDK sharedSDK].chatManager addDelegate:self]; [self initEvents]; [self initSubViews]; [self initSubViewConstraints]; @@ -64,11 +76,13 @@ return self; } #pragma mark - Public Method -- (void)configRoomMiniView:(RoomInfoModel *)roomInfo { +- (void)configRoomMiniView:(RoomInfoModel *)roomInfo userInfo:(UserInfoModel *)userInfo micQueue:(NSMutableDictionary *)micQueue { self.hidden = NO; self.currentRoomUid = [NSString stringWithFormat:@"%ld", roomInfo.uid]; self.currentRoomId = [NSString stringWithFormat:@"%ld", roomInfo.roomId]; self.roomInfo = roomInfo; + self.userInfo = userInfo; + self.micQueue = micQueue; [self startAvatarAnimation]; [self.noteView startAnimation]; } @@ -78,6 +92,9 @@ [self.noteView stopAnimation]; self.currentRoomUid = nil; self.currentRoomId = nil; + self.roomInfo = nil; + self.userInfo = nil; + self.micQueue = nil; self.hidden = YES; } @@ -145,6 +162,177 @@ rotationAnimation.removedOnCompletion = NO; [self.avatarImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } +#pragma mark - NIMChatroomManagerDelegate +- (void)chatroomBeKicked:(NIMChatroomBeKickedResult *)result { + if (result.reason == 2) { + [XCHUDTool showErrorWithMessage:@"您被管理员踢出直播间"]; + } else if (result.reason == 5) { + [XCHUDTool showErrorWithMessage:@"您已被管理员拉黑"]; + } + [[RtcManager instance] exitRoom]; + [self hiddenRoomMiniView]; +} +#pragma mark - NIMChatManagerDelegate +- (void)onRecvMessages:(NSArray *)messages { + for (NIMMessage * message in messages) { + // 非房间内消息不处理 + if (message.session.sessionType != NIMSessionTypeChatroom) { + continue; + } + + // 非本房间不处理 + if (![message.session.sessionId isEqualToString:[NSString stringWithFormat:@"%ld", self.roomInfo.roomId]]) { + continue; + } + + if (message.messageType == NIMMessageTypeNotification) { + NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject; + NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content; + switch (content.eventType) { + case NIMChatroomEventTypeInfoUpdated: // 麦序状态更新 + { + NSDictionary *data = [content.notifyExt toJSONObject]; + int type = [data[@"type"] intValue]; + switch (type) { + case 1: + break; + case 2: + case 3: + [self handleNIMNotificationMessage:content]; + break; + } + } + default: + break; + } + } else if (message.messageType == NIMMessageTypeCustom) { + NIMCustomObject *obj = (NIMCustomObject *)message.messageObject; + if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) { + [self handleNIMCustomMessage:obj.attachment]; + } + } + } +} + +- (void)handleNIMNotificationMessage:(NIMChatroomNotificationContent *)content { + BOOL microQueueChanged = NO; + switch (content.eventType) { + case NIMChatroomEventTypeInfoUpdated: // 麦序状态更新 + { + NSDictionary *data = [content.notifyExt toJSONObject]; + int type = [data[@"type"] intValue]; + NSArray* microStates; + switch (type) { + case 2: + microStates = @[[MicroStateModel modelWithJSON:data[@"micInfo"]]]; + break; + case 3: + microStates = [MicroStateModel modelsWithArray:data[@"micInfo"]]; + break; + } + if (microStates && microStates.count > 0) { + for (MicroStateModel *microState in microStates) { + MicroQueueModel *micSequence = [self.micQueue objectForKey:[NSString stringWithFormat:@"%d", microState.position]]; + micSequence.microState = microState; + } + microQueueChanged = YES; + } + } + break; + + case NIMChatroomEventTypeQueueChange: // 麦序上下麦 + { + NSDictionary* data = (NSDictionary *)content.ext; + NSString* position = [data objectForKey:NIMChatroomEventInfoQueueChangeItemKey]; + UserInfoModel* userInfo = [UserInfoModel modelWithJSON:[data objectForKey:NIMChatroomEventInfoQueueChangeItemValueKey]]; + NSInteger changeType = [data[NIMChatroomEventInfoQueueChangeTypeKey] integerValue]; + + // 先清除该用户旧的麦位 + for (MicroQueueModel *sequence in self.micQueue.allValues) { + if (userInfo.uid == sequence.userInfo.uid) { + sequence.userInfo = nil; + } + } + + if (changeType == 1) { // 上麦 + MicroQueueModel *sequence = [self.micQueue objectForKey:position]; + sequence.userInfo = userInfo; + } + + microQueueChanged = YES; + } + break; + case NIMChatroomEventTypeExit: + case NIMChatroomEventTypeKicked: + { + for (NIMChatroomNotificationMember *member in content.targets) { + for (MicroQueueModel *sequence in self.micQueue.allValues) { + if (member.userId.integerValue == sequence.userInfo.uid) { + sequence.userInfo = nil; + microQueueChanged = YES; + } + } + } + } + break; + default: + break; + } + + if (microQueueChanged) { + [self microQueueUpdated]; + } +} + +- (void)microQueueUpdated { + BOOL selfNeedBroadcast = NO; + for (int i = 0; i < self.micQueue.allKeys.count; i++) { + MicroQueueModel * model = [self.micQueue objectForKey:[NSString stringWithFormat:@"%d", i]]; + if (self.userInfo.uid && model.userInfo.uid == self.userInfo.uid) { + selfNeedBroadcast = model.microState.micState == MicroMicStateType_Open; + } + } + [[RtcManager instance] broadcast:selfNeedBroadcast]; +} + +- (void)handleNIMCustomMessage:(AttachmentModel *)attachment { + if(attachment.first == CustomMessageType_Queue && attachment.second == Custom_Message_Sub_Queue_Invite) { + NSDictionary *dic = attachment.data; + + NSString *uid = dic[@"uid"]; + if (uid.integerValue == self.userInfo.uid) { + NSNumber *micPosition = dic[@"micPosition"]; + NSString *position = micPosition.stringValue; + + MicroQueueModel *micro = [self.micQueue objectForKey:position]; + if (!micro || micro.userInfo) return; // 当前麦位有人,什么都不做。 + + RoomInfoModel* roomInfo = self.roomInfo; + NSString* roomId = [NSString stringWithFormat:@"%ld", roomInfo.roomId]; + UserInfoModel* userInfo = self.userInfo; + + NIMChatroomQueueUpdateRequest *request = [[NIMChatroomQueueUpdateRequest alloc]init]; + request.key = position; + request.value = [userInfo toJSONString]; + request.roomId = roomId; + request.transient = YES; + + [[NIMSDK sharedSDK].chatroomManager updateChatroomQueueObject:request completion:^(NSError * _Nullable error) { + if (error) return; + [RtcManager instance].localMuted = YES; + TTAlertConfig *config = [[TTAlertConfig alloc] init]; + config.title = @"房主或管理员拉你上麦"; + config.message = @"你已被房主或管理员拉上麦,但并未开启麦克风。如需要说话,请打开麦克风。"; + [TTPopup alertWithConfig:config confirmHandler:^{ + + } cancelHandler:^{ + + }]; + }]; + } + } +} + #pragma mark - Event Response @@ -157,7 +345,6 @@ - (void)enterRomRecognizer:(UITapGestureRecognizer *)tap { if (self.currentRoomUid > 0 && self.controller) { [XPRoomViewController openRoom:self.currentRoomUid viewController:self.controller]; - [self hiddenRoomMiniView]; } } @@ -209,12 +396,8 @@ self.center = newCenter; } }]; - } else { - NSLog(@"pan state : %zd", p.state); } } - - #pragma mark - Getters And Setters - (void)setRoomInfo:(RoomInfoModel *)roomInfo { _roomInfo = roomInfo; diff --git a/xplan-ios/Main/Room/View/XPRoomViewController.m b/xplan-ios/Main/Room/View/XPRoomViewController.m index 53ea5561..0f01c671 100644 --- a/xplan-ios/Main/Room/View/XPRoomViewController.m +++ b/xplan-ios/Main/Room/View/XPRoomViewController.m @@ -207,7 +207,7 @@ [self dismissViewControllerAnimated:YES completion:nil]; } -#pragma mark - NIMChatManagerDelegate +#pragma mark - NIMChatroomManagerDelegate - (void)chatroomBeKicked:(NIMChatroomBeKickedResult *)result { if (result.reason == 2) { [self showErrorToast:@"您被管理员踢出直播间"]; @@ -215,7 +215,7 @@ [self showErrorToast:@"您已被管理员拉黑"]; } [[RtcManager instance] exitRoom]; - [self enterRoomFail]; + [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - NIMChatManagerDelegate @@ -280,7 +280,7 @@ } - (void)miniRoom { - [[XPMiniRoomView shareMiniRoomView] configRoomMiniView:self.roomInfo]; + [[XPMiniRoomView shareMiniRoomView] configRoomMiniView:self.roomInfo userInfo:self.userInfo micQueue:[self getMicroQueue]]; [self.view endEditing:YES]; [self dismissViewControllerAnimated:YES completion:nil]; }