UserInfoManager.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // UserInfoManager.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/7/10.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "UserInfoManager.h"
  9. #import <RongIMKit/RongIMKit.h>
  10. #import "RCConnectionManager.h"
  11. #import "AppDelegate.h"
  12. #import "JPUSHService.h"
  13. #import "ClassroomViewController.h"
  14. #import <Bugly/Bugly.h>
  15. @interface UserInfoManager ()
  16. @property (nonatomic, copy) UserInfoCallback callback;
  17. @end
  18. @implementation UserInfoManager
  19. + (instancetype)shareInstance {
  20. static UserInfoManager *manager = nil;
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. manager = [[UserInfoManager alloc] init];
  24. });
  25. return manager;
  26. }
  27. - (instancetype)init {
  28. if (self = [super init]) {
  29. self.userInfo = [[UserInfo alloc] init];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(otherLogin) name:@"otherLogin" object:nil];
  31. }
  32. return self;
  33. }
  34. - (void)otherLogin {
  35. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  36. if ([vc.presentedViewController isKindOfClass:[ClassroomViewController class]]) {
  37. [[NSNotificationCenter defaultCenter] postNotificationName:@"classroomLogout" object:nil];
  38. }
  39. else if ([vc isKindOfClass:[UITabBarController class]]) {
  40. [self showMessage:@"该账号在其他地方登录"];
  41. [KSRequestManager logoutAction];
  42. }
  43. }
  44. // 提示信息
  45. - (void)showMessage:(NSString *)message {
  46. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows lastObject] animated:YES];
  47. hud.removeFromSuperViewOnHide =YES;
  48. hud.mode = MBProgressHUDModeText;
  49. hud.label.text = message;
  50. hud.minSize = CGSizeMake(132.f, 60.0f);
  51. hud.label.textColor = [UIColor whiteColor];
  52. hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.8];
  53. [hud hideAnimated:YES afterDelay:2];
  54. }
  55. // 获取信息回调
  56. - (void)queryUserInfoCallback:(UserInfoCallback)callback {
  57. if (callback) {
  58. self.callback = callback;
  59. }
  60. [self queryUserInfoConnectRongCloud:NO connectionJPush:NO];
  61. }
  62. - (void)queryUserInfoConnectRongCloud:(BOOL)connectRM connectionJPush:(BOOL)connectJPush {
  63. [KSRequestManager queryUserInfo:KS_GET success:^(NSDictionary * _Nonnull dic) {
  64. if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) {
  65. // 保存用户信息
  66. self.userInfo = [[UserInfo alloc] initWithDictionary:[dic dictionaryValueForKey:@"data"]];
  67. NSString *uid = [NSString stringWithFormat:@"%.0f",self.userInfo.userId];
  68. UserDefaultSet(uid, UIDKey);
  69. UserDefaultSet(self.userInfo.imToken, RongTokenKey);
  70. UserDefaultSet(self.userInfo.realName, NicknameKey);
  71. UserDefaultSet(self.userInfo.avatar, AvatarUrlKey);
  72. [[NSUserDefaults standardUserDefaults] synchronize];
  73. if (connectJPush) {
  74. [JPUSHService setAlias:UserDefault(UIDKey) completion:nil seq:0];
  75. // 设置推送别名
  76. [Bugly setUserIdentifier:UserDefault(UIDKey)];
  77. }
  78. if (connectRM) {
  79. [self connectRongCloud];
  80. }
  81. else if (self.callback) {
  82. self.callback(self.userInfo);
  83. }
  84. }
  85. else {
  86. if (self.callback) {
  87. self.callback(self.userInfo);
  88. }
  89. }
  90. } faliure:^(NSError * _Nonnull error) {
  91. if (self.callback) {
  92. self.callback(self.userInfo);
  93. }
  94. }];
  95. }
  96. - (void)connectRongCloud {
  97. NSString *rongToken = UserDefault(RongTokenKey);
  98. if ([NSString isEmptyString:rongToken]) {
  99. return;
  100. }
  101. // 融云登录
  102. [[RCIM sharedRCIM] connectWithToken:UserDefault(RongTokenKey) dbOpened:^(RCDBErrorCode code) {
  103. } success:^(NSString *userId) {
  104. [RCConnectionManager shareManager].isConnected = YES;
  105. // 设置个人信息
  106. RCUserInfo *currentUserInfo =
  107. [[RCUserInfo alloc] initWithUserId:userId name:UserDefault(NicknameKey) portrait:UserDefault(AvatarUrlKey)];
  108. [RCIM sharedRCIM].currentUserInfo = currentUserInfo;
  109. } error:^(RCConnectErrorCode errorCode) {
  110. if (errorCode == RC_CONN_TOKEN_INCORRECT) {
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. USER_MANAGER.needConnect = NO;
  113. // 再次获取token
  114. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  115. [appDelegate requestRongCloudToken];
  116. });
  117. }
  118. else {
  119. NSString *message = [NSString stringWithFormat:@"IM连接错误%ld",errorCode];
  120. dispatch_main_async_safe(^{
  121. [self showMessage:message];
  122. });
  123. }
  124. }];
  125. }
  126. @end