Files
real-e-party-iOS/YuMi/Modules/YMRoom/Manager/PublicRoomManager.m
edwinQQQ a35a711be6 chore: Initial clean commit
- Removed YuMi/Library/ (138 MB, not tracked)
- Removed YuMi/Resources/ (23 MB, not tracked)
- Removed old version assets (566 files, not tracked)
- Excluded Pods/, xcuserdata/ and other build artifacts
- Clean repository optimized for company server deployment
2025-10-09 16:19:14 +08:00

326 lines
10 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// PublicRoomManager.m
// YUMI
//
// Created by YUMI on 2024/12/19.
//
#import "PublicRoomManager.h"
#import "UserInfoModel.h"
#import "ClientConfig.h"
#import "AccountModel.h"
#import "AccountInfoStorage.h"
#import "XPMessageRemoteExtModel.h"
#import "AttachmentModel.h"
#import "YUMIConstant.h"
#import "XPSkillCardPlayerManager.h"
@interface PublicRoomManager () <NIMChatroomManagerDelegate, NIMChatManagerDelegate>
@property (nonatomic, assign) BOOL isInitialized;
@property (nonatomic, assign) BOOL isInPublicRoom;
@property (nonatomic, copy) NSString *currentPublicRoomId;
@property (nonatomic, copy) NSString *currentUserId;
@property (nonatomic, strong) UserInfoModel *userInfo;
@end
@implementation PublicRoomManager
#pragma mark - 单例方法
+ (instancetype)sharedManager {
static dispatch_once_t onceToken;
static PublicRoomManager *instance;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
[instance initialize];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_isInitialized = NO;
_isInPublicRoom = NO;
_currentPublicRoomId = nil;
_currentUserId = nil;
_userInfo = nil;
}
return self;
}
#pragma mark - 生命周期管理
- (void)initialize {
// 防止重复初始化
if (self.isInitialized) {
NSLog(@"PublicRoomManager: 已经初始化,跳过重复初始化");
return;
}
// 注册云信代理
[[NIMSDK sharedSDK].chatManager addDelegate:self];
[[NIMSDK sharedSDK].chatroomManager addDelegate:self];
// 标记为已初始化
self.isInitialized = YES;
}
- (BOOL)checkConfigPublicRoomID:(NSString *)partitionId {
ClientDataModel *configInfo = [ClientConfig shareConfig].configInfo;
if (!configInfo || !configInfo.publicChatRoomIdMap) {
NSLog(@"PublicRoomManager: 配置信息未加载,等待配置更新");
return NO;
}
self.currentPublicRoomId = [configInfo.publicChatRoomIdMap objectForKey:partitionId];
return [NSString isEmpty:self.currentPublicRoomId];
}
- (void)reset {
NSLog(@"PublicRoomManager: 开始重置");
// 退出公共房间(同步等待完成)
if (self.isInPublicRoom && self.currentPublicRoomId) {
[self exitPublicRoomWithCompletion:^(NSError * _Nullable error) {
if (error) {
NSLog(@"PublicRoomManager: 退出公共房间失败: %@", error);
} else {
NSLog(@"PublicRoomManager: 退出公共房间成功");
self.currentUserId = nil;
self.userInfo = nil;
NSLog(@"PublicRoomManager: 重置完成");
}
}];
} else {
NSLog(@"PublicRoomManager: 不在房间,不处理");
}
// 重置状态
self.isInPublicRoom = NO;
self.currentPublicRoomId = nil;
}
#pragma mark - 状态查询
- (BOOL)isInitialized {
return _isInitialized;
}
- (BOOL)isInPublicRoom {
return _isInPublicRoom;
}
- (NSString *)currentPublicRoomId {
return _currentPublicRoomId;
}
#pragma mark - 私有方法
- (void)tryEnterPublicRoom {
if (!self.isInitialized) {
return;
}
if (!self.userInfo) {
return;
}
// 获取公共房间ID
NSString *partitionId = self.userInfo.partitionId;
ClientDataModel *configInfo = [ClientConfig shareConfig].configInfo;
NSDictionary *publicChatRoomIdMap = configInfo.publicChatRoomIdMap;
if (!publicChatRoomIdMap) {
return;
}
if (!partitionId) {
return;
}
NSNumber *roomId = publicChatRoomIdMap[partitionId];
if (!roomId) {
return;
}
// 进入公共房间
[self enterPublicRoomWithRoomId:roomId.stringValue completion:^(NSError * _Nullable error) {
if (error) {
NSLog(@"PublicRoomManager: 进入公共房间失败: %@", error);
} else {
NSLog(@"PublicRoomManager: 进入公共房间成功房间ID: %@", roomId.stringValue);
}
}];
}
- (void)enterPublicRoomWithRoomId:(NSString *)roomId completion:(void(^)(NSError * _Nullable error))completion {
if (!self.userInfo) {
NSError *error = [NSError errorWithDomain:@"PublicRoomManager"
code:-1
userInfo:@{NSLocalizedDescriptionKey: @"用户信息缺失 nnn"}];
if (completion) {
completion(error);
}
return;
}
// 创建进房请求
NIMChatroomEnterRequest *request = [[NIMChatroomEnterRequest alloc] init];
request.roomId = roomId;
// 设置扩展信息
XPMessageRemoteExtModel *extModel = [[XPMessageRemoteExtModel alloc] init];
extModel.defUser = self.userInfo.defUser;
extModel.erbanNo = self.userInfo.erbanNo;
extModel.carName = self.userInfo.carName;
extModel.inRoomNameplatePic = self.userInfo.nameplatePic;
extModel.inRoomNameplateWord = self.userInfo.nameplateWord;
extModel.isCustomWord = self.userInfo.isCustomWord;
extModel.charmUrl = self.userInfo.userLevelVo.charmUrl;
extModel.experLevelSeq = self.userInfo.userLevelVo.experLevelSeq;
extModel.experUrl = self.userInfo.userLevelVo.experUrl;
extModel.newUser = self.userInfo.newUser;
extModel.vipIcon = self.userInfo.userVipInfoVO.nameplateUrl;
extModel.iosBubbleUrl = self.userInfo.iosBubbleUrl;
extModel.androidBubbleUrl = self.userInfo.androidBubbleUrl;
extModel.enterHide = self.userInfo.userVipInfoVO.enterHide;
extModel.preventKick = self.userInfo.userVipInfoVO.preventKick;
extModel.enterRoomEffects = self.userInfo.userVipInfoVO.enterRoomEffects;
extModel.gender = self.userInfo.gender;
extModel.fromSayHelloChannel = self.userInfo.fromSayHelloChannel;
extModel.platformRole = self.userInfo.platformRole;
extModel.nick = self.userInfo.nick;
NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithObject:extModel.model2dictionary
forKey:[NSString stringWithFormat:@"%ld", self.userInfo.uid]];
request.roomExt = [ext toJSONString];
request.retryCount = 3;
// 进入房间
@kWeakify(self);
[[NIMSDK sharedSDK].chatroomManager enterChatroom:request completion:^(NSError * _Nullable error, NIMChatroom * _Nullable chatroom, NIMChatroomMember * _Nullable me) {
@kStrongify(self);
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
if (completion) {
completion(error);
}
} else {
self.isInPublicRoom = YES;
self.currentPublicRoomId = roomId;
if (completion) {
completion(nil);
}
}
});
}];
}
#pragma mark - 手动控制
- (void)enterPublicRoomWithCompletion:(void(^)(NSError * _Nullable error))completion {
if (!self.isInitialized) {
NSError *error = [NSError errorWithDomain:@"PublicRoomManager"
code:-2
userInfo:@{NSLocalizedDescriptionKey: @"管理器未初始化"}];
if (completion) {
completion(error);
}
} else if (self.isInPublicRoom) {
if (completion) {
completion(nil);
}
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self tryEnterPublicRoom];
});
}
}
- (void)exitPublicRoomWithCompletion:(void(^)(NSError * _Nullable error))completion {
if (!self.isInPublicRoom || !self.currentPublicRoomId) {
NSLog(@"PublicRoomManager: 未在公共房间中");
if (completion) {
completion(nil);
}
return;
}
NSLog(@"PublicRoomManager: room id-%@ 正在尝试退出公共房间", self.currentPublicRoomId);
@kWeakify(self);
[[NIMSDK sharedSDK].chatroomManager exitChatroom:self.currentPublicRoomId completion:^(NSError * _Nullable error) {
@kStrongify(self);
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
// NSLog(@"PublicRoomManager: 退出公共房间失败: %@", error);
} else {
// NSLog(@"PublicRoomManager: 退出公共房间成功");
self.isInPublicRoom = NO;
self.currentPublicRoomId = nil;
}
if (completion) {
completion(error);
}
});
}];
}
#pragma mark - 用户信息更新处理
- (void)updateUserInfo:(UserInfoModel *)userInfo {
if (!userInfo) {
return;
}
if (!userInfo.partitionId) {
return;
}
// 检查用户是否切换
if (self.currentUserId && ![self.currentUserId isEqualToString:[NSString stringWithFormat:@"%ld", userInfo.uid]]) {
[self reset];
}
self.userInfo = userInfo;
self.currentUserId = [NSString stringWithFormat:@"%ld", userInfo.uid];
}
#pragma mark - NIMChatRoomManagerDelegate
#define ConnectionStateStyleString(enum) \
[@[@"Entering", @"EnterOK", @"EnterFailed", @"LoseConnection"] objectAtIndex:(enum)]
- (void)chatroom:(NSString *)roomId connectionStateChanged:(NIMChatroomConnectionState)state {
NSLog(@"PublicRoomManager 房间连接状态: %@", ConnectionStateStyleString(state));
}
#pragma mark - NIMChatManagerDelegate
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages {
// 只处理公共房间的消息
for (NIMMessage *message in messages) {
if ([message.session.sessionId isEqualToString:self.currentPublicRoomId]) {
[self handleMessageWithAttachmentAndFirstSecond:message];
}
}
}
- (void)handleMessageWithAttachmentAndFirstSecond:(NIMMessage *)message {
// 只有用户在房间时,才会转发
if (![XPSkillCardPlayerManager shareInstance].isInRoom) {
// NSLog(@"PublicRoomManager: 用户未在房间中,跳过消息转发");
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"MessageFromPublicRoomWithAttachmentNotification"
object:message];
}
@end