123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- //
- // UserInfoManager.m
- // MusicGradeExam
- //
- // Created by Kyle on 2020/7/10.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "UserInfoManager.h"
- #import <RongIMKit/RongIMKit.h>
- #import "RCConnectionManager.h"
- #import "AppDelegate.h"
- #import "JPUSHService.h"
- #import "ClassroomViewController.h"
- #import <Bugly/Bugly.h>
- @interface UserInfoManager ()
- @property (nonatomic, copy) UserInfoCallback callback;
- @end
- @implementation UserInfoManager
- + (instancetype)shareInstance {
- static UserInfoManager *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[UserInfoManager alloc] init];
- });
- return manager;
- }
- - (instancetype)init {
- if (self = [super init]) {
- self.userInfo = [[UserInfo alloc] init];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(otherLogin) name:@"otherLogin" object:nil];
- }
- return self;
- }
- - (void)otherLogin {
- UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([vc.presentedViewController isKindOfClass:[ClassroomViewController class]]) {
- [[NSNotificationCenter defaultCenter] postNotificationName:@"classroomLogout" object:nil];
- }
- else if ([vc isKindOfClass:[UITabBarController class]]) {
- [self showMessage:@"该账号在其他地方登录"];
- [KSRequestManager logoutAction];
- }
- }
- // 提示信息
- - (void)showMessage:(NSString *)message {
- MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows lastObject] animated:YES];
- hud.removeFromSuperViewOnHide =YES;
- hud.mode = MBProgressHUDModeText;
- hud.label.text = message;
- hud.minSize = CGSizeMake(132.f, 60.0f);
- hud.label.textColor = [UIColor whiteColor];
- hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.8];
- [hud hideAnimated:YES afterDelay:2];
- }
- // 获取信息回调
- - (void)queryUserInfoCallback:(UserInfoCallback)callback {
- if (callback) {
- self.callback = callback;
- }
- [self queryUserInfoConnectRongCloud:NO connectionJPush:NO];
- }
- - (void)queryUserInfoConnectRongCloud:(BOOL)connectRM connectionJPush:(BOOL)connectJPush {
-
- [KSRequestManager queryUserInfo:KS_GET success:^(NSDictionary * _Nonnull dic) {
- if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
- // 保存用户信息
- self.userInfo = [[UserInfo alloc] initWithDictionary:[dic dictionaryValueForKey:@"data"]];
- NSString *uid = [NSString stringWithFormat:@"%.0f",self.userInfo.userId];
- UserDefaultSet(uid, UIDKey);
- UserDefaultSet(self.userInfo.imToken, RongTokenKey);
- UserDefaultSet(self.userInfo.realName, NicknameKey);
- UserDefaultSet(self.userInfo.avatar, AvatarUrlKey);
- [[NSUserDefaults standardUserDefaults] synchronize];
-
- if (connectJPush) {
- [JPUSHService setAlias:UserDefault(UIDKey) completion:nil seq:0];
- // 设置推送别名
- [Bugly setUserIdentifier:UserDefault(UIDKey)];
- }
- if (connectRM) {
-
- [self connectRongCloud];
- }
- else if (self.callback) {
- self.callback(self.userInfo);
- }
- }
- else {
- if (self.callback) {
- self.callback(self.userInfo);
- }
- }
-
- } faliure:^(NSError * _Nonnull error) {
- if (self.callback) {
- self.callback(self.userInfo);
- }
- }];
- }
- - (void)connectRongCloud {
- NSString *rongToken = UserDefault(RongTokenKey);
- if ([NSString isEmptyString:rongToken]) {
- return;
- }
- // 融云登录
- [[RCIM sharedRCIM] connectWithToken:UserDefault(RongTokenKey) dbOpened:^(RCDBErrorCode code) {
-
- } success:^(NSString *userId) {
- [RCConnectionManager shareManager].isConnected = YES;
- // 设置个人信息
- RCUserInfo *currentUserInfo =
- [[RCUserInfo alloc] initWithUserId:userId name:UserDefault(NicknameKey) portrait:UserDefault(AvatarUrlKey)];
- [RCIM sharedRCIM].currentUserInfo = currentUserInfo;
- } error:^(RCConnectErrorCode errorCode) {
- if (errorCode == RC_CONN_TOKEN_INCORRECT) {
- dispatch_async(dispatch_get_main_queue(), ^{
- USER_MANAGER.needConnect = NO;
- // 再次获取token
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- [appDelegate requestRongCloudToken];
- });
- }
- else {
- NSString *message = [NSString stringWithFormat:@"IM连接错误%ld",errorCode];
- dispatch_main_async_safe(^{
- [self showMessage:message];
- });
- }
- }];
- }
- @end
|