IMAPlatform.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // IMAPlatform.m
  3. // TIMAdapter
  4. //
  5. // Created by AlexiChen on 16/2/17.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMAPlatform.h"
  9. @interface IMAPlatform ()<V2TIMSDKListener>
  10. @property (nonatomic, assign) TCQALNetwork networkType;
  11. @end
  12. @implementation IMAPlatform
  13. #define kIMAPlatformConfig @"IMAPlatformConfig"
  14. static IMAPlatform *_sharedInstance = nil;
  15. + (instancetype)configWith:(IMAPlatformConfig *)cfg
  16. {
  17. static dispatch_once_t predicate;
  18. dispatch_once(&predicate, ^{
  19. _sharedInstance = [[IMAPlatform alloc] init];
  20. [_sharedInstance configIMSDK:cfg];
  21. });
  22. return _sharedInstance;
  23. }
  24. static Class kHostClass = Nil;
  25. + (void)configHostClass:(Class)hostcls
  26. {
  27. if (![hostcls isSubclassOfClass:[IMAHost class]])
  28. {
  29. DebugLog(@"%@ 必须是IMAHost的子类型", hostcls);
  30. }
  31. else
  32. {
  33. kHostClass = hostcls;
  34. }
  35. }
  36. + (instancetype)sharedInstance
  37. {
  38. // TODO:
  39. return _sharedInstance;
  40. }
  41. #define kIMAPlatform_AutoLogin_Key @"kIMAPlatform_AutoLogin_Key"
  42. + (BOOL)isAutoLogin
  43. {
  44. NSNumber *num = [[NSUserDefaults standardUserDefaults] objectForKey:@"kIMAPlatform_AutoLogin_Key"];
  45. return [num boolValue];
  46. }
  47. + (void)setAutoLogin:(BOOL)autologin
  48. {
  49. [[NSUserDefaults standardUserDefaults] setObject:@(autologin) forKey:kIMAPlatform_AutoLogin_Key];
  50. }
  51. //
  52. //- (void)initialFriendProxy:(TIMSucc)succ fail:(TIMFail)fail
  53. //{
  54. // // 说明登录成功
  55. //// _friendShipMgr = [[TIMFriendshipManager sharedInstance] getFriendshipProxy];
  56. //// [_friendShipMgr SetProxyListener:self];
  57. //// [_friendShipMgr SyncWithFlags:0XFF custom:nil succ:succ fail:fail];
  58. //
  59. //
  60. //}
  61. - (IMAContactManager *)contactMgr
  62. {
  63. if (!_contactMgr)
  64. {
  65. _contactMgr = [[IMAContactManager alloc] init];
  66. }
  67. return _contactMgr;
  68. }
  69. - (IMAConversationManager *)conversationMgr
  70. {
  71. if (!_conversationMgr)
  72. {
  73. _conversationMgr = [[IMAConversationManager alloc] init];
  74. }
  75. return _conversationMgr;
  76. }
  77. - (IMAUser *)getReceiverOf:(IMAConversation *)conv
  78. {
  79. NSString *receiver = [conv receiver];
  80. if (conv.type == TIM_C2C)
  81. {
  82. return [self.contactMgr getUserByUserId:receiver];
  83. }
  84. else if (conv.type == TIM_GROUP)
  85. {
  86. // 查询群列表
  87. return [self.contactMgr getUserByGroupId:receiver];
  88. }
  89. else
  90. {
  91. DebugLog(@"不支持的会话模式");
  92. return nil;
  93. }
  94. }
  95. - (void)configIMSDK:(IMAPlatformConfig *)cfg
  96. {
  97. TIMManager *manager = [TIMManager sharedInstance];
  98. [manager setEnv:cfg.environment];
  99. TIMSdkConfig *config = [[TIMSdkConfig alloc] init];
  100. config.sdkAppId = [TXYSdkAppId intValue] ;
  101. config.accountType = TXYSdkAccountType;
  102. config.disableCrashReport = NO;
  103. config.connListener = self;
  104. config.logLevel = TIM_LOG_NONE;
  105. [manager initSdk:config];
  106. TIMUserConfig *userConfig = [[TIMUserConfig alloc] init];
  107. // userConfig.disableStorage = YES;//禁用本地存储(加载消息扩展包有效)
  108. // userConfig.disableAutoReport = YES;//禁止自动上报(加载消息扩展包有效)
  109. // userConfig.enableReadReceipt = YES;//开启C2C已读回执(加载消息扩展包有效)
  110. userConfig.disableRecnetContact = NO;//不开启最近联系人(加载消息扩展包有效)
  111. userConfig.disableRecentContactNotify = YES;//不通过onNewMessage:抛出最新联系人的最后一条消息(加载消息扩展包有效)
  112. userConfig.enableFriendshipProxy = YES;//开启关系链数据本地缓存功能(加载好友扩展包有效)
  113. userConfig.enableGroupAssistant = YES;//开启群组数据本地缓存功能(加载群组扩展包有效)
  114. TIMGroupInfoOption *giOption = [[TIMGroupInfoOption alloc] init];
  115. giOption.groupFlags = 0xffffff;//需要获取的群组信息标志(TIMGetGroupBaseInfoFlag),默认为0xffffff
  116. giOption.groupCustom = nil;//需要获取群组资料的自定义信息(NSString*)列表
  117. userConfig.groupInfoOpt = giOption;//设置默认拉取的群组资料
  118. TIMGroupMemberInfoOption *gmiOption = [[TIMGroupMemberInfoOption alloc] init];
  119. gmiOption.memberFlags = 0xffffff;//需要获取的群成员标志(TIMGetGroupMemInfoFlag),默认为0xffffff
  120. gmiOption.memberCustom = nil;//需要获取群成员资料的自定义信息(NSString*)列表
  121. userConfig.groupMemberInfoOpt = gmiOption;//设置默认拉取的群成员资料
  122. TIMFriendProfileOption *fpOption = [[TIMFriendProfileOption alloc] init];
  123. fpOption.friendFlags = 0xffffff;//需要获取的好友信息标志(TIMProfileFlag),默认为0xffffff
  124. fpOption.friendCustom = nil;//需要获取的好友自定义信息(NSString*)列表
  125. fpOption.userCustom = nil;//需要获取的用户自定义信息(NSString*)列表
  126. userConfig.friendProfileOpt = fpOption;//设置默认拉取的好友资料
  127. userConfig.userStatusListener = self;//用户登录状态监听器
  128. userConfig.refreshListener = self;//会话刷新监听器(未读计数、已读同步)(加载消息扩展包有效)
  129. // userConfig.receiptListener = self;//消息已读回执监听器(加载消息扩展包有效)
  130. // userConfig.messageUpdateListener = self;//消息svr重写监听器(加载消息扩展包有效)
  131. // userConfig.uploadProgressListener = self;//文件上传进度监听器
  132. // userConfig.groupEventListener todo
  133. userConfig.messgeRevokeListener = self.conversationMgr;
  134. userConfig.friendshipListener = self;//关系链数据本地缓存监听器(加载好友扩展包、enableFriendshipProxy有效)
  135. userConfig.groupListener = self;//群组据本地缓存监听器(加载群组扩展包、enableGroupAssistant有效)
  136. [manager setUserConfig:userConfig];
  137. // 1. 从 IM 控制台获取应用 SDKAppID,详情请参考 SDKAppID。
  138. // 2. 初始化 config 对象
  139. V2TIMSDKConfig *config2 = [[V2TIMSDKConfig alloc] init];
  140. // 3. 指定 log 输出级别,详情请参考 [SDKConfig](#SDKAppID)。
  141. config2.logLevel = V2TIM_LOG_INFO;
  142. // 4. 初始化 SDK 并设置 V2TIMSDKListener 的监听对象。
  143. // initSDK 后 SDK 会自动连接网络,网络连接状态可以在 V2TIMSDKListener 回调里面监听。
  144. // [[V2TIMManager sharedInstance] initSDK:TXYSdkAppId.intValue config:config2 listener:self];
  145. [[V2TIMManager sharedInstance] initSDK:TXYSdkAppId.intValue config:config2];
  146. [[V2TIMManager sharedInstance] addIMSDKListener:self];
  147. }
  148. // 5. 监听 V2TIMSDKListener 回调
  149. - (void)onConnecting {
  150. // 正在连接到腾讯云服务器
  151. }
  152. - (void)onConnectSuccess {
  153. // 已经成功连接到腾讯云服务器
  154. }
  155. - (void)onConnectFailed:(int)code err:(NSString*)err {
  156. // 连接腾讯云服务器失败
  157. }
  158. - (void)saveToLocal
  159. {
  160. // 保存上一次联系人列表状态
  161. [_contactMgr saveToLocal];
  162. // 保存Config状态
  163. }
  164. - (void)onLogoutCompletion
  165. {
  166. [self offlineLogin];
  167. self.offlineExitLivingBlock = nil;
  168. [IMAPlatform setAutoLogin:NO];
  169. _host = nil;
  170. #if kIsUseAVSDKAsLiveScene
  171. [TCAVSharedContext destroyContextCompletion:nil];
  172. #endif
  173. }
  174. - (void)offlineLogin
  175. {
  176. // 被踢下线,则清空单例中的数据,再登录后再重新创建
  177. [self saveToLocal];
  178. _contactMgr = nil;
  179. [[TIMManager sharedInstance] removeMessageListener:_conversationMgr];
  180. _conversationMgr = nil;
  181. }
  182. - (void)logout:(TIMLoginSucc)succ fail:(TIMFail)fail
  183. {
  184. __weak IMAPlatform *ws = self;
  185. [[TIMManager sharedInstance] logout:^{
  186. [ws onLogoutCompletion];
  187. if (succ)
  188. {
  189. succ();
  190. }
  191. } fail:^(int code, NSString *err) {
  192. [ws onLogoutCompletion];
  193. if (fail)
  194. {
  195. fail(code, err);
  196. }
  197. }];
  198. }
  199. - (IMAPlatformConfig *)localConfig
  200. {
  201. return _host.loginParm.config;
  202. }
  203. - (void)configHost:(TIMLoginParam *)param
  204. {
  205. if (!_host)
  206. {
  207. if (kHostClass == Nil)
  208. {
  209. kHostClass = [IMAHost class];
  210. }
  211. _host = [[kHostClass alloc] init];
  212. }
  213. _host.loginParm = param;
  214. [_host getMyInfo:nil];
  215. //[_host asyncProfile];
  216. #if kIsUseAVSDKAsLiveScene
  217. [TCAVSharedContext configWithStartedContext:_host completion:nil];
  218. #endif
  219. }
  220. - (void)changeToNetwork:(TCQALNetwork)work
  221. {
  222. if (work > EQALNetworkType_ReachableViaWWAN)
  223. {
  224. // 不处理这些
  225. work = EQALNetworkType_ReachableViaWWAN;
  226. }
  227. DebugLog(@"网络切换到(-1:未知 0:无网 1:wifi 2:移动网):%d", work);
  228. // if (work != _networkType)
  229. // {
  230. self.networkType = work;
  231. // }
  232. }
  233. // 判断是否主播
  234. + (BOOL)isHost:(NSString *)userId
  235. {
  236. if (![BGUtils isBlankString:userId])
  237. {
  238. if ([[[IMAPlatform sharedInstance].host imUserId] isEqualToString:userId])
  239. {
  240. return YES;
  241. }
  242. else
  243. {
  244. return NO;
  245. }
  246. }
  247. else
  248. {
  249. return NO;
  250. }
  251. }
  252. @end