// // YOUPAIBBIMSessionVC.m // VQU // // Created by Elaine on 2021/10/25. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAIBBIMSessionVC.h" #import "YOUPAILCGiftModel.h" #import "YOUPAIUCIMVideoVC.h" #import "YOUPAILPChatTipView.h" #import "YOUPAIZYXianNvBangModel.h" #import "YOUPAIZYXianNvBangVC.h" #import "UIViewController+TFPresent.h" #import #import #import "YOUPAIBBCommonGiftView.h" #import "YOUPAILCUserShowInfo.h" #import "YOUPAIBBVideoRequestVC.h" @interface YOUPAIBBIMSessionVC() @property (nonatomic,strong) UIImageView *youpaipcurrentImgV; @property (nonatomic,strong) UIButton *youpaipxianNvBangBtn; @property(nonatomic,strong)YOUPAIZYXianNvBangModel *youpaipfairyInfo; @property (nonatomic,strong)SVGAPlayer *youpaipsvgaPlayer; @property (nonatomic,strong)SVGAParser *youpaipparser; @end @implementation YOUPAIBBIMSessionVC - (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; // [self youpaifaddTip]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NIMSDK sharedSDK].mediaManager stopRecord]; [[NIMSDK sharedSDK].mediaManager stopPlay]; } - (void)viewDidLoad{ [super viewDidLoad]; [self youpaifrefreshUserInfo]; self.youpaipsvgaPlayer = [[SVGAPlayer alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.youpaipsvgaPlayer.contentMode = UIViewContentModeScaleAspectFit; self.youpaipsvgaPlayer.delegate = self; self.youpaipsvgaPlayer.userInteractionEnabled = NO; [[[UIApplication sharedApplication] keyWindow] addSubview:self.youpaipsvgaPlayer]; self.youpaipsvgaPlayer.hidden = YES; self.youpaipsvgaPlayer.loops = 1; self.youpaipsvgaPlayer.clearsAfterStop = true; self.youpaipparser = [[SVGAParser alloc] init]; } //反色情提示 add by leo 注释 - (void)youpaifaddTip{ [LCHttpHelper requestWithURLString:ChatTip parameters:nil needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 if ([[[dict objectForKey:@"data"] allKeys] containsObject:@"fairy"]) { if ((![[LCSaveData getSysId] isEqualToString:self.session.sessionId]) && (![self.session.sessionId isEqualToString:@"11"]) ) { self.youpaipxianNvBangBtn.hidden = NO; self.youpaipfairyInfo = [YOUPAIZYXianNvBangModel mj_objectWithKeyValues:[[dict objectForKey:@"data"] objectForKey:@"fairy"]]; } }else{ self.youpaipxianNvBangBtn.hidden = YES; } NSString *tip = [[dict objectForKey:@"data"]objectForKey:@"tip"]; YOUPAILPChatTipView* chatTip = [[YOUPAILPChatTipView alloc]initWithFrame:CGRectMake(0, -40, KScreenWidth, 40)]; chatTip.tipLabel.text = tip; [self.view addSubview:chatTip]; [UIView animateWithDuration:1 animations:^{ chatTip.frame = CGRectMake(0, 0, KScreenWidth, 40); }completion:^(BOOL finished) { if (finished) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1 animations:^{ chatTip.frame = CGRectMake(0, -40, KScreenWidth, 40); }completion:^(BOOL finished) { if (finished) { [chatTip removeFromSuperview]; } }]; }); } }]; } } failure:^(NSError *error) { }]; } #pragma mark - 刷新对方用户信息 - (void)youpaifrefreshUserInfo{ [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[self.session.sessionId] completion:^(NSArray * _Nullable users, NSError * _Nullable error) { // NIMUser* user = users[0]; }]; } #pragma mark - Cell事件 图片/视频点击查看 add by leo 注释 - (BOOL)onTapCell:(NIMKitEvent *)event { BOOL youpaiphandled = [super onTapCell:event]; NSString *eventName = event.eventName; if ([eventName isEqualToString:NIMKitEventNameTapContent]) { NIMMessage *message = event.messageModel.message; NSDictionary *actions = [self youpaifcellActions]; NSString *value = actions[@(message.messageType)]; if (value) { SEL selector = NSSelectorFromString(value); if (selector && [self respondsToSelector:selector]) { if (message.messageType == NIMMessageTypeImage) { self.youpaipcurrentImgV = event.contentImage; } SuppressPerformSelectorLeakWarning([self performSelector:selector withObject:message]); youpaiphandled = YES; } } } else if([eventName isEqualToString:NIMKitEventNameTapLabelLink]) { NSString *link = event.data; NSArray* tempArray = [[LCSaveData getTAppUrl] componentsSeparatedByString:@","]; for (NSString* str in tempArray) { if([link rangeOfString:str].location !=NSNotFound) { ZCBaseWebVC* base = [[ZCBaseWebVC alloc]init]; base.contentUrl = link; [self.navigationController pushViewController:base animated:YES]; } } //跳转到控制器 youpaiphandled = YES; } if (!youpaiphandled) { NSAssert(0, @"invalid event"); } return youpaiphandled; } //头像点击事件 add by leo 注释 - (BOOL)onTapAvatar:(NIMMessage *)message{ [self.youpaipsessionInputView endEditing:YES]; YOUPAILZUserShowVC *vc = [[YOUPAILZUserShowVC alloc] init]; // if ([message.from isEqualToString:self.session.sessionId]) { // vc.youpaipuserId = message.session.sessionId; // }else{ // vc.youpaipuserId = [LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id; // } // [self.navigationController pushViewController:vc animated:YES]; NSString *userId = @""; if ([message.from isEqualToString:self.session.sessionId]) { userId = message.session.sessionId; }else{ userId = [LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id; } YMPersonalPageViewModel *personalPageVM = [[YMPersonalPageViewModel alloc]initWithParams:@{ ParamsId:@([userId intValue]) }]; [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_PERSONAL_PAGE) withUserInfo:@{ RouterViewModel:personalPageVM } completion:nil]; return YES; } #pragma mark - Cell Actions 图片 视频点击事件 - (NSDictionary *)youpaifcellActions { static NSDictionary *actions = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ actions = @{@(NIMMessageTypeImage) : @"showImage:", @(NIMMessageTypeVideo) : @"showVideo:", @(NIMMessageTypeCustom): @"showCustom:"}; }); return actions; } - (void)showImage:(NIMMessage *)message { [self.view endEditing:YES]; NSMutableArray* imageArray = [NSMutableArray array]; for (int i =0; i obj2.timestamp; }]; NSInteger count = imageArray.count; NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count]; for (NIMMessage *imageMessage in sortImageArray) { if (imageMessage.messageType == NIMMessageTypeImage) { NIMImageObject *object = (NIMImageObject *)imageMessage.messageObject; YBIBImageData *model = [YBIBImageData new]; model.imageURL = [NSURL URLWithString:object.url]; model.projectiveView = self.youpaipcurrentImgV; [photos addObject:model]; } } if (count!=0) { YBImageBrowser *browser = [YBImageBrowser new]; browser.dataSourceArray = photos; browser.currentPage = [sortImageArray indexOfObject:message]; [browser show]; } } - (void)showVideo:(NIMMessage *)message { NIMVideoObject *object = (NIMVideoObject*)message.messageObject; YOUPAILCIMVideoVC *playerViewController = [[YOUPAILCIMVideoVC alloc] initWithVideoObject:object]; [self pushEffectPresentToVC:playerViewController]; if(![[NSFileManager defaultManager] fileExistsAtPath:object.coverPath]){ //如果封面图下载了,点进视频的时候再去下一把封面图 __weak typeof(self) wself = self; [[NIMSDK sharedSDK].resourceManager download:object.coverUrl filepath:object.coverPath progress:nil completion:^(NSError *error) { if (!error) { [wself uiUpdateMessage:message]; } }]; } } //add by leo 注释 自定义消息 - (void)showCustom:(NIMMessage *)message { //普通的自定义消息点击事件可以在这里做哦~ NIMCustomObject *object = (NIMCustomObject*)message.messageObject; if ([object.attachment isKindOfClass:[YOUPAILCOneImageAttachment class]]) { YOUPAILCOneImageAttachment *attachment = (YOUPAILCOneImageAttachment *)object.attachment; if (attachment.link_type==1) {//打开H5页面 ZCBaseWebVC* web = [[ZCBaseWebVC alloc]init]; web.contentUrl = attachment.link_url; [self.navigationController pushViewController:web animated:YES]; }else if (attachment.link_type==2){//跳转本地页面 [self youpaifpageToStr:attachment.link_url]; } }else if ([object.attachment isKindOfClass:[YOUPAILCMultiImageAttachment class]]){ YOUPAILCMultiImageAttachment *attachment = (YOUPAILCMultiImageAttachment *)object.attachment; NSDictionary* dic; if ([message.localExt objectForKey:@"serial"]) { NSInteger ser = [[message.localExt objectForKey:@"serial"]integerValue]; dic = attachment.imageArray[ser]; }else{ dic = attachment.imageArray[0]; } if ([[dic objectForKey:@"link_type"] integerValue]==1) {//打开H5页面 ZCBaseWebVC* web = [[ZCBaseWebVC alloc]init]; web.contentUrl = [dic objectForKey:@"link_url"]; [self.navigationController pushViewController:web animated:YES]; }else if ([[dic objectForKey:@"link_type"] integerValue]==2){//跳转本地页面 [self youpaifpageToStr:[dic objectForKey:@"link_url"]]; } }else if([object.attachment isKindOfClass:[YOUPAILCMessageTxtAttachment class]]){ YOUPAILCMessageTxtAttachment *attachment = (YOUPAILCMessageTxtAttachment *)object.attachment; if (attachment.link_type!=0) { if (attachment.link_type==1) {//打开H5页面 ZCBaseWebVC* web = [[ZCBaseWebVC alloc]init]; web.contentUrl = attachment.link_url; [self.navigationController pushViewController:web animated:YES]; }else if (attachment.link_type==2){//跳转本地页面 [self youpaifpageToStr:attachment.link_url]; }else if(attachment.link_type==3){//执行本地逻辑 NSDictionary* dict = [attachment.link_url mj_JSONObject]; if([[dict objectForKey:@"action"]isEqualToString:@"add_qq_group"]){ NSString* qqgroup = [[dict objectForKey:@"data"]objectForKey:@"groupId"]; NSString* qqkey = [[dict objectForKey:@"data"]objectForKey:@"iosKey"]; NSString *urlStr = [NSString stringWithFormat:@"mqqapi://card/show_pslcard?src_type=internal&version=1&uin=%@&key=%@&card_type=group&source=external", qqgroup,qqkey]; NSURL *url = [NSURL URLWithString:urlStr]; if([[UIApplication sharedApplication] canOpenURL:url]){ [[UIApplication sharedApplication] openURL:url]; }else{ [ZCHUDHelper showTitle:[NSString stringWithFormat:@"你可以加入我们官方QQ群:%@",qqgroup]showtime:3.0]; } } } } }else if ([object.attachment isKindOfClass:[YOUPAILPFollowAttachment class]]){ YOUPAILPFollowAttachment *attachment = (YOUPAILPFollowAttachment *)object.attachment; //v1.0.7 type为18跳到聊天页面 if(attachment.type==16||attachment.type==17) { // LCUserShowVC* userShow = [[LCUserShowVC alloc]init]; // YOUPAILZUserShowVC* userShow = [[YOUPAILZUserShowVC alloc]init]; // userShow.youpaipuserId = [NSString stringWithFormat:@"%zd",attachment.userid]; // [self.navigationController pushViewController:userShow animated:YES]; YMPersonalPageViewModel *personalPageVM = [[YMPersonalPageViewModel alloc]initWithParams:@{ ParamsId:@(attachment.userid) }]; [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_PERSONAL_PAGE) withUserInfo:@{ RouterViewModel:personalPageVM } completion:nil]; } else { NSString* userId = [NSString stringWithFormat:@"%zd",attachment.userid]; if (userId) { NIMSession *session = [NIMSession session:userId type:NIMSessionTypeP2P]; if (session) { @weakify(self); [ZCHUDHelper show]; [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[session.sessionId] completion:^(NSArray * _Nullable users, NSError * _Nullable error) { @strongify(self); [ZCHUDHelper dismiss]; YOUPAIBBIMSessionVC *vc = [[YOUPAIBBIMSessionVC alloc] initWithSession:session]; [self.navigationController pushViewController:vc animated:YES]; }]; } } } } } #pragma mark - NIMInputActionDelegate 礼物 视频 语音 照片 啥的 注释by leo - (void)onCameraClick{ [self.youpaipinteractor mediaShootPressed]; } - (void)onPhotoClick{ [self.youpaipinteractor mediaPicturePressed]; } #pragma mark SVGAPlayerDelegate //- (void)svgaPlayerDidAnimatedToPercentage:(CGFloat)percentage //{ // NSLog(@"percentage == %lf",percentage); // if (percentage == 1) { //// [self.player stopAnimation]; //// self.player.hidden = YES; // } //} - (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player { self.youpaipsvgaPlayer.hidden = YES; } - (void)onGiftClick{ [self.youpaipsessionInputView endEditing:YES]; YOUPAIBBCommonGiftView *view = [YOUPAIBBCommonGiftView new]; view.isTouchDismiss = YES; view.youpaipisAnchor = NO; view.vc = self; @weakify(self); [view setYoupaiplzgiftblock:^(YOUPAILCGiftModel * _Nonnull giftModel, NSInteger count,NSString*categoryID) { @strongify(self); [LCHttpHelper requestWithURLString:SendGift_New parameters:@{@"type":@(2),@"to_uid":self.session.sessionId,@"gift_id":giftModel.youpaipid,@"num":@(count),@"gift_type_id":categoryID} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 NSInteger coin = [[[dict objectForKey:@"data"]objectForKey:@"coin"]integerValue]; // [ZCHUDHelper showTitle:@"礼物赠送成功"]; [self.youpaipsvgaPlayer stopAnimation]; if (giftModel.youpaiptype == 1 && ![giftModel.youpaipsvga isEqualToString:@""]) { self.youpaipsvgaPlayer.hidden = NO; } NSString* svgaName = [[giftModel.youpaipsvga componentsSeparatedByString:@"/"] lastObject]; NSLog(@"%@",giftModel.youpaipsvga); NSString* svgaCanchesPath= [[NSString alloc]initWithFormat:@"%@/%@/%@",CachesPath,@"SVGA",svgaName]; NSLog(@"%@",svgaCanchesPath); if (![LCTools giftSVGAWithSvgaUrlStr:giftModel.youpaipsvga]){ NSString* urlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,giftModel.youpaipsvga]; @weakify(self); [self.youpaipparser parseWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) { @strongify(self); if (videoItem != nil) { self.youpaipsvgaPlayer.videoItem = videoItem; [self.youpaipsvgaPlayer startAnimation]; } } failureBlock:^(NSError * _Nullable error) { @strongify(self); self.youpaipsvgaPlayer.hidden = YES; }]; [LCTools giftSVGAWithSvgaUrlStr:giftModel.youpaipsvga]; }else{ @weakify(self); [self.youpaipparser parseWithData:[LCTools giftSVGAWithSvgaUrlStr:giftModel.youpaipsvga] cacheKey:svgaCanchesPath completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) { @strongify(self); if (videoItem != nil) { self.youpaipsvgaPlayer.videoItem = videoItem; [self.youpaipsvgaPlayer startAnimation]; } } failureBlock:^(NSError * _Nonnull error) { @strongify(self); self.youpaipsvgaPlayer.hidden = YES; }]; } } } failure:^(NSError *error) { }]; }]; [self TFPresentVC:view completion:^{ }]; } - (void)onVideoClick{ if ([self.session.sessionId isEqualToString:[LCSaveData getServerId]]) { [ZCHUDHelper showTitle:@"暂时不能和客服视频通话"]; return ; } //v1.1.2 增加相机权限判断 [UCAuthorityManager cameraAuthority:^{ [self.youpaipsessionInputView endEditing:YES]; [LCHttpHelper requestWithURLString:UserShowPage parameters:@{@"user_id":self.session.sessionId} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code==0) {//成功 NSDictionary *data = [dict objectForKey:@"data"]; YOUPAILCUserShowInfo *info = [YOUPAILCUserShowInfo mj_objectWithKeyValues:[data objectForKey:@"info"]]; YOUPAIBBVideoRequestVC* chatRequest = [[YOUPAIBBVideoRequestVC alloc]init]; YOUPAILCVideoChatModel *videoModel = [[YOUPAILCVideoChatModel alloc] init]; videoModel.youpaiproom_id = [NSString stringWithFormat:@"C%@",[LCTools randomNumberWithLength:6]]; videoModel.youpaipauditState = 1; videoModel.youpaipfrom_uid = [LCSaveModel getUserModel].youpaipuserinfo.youpaipuser_id; videoModel.youpaipfrom_nickname = [LCSaveModel getUserModel].youpaipuserinfo.youpaipnickname; videoModel.youpaipfrom_avatar = [LCSaveModel getUserModel].youpaipuserinfo.youpaipavatar; videoModel.youpaipto_uid = self.session.sessionId; videoModel.youpaipto_nickname = info.youpaipnickname; videoModel.youpaipto_avatar = info.youpaipavatar; chatRequest.youpaipvideoModel = videoModel; ZCBaseNavigationVC *nav = [[ZCBaseNavigationVC alloc]initWithRootViewController:chatRequest]; //modify by leo fix bug ios13 nav.modalPresentationStyle =0; [self.navigationController presentViewController:nav animated:YES completion:^{ }]; } } failure:^(NSError *error) { }]; } denied:^{ UIAlertController *systemAlert = [UIAlertController alertControllerWithTitle:@"请在iphone的“设置-隐私-相机”选项中,允许APP访问您的相机。" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"现在去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } else { // 去系统设置页面 if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil]; } else { // Fallback on earlier versions } } }]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [systemAlert addAction:cancelAction]; [systemAlert addAction:action]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController: systemAlert animated: YES completion: nil]; }); }]; } - (void)pushEffectPresentToVC:(UIViewController*)vc{ CATransition *transition = [CATransition animation]; transition.duration = 0.25; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; transition.type = kCATransitionPush; transition.subtype = kCATransitionFromTop; [self.navigationController.view.layer addAnimation:transition forKey:nil]; [self.navigationController pushViewController:vc animated:NO ]; } //接收消息 - (void)onRecvMessages:(NSArray *)messages { [super onRecvMessages:messages]; NIMMessage *message = messages.firstObject; NIMSession *session = message.session; if (![session isEqual:self.session] || !messages.count) { return; } if (message.messageType == NIMMessageTypeCustom){ NIMCustomObject *object = (NIMCustomObject*)message.messageObject; if ([object.attachment isKindOfClass:[YOUPAILCGiftAttachment class]]) { YOUPAILCGiftAttachment* attachment = (YOUPAILCGiftAttachment*)object.attachment; // [ZCHUDHelper showTitle:@"收到一个礼物"]; NSLog(@"%@",attachment.gift_url); NSLog(@"%@",attachment.gift_svga); [self.youpaipsvgaPlayer stopAnimation]; if (attachment.gift_type == 1 && ![attachment.gift_svga isEqualToString:@""]) { self.youpaipsvgaPlayer.hidden = NO; } NSString* svgaName = [[attachment.gift_svga componentsSeparatedByString:@"/"] lastObject]; NSString* svgaCanchesPath= [[NSString alloc]initWithFormat:@"%@/%@/%@",CachesPath,@"SVGA",svgaName]; NSLog(@"%@",svgaCanchesPath); if (![LCTools giftSVGAWithSvgaUrlStr:attachment.gift_svga]){ NSString* urlStr = [NSString stringWithFormat:@"%@/%@",[LCSaveData getImageUrl]?[LCSaveData getImageUrl]:BaseImgUrl,attachment.gift_svga]; @weakify(self); [self.youpaipparser parseWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) { @strongify(self); if (videoItem != nil) { self.youpaipsvgaPlayer.videoItem = videoItem; [self.youpaipsvgaPlayer startAnimation]; } } failureBlock:^(NSError * _Nullable error) { @strongify(self); self.youpaipsvgaPlayer.hidden = YES; }]; [LCTools giftSVGAWithSvgaUrlStr:attachment.gift_svga]; }else{ @weakify(self); [self.youpaipparser parseWithData:[LCTools giftSVGAWithSvgaUrlStr:attachment.gift_svga] cacheKey:svgaCanchesPath completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) { @strongify(self); if (videoItem != nil) { self.youpaipsvgaPlayer.videoItem = videoItem; [self.youpaipsvgaPlayer startAnimation]; } } failureBlock:^(NSError * _Nonnull error) { @strongify(self); self.youpaipsvgaPlayer.hidden = YES; }]; } } } } @end