UserInfoManager.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //
  2. // UserInfoManager.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/20.
  6. //
  7. #import "UserInfoManager.h"
  8. #import "ArchiveTools.h"
  9. #import "JPUSHService.h"
  10. #import <Bugly/Bugly.h>
  11. #import "AppDelegate+AppService.h"
  12. #import "KSTipsAlert.h"
  13. #import <UMCommon/MobClick.h>
  14. #import "TXIMLinsenter.h"
  15. #import "KSEnterLiveroomManager.h"
  16. @interface UserInfoManager ()
  17. @property (nonatomic, copy) UserInfoCallback callback;
  18. @property (nonatomic, copy) ConnectIMCallback IMConnCallback;
  19. @end
  20. @implementation UserInfoManager
  21. + (instancetype)shareInstance {
  22. static UserInfoManager *manager = nil;
  23. static dispatch_once_t onceToken;
  24. dispatch_once(&onceToken, ^{
  25. manager = [[UserInfoManager alloc] init];
  26. });
  27. return manager;
  28. }
  29. - (instancetype)init {
  30. if (self = [super init]) {
  31. self.userInfo = [[UserInfo alloc] init];
  32. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(otherLogin) name:@"otherLogin" object:nil];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshLiveClickStatus) name:@"refreshLiveClickStatus" object:nil];
  34. }
  35. return self;
  36. }
  37. - (void)refreshLiveClickStatus {
  38. [KSEnterLiveroomManager refreshClickStatus];
  39. }
  40. - (void)otherLogin {
  41. dispatch_main_async_safe(^{
  42. UIWindow *window = [NSObject getKeyWindow];
  43. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  44. if ([vc isKindOfClass:[UITabBarController class]]) {
  45. UITabBarController *tab = (UITabBarController *)window.rootViewController;
  46. CustomNavViewController *ctrl = (CustomNavViewController *)tab.selectedViewController;
  47. if ([ctrl.visibleViewController isKindOfClass:NSClassFromString(@"TXClassroomViewController")]) {
  48. [[NSNotificationCenter defaultCenter] postNotificationName:@"classroomLogout" object:nil];
  49. return;
  50. }
  51. else if ([vc.presentedViewController isKindOfClass:NSClassFromString(@"CustomNavViewController")]) {
  52. CustomNavViewController *nav = (CustomNavViewController *)vc.presentedViewController;
  53. if ([nav.visibleViewController isKindOfClass:NSClassFromString(@"TXLiveRoomViewController")] || [nav.visibleViewController isKindOfClass:NSClassFromString(@"KSLiveWebViewController")]) {
  54. [[NSNotificationCenter defaultCenter] postNotificationName:@"liveroomLogout" object:nil];
  55. return;
  56. }
  57. }
  58. [self showMessage:@"登录过期,请重新登录"];
  59. [APPLOGIN_MANAGER logoutAction];
  60. }
  61. });
  62. }
  63. - (void)showTipsAlert {
  64. [KSTipsAlert shareInstanceWithTitle:@"提示" descMessage:@"聊天功能已断开,是否重新连接?" leftTitle:@"取消" rightTitle:@"连接" callback:^(BOOL isSure) {
  65. if (isSure) {
  66. [self connectIM];
  67. }
  68. }];
  69. }
  70. // 提示信息
  71. - (void)showMessage:(NSString *)message {
  72. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication].windows lastObject] animated:YES];
  73. hud.removeFromSuperViewOnHide =YES;
  74. hud.mode = MBProgressHUDModeText;
  75. hud.label.text = message;
  76. hud.minSize = CGSizeMake(132.f, 60.0f);
  77. hud.label.textColor = [UIColor whiteColor];
  78. hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  79. hud.bezelView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.8];
  80. [hud hideAnimated:YES afterDelay:2];
  81. }
  82. // 获取信息回调
  83. - (void)queryUserInfoCallback:(UserInfoCallback)callback {
  84. if (callback) {
  85. self.callback = callback;
  86. }
  87. [self queryUserInfoConnectIM];
  88. }
  89. // 获取信息并连接IM
  90. - (void)queryUserInfoConnectionIMCallback:(UserInfoCallback)callback {
  91. if (callback) {
  92. self.callback = callback;
  93. }
  94. [self queryUserInfoConnectIM];
  95. }
  96. - (void)checkTokenEnableCallback:(void(^)(BOOL enable))checkCallback {
  97. NSString *token = UserDefault(TokenKey);
  98. if ([NSString isEmptyString:token]) {
  99. checkCallback(NO);
  100. return;
  101. }
  102. [KSNetworkingManager checkTokenRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  103. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  104. checkCallback(YES);
  105. }
  106. else {
  107. checkCallback(NO);
  108. }
  109. } faliure:^(NSError * _Nonnull error) {
  110. checkCallback(NO);
  111. }];
  112. }
  113. - (void)addRequestHeader {
  114. NSInteger tenantId = [UserDefaultObjectForKey(TENANT_ID) integerValue];
  115. if (tenantId > 0) {
  116. [KSNetworkingManager addHeader:[NSString stringWithFormat:@"%zd", tenantId] forKey:@"coopId"];
  117. }
  118. }
  119. - (void)queryUserInfoConnectIM {
  120. [KSNetworkingManager queryStudentInfoRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  121. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  122. // 保存用户信息
  123. self.userInfo = [[UserInfo alloc] initWithDictionary:[dic ks_dictionaryValueForKey:@"data"]];
  124. // 判断是否实名
  125. if (![NSString isEmptyString:self.userInfo.realName] && ![NSString isEmptyString:self.userInfo.idCardNo]) {
  126. self.hasAuth = YES;
  127. }
  128. else {
  129. self.hasAuth = NO;
  130. }
  131. NSString *uid = [NSString stringWithFormat:@"%.0f",self.userInfo.userId];
  132. UserDefaultSet(uid, UIDKey);
  133. NSString *imUserID = self.userInfo.imUserId;
  134. UserDefaultSet(imUserID, IM_USERID);
  135. UserDefaultSet(self.userInfo.imToken, IM_TOKEN);
  136. UserDefaultSet(self.userInfo.username, NicknameKey);
  137. UserDefaultSet(self.userInfo.heardUrl, AvatarUrlKey);
  138. if (self.userInfo.tenantId != 0) {
  139. UserDefaultSetObjectForKey(@(self.userInfo.tenantId), TENANT_ID);
  140. }
  141. [[NSUserDefaults standardUserDefaults] synchronize];
  142. [self addRequestHeader];
  143. NSString *imToken = UserDefault(IM_TOKEN);
  144. BOOL needConnect = NO;
  145. if (![NSString isEmptyString:imToken] && [TXIM_LINSENTER isCurrentUserLoginIM] == NO) {
  146. needConnect = YES;
  147. }
  148. if (needConnect) {
  149. NSString *access_token = UserDefault(TokenKey);
  150. // 设置推送别名
  151. [JPUSHService setAlias:UserDefault(UIDKey) completion:nil seq:0];
  152. // 上传registrationID
  153. [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
  154. if (![NSString isEmptyString:registrationID] && ![NSString isEmptyString:access_token]) {
  155. [self saveRegisterId:registrationID accessToken:access_token];
  156. }
  157. }];
  158. [Bugly setUserIdentifier:UserDefault(UIDKey)];
  159. [self connectIM];
  160. }
  161. if (self.callback) {
  162. self.callback(self.userInfo);
  163. self.callback = nil;
  164. }
  165. }
  166. else {
  167. if (self.callback) {
  168. self.callback(self.userInfo);
  169. self.callback = nil;
  170. }
  171. }
  172. } faliure:^(NSError * _Nonnull error) {
  173. if (self.callback) {
  174. self.callback(self.userInfo);
  175. self.callback = nil;
  176. }
  177. }];
  178. }
  179. - (void)checkTokenEnableConnectIM {
  180. [self checkTokenEnableCallback:^(BOOL enable) {
  181. if (enable) {
  182. [self connectIM];
  183. }
  184. }];
  185. }
  186. - (BOOL)checkIMConnected {
  187. if ([TXIM_LINSENTER isCurrentUserLoginIM]) {
  188. return YES;
  189. }
  190. else {
  191. return NO;
  192. }
  193. }
  194. - (void)connectionIMCallback:(ConnectIMCallback)callback {
  195. if (callback) {
  196. self.IMConnCallback = callback;
  197. }
  198. [self delayCheckIMConnection];
  199. }
  200. - (void)delayCheckIMConnection {
  201. __block BOOL isConnected = [self checkIMConnected];
  202. if (isConnected) {
  203. if (self.IMConnCallback) {
  204. self.IMConnCallback(YES);
  205. }
  206. return;
  207. }
  208. else {
  209. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  210. isConnected = [self checkIMConnected];
  211. if (isConnected) {
  212. if (self.IMConnCallback) {
  213. self.IMConnCallback(YES);
  214. }
  215. }
  216. else {
  217. [self delayCheckIMConnection];
  218. }
  219. });
  220. }
  221. }
  222. - (void)connectIM {
  223. NSString *imToken = UserDefault(IM_TOKEN);
  224. if ([NSString isEmptyString:imToken]) {
  225. return;
  226. }
  227. [TXIM_LINSENTER TXIMLoginWithUserId:UserDefault(IM_USERID) sig:imToken callback:^(BOOL isSuccess, NSString * _Nullable msg) {
  228. if (isSuccess) {
  229. dispatch_async(dispatch_get_main_queue(), ^{
  230. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  231. [appDelegate displayUnreadMessageCount];
  232. });
  233. }
  234. else {
  235. // NSString *message = [NSString stringWithFormat:@"IM连接错误:%@",msg];
  236. // dispatch_main_async_safe(^{
  237. // [self showMessage:message];
  238. // });
  239. }
  240. }];
  241. }
  242. - (NSString *)getCurrentVersion {
  243. return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  244. }
  245. - (void)requsetconfigCallback:(void(^)(BOOL success))callback {
  246. [KSNetworkingManager appVersionInfoRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  247. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  248. NSString *serviceVersion = [[dic ks_dictionaryValueForKey:@"data"] ks_stringValueForKey:@"version"];
  249. NSString *currentVersion = [self getCurrentVersion];
  250. if ([self isLowerVersionCompareLocalVersion:currentVersion serviceVersion:serviceVersion]) {
  251. callback(YES);
  252. }
  253. else {
  254. callback(NO);
  255. }
  256. }
  257. else {
  258. callback(NO);
  259. }
  260. } faliure:^(NSError * _Nonnull error) {
  261. callback(NO);
  262. }];
  263. }
  264. - (BOOL)isLowerVersionCompareLocalVersion:(NSString *)localVersion serviceVersion:(NSString *)version {
  265. localVersion = [localVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
  266. if (localVersion.length == 3) {
  267. localVersion = [localVersion stringByAppendingString:@"0"];
  268. }
  269. else if (localVersion.length==2) {
  270. localVersion = [localVersion stringByAppendingString:@"00"];
  271. }else if (localVersion.length==1){
  272. localVersion = [localVersion stringByAppendingString:@"000"];
  273. }
  274. version = [version stringByReplacingOccurrencesOfString:@"." withString:@""];
  275. if (version.length == 3) {
  276. version = [version stringByAppendingString:@"0"];
  277. }
  278. else if (version.length==2) {
  279. version = [version stringByAppendingString:@"00"];
  280. }else if (version.length==1){
  281. version = [version stringByAppendingString:@"000"];
  282. }
  283. if (([localVersion floatValue] > [version floatValue])) {
  284. return NO;
  285. }
  286. else {
  287. return YES;
  288. }
  289. }
  290. - (void)queryUserInfoSendLoginUMCount {
  291. [KSNetworkingManager queryStudentInfoRequest:KS_GET success:^(NSDictionary * _Nonnull dic) {
  292. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  293. // 保存用户信息
  294. self.userInfo = [[UserInfo alloc] initWithDictionary:[dic ks_dictionaryValueForKey:@"data"]];
  295. // 判断是否实名
  296. if (![NSString isEmptyString:self.userInfo.realName] && ![NSString isEmptyString:self.userInfo.idCardNo]) {
  297. self.hasAuth = YES;
  298. }
  299. else {
  300. self.hasAuth = NO;
  301. }
  302. NSString *uid = [NSString stringWithFormat:@"%.0f",self.userInfo.userId];
  303. UserDefaultSet(uid, UIDKey);
  304. // IM ID
  305. NSString *IMUserId = self.userInfo.imUserId;
  306. UserDefaultSet(IMUserId, IM_USERID);
  307. UserDefaultSet(self.userInfo.imToken, IM_TOKEN);
  308. UserDefaultSet(self.userInfo.username, NicknameKey);
  309. UserDefaultSet(self.userInfo.heardUrl, AvatarUrlKey);
  310. if (self.userInfo.tenantId != 0) {
  311. UserDefaultSetObjectForKey(@(self.userInfo.tenantId), TENANT_ID);
  312. }
  313. [[NSUserDefaults standardUserDefaults] synchronize];
  314. // 请求头
  315. [self addRequestHeader];
  316. NSString *access_token = UserDefault(TokenKey);
  317. // 设置推送别名
  318. [JPUSHService setAlias:UserDefault(UIDKey) completion:nil seq:0];
  319. // 上传registrationID
  320. [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
  321. if (![NSString isEmptyString:registrationID] && ![NSString isEmptyString:access_token]) {
  322. [self saveRegisterId:registrationID accessToken:access_token];
  323. }
  324. }];
  325. [Bugly setUserIdentifier:UserDefault(UIDKey)];
  326. [self connectIM];
  327. [self startUMCountAndLoginCount];
  328. }
  329. else {
  330. }
  331. } faliure:^(NSError * _Nonnull error) {
  332. }];
  333. }
  334. - (void)startUMCountAndLoginCount {
  335. [self startCountUMEvent];
  336. [self sendUMEvent:@"klx_login"];
  337. }
  338. - (BOOL)startCountUMEvent {
  339. NSString *userId = UserDefault(UIDKey);
  340. if ([NSString isEmptyString:userId]) {
  341. return NO;
  342. }
  343. else {
  344. [MobClick profileSignInWithPUID:userId];
  345. return YES;
  346. }
  347. }
  348. - (void)stopCountUMEvent {
  349. [MobClick profileSignOff];
  350. }
  351. - (void)sendUMEvent:(NSString *)eventId {
  352. [MobClick event:eventId];
  353. }
  354. - (void)dealloc {
  355. [[NSNotificationCenter defaultCenter] removeObserver:self];
  356. }
  357. - (void)saveRegisterId:(NSString *)registerId accessToken:(NSString *)accessToken {
  358. [KSNetworkingManager updateRemotePushInfoRequest:KS_POST pushDeviceId:registerId access_token:accessToken success:^(NSDictionary * _Nonnull dic) {
  359. NSLog(@"---");
  360. } faliure:^(NSError * _Nonnull error) {
  361. NSLog(@"---");
  362. }];
  363. }
  364. @end