YOUPAILCIMLoginManager.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // YOUPAILCIMLoginManager.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/9/1.
  6. // Copyright © 2018年 caicai. All rights reserved.
  7. //
  8. #import "YOUPAILCIMLoginManager.h"
  9. #import "YOUPAILCCellLayoutconfig.h"
  10. #import "YOUPAILCCustomAttachmentDecoder.h"
  11. @interface YOUPAILCIMLoginManager()<NIMLoginManagerDelegate>
  12. @property(nonatomic,strong)NSString* account;
  13. @property(nonatomic,strong)NSString* token;
  14. @property(nonatomic,assign)NSInteger repeatNum;
  15. @end
  16. @implementation YOUPAILCIMLoginManager
  17. + (instancetype)sharedCenter
  18. {
  19. static YOUPAILCIMLoginManager *instance = nil;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. instance = [[YOUPAILCIMLoginManager alloc] init];
  23. instance.repeatNum = 3;
  24. });
  25. return instance;
  26. }
  27. - (instancetype)init {
  28. self = [super init];
  29. if(self) {
  30. [[[NIMSDK sharedSDK] loginManager] addDelegate:self];
  31. }
  32. return self;
  33. }
  34. - (void)dealloc{
  35. [[[NIMSDK sharedSDK] loginManager] removeDelegate:self];
  36. }
  37. - (void)youpaifNIMinit:(BOOL)isDistribution{
  38. if (isDistribution) {
  39. [[NIMSDK sharedSDK] registerWithAppID:NIMSDKID
  40. cerName:@"DongerAPNS-PRD"];//生产推送证书 // 正式
  41. } else {
  42. [[NIMSDK sharedSDK] registerWithAppID:NIMSDKID
  43. cerName:@"DongerAPNS-DEV"];//开发推送证书 // 测试
  44. }
  45. #if DEBUG
  46. [NIMSDK.sharedSDK enableConsoleLog];
  47. #endif
  48. //设置头像为http格式
  49. [NIMSDKConfig sharedConfig].enabledHttpsForInfo = NO;
  50. [NIMCustomObject registerCustomDecoder:[YOUPAILCCustomAttachmentDecoder new]];//注册自定义消息解析器
  51. //注册 NIMKit 自定义排版配置
  52. [[NIMKit sharedKit] registerLayoutConfig:[[YOUPAILCCellLayoutConfig alloc]init]];
  53. }
  54. - (void)youpaifupdateDeviceToken:(NSData*)deviceToken{
  55. [[NIMSDK sharedSDK] updateApnsToken:deviceToken];
  56. }
  57. - (BOOL)youpaifIMIsLogined{
  58. return [[[NIMSDK sharedSDK] loginManager] isLogined];
  59. }
  60. - (void)IMLogin:(NSString*)account token:(NSString *)token{
  61. self.account = account;
  62. self.token = token;
  63. if ([LCSaveData getIMNotFirstLogin]) {//代表不是第一次登录
  64. [self IMAutoLogin:account token:token];
  65. [self IMHandLogin:account token:token];
  66. }else{
  67. [self IMHandLogin:account token:token];
  68. [LCSaveData saveIMNotFirstLogin:YES];
  69. }
  70. }
  71. //手动登录
  72. - (void)IMHandLogin:(NSString *)account token:(NSString *)token{
  73. [[[NIMSDK sharedSDK]loginManager]login:account token:token completion:^(NSError * _Nullable error) {
  74. NSLog(@"%@",error);
  75. if (error.code == 302) {
  76. [ZCHUDHelper showTitle:@"请重新登录"];
  77. [LCTools kickBackToLogin];
  78. }else{
  79. NSMutableDictionary *mutDic = [NSMutableDictionary new];
  80. [mutDic setValue:[LCTools getbindData] forKey:@"bindData"];
  81. [mutDic setValue:[LCTools getchannelCode] forKey:@"channelCode"];
  82. [ZCHUDHelper show];
  83. [LCHttpHelper requestWithURLString:SystemIndex parameters:mutDic needToken:NO type:(HttpRequestTypePost) success:^(id responseObject) {
  84. [ZCHUDHelper dismiss];
  85. NSDictionary* dict = (NSDictionary*)responseObject;
  86. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  87. if (code==0) {//成功
  88. NSString *audit_status = [[dict objectForKey:@"data"] objectForKey:@"audit_status"];
  89. [LCSaveData saveWhiteVersion:[audit_status boolValue]];
  90. [LCTools changeRootToTabbar];
  91. }else{
  92. [LCSaveData saveWhiteVersion:YES];
  93. [LCTools changeRootToTabbar];
  94. }
  95. } failure:^(NSError *error) {
  96. [ZCHUDHelper dismiss];
  97. [LCSaveData saveWhiteVersion:YES];
  98. [LCTools changeRootToTabbar];
  99. }];
  100. }
  101. NSArray<NIMRecentSession *> *recentSessions = [NIMSDK sharedSDK].conversationManager.allRecentSessions;
  102. NSMutableString *uids = [NSMutableString string];
  103. for (NIMRecentSession *recentSession in recentSessions) {
  104. [uids appendFormat:@"%@,",recentSession.session.sessionId];
  105. }
  106. // @weakify(self);
  107. // [LCHttpHelper requestWithURLString:GetIntimateList parameters:@{@"ids":uids} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  108. // @strongify(self);
  109. // NSDictionary* dict = (NSDictionary*)responseObject;
  110. // NSInteger code = [[dict objectForKey:@"code"] integerValue];
  111. // if (code == 0) {//成功
  112. // NSArray *list = [[dict objectForKey:@"data"] objectForKey:@"list"];
  113. // NSMutableDictionary * intimateDict = [NSMutableDictionary dictionary];
  114. // for (NSDictionary *intimate in list) {
  115. // [intimateDict setObject:@{
  116. // @"score":[NSString stringWithFormat:@"%@",[intimate objectForKey:@"score"]],
  117. // @"grade":[NSString stringWithFormat:@"%@",[intimate objectForKey:@"grade"]],
  118. // } forKey:[NSString stringWithFormat:@"%@",[intimate objectForKey:@"user_id"]]];
  119. // }
  120. // [YOUPAILZIntimateManager shareManager].intimateDict = intimateDict;
  121. // }
  122. // } failure:^(NSError *error) {
  123. //
  124. // }];
  125. }];
  126. }
  127. //自动登录
  128. - (void)IMAutoLogin:(NSString*)account token:(NSString *)token{
  129. [[[NIMSDK sharedSDK] loginManager] autoLogin:account token:token];
  130. }
  131. - (void)youpaifIMLogout:(youpaifIMLogoutBlock)block{
  132. [[[NIMSDK sharedSDK]loginManager]logout:^(NSError * _Nullable error) {
  133. //do clean
  134. block();
  135. }];
  136. }
  137. #pragma mark - NIMLoginManagerDelegate
  138. //被踢下线
  139. - (void)onKick:(NIMKickReason)code clientType:(NIMLoginClientType)clientType{
  140. [[NSNotificationCenter defaultCenter] postNotificationName:@"AbnormalLogout" object:nil userInfo:nil];
  141. [ZCHUDHelper showTitle:@"账号在其它客户端登录,被踢下线"];
  142. [LCTools kickBackToLogin];
  143. }
  144. //登录步骤,用于刷新UI
  145. - (void)onLogin:(NIMLoginStep)step{
  146. if (NIMLoginStepLoginOK==step) {
  147. NSLog(@"登录成功");
  148. [LCTools updateMessageUnread];
  149. }else if(NIMLoginStepLoginFailed==step){
  150. //重新登录
  151. if (self.repeatNum>0) {
  152. self.repeatNum--;
  153. [self IMHandLogin:self.account token:self.token];
  154. }
  155. }else if(NIMLoginStepSyncOK==step){ //这里表示同步数据完成
  156. NSLog(@"同步数据完成");
  157. [[NSNotificationCenter defaultCenter] postNotificationName:@"youpaifuserOnlineUpdate" object:nil];
  158. }
  159. else if (NIMLoginStepLoseConnection == step){
  160. NSLog(@"NIMLoginStepLoseConnection 11连接断开");
  161. }else if (NIMLoginStepNetChanged == step){
  162. NSLog(@"网络切换");
  163. }
  164. else{
  165. NSLog(@"22连接断开");
  166. }
  167. NSLog(@"NIMLoginStep == %ld",(long)step);
  168. }
  169. //自动登录失败
  170. - (void)onAutoLoginFailed:(NSError *)error{
  171. //自动登录失败调用接口服务器注册
  172. LCLog(@"自动登录失败");
  173. if (error.code==417) {
  174. //调用手动登录
  175. [self IMHandLogin:self.account token:self.token];
  176. }
  177. }
  178. //多端登录发生变化
  179. - (void)onMultiLoginClientsChanged{
  180. }
  181. @end