123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // KSLoginManager.m
- // KulexiuForTeacher
- //
- // Created by 王智 on 2023/8/7.
- //
- #import "KSLoginManager.h"
- #import "LoginViewController.h"
- #import "JPUSHService.h"
- #import "CustomNavViewController.h"
- #import "UserInfoManager.h"
- #import "TXIMLinsenter.h"
- #import <KSToolLibrary/UIDevice+TFDevice.h>
- #import "AppDelegate+AppService.h"
- #import "CoursewareDownloadManager.h"
- @interface KSLoginManager ()
- @property (nonatomic, assign) BOOL isQuitting; // 是否正在退出
- @end
- @implementation KSLoginManager
- + (instancetype)shareInstance {
- static KSLoginManager *manager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- manager = [[KSLoginManager alloc] init];
- });
- return manager;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(otherLogin) name:@"otherLogin" object:nil];
- }
- return self;
- }
- - (void)configSetting {
- NSLog(@"------configSetting ");
- }
- - (void)otherLogin {
- dispatch_main_async_safe(^{
- UIWindow *window = [NSObject getKeyWindow];
- UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
-
- if ([vc isKindOfClass:[UITabBarController class]]) {
- UITabBarController *tab = (UITabBarController *)window.rootViewController;
- CustomNavViewController *ctrl = (CustomNavViewController *)tab.selectedViewController;
- if ([ctrl.visibleViewController isKindOfClass:NSClassFromString(@"TXClassroomViewController")]) {
- [[NSNotificationCenter defaultCenter] postNotificationName:@"classroomLogout" object:nil];
- return;
- }
- else if ([vc.presentedViewController isKindOfClass:NSClassFromString(@"CustomNavViewController")]) {
- CustomNavViewController *nav = (CustomNavViewController *)vc.presentedViewController;
- if ([nav.visibleViewController isKindOfClass:NSClassFromString(@"TXLiveRoomViewController")]) {
- [[NSNotificationCenter defaultCenter] postNotificationName:@"liveroomLogout" object:nil];
- return;
- }
- }
- [self logoutActionWithMsg:@"登录过期,请重新登录"];
- }
- });
- }
- - (void)clearUMCount {
- [USER_MANAGER sendUMEvent:@"klx_logout"];
- [USER_MANAGER stopCountUMEvent];
- }
- - (void)closeDownload {
- if (COURSEWARE_MANAGER.isDownloading) { // 如果在下载 取消
- [COURSEWARE_MANAGER cancleDownloadCourseware];
- }
- }
- - (void)logoutActionWithMsg:(NSString *)msg {
- if (self.isQuitting) {
- NSLog(@"----1");
- return;
- }
- self.isQuitting = YES;
- [self closeDownload];
- [self clearUMCount];
-
- [KSNetworkingManager cancelAllRequest];
- [KSNetworkingManager clearRequestHeader];
-
- // 取消推送别名
- [JPUSHService deleteAlias:nil seq:0];
- // 退出IM连接
- [TXIM_LINSENTER logoutTXIM];
- // 清除数据
- [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:TokenKey];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:Token_type];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:RefreshToken];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:IM_TOKEN];
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:TENANT_ID];
- // 返回登录页面
- UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([vc isKindOfClass:[UITabBarController class]]) {
- UITabBarController *tab = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
- CustomNavViewController *ctrl = (CustomNavViewController *)tab.selectedViewController;
- if (ctrl.presentedViewController) {
- UIViewController *presentCtrl = ctrl.presentedViewController;
- [presentCtrl dismissViewControllerAnimated:YES completion:^{
- [ctrl popToRootViewControllerAnimated:NO];
- [self performSelector:@selector(resetToLoginViewController:) withObject:msg afterDelay:0.1];
- }];
- }
- else {
- [ctrl popToRootViewControllerAnimated:NO];
- [self performSelector:@selector(resetToLoginViewController:) withObject:msg afterDelay:0.1];
- }
- }
- else if ([vc isKindOfClass:NSClassFromString(@"CustomNavViewController")]) {
- if (vc.presentedViewController) {
- UIViewController *presentCtrl = vc.presentedViewController;
- [presentCtrl dismissViewControllerAnimated:YES completion:^{
- CustomNavViewController *ctrl = (CustomNavViewController *)vc;
- [ctrl popToRootViewControllerAnimated:NO];
- self.isQuitting = NO;
- }];
- }
- else {
- CustomNavViewController *ctrl = (CustomNavViewController *)vc;
- [ctrl popToRootViewControllerAnimated:NO];
- self.isQuitting = NO;
- }
- }
- }
- - (void)resetToLoginViewController:(NSString *)tipsMsg {
- LoginViewController *logonVC = [[LoginViewController alloc] init];
- CustomNavViewController *navCtrl = [[CustomNavViewController alloc] initWithRootViewController:logonVC];
- [AppDelegate shareAppDelegate].window.rootViewController = navCtrl;
- // 切换到横屏
- AppDelegate* delegate = [AppDelegate shareAppDelegate];
- if (delegate.allowAutoRotate) {
- delegate.allowAutoRotate = NO;
- if (IS_IPAD) {
- logonVC.zh_statusBarHidden = NO;
- }
- [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait inController:logonVC];
- }
- if (![NSString isEmptyString:tipsMsg]) {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:tipsMsg];
- }
- self.isQuitting = NO;
- }
- - (NSString *)getVisableControllerName {
- UIViewController *vc = [AppDelegate shareAppDelegate].window.rootViewController;
- if ([vc isKindOfClass:[UITabBarController class]]) {
- UITabBarController *tab = (UITabBarController *)vc;
- CustomNavViewController *ctrl = (CustomNavViewController *)tab.selectedViewController;
- return NSStringFromClass(ctrl.visibleViewController.class);
- }
- else if ([vc isKindOfClass:[CustomNavViewController class]]) {
- CustomNavViewController *ctrl = (CustomNavViewController *)vc;
- return NSStringFromClass(ctrl.visibleViewController.class);
- }
- else {
- return NSStringFromClass(vc.class);
- }
- }
- @end
|