// // QXGlobal.m // TrucksProject // // Created by 党凯 on 2020/6/30. // Copyright © 2020 党凯. All rights reserved. // #import "QXGlobal.h" #import "AppDelegate.h" #import "QXLoginViewController.h" #import "QXBaseNavigationController.h" #import "TUILogin.h" #import "QXChatViewController.h" #import "QXRoomViewController.h" #import "QXMineNetwork.h" #import "QXGiftPlayerManager.h" #import "QXAgoraEngine.h" #import "QXRoomMessageManager.h" #import "QXTimer.h" #import #import "QXManagerMqtt.h" #import "QXRedPacketManager.h" #import "QXGiftDisplayManager.h" @interface QXGlobal() @property (nonatomic,assign)BOOL isLogin; @property (nonatomic,assign)BOOL isClickJoinRoom; @property (nonatomic,strong)QXTimer *timer; @property (nonatomic,assign)NSInteger taskTime; @end @implementation QXGlobal + (instancetype)shareGlobal { static QXGlobal *global = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ global = [[self alloc] init]; global.isShowLoginVC = NO; }); return global; } -(UIWindow *)getKeyWindow{ AppDelegate*appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; UIWindow *window = appDelegate.window; return window; } -(void)logOut{ [TUILogin logout:^{ QXLOG(@"腾讯im退出登录成功"); } fail:^(int code, NSString * _Nullable msg) { QXLOG(@"腾讯im退出登录失败"); }]; self.isLogin = NO; [self removeLocalData]; [self removeMemory]; AppDelegate *appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; /// 移除推送监听 [TIMPushManager removePushListener:appdelegate]; /// 移除会话监听 [[V2TIMManager sharedInstance] removeConversationListener:appdelegate]; [TIMPushManager unRegisterPush:^{ //success } fail:^(int code, NSString * _Nonnull desc) { //error }]; if (self.isShowLoginVC == YES) { return; } QXLoginViewController *vc = [[QXLoginViewController alloc] init]; QXBaseNavigationController *na = [[QXBaseNavigationController alloc] initWithRootViewController:vc]; na.modalPresentationStyle = UIModalPresentationFullScreen; [self.getKeyWindow.rootViewController presentViewController:na animated:YES completion:nil]; self.isShowLoginVC = YES; /// 断开mqtt连接 [[QXManagerMqtt sharedInstance] disconnect]; } -(void)saveLoginData:(NSString *)userInfoJson{ [[NSUserDefaults standardUserDefaults] setObject:userInfoJson forKey:kUserLoginData]; [[NSUserDefaults standardUserDefaults] synchronize]; } -(void)updateUserInfoWithMolde:(QXLoginModel *)loginModel{ NSString *jsonStr = [loginModel yy_modelToJSONString]; [self saveLoginData:jsonStr]; } //-(void)setLoginModel:(QXLoginModel *)loginModel{ // _loginModel = loginModel; // if (loginModel == nil) { // self.isLogin = NO; // }else{ // self.isLogin = YES; // } //} -(BOOL)isRealName{ return [QXGlobal shareGlobal].loginModel.auth == 1; } -(QXLoginModel *)loginModel{ if (!_loginModel) { NSString *jsonStr = [[NSUserDefaults standardUserDefaults] objectForKey:kUserLoginData]; if (jsonStr.length > 0) { _loginModel = [QXLoginModel yy_modelWithJSON:jsonStr]; self.isLogin = YES; }else{ _loginModel = nil; self.isLogin = NO; } } return _loginModel; } -(void)toLogin{ } -(BOOL)isLogin{ NSString *jsonStr = [[NSUserDefaults standardUserDefaults] objectForKey:kUserLoginData]; return jsonStr.length > 0; } -(void)removeLocalData{ [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserLoginData]; [[NSUserDefaults standardUserDefaults] synchronize]; } -(void)removeMemory{ QXGlobal.shareGlobal.loginModel = nil; } -(void)showView:(UIView *)view popType:(PopViewType)popType tapDismiss:(BOOL)tapDismiss finishBlock:(showFinishBlock)finishBlock{ self.alertViewController = [[QXAlertViewController alloc] init]; self.alertViewController.popType = popType; self.alertViewController.alertView = view; self.alertViewController.tapDismiss = tapDismiss; self.alertViewController.modalPresentationStyle = UIModalPresentationOverFullScreen; [[QXGlobal shareGlobal].getKeyWindow.rootViewController presentViewController:self.alertViewController animated:NO completion:finishBlock]; } -(void)showView:(UIView *)view controller:(UIViewController*)controller popType:(PopViewType)popType tapDismiss:(BOOL)tapDismiss finishBlock:(showFinishBlock)finishBlock{ self.alertViewController = [[QXAlertViewController alloc] init]; self.alertViewController.popType = popType; self.alertViewController.alertView = view; self.alertViewController.tapDismiss = tapDismiss; self.alertViewController.modalPresentationStyle = UIModalPresentationOverFullScreen; [controller presentViewController:self.alertViewController animated:NO completion:finishBlock]; } -(void)hideViewBlock:(closeBlock)closeBlock{ [self.alertViewController hideViewFinishBlock:closeBlock]; } -(BOOL)isOpenRecharge{ BOOL isOpenRecharge = [[NSUserDefaults standardUserDefaults] boolForKey:kIsOpenRecharge]; return isOpenRecharge; } -(void)finishTask{ if (QXGlobal.shareGlobal.taskModel != nil) { [QXMineNetwork dayTaskFinishedWithTaskId:QXGlobal.shareGlobal.taskModel.task_id successBlock:^(NSDictionary * _Nonnull dict) { NSString *is_completed = [NSString stringWithFormat:@"%@",dict[@"is_completed"]]; if (is_completed.intValue == 1) { QXGlobal.shareGlobal.taskModel = nil; QXGlobal.shareGlobal.taskTime = 0; if (self->_timer) { [self->_timer invalidate]; self->_timer = nil; } } } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; } } -(void)chatWithUserID:(NSString *)userId nickname:(NSString *)nickname avatar:(NSString *)avatar navagationController:(UINavigationController *)navagationController{ QXChatViewController *vc = [[QXChatViewController alloc] init]; TUIChatConversationModel *data = [[TUIChatConversationModel alloc] init]; data.userID = [NSString stringWithFormat:@"u%@",userId]; data.title = nickname; data.faceUrl = avatar; vc.data = data; [navagationController pushViewController:vc animated:YES]; } -(void)chatWithGroupId:(NSString *)groupId cover:(NSString *)cover name:(NSString *)name navagationController:(UINavigationController *)navagationController{ QXChatViewController *vc = [[QXChatViewController alloc] init]; TUIChatConversationModel *data = [[TUIChatConversationModel alloc] init]; if ([groupId containsString:@"g"]) { data.groupID = groupId; }else{ data.groupID = [NSString stringWithFormat:@"g%@",groupId]; } data.faceUrl = cover; data.title = name; vc.data = data; [navagationController pushViewController:vc animated:YES]; } -(void)joinRoomWithRoomId:(NSString *)roomId isRejoin:(BOOL)isRejoin navagationController:(UINavigationController *)navagationController{ if (!QXGlobal.shareGlobal.isLogin) { [[QXGlobal shareGlobal] logOut]; return; } navagationController = (QXRoomNavigationController*)KEYWINDOW.rootViewController; // navagationController.interactivePopGestureRecognizer.enabled = NO; if (_miniView) { [_miniView removeFromSuperview]; _miniView = nil; } if (QXGlobal.shareGlobal.taskModel != nil) { /// 是否带有每日任务 if (QXGlobal.shareGlobal.taskModel.task_id.intValue == 9) { /// 是计时任务 if (_timer == nil) { _timer = [QXTimer scheduledTimerWithTimeInterval:1 repeats:YES queue:dispatch_get_main_queue() block:^{ QXGlobal.shareGlobal.taskTime++; if (QXGlobal.shareGlobal.taskTime%60 == 0) { [[QXGlobal shareGlobal] finishTask]; } }]; } } } if ([roomId isEqualToString:self.roomId]) { if ([navagationController.viewControllers containsObject:self.roomVC]) { return; }else{ [UIApplication sharedApplication].idleTimerDisabled = YES; self.roomVC.hidesBottomBarWhenPushed = YES; [self.roomVC playHWDMP4]; [navagationController pushViewController:self.roomVC animated:YES]; } return; } if (self.roomId && ![self.roomId isEqualToString:roomId]) { [self quitRoomWithRoomId:self.roomId]; } [UIApplication sharedApplication].idleTimerDisabled = YES; MJWeakSelf if (isRejoin) { if (self.roomVC!=nil) { [self.roomVC playHWDMP4]; self.roomVC.hidesBottomBarWhenPushed = YES; [navagationController pushViewController:self.roomVC animated:YES]; }else{ if (weakSelf.isClickJoinRoom) { return; } weakSelf.isClickJoinRoom = YES; [[QXRoomMessageManager shared] joinGroupWithRoomId:roomId]; [QXMineNetwork joinRoomWithRoomId:roomId successBlock:^(QXRoomModel * _Nonnull roomModel) { [UIApplication sharedApplication].idleTimerDisabled = YES; weakSelf.isClickJoinRoom = NO; weakSelf.roomVC = [[QXRoomViewController alloc] init]; weakSelf.roomVC.roomId = roomId; weakSelf.roomVC.isReJoin = isRejoin; weakSelf.roomId = roomId; weakSelf.roomVC.roomModel = roomModel; weakSelf.roomVC.hidesBottomBarWhenPushed = YES; [navagationController pushViewController:weakSelf.roomVC animated:YES]; weakSelf.roomId = roomId; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { showToast(msg); weakSelf.isClickJoinRoom = NO; }]; } }else{ if ([roomId isEqualToString:self.roomVC.roomId]) { [self.roomVC playHWDMP4]; [navagationController pushViewController:self.roomVC animated:YES]; }else{ // showLoadingInView(KEYWINDOW) if (weakSelf.isClickJoinRoom) { return; } weakSelf.isClickJoinRoom = YES; [[QXRoomMessageManager shared] joinGroupWithRoomId:roomId]; [QXMineNetwork joinRoomWithRoomId:roomId successBlock:^(QXRoomModel * _Nonnull roomModel) { // hideLoadingInView(KEYWINDOW) // roomModel.room_info.pk_room_id = @"33"; [UIApplication sharedApplication].idleTimerDisabled = YES; weakSelf.isClickJoinRoom = NO; weakSelf.roomVC = [[QXRoomViewController alloc] init]; weakSelf.roomVC.roomId = roomId; weakSelf.roomVC.isReJoin = isRejoin; weakSelf.roomId = roomId; weakSelf.roomVC.roomModel = roomModel; weakSelf.roomVC.hidesBottomBarWhenPushed = YES; [navagationController pushViewController:weakSelf.roomVC animated:YES]; weakSelf.roomId = roomId; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { weakSelf.isClickJoinRoom = NO; showToast(msg); // hideLoadingInView(KEYWINDOW) }]; } } } -(void)popToRoomViewController{ QXRoomNavigationController *navagationController = (QXRoomNavigationController*)KEYWINDOW.rootViewController; for (UIViewController *vc in navagationController.viewControllers) { if ([vc isKindOfClass:[QXRoomViewController class]]) { [navagationController popToViewController:vc animated:YES]; break; } } } -(void)quitRoomWithRoomId:(NSString *)roomId{ [UIApplication sharedApplication].idleTimerDisabled = NO; /// 移除所有红包 [[QXRedPacketManager sharedManager] destoryRedpacketInfo]; /// 移除房间内礼物飘屏 [[QXGiftDisplayManager sharedManager] clearAll]; /// 移除特效 [[QXGiftPlayerManager shareManager] destroyEffectSvga]; /// 退出声网 [[QXAgoraEngine sharedEngine] leaveChannel]; /// 销毁播放器 [[QXAgoraEngine sharedEngine] ktv_DestoryKtvPlayer]; /// 销毁整个声网示例 [[QXAgoraEngine sharedEngine] destroyEngine]; /// 退出直播群 [[QXRoomMessageManager shared] quitGroupWithRoomId:roomId]; [[QXGlobal shareGlobal].offLineDict removeAllObjects]; [[NSNotificationCenter defaultCenter] removeObserver:self]; if (_roomVC) { /// 回收麦位视图内存 [_roomVC.seatContentView destroyViews]; /// 回收控制器内存 [_roomVC destroyViews]; } self.roomId = nil; _roomVC = nil; /// 业务http退出 [QXMineNetwork quitRoomWithRoomId:roomId user_id:[QXGlobal shareGlobal].loginModel.user_id successBlock:^(NSDictionary * _Nonnull dict) { } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; if (_timer) { [_timer invalidate]; _timer = nil; } } -(void)miniRoomWithRoomId:(NSString*)roomId roomCover:(NSString*)roomCover{ self.miniView.roomId = self.roomId; self.miniView.roomCoverImage = roomCover; [self.miniView show]; } -(QXMiniRoomView *)miniView{ if (!_miniView) { _miniView = [[QXMiniRoomView alloc] init]; } return _miniView; } -(void)vibrationFeedback{ UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy]; [generator impactOccurred]; } -(NSMutableDictionary *)offLineDict{ if (!_offLineDict) { _offLineDict = [NSMutableDictionary dictionary]; } return _offLineDict; } @end