IMAPlatform+IMSDKCallBack.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. //
  2. // IMAPlatform+IMSDKCallBack.m
  3. // TIMChat
  4. //
  5. // Created by AlexiChen on 16/2/26.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMAPlatform+IMSDKCallBack.h"
  9. #import "BogoNetworkKit.h"
  10. // 所有回调中的核心逻辑最终都放入到主线程中执行
  11. @implementation IMAPlatform (IMSDKCallBack)
  12. #pragma mark - TIMConnListener
  13. /**
  14. * 网络连接成功
  15. */
  16. - (void)onConnSucc
  17. {
  18. self.isConnected = YES;
  19. TCQALNetwork net = [[QalSDKProxy sharedInstance] getNetType];
  20. [self changeToNetwork:net];
  21. [self.conversationMgr onConnect];
  22. }
  23. /**
  24. * 网络连接失败
  25. *
  26. * @param code 错误码
  27. * @param err 错误描述
  28. */
  29. - (void)onConnFailed:(int)code err:(NSString*)err
  30. {
  31. self.isConnected = NO;
  32. [self.conversationMgr onDisConnect];
  33. DebugLog(@"网络连接失败");
  34. }
  35. /**
  36. * 网络连接断开
  37. *
  38. * @param code 错误码
  39. * @param err 错误描述
  40. */
  41. - (void)onDisconnect:(int)code err:(NSString*)err
  42. {
  43. self.isConnected = NO;
  44. [self.conversationMgr onDisConnect];
  45. DebugLog(@"网络连接断开 code = %d, err = %@", code, err);
  46. }
  47. /**
  48. * 连接中
  49. */
  50. - (void)onConnecting
  51. {
  52. DebugLog(@"连接中");
  53. }
  54. - (void)doLogout
  55. {
  56. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  57. // 然后退出登录
  58. [self logout:^{
  59. #if kSupportH5Shopping
  60. kNotifPost(@"H5qiut", nil); //需要告诉H5 那边退出
  61. [[AppDelegate sharedAppDelegate] beginEnterMianUI];
  62. #else
  63. [[AppDelegate sharedAppDelegate] enterLoginUI];
  64. #endif
  65. } fail:^(int code, NSString *msg) {
  66. #if kSupportH5Shopping
  67. kNotifPost(@"H5qiut", nil); //需要告诉H5 那边退出
  68. [[AppDelegate sharedAppDelegate] beginEnterMianUI];
  69. #else
  70. [[AppDelegate sharedAppDelegate] enterLoginUI];
  71. #endif
  72. }];
  73. });
  74. }
  75. //- (void)registNotification
  76. //{
  77. // if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
  78. // {
  79. // [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
  80. // [[UIApplication sharedApplication] registerForRemoteNotifications];
  81. // }
  82. // else
  83. // {
  84. // [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
  85. // }
  86. //}
  87. #pragma mark - TIMUserStatusListener
  88. /**
  89. * 踢下线通知
  90. */
  91. static BOOL kIsAlertingForceOffline = NO;
  92. - (void)onForceOffline
  93. {
  94. if (!kIsAlertingForceOffline)
  95. {
  96. [[NSNotificationCenter defaultCenter] postNotificationName:KLOGIN_OUT_Notification object:nil];
  97. [[AppDelegate sharedAppDelegate] popToRootViewController];
  98. kIsAlertingForceOffline = YES;
  99. DebugLog(@"踢下线通知");
  100. FWWeakify(self)
  101. [IMAPlatform setAutoLogin:NO];
  102. [FanweMessage alert:ASLocalizedString(@"下线通知") message:ASLocalizedString(@"您的账号在其他设备登录,如果不是您的操作,请及时修改密码") isHideTitle:NO destructiveAction:^{
  103. FWStrongify(self)
  104. // 退出
  105. [self logout:^{
  106. [[AppDelegate sharedAppDelegate] enterLoginUI];
  107. } fail:^(int code, NSString *msg) {
  108. [[AppDelegate sharedAppDelegate] enterLoginUI];
  109. }];
  110. }];
  111. // [FanweMessage alert:@"下线通知" message:@"您的账号在其他设备登录,如果不是您的操作,请及时修改密码" destructiveAction:^{
  112. //
  113. //
  114. // FWStrongify(self)
  115. // // 退出
  116. // [self logout:^{
  117. // [[AppDelegate sharedAppDelegate] enterLoginUI];
  118. // } fail:^(int code, NSString *msg) {
  119. // [[AppDelegate sharedAppDelegate] enterLoginUI];
  120. // }];
  121. //
  122. // kIsAlertingForceOffline = NO;
  123. //
  124. //
  125. // } cancelAction:^{
  126. //
  127. //
  128. // FWStrongify(self)
  129. //
  130. //
  131. //
  132. // [IMAPlatform setAutoLogin:YES];
  133. // [self offlineLogin];
  134. // // 重新登录
  135. // [self login:self.host.loginParm succ:^{
  136. //
  137. // [[AppDelegate sharedAppDelegate] enterMainUI];
  138. //
  139. //
  140. // [self registNotification];
  141. // } fail:^(int code, NSString *msg) {
  142. //// [self onLogoutCompletion];
  143. //// [IMAPlatform setAutoLogin:YES];
  144. // [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, msg) delay:1.0];
  145. //
  146. // [self logout:^{
  147. // [[AppDelegate sharedAppDelegate] enterLoginUI];
  148. // } fail:^(int code, NSString *msg) {
  149. // [[AppDelegate sharedAppDelegate] enterLoginUI];
  150. // }];
  151. //
  152. //
  153. // }];
  154. //
  155. // kIsAlertingForceOffline = NO;
  156. //
  157. // }];
  158. }
  159. }
  160. /**
  161. * 断线重连失败
  162. */
  163. - (void)onReConnFailed:(int)code err:(NSString*)err
  164. {
  165. DebugLog(@"断线重连失败");
  166. FWWeakify(self)
  167. [FanweMessage alert:ASLocalizedString(@"下线通知") message:ASLocalizedString(@"断线重连失败,是否重新登录?") destructiveAction:^{
  168. FWStrongify(self)
  169. [self offlineLogin];
  170. // 重新登录
  171. [self loginTIMSDK:self.host.loginParm succ:^{
  172. [[AppDelegate sharedAppDelegate] enterMainUI];
  173. IMALoginParam *wp = [IMALoginParam loadFromLocal];
  174. //[[IMAPlatform sharedInstance] configOnLoginSucc:wp];
  175. // [self registNotification];
  176. } fail:^(int code, NSString *msg) {
  177. [[AppDelegate sharedAppDelegate] enterLoginUI];
  178. }];
  179. kIsAlertingForceOffline = NO;
  180. } cancelAction:^{
  181. // 退出
  182. [[IMAPlatform sharedInstance] logout:^{
  183. [[AppDelegate sharedAppDelegate] enterLoginUI];
  184. } fail:^(int code, NSString *msg) {
  185. [[AppDelegate sharedAppDelegate] enterLoginUI];
  186. }];
  187. kIsAlertingForceOffline = NO;
  188. }];
  189. }
  190. /**
  191. * 用户登录的userSig过期,需要重新登录
  192. */
  193. - (void)onUserSigExpired
  194. {
  195. [[BGHUDHelper sharedInstance] syncLoading];
  196. //刷新票据
  197. [[TLSHelper getInstance] TLSRefreshTicket:[IMAPlatform sharedInstance].host.profile.identifier andTLSRefreshTicketListener:self];
  198. }
  199. - (void)OnRefreshTicketSuccess:(TLSUserInfo *)userInfo
  200. {
  201. [[BGHUDHelper sharedInstance] syncStopLoading];
  202. //更新本地票据
  203. IMALoginParam *param = [IMALoginParam loadFromLocal];
  204. param.userSig = [[TLSHelper getInstance] getTLSUserSig:userInfo.identifier];
  205. param.tokenTime = [[NSDate date] timeIntervalSince1970];
  206. [param saveToLocal];
  207. [IMAPlatform sharedInstance].host.loginParm.userSig = param.userSig;
  208. [[IMAPlatform sharedInstance].host.loginParm saveToLocal];
  209. // 重新登录
  210. [[TIMManager sharedInstance] login:param succ:^{
  211. [IMAPlatform setAutoLogin:YES];
  212. [BogoNetwork shareInstance].uid = param.identifier;
  213. } fail:^(int code, NSString *msg) {
  214. DebugLog(@"TIMLogin Failed: code=%d err=%@", code, msg);
  215. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"刷新票据,登录失败")];
  216. }];
  217. }
  218. - (void)OnRefreshTicketFail:(TLSErrInfo *)errInfo
  219. {
  220. [[BGHUDHelper sharedInstance] syncStopLoading];
  221. NSString *err = [[NSString alloc] initWithFormat:ASLocalizedString(@"刷新票据失败\ncode:%d, error:%@"), errInfo.dwErrorCode, errInfo.sErrorTitle];
  222. DebugLog(@"%@",err);
  223. [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"刷新票据失败,正在退出")];
  224. IMALoginParam *param = [IMALoginParam loadFromLocal];
  225. param.tokenTime = 0;
  226. [param saveToLocal];
  227. [[IMAPlatform sharedInstance] logout:^{
  228. [[BGHUDHelper sharedInstance] syncStopLoading];
  229. [[AppDelegate sharedAppDelegate] enterLoginUI];
  230. } fail:^(int code, NSString *err) {
  231. [[BGHUDHelper sharedInstance] syncStopLoading];
  232. [[AppDelegate sharedAppDelegate] enterLoginUI];
  233. }];
  234. }
  235. - (void)OnRefreshTicketTimeout:(TLSErrInfo *)errInfo
  236. {
  237. [self OnRefreshTicketFail:errInfo];
  238. }
  239. #pragma mark -TIMRefreshListener
  240. - (void)onRefresh
  241. {
  242. // TODO:重新刷新会话列列
  243. DebugLog(@"=========>>>>> 刷新会话列表");
  244. dispatch_async(dispatch_get_main_queue(), ^{
  245. [self.contactMgr asyncConfigContact];//从以前的OnProxyStatusChange里面移动过来的
  246. [self.conversationMgr asyncConversationList];
  247. [[TIMManager sharedInstance] addMessageListener:self.conversationMgr];
  248. });
  249. [self.contactMgr asyncConfigGroup];
  250. }
  251. - (void)onRefreshConversations:(NSArray*)conversations
  252. {
  253. [self.conversationMgr asyncConversationList];
  254. }
  255. @end
  256. @implementation IMAPlatform (FriendShipListener)
  257. /**
  258. * 收到代理状态变更通知
  259. *
  260. * @param status 当前状态
  261. */
  262. //- (void) OnProxyStatusChange:(TIM_FRIENDSHIP_PROXY_STATUS)status
  263. //{
  264. // if (status == TIM_FRIENDSHIP_STATUS_SYNCED)
  265. // {
  266. // dispatch_async(dispatch_get_main_queue(), ^{
  267. // [self.contactMgr asyncConfigContact];
  268. // });
  269. // }
  270. // DebugLog(@"同步状态为:%d", (int)status);
  271. //}
  272. /**
  273. * 添加好友通知
  274. *
  275. * @param users 好友列表(TIMUserProfile*)
  276. */
  277. - (void)onAddFriends:(NSArray *)users
  278. {
  279. NSString *info = [NSString stringWithFormat:@"timchat onaddfriends %@",users];
  280. [[TIMManager sharedInstance] log:TIM_LOG_DEBUG tag:@"111" msg:info];
  281. dispatch_async(dispatch_get_main_queue(), ^{
  282. if (!self.contactMgr.hasNewDependency)
  283. {
  284. self.contactMgr.hasNewDependency = YES;
  285. }
  286. DebugLog(@"%@", users);
  287. for (TIMUserProfile *u in users)
  288. {
  289. IMAUser *user = [[IMAUser alloc] initWithUserInfo:u];
  290. NSString *fg = nil;
  291. if (u.friendGroups.count > 0)
  292. {
  293. fg = u.friendGroups[0];
  294. }
  295. IMASubGroup *sg = [self.contactMgr getSubGroupOf:fg];
  296. [self.contactMgr addUser:user toSubGroup:sg];
  297. }
  298. });
  299. }
  300. /**
  301. * 删除好友通知
  302. *
  303. * @param identifiers 用户id列表(NSString*)
  304. */
  305. - (void)onDelFriends:(NSArray*)identifiers
  306. {
  307. DebugLog(@"%@", identifiers);
  308. dispatch_async(dispatch_get_main_queue(), ^{
  309. if (!self.contactMgr.hasNewDependency)
  310. {
  311. self.contactMgr.hasNewDependency = YES;
  312. }
  313. for (NSString *uid in identifiers)
  314. {
  315. IMAUser *user = [[IMAUser alloc] initWith:uid];
  316. [self.contactMgr removeUser:user];
  317. }
  318. });
  319. }
  320. /**
  321. * 好友资料更新通知
  322. *
  323. * @param profiles 资料列表(TIMUserProfile*)
  324. */
  325. - (void)onFriendProfileUpdate:(NSArray*)profiles
  326. {
  327. DebugLog(@"%@", profiles);
  328. if (!self.contactMgr.hasNewDependency)
  329. {
  330. self.contactMgr.hasNewDependency = YES;
  331. }
  332. }
  333. /**
  334. * 好友申请通知
  335. *
  336. * @param reqs 好友申请者id列表(TIMSNSChangeInfo*)
  337. */
  338. - (void)onAddFriendReqs:(NSArray*)reqs
  339. {
  340. dispatch_async(dispatch_get_main_queue(), ^{
  341. DebugLog(@"%@", reqs);
  342. if (!self.contactMgr.hasNewDependency)
  343. {
  344. self.contactMgr.hasNewDependency = YES;
  345. }
  346. });
  347. }
  348. /**
  349. * 添加好友分组通知
  350. *
  351. * @param friendgroups 好友分组列表(TIMFriendGroup*)
  352. */
  353. - (void)onAddFriendGroups:(NSArray*)friendgroups
  354. {
  355. DebugLog(@"%@", friendgroups);
  356. if (!self.contactMgr.hasNewDependency)
  357. {
  358. self.contactMgr.hasNewDependency = YES;
  359. }
  360. }
  361. /**
  362. * 删除好友分组通知
  363. *
  364. * @param names 好友分组名称列表(NSString*)
  365. */
  366. - (void)onDelFriendGroups:(NSArray*)names
  367. {
  368. DebugLog(@"%@", names);
  369. if (!self.contactMgr.hasNewDependency)
  370. {
  371. self.contactMgr.hasNewDependency = YES;
  372. }
  373. }
  374. /**
  375. * 好友分组更新通知
  376. *
  377. * @param friendgroups 好友分组列表(TIMFriendGroup*)
  378. */
  379. - (void)onFriendGroupUpdate:(NSArray*)friendgroups
  380. {
  381. DebugLog(@"%@", friendgroups);
  382. if (!self.contactMgr.hasNewDependency)
  383. {
  384. self.contactMgr.hasNewDependency = YES;
  385. }
  386. }
  387. @end
  388. @implementation IMAPlatform (GroupAssistantListener)
  389. /**
  390. * 有新用户加入群时的通知回调
  391. *
  392. * @param groupId 群ID
  393. * @param membersInfo 加群用户的群资料(TIMGroupMemberInfo*)列表
  394. */
  395. -(void) onMemberJoin:(NSString *)groupId membersInfo:(NSArray *)membersInfo
  396. {
  397. dispatch_async(dispatch_get_main_queue(), ^{
  398. DebugLog(@"groupId = %@, membersInfo = %@", groupId, membersInfo);
  399. IMAGroup *temp = [[IMAGroup alloc] initWith:groupId];
  400. IMAGroup *group = (IMAGroup *)[[IMAPlatform sharedInstance].contactMgr isContainUser:temp];
  401. if (group)
  402. {
  403. group.groupInfo.memberNum += (uint32_t)membersInfo.count;
  404. }
  405. });
  406. }
  407. /**
  408. * 有群成员退群时的通知回调
  409. *
  410. * @param groupId 群ID
  411. * @param members 退群成员的identifier(NSString*)列表
  412. */
  413. -(void) onMemberQuit:(NSString*)groupId members:(NSArray*)members
  414. {
  415. dispatch_async(dispatch_get_main_queue(), ^{
  416. //DebugLog(@"groupId = %@, membersInfo = %@", groupId, members);
  417. IMAGroup *temp = [[IMAGroup alloc] initWith:groupId];
  418. IMAGroup *group = (IMAGroup *)[[IMAPlatform sharedInstance].contactMgr isContainUser:temp];
  419. if (group)
  420. {
  421. group.groupInfo.memberNum -= (uint32_t)members.count;
  422. }
  423. });
  424. }
  425. /**
  426. * 群成员信息更新的通知回调
  427. *
  428. * @param groupId 群ID
  429. * @param membersInfo 更新后的群成员资料(TIMGroupMemberInfo*)列表
  430. */
  431. -(void) onMemberUpdate:(NSString*)groupId membersInfo:(NSArray*)membersInfo
  432. {
  433. //DebugLog(@"groupId = %@, membersInfo = %@", groupId, membersInfo);
  434. }
  435. /**
  436. * 加入群的通知回调
  437. *
  438. * @param groupInfo 加入群的群组资料
  439. */
  440. -(void) onGroupAdd:(TIMGroupInfo*)groupInfo
  441. {
  442. dispatch_async(dispatch_get_main_queue(), ^{
  443. //DebugLog(@"groupInfo = %@", groupInfo);
  444. IMAUser *group = (IMAGroup *) [self.contactMgr getUserByGroupId:groupInfo.group];
  445. if (!group)
  446. {
  447. IMAGroup *gr = [[IMAGroup alloc] initWithInfo:groupInfo];
  448. [self.contactMgr onAddGroup:gr];
  449. //注:刚加入群时需要刷新一下会话列表,不然会话列表上显示的是群id,而不是群名称
  450. IMAConversation *gc = [self.conversationMgr queryConversationWith:gr];
  451. if (gc)
  452. {
  453. //重新设置一下lastMessage就可以更新整个会话了(lastmessage有kvo监听)
  454. gc.lastMessage = gc.lastMessage;
  455. }
  456. }
  457. });
  458. }
  459. /**
  460. * 解散群的通知回调
  461. *
  462. * @param groupId 解散群的群ID
  463. */
  464. -(void) onGroupDelete:(NSString*)groupId
  465. {
  466. // DebugLog(@"groupInfo = %@", groupId);
  467. dispatch_async(dispatch_get_main_queue(), ^{
  468. IMAUser *group = (IMAGroup *) [self.contactMgr getUserByGroupId:groupId];
  469. if (group)
  470. {
  471. [self.contactMgr removeUser:group];
  472. }
  473. });
  474. }
  475. /**
  476. * 群资料更新的通知回调
  477. *
  478. * @param groupInfo 更新后的群资料信息
  479. */
  480. - (void)onGroupUpdate:(TIMGroupInfo*)groupInfo
  481. {
  482. dispatch_async(dispatch_get_main_queue(), ^{
  483. // DebugLog(@"groupInfo = %@", groupInfo);
  484. IMAGroup *group = (IMAGroup *) [self.contactMgr getUserByGroupId:groupInfo.group];
  485. if (group)
  486. {
  487. [group changeGroupInfo:groupInfo];
  488. }
  489. });
  490. }
  491. @end