| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //
- // YOUPAILCIMLoginManager.m
- // LiveChat
- //
- // Created by 张灿 on 2018/9/1.
- // Copyright © 2018年 caicai. All rights reserved.
- //
- #import "YOUPAILCIMLoginManager.h"
- #import "YOUPAILCCellLayoutconfig.h"
- #import "YOUPAILCCustomAttachmentDecoder.h"
- @interface YOUPAILCIMLoginManager()<NIMLoginManagerDelegate>
- @property(nonatomic,strong)NSString* account;
- @property(nonatomic,strong)NSString* token;
- @property(nonatomic,assign)NSInteger repeatNum;
- @end
- @implementation YOUPAILCIMLoginManager
- + (instancetype)sharedCenter
- {
- static YOUPAILCIMLoginManager *instance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [[YOUPAILCIMLoginManager alloc] init];
- instance.repeatNum = 3;
- });
- return instance;
- }
- - (instancetype)init {
- self = [super init];
- if(self) {
- [[[NIMSDK sharedSDK] loginManager] addDelegate:self];
- }
- return self;
- }
- - (void)dealloc{
- [[[NIMSDK sharedSDK] loginManager] removeDelegate:self];
- }
- - (void)youpaifNIMinit:(BOOL)isDistribution{
- if (isDistribution) {
- [[NIMSDK sharedSDK] registerWithAppID:NIMSDKID
- cerName:@"DongerAPNS-PRD"];//生产推送证书 // 正式
- } else {
- [[NIMSDK sharedSDK] registerWithAppID:NIMSDKID
- cerName:@"DongerAPNS-DEV"];//开发推送证书 // 测试
- }
-
- #if DEBUG
- [NIMSDK.sharedSDK enableConsoleLog];
- #endif
- //设置头像为http格式
- [NIMSDKConfig sharedConfig].enabledHttpsForInfo = NO;
-
- [NIMCustomObject registerCustomDecoder:[YOUPAILCCustomAttachmentDecoder new]];//注册自定义消息解析器
- //注册 NIMKit 自定义排版配置
- [[NIMKit sharedKit] registerLayoutConfig:[[YOUPAILCCellLayoutConfig alloc]init]];
- }
- - (void)youpaifupdateDeviceToken:(NSData*)deviceToken{
- [[NIMSDK sharedSDK] updateApnsToken:deviceToken];
- }
- - (BOOL)youpaifIMIsLogined{
- return [[[NIMSDK sharedSDK] loginManager] isLogined];
- }
- - (void)IMLogin:(NSString*)account token:(NSString *)token{
- self.account = account;
- self.token = token;
- if ([LCSaveData getIMNotFirstLogin]) {//代表不是第一次登录
- [self IMAutoLogin:account token:token];
- [self IMHandLogin:account token:token];
- }else{
- [self IMHandLogin:account token:token];
- [LCSaveData saveIMNotFirstLogin:YES];
- }
-
- }
- //手动登录
- - (void)IMHandLogin:(NSString *)account token:(NSString *)token{
- [[[NIMSDK sharedSDK]loginManager]login:account token:token completion:^(NSError * _Nullable error) {
- NSLog(@"%@",error);
- if (error.code == 302) {
- [ZCHUDHelper showTitle:@"请重新登录"];
- [LCTools kickBackToLogin];
- }else{
-
- NSMutableDictionary *mutDic = [NSMutableDictionary new];
- [mutDic setValue:[LCTools getbindData] forKey:@"bindData"];
- [mutDic setValue:[LCTools getchannelCode] forKey:@"channelCode"];
- [ZCHUDHelper show];
- [LCHttpHelper requestWithURLString:SystemIndex parameters:mutDic needToken:NO type:(HttpRequestTypePost) success:^(id responseObject) {
- [ZCHUDHelper dismiss];
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code==0) {//成功
- NSString *audit_status = [[dict objectForKey:@"data"] objectForKey:@"audit_status"];
- [LCSaveData saveWhiteVersion:[audit_status boolValue]];
- [LCTools changeRootToTabbar];
- }else{
- [LCSaveData saveWhiteVersion:YES];
- [LCTools changeRootToTabbar];
- }
- } failure:^(NSError *error) {
- [ZCHUDHelper dismiss];
- [LCSaveData saveWhiteVersion:YES];
- [LCTools changeRootToTabbar];
- }];
-
- }
-
- NSArray<NIMRecentSession *> *recentSessions = [NIMSDK sharedSDK].conversationManager.allRecentSessions;
- NSMutableString *uids = [NSMutableString string];
- for (NIMRecentSession *recentSession in recentSessions) {
- [uids appendFormat:@"%@,",recentSession.session.sessionId];
- }
- // @weakify(self);
- // [LCHttpHelper requestWithURLString:GetIntimateList parameters:@{@"ids":uids} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
- // @strongify(self);
- // NSDictionary* dict = (NSDictionary*)responseObject;
- // NSInteger code = [[dict objectForKey:@"code"] integerValue];
- // if (code == 0) {//成功
- // NSArray *list = [[dict objectForKey:@"data"] objectForKey:@"list"];
- // NSMutableDictionary * intimateDict = [NSMutableDictionary dictionary];
- // for (NSDictionary *intimate in list) {
- // [intimateDict setObject:@{
- // @"score":[NSString stringWithFormat:@"%@",[intimate objectForKey:@"score"]],
- // @"grade":[NSString stringWithFormat:@"%@",[intimate objectForKey:@"grade"]],
- // } forKey:[NSString stringWithFormat:@"%@",[intimate objectForKey:@"user_id"]]];
- // }
- // [YOUPAILZIntimateManager shareManager].intimateDict = intimateDict;
- // }
- // } failure:^(NSError *error) {
- //
- // }];
- }];
- }
- //自动登录
- - (void)IMAutoLogin:(NSString*)account token:(NSString *)token{
- [[[NIMSDK sharedSDK] loginManager] autoLogin:account token:token];
- }
- - (void)youpaifIMLogout:(youpaifIMLogoutBlock)block{
- [[[NIMSDK sharedSDK]loginManager]logout:^(NSError * _Nullable error) {
- //do clean
- block();
- }];
- }
- #pragma mark - NIMLoginManagerDelegate
- //被踢下线
- - (void)onKick:(NIMKickReason)code clientType:(NIMLoginClientType)clientType{
- [[NSNotificationCenter defaultCenter] postNotificationName:@"AbnormalLogout" object:nil userInfo:nil];
- [ZCHUDHelper showTitle:@"账号在其它客户端登录,被踢下线"];
- [LCTools kickBackToLogin];
- }
- //登录步骤,用于刷新UI
- - (void)onLogin:(NIMLoginStep)step{
- if (NIMLoginStepLoginOK==step) {
- NSLog(@"登录成功");
- [LCTools updateMessageUnread];
-
- }else if(NIMLoginStepLoginFailed==step){
- //重新登录
- if (self.repeatNum>0) {
- self.repeatNum--;
- [self IMHandLogin:self.account token:self.token];
- }
- }else if(NIMLoginStepSyncOK==step){ //这里表示同步数据完成
- NSLog(@"同步数据完成");
- [[NSNotificationCenter defaultCenter] postNotificationName:@"youpaifuserOnlineUpdate" object:nil];
- }
- else if (NIMLoginStepLoseConnection == step){
- NSLog(@"NIMLoginStepLoseConnection 11连接断开");
- }else if (NIMLoginStepNetChanged == step){
- NSLog(@"网络切换");
- }
- else{
- NSLog(@"22连接断开");
- }
-
- NSLog(@"NIMLoginStep == %ld",(long)step);
-
- }
- //自动登录失败
- - (void)onAutoLoginFailed:(NSError *)error{
- //自动登录失败调用接口服务器注册
- LCLog(@"自动登录失败");
- if (error.code==417) {
- //调用手动登录
- [self IMHandLogin:self.account token:self.token];
- }
-
- }
- //多端登录发生变化
- - (void)onMultiLoginClientsChanged{
-
- }
- @end
|