Sfoglia il codice sorgente

移除自动重发逻辑

zwp 4 mesi fa
parent
commit
63ec95b6ef

+ 4 - 0
AIIM/AppDelegate.m

@@ -1077,5 +1077,9 @@
     }
 }
 
+- (void)applicationWillTerminate:(UIApplication *)application {
+    [[OSSManager sharedManager] applicationWillTerminate];
+}
+
 
 @end

+ 2 - 1
AIIM/Common/Network/aliOSS/OSSManager.h

@@ -50,7 +50,8 @@ NS_ASSUME_NONNULL_BEGIN
 -(void)stopUploadTanck:(NSString *)thrid;
 
 - (NSString *)fullUploadURLByAppendPath:(NSString *)path;
-
+- (void)applicationWillTerminate;
+- (NSString *)fileMessageKeyWith:(NSString *)strtime;
 @end
 
 NS_ASSUME_NONNULL_END

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

@@ -25,6 +25,7 @@
 
 @property (nonatomic, strong) NSDateFormatter *formatter;
 @property (nonatomic, copy) NSString *month;
+@property (nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *taskDict;
 @end
 
 
@@ -43,6 +44,7 @@
 - (instancetype)init {
     if (self = [super init]) {
         _uploadTanck = [NSMutableArray new];
+        _taskDict = [NSMutableDictionary dictionary];
     }
     return self;
 }
@@ -256,7 +258,7 @@
         NSLog(@"PrasyncResumableUploadFile error: %@", path);
         return;
     }
-    
+    NSString *fileKey = [self fileMessageKeyWith:strtime];
     // 获取UploadId上传文件。
     OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
     resumableUpload.bucketName = self.OSSInfo[@"bucket"];
@@ -278,6 +280,10 @@
             [self.delegate stateChange:dic];
         }
         
+        dispatch_main_async_safe(^{
+            [self.taskDict setValue:@(pcent) forKey:fileKey];
+        })
+        
 //        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 //            prgress(pcent,strtime);
 //        });
@@ -297,6 +303,9 @@
             if ([task.error.domain isEqualToString:OSSClientErrorDomain] && task.error.code == OSSClientErrorCodeCannotResumeUpload) {
                 // 此任务无法续传,需获取新的uploadId重新上传。
             }
+            dispatch_main_async_safe(^{
+                [[NSUserDefaults standardUserDefaults] setValue:@(-1) forKey:fileKey];
+            })
             [self updataFileMsg:strtime state:@"3"];
 //            if(self.delegate){
 //                NSDictionary *dic=@{
@@ -308,6 +317,9 @@
 //            }
         } else {
             NSLog(@"Upload file success");
+            dispatch_main_async_safe(^{
+                [[NSUserDefaults standardUserDefaults] setValue:@(100) forKey:fileKey];
+            })
             [self updataFileMsg:strtime state:@"2"];
 //            if(self.delegate){
 //                NSDictionary *dic=@{
@@ -490,4 +502,18 @@
     return [NSString stringWithFormat:@"%@/%@", self.month, objectKey];;
 }
 
+- (void)applicationWillTerminate {
+    NSLog(@"applicationWillTerminate: %@", self.taskDict);
+    for (NSString *key in self.taskDict.allKeys) {
+        NSInteger progress = [self.taskDict[key] integerValue];
+        if (progress < 100) {
+            [[NSUserDefaults standardUserDefaults] setValue:@(-1) forKey:key];
+        }
+    }
+}
+
+- (NSString *)fileMessageKeyWith:(NSString *)strtime {
+    return [NSString stringWithFormat:@"k_oss_file_%@", strtime];
+}
+
 @end

+ 2 - 125
AIIM/Controller/chat/ChatController.m

@@ -291,14 +291,11 @@ static const CGFloat kQuoteViewHeight = 46.0f;
 -(void)viewDidAppear:(BOOL)animated{
     [super viewDidAppear:animated];
     _titlelb.text = self.titlename;
-    [self startTimer];
-//    NSLog(@"viewDidAppear-----:%@",self);
+
 }
 
 -(void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];
-    [self endTimer];
-//    NSLog(@"viewWillDisappear-----");
     
 }
 
@@ -1343,130 +1340,10 @@ static const CGFloat kQuoteViewHeight = 46.0f;
 -(void)dismissBotton{
     [self hidBottonView];
 }
-#pragma mark 定时服务
--(void)startTimer{
-    if(self.timer==nil){
-        self.timer = [NSTimer scheduledTimerWithTimeInterval:10.1 target:self selector:@selector(TimerAction) userInfo:nil repeats:YES];
-        [self.timer setFireDate:[NSDate distantPast]];
-        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
-    }
-}
-
--(void)endTimer{
-    [self.timer invalidate];
-    self.timer = nil;
-}
-
--(void)TimerAction{
-    //处理重发事件
-//    NSLog(@"self.msgList:%@",self.msgList);
-    for(NSDictionary *dis in self.msgList){
-        NSString *mine = dis[@"mine"];
-        if(mine.boolValue){
-            if([dis[@"id"] isEqualToString:dis[@"localtime"]]){
-                [self resendMsg:dis.copy];
-            }
-        }
-    }
-}
-
--(BOOL)canResend:(NSDictionary *)msg{
-    NSLog(@"canResend:%@",msg);
-    NSString *localtime = msg[@"localtime"];
-    NSString *messageType = msg[@"messageType"];
-    if([messageType isEqualToString:MessageType_text]){
-        for (NSDictionary *item in self.msgList) {
-            NSString *itlocaltime =item[@"localtime"];
-            if([localtime isEqualToString:itlocaltime]){
-                NSLog(@"canResend2:%@",item);
-                if(![item[@"id"] isEqualToString:item[@"localtime"]]){
-                    return false;
-                }
-                NSDate *now = [NSDate date];
-                NSTimeInterval trt = [now timeIntervalSince1970];
-                NSNumber *tt =msg[@"localtime"];
-                if((trt-tt.integerValue/1000)>40){
-                    //发送失败
-                    NSLog(@"发送失败-------");
-                    return false;
-                }
-                
-            }
-        }
-    }
-    else{
-        [self fileUploadState:msg];
-        return false;
-    }
-    
-    return YES;
-}
-
--(void)fileUploadState:(NSDictionary *)msg{
-    NSString *localtime = msg[@"localtime"];
-    NSString *messageType = msg[@"messageType"];
-    if([messageType isEqualToString:MessageType_file]||[messageType isEqualToString:MessageType_image]||[messageType isEqualToString:MessageType_video]) {
-        if([OSSManager.sharedManager isuploadTanck:localtime]){
-            return;
-        }
-        for (NSDictionary *item in self.msgList) {
-            
-            NSString *itlocaltime =item[@"localtime"];
-            if([localtime isEqualToString:itlocaltime]){
-                NSLog(@"canResend2:%@",item);
-                if(![item[@"id"] isEqualToString:item[@"localtime"]]){
-                    return;
-                }
-                NSDictionary *extend =item[@"extend"];
-                NSString *url =extend[@"url"];
-                if([url isEqualToString:@""]){
-                    NSString *savedPath =extend[@"localurl"];
-                    NSURL * fileUrl = [NSURL fileURLWithPath:savedPath];
-                    ChatFileModel * model = [[ChatFileModel alloc] initWithURL:fileUrl];
-                    model.fileURL = fileUrl;
-                    model.filePath = savedPath;
-                    [self OSSuploadFile:model thrid:localtime];
-                    NSLog(@"重发文件");
-                    [self FileReUpload:localtime];
-                }
-                
-            }
-        }
-    }
-}
-
-
--(void)FileReUpload:(NSString *)localtime{
-    [GDBManager.shareInstance selectLocalmsgWithLocaltime:localtime succ:^(NSArray * _Nullable array) {
-        NSLog(@"selectLocalmsgWithLocaltime:%@",array);
-        
-        if(array){
-            if(array.count>0){
-                NSDictionary *msg =array[0];
-                NSString *msgtype = msg[@"messageType"];
-                
-                if (([msgtype isEqualToString:MessageType_Del]||[msgtype isEqualToString:MessageType_CallBack2])) {
-                    return;
-                }
-                NSDictionary *extend=msg[@"extend"];
-                NSMutableDictionary *mextend = [extend mutableCopy];
-                [mextend setObject:[NSNumber numberWithInt:0] forKey:@"fileError"];
-                NSMutableDictionary *mmsg = [msg mutableCopy];
-                [mmsg setObject:mextend forKey:@"extend"];
-                [ChatsStore.shareInstance reciveMsg:mmsg];
-            }
-        }
-    } fail:^(NSString * _Nullable error) {
-        ;
-    }];
-}
-
 
 //重发消息
 -(void)resendMsg:(NSDictionary *)msg{
-    if(![self canResend:msg]){
-        return;
-    }
+    
     NSMutableDictionary *dic = [msg mutableCopy];
     NSString *resend =dic[@"reSend"];
     NSLog(@"resend:%@",[resend class]);

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

@@ -10,6 +10,12 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+typedef enum : NSUInteger {
+    FileUploadCompleted     = 0,
+    FileUploading           = 1,
+    FileUploadFail          = 2,
+} FileUploadStatus;
+
 typedef NS_ENUM(NSUInteger, ChatMessageType) {
     ChatMessageTypeText=0,       // 文字
     ChatMessageTypeImage=1,      // 图片
@@ -26,6 +32,7 @@ typedef NS_ENUM(NSUInteger, ChatMessageType) {
 };
 
 @interface ChatMessageModel : NSObject
+@property (nonatomic, assign, readonly) FileUploadStatus fileUploadStatus;
 @property (nonatomic, assign) ChatMessageType messageType;
 @property (nonatomic, assign) NSInteger type; //0=友聊 1=群聊
 @property (nonatomic, assign) BOOL isSender; // 是否是发送方
@@ -97,7 +104,7 @@ typedef NS_ENUM(NSUInteger, ChatMessageType) {
 -(void)downloadFileIfNeed:(void(^)(NSInteger persent))loading;
 
 - (BOOL)checkIsSendFail;
-
+- (void)updateFileStatusToUploading;
 @end
 
 NS_ASSUME_NONNULL_END

+ 25 - 0
AIIM/Controller/chat/chetCell/ChatMessageModel.m

@@ -11,6 +11,7 @@
 #import "AVFoundation/AVFoundation.h"
 #import "FileNetApi.h"
 #import <SDWebImage/SDImageCache.h>
+#import "OSSManager.h"
 
 @implementation ChatMessageModel
 
@@ -479,4 +480,28 @@
     return NO;
 }
 
+- (FileUploadStatus)fileUploadStatus {
+    NSString *key = [self getKey];
+    NSInteger value = [[[NSUserDefaults standardUserDefaults] valueForKey:key] integerValue];
+    NSLog(@"checkIsNeedResend key: %@  value: %ld", key, value);
+
+    if (value >= 100) {
+        return FileUploadCompleted;
+    }
+    
+    if (value == -1) {
+        return FileUploadFail;
+    }
+    return FileUploading;
+}
+
+- (void)updateFileStatusToUploading {
+    [[NSUserDefaults standardUserDefaults] setValue:@(0) forKey:[self getKey]];
+}
+
+-(NSString *)getKey {
+    NSString *localtime = [NSString stringWithFormat:@"%ld", self.localtime];
+    return [[OSSManager sharedManager] fileMessageKeyWith:localtime];
+}
+
 @end

+ 22 - 8
AIIM/Controller/chat/chetCell/chatCellView.m

@@ -280,6 +280,9 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     NSLog(@"点击重发按钮: msgId=%@", _messageModel.msgId);
     [self hideResendButton];
     self.messageModel.fileError = 0;
+    if ([self.messageModel isUploadFile]) {
+        [self.messageModel updateFileStatusToUploading];
+    }
     
     // 触发重发回调
     if (self.resendMessageBlock) {
@@ -610,6 +613,8 @@ static const CGFloat kMediaCornerRadius = 4.0f;
         case ChatMessageTypeDel:
             [self setupDeleateContent];
             break;
+        case ChatMessageTypeDFBUSY:
+            break;
     }
     
     // 配置头像和名字
@@ -753,16 +758,25 @@ static const CGFloat kMediaCornerRadius = 4.0f;
     
     if (self.messageModel.isSent) {
         [self hideProgressView];
-    } else {
-        // 发送中,显示进度圆环,隐藏失败按钮
-        [self showProgressView];
+        [self hideResendButton];
+        return;
     }
     
-    if (self.messageModel.fileError) {
-        [self showResendButton];
-        [self hideProgressView];
-    } else {
-        [self hideResendButton];
+    switch (self.messageModel.fileUploadStatus) {
+        case FileUploadCompleted:
+            [self hideProgressView];
+            [self hideResendButton];
+            break;
+            
+        case FileUploading:
+            [self showProgressView];
+            [self hideResendButton];
+            break;
+            
+        case FileUploadFail:
+            [self showResendButton];
+            [self hideProgressView];
+            break;
     }
     
 }