Browse Source

修改视频重复下载问题

ganzf 5 months ago
parent
commit
49731109f6

+ 5 - 1
AIIM/AIIM/Common/Network/GBaseNetApi.m

@@ -522,7 +522,11 @@
         }
     } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
         NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
-            return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
+        NSString *fileName = [response suggestedFilename];
+        NSString *fileExtension = [fileName pathExtension];
+        NSString *newfileName = [NSString stringWithFormat:@"%@.%@", [[NSUUID UUID] UUIDString],fileExtension];
+        documentsDirectoryURL = [documentsDirectoryURL URLByAppendingPathComponent:newfileName];
+        return documentsDirectoryURL;
     } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
 //        NSLog(@"333:%@",filePath);
         [self removeDownloadTanckobj:thrid];

+ 1 - 1
AIIM/AIIM/Common/Network/aliOSS/OSSManager.m

@@ -150,7 +150,7 @@
     resumableUpload.objectKey = objectKey;
     resumableUpload.partSize = 1024 * 1024;
     resumableUpload.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
-        NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
+//        NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
         CGFloat progress = 1.f * totalByteSent / totalBytesExpectedToSend;
 //        NSLog(@"上传文件进度: %f", progress);
         NSInteger pcent =progress*100;

+ 3 - 3
AIIM/AIIM/Common/Store/LoginStateManager.m

@@ -98,9 +98,9 @@
             }
         } fail:^(NSError * _Nonnull error) {
             NSLog(@"%@", error);
-            self->_loggedIn = NO;
-            [self clearLoginStatus];
-            !self.checkLoginState ?: self.checkLoginState(NO);
+//            self->_loggedIn = NO;
+//            [self clearLoginStatus];
+//            !self.checkLoginState ?: self.checkLoginState(NO);
         }];
     }
     else{

+ 43 - 16
AIIM/AIIM/Controller/chat/ChatController.m

@@ -1984,28 +1984,45 @@ static const CGFloat kQuoteViewHeight = 46.0f;
     NSLog(@"UpInside----------");
     [_recorder stop];
     _recorder=nil;
-    NSURL *fileURL = [NSURL fileURLWithPath:_AudiofilePath];
+    NSURL *yuanfileURL = [NSURL fileURLWithPath:_AudiofilePath];
     if(_rcdL<2){//语音太短
-        [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
+        [[NSFileManager defaultManager] removeItemAtURL:yuanfileURL error:nil];
         [self.heatBeat invalidate];
         self.heatBeat = nil;
         [MBProgressHUD showWithText:@"語音太短"];
         return;
     }
+    NSLog(@"_AudiofilePath:%@",_AudiofilePath);
+    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
+    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:yuanfileURL.lastPathComponent]; // 目标文件路径
+    
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSError *error = nil;
+    BOOL success = [fileManager copyItemAtPath:_AudiofilePath toPath:destinationPath error:&error];
+    if (success) {
+        NSLog(@"文件成功复制到本地");
+    } else {
+        NSLog(@"文件复制失败: %@", error.localizedDescription);
+    }
+    NSLog(@"destinationPath:%@",destinationPath);
+    
+    
+    NSURL *fileURL = [NSURL fileURLWithPath:destinationPath];
     
     ChatFileModel * model = [[ChatFileModel alloc] initWithURL:fileURL];
     model.fileURL = fileURL;
-    model.filePath = _AudiofilePath;
+    model.filePath = destinationPath;
     NSString *strtime = [self getLocalTime];
     //普通上传
+    [self sendYuyinMsg:model.fileURL.lastPathComponent filePth:destinationPath localtime:strtime state:0];
     [OSSManager.sharedManager asyncPutFile:model.fileURL.lastPathComponent localFilePath:model.filePath thrid:strtime progress:^(NSInteger pcent, NSString * loacaltime) {
 //        [self Ossupdatajidu:loacaltime uploadProgress:pcent];
     } success:^(id res, NSString * loacaltime) {
-        [self sendYuyinMsg:model.fileURL.lastPathComponent localtime:loacaltime];
-        [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
+        [self sendYuyinMsg:model.fileURL.lastPathComponent filePth:destinationPath localtime:loacaltime state:1];
+        [[NSFileManager defaultManager] removeItemAtURL:yuanfileURL error:nil];
     } failure:^(NSError * error, NSString * loacaltime) {
-        
-        [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
+//        [self sendYuyinMsg:model.fileURL.lastPathComponent localtime:loacaltime state:2];
+        [[NSFileManager defaultManager] removeItemAtURL:yuanfileURL error:nil];
     }];
    
     
@@ -2014,21 +2031,24 @@ static const CGFloat kQuoteViewHeight = 46.0f;
 
 }
 
--(void)sendYuyinMsg:(NSString *)url localtime:(NSString *)loacaltime{
+-(void)sendYuyinMsg:(NSString *)fileName filePth:(NSString *)filePtah localtime:(NSString *)loacaltime state:(NSInteger)index{
     NSString *filePath;
-    if([url containsString:@"http"]){
-        filePath=url;
+    if([fileName containsString:@"http"]){
+        filePath=fileName;
     }
     else{
-        filePath = [NSString stringWithFormat:@"http://oss.abtim-my.com/%@",url];
+        filePath = [NSString stringWithFormat:@"http://oss.abtim-my.com/%@",fileName];
     }
     NSString *content=@"【语音】";
     NSString *messageType=[NSString stringWithFormat:@"%@",MessageType_voice];
     content= [CryptoAES.shareInstance encryptStringL:content];
     NSString *type = [NSString stringWithFormat:@"%ld",(long)self.type];
-    
+    NSString *localId =loacaltime;
+    if(index==1){
+        localId=@"";
+    }
     NSDictionary *dic =@{
-        @"id":@"",
+        @"id":localId,
         @"messageType":messageType,
         @"chatId":self.chatId,
         @"fromId":self.userId,
@@ -2043,9 +2063,12 @@ static const CGFloat kQuoteViewHeight = 46.0f;
         @"extend":@{
             @"url":filePath,
             @"time":[NSString stringWithFormat:@"%ld",self->_rcdL],
-            @"localurl":@""
+            @"localurl":filePtah
         }
     };
+    if(index==0){
+        [ChatsStore.shareInstance reciveMsg:dic];
+    }
     NSDictionary *sendInfo = @{
         @"code":@"2",
         @"message":dic,
@@ -2058,7 +2081,10 @@ static const CGFloat kQuoteViewHeight = 46.0f;
         NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
         NSLog(@"%@", jsonString); // 输出: {"name":"John","age":25}
         NSLog(@"----发送语音----");
-        [GWebSocket.shareInstance sendMsg:jsonString];
+        if(index==1){
+            [GWebSocket.shareInstance sendMsg:jsonString];
+        }
+        
     }
 }
 
@@ -2435,13 +2461,14 @@ static const CGFloat kQuoteViewHeight = 46.0f;
 }
 
 
+
 #pragma mark 其他聊天室的消息弹窗
 - (void)othersChatMessageAlert:(NSNotification *)notification {
 //    NSLog(@"othersChatMessageAlert---1");
     if ([self isViewLoaded] && self.view.window) {
         NSDictionary * msgDict = notification.object;
         NSMutableDictionary * mutablemsg = msgDict.mutableCopy;
-//        NSLog(@"othersChatMessageAlert---2:%@",mutablemsg);
+        NSLog(@"othersChatMessageAlert---2:%@",mutablemsg);
         if (msgDict) {
             ChatMessageModel *message = [ChatMessageModel modelWithDictionary:mutablemsg];
             if ([message.chatId isEqualToString:self.chatId] || message.isSender ) {

+ 1 - 0
AIIM/AIIM/Controller/chat/chetCell/ChatMessageModel.h

@@ -89,6 +89,7 @@ typedef NS_ENUM(NSUInteger, ChatMessageType) {
 //下载文件保存本地
 -(void)xiazaiwenjian:(void(^)(NSInteger persent))loading;
 -(void)xiazaishipin:(void(^)(NSInteger persent))loading;
+-(void)xiazaiyuyin:(void(^)(NSInteger persent))loading;
 
 
 //上传文件

+ 58 - 3
AIIM/AIIM/Controller/chat/chetCell/ChatMessageModel.m

@@ -341,15 +341,18 @@
 
 //下载视频文件
 -(void)xiazaishipin:(void(^)(NSInteger persent))loading{
-   
+    NSLog(@"xiazaishipin--------");
+    
     if(!self.isSender){
-        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
-            NSLog(@"文件已经存在本地");
+        NSString *localFileurl = [self.localurl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLPathAllowedCharacterSet]];
+        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:localFileurl]){
+//            NSLog(@"文件已经存在本地");
             if (loading) {
                 loading(100);
             }
         }
         else{
+//            NSLog(@"文件不存在");
             if(self.url&&self.url.length != 0){
                 [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
     //                NSLog(@"11---downLoadWToken:%d",code);
@@ -394,6 +397,58 @@
         return;
     }
     else{
+//        NSLog(@"self.localurl2:%@",self.localurl);
+        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
+//            NSLog(@"文件已经存在本地");
+            if (loading) {
+                loading(100);
+            }
+            return;
+        }
+        
+        [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
+            if(res!=nil){
+                NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];
+                NSMutableDictionary *extend = [self.formerMessage[@"extend"] mutableCopy];
+                [extend setObject:res[@"filePath"] forKey:@"localurl"];
+                [mutableDict setObject:extend forKey:@"extend"];
+                self.localurl = res[@"filePath"];
+                NSDictionary *NewMsg = [mutableDict copy];
+                [ChatsStore.shareInstance reciveMsg:NewMsg];
+                if (loading) {
+                    loading(100);
+                }
+            }
+            else{
+                if (code>=0&&code<200) {
+                    if (loading) {
+                        loading(code);
+                    }
+                }
+            }
+        } fail:^(NSError * _Nonnull error) {
+            NSLog(@"error:%@",error);
+            NSLog(@"%@",self.url);
+        }];
+    }
+}
+
+
+-(void)xiazaiyuyin:(void(^)(NSInteger persent))loading{
+    NSLog(@"xiazaiwenjian---");
+    if (self.isSender) {
+        return;
+    }
+    else{
+//        NSLog(@"self.localurl2:%@",self.localurl);
+        if (self.localurl.length != 0 && [[NSFileManager defaultManager] fileExistsAtPath:self.localurl]){
+//            NSLog(@"文件已经存在本地");
+            if (loading) {
+                loading(100);
+            }
+            return;
+        }
+        
         [FileNetApi downLoadWToken:getURL(self.url) thrid:self.msgId succ:^(int code, NSDictionary * res) {
             if(res!=nil){
                 NSMutableDictionary *mutableDict = [self.formerMessage mutableCopy];

+ 32 - 5
AIIM/AIIM/Controller/chat/chetCell/chatCellView.m

@@ -910,7 +910,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
                 }
             });
             
-//            NSLog(@"下载完成:%@",self->_messageModel.localurl);
+            NSLog(@"下载完成:%@",self->_messageModel.localurl);
         }
         else{
 //            NSLog(@"下载中:%ld",(long)persent);
@@ -992,7 +992,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
 
     
     [_messageModel xiazaiwenjian:^(NSInteger persent) {
-        NSLog(@"setupFileContent----------");
+//        NSLog(@"setupFileContent----------");
         if(persent>=100){
             NSLog(@"setupFileContent:下载完成:%@",self->_messageModel.localurl);
             dispatch_async(dispatch_get_main_queue(), ^{
@@ -1002,7 +1002,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
             });
         }
         else{
-            NSLog(@"setupFileContent 下载中:%ld",(long)persent);
+//            NSLog(@"setupFileContent 下载中:%ld",(long)persent);
             dispatch_async(dispatch_get_main_queue(), ^{
                 if(sizeLabel){
                     sizeLabel.alpha = 1;
@@ -1057,6 +1057,17 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         voiceIcon.animationImages = @[kImageMake(@"audio_receiver_animate1"),kImageMake(@"audio_receiver_animate2"),kImageMake(@"audio_receiver_animate3")];
     }
     
+    [_messageModel xiazaiyuyin:^(NSInteger persent) {
+//        NSLog(@"setupFileContent----------");
+        if(persent>=100){
+            NSLog(@"setupFileContent:下载完成:%@",self->_messageModel.localurl);
+        }
+        else{
+//            NSLog(@"setupFileContent 下载中:%ld",(long)persent);
+        }
+    }];
+    
+    
     _currentContentView = _voiceContentView;
 }
 
@@ -1597,6 +1608,7 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     if (_messageModel.messageType == ChatMessageTypeVoice) {
         // 处理语音点击
         NSLog(@"点击了语音: %@", _messageModel.url);
+        
         if (self.player) {
             AVPlayerItem *playerItem = self.player.currentItem;
             CMTime currentTime = playerItem.currentTime;
@@ -1623,13 +1635,28 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         
         
         NSString *urlstr = _messageModel.url;
+        NSURL *Url = [NSURL URLWithString:urlstr];
+        
+        NSLog(@"本地路径:%@",_messageModel.localurl);
+        if (_messageModel.localurl && [[NSFileManager defaultManager] fileExistsAtPath:_messageModel.localurl]) {
+            NSLog(@"本地路径:");
+            urlstr=_messageModel.localurl;
+            Url = [NSURL fileURLWithPath:urlstr];
+        }
+        else{
+            
+        }
+        NSLog(@"播放: %@", Url);
+        
         AVAudioSession *audioSession = [AVAudioSession sharedInstance];
         [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
         [audioSession setActive:YES error:nil];
         
-        NSURL *Url = [NSURL URLWithString:urlstr];
         
-        AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
+        
+        AVPlayerItem *pitem = [AVPlayerItem playerItemWithURL:Url];
+        
+        //AVPlayerItem *pitem = [[AVPlayerItem alloc] initWithURL:Url];
         
         _player = [AVPlayer playerWithPlayerItem:pitem];
         [[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //建议在播放之前设置yes,播放结束设置NO。这个功能是开启红外感应

+ 1 - 1
AIIM/AIIM/Controller/login/APPLockView.m

@@ -258,7 +258,7 @@
             }
         } fail:^(NSError * _Nonnull error) {
             NSLog(@"%@", error);
-            [[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
+//            [[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
             [self hide];
         }];
     }

+ 11 - 3
AIIM/AIIM/Controller/login/GLoginController.m

@@ -26,6 +26,8 @@
 
 @property (nonatomic)  BOOL isagreed;
 
+@property (nonatomic)  NSInteger reLoginC;
+
 
 @end
 
@@ -48,7 +50,7 @@
         }
     }
     [self initsubView];
-    
+    _reLoginC = 0;
     [self getUserinfo];
 }
 
@@ -183,7 +185,12 @@
         }
     } fail:^(NSError * _Nonnull error) {
         NSLog(@"--%@",error);
-        [MBProgressHUD showWithText:@"网络或服务器异常"];
+        if(self->_reLoginC<3){
+            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+                self->_reLoginC=self->_reLoginC+1;
+                
+            });
+        }
     }];
 }
 
@@ -248,7 +255,8 @@
             
         } fail:^(NSError * _Nonnull error) {
             NSLog(@"%@", error);
-            [MBProgressHUD showWithText:@"网络或服务器异常"];
+            // 使用GCD创建一个在3秒后执行的定时器
+            
         }];
     }
 }

+ 2 - 2
AIIM/AIIM/Controller/mine/AppLockPasswordController.m

@@ -439,8 +439,8 @@
             }
         } fail:^(NSError * _Nonnull error) {
             NSLog(@"%@", error);
-            [[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
-            [self dismissViewControllerAnimated:YES completion:nil];
+            //[[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
+            //[self dismissViewControllerAnimated:YES completion:nil];
         }];
     }
     else{