Ver Fonte

重写底层文件下载接口,简化操作。
调试和修复Apex Mobile无法下载文件的问题。

Ray Zhang há 6 anos atrás
pai
commit
aafe84dbfb

+ 8 - 4
Apex Mobile/Apex Mobile/DetailPageViewController.m

@@ -601,9 +601,9 @@
     
     [self presentViewController: alertController animated: YES completion: nil];
     
-    
-    
-    [RANetwork request_download:nil  url:url completionHandler:^(NSMutableDictionary *result) {
+//    NSString *cacheDir = NSTemporaryDirectory();
+    [RANetwork request_fastdownload:nil url:url completionHandler:^(NSMutableDictionary *result) {
+//    [RANetwork request_download:nil url:url err_record_url:nil offset:0 method:nil storein:cacheDir progressHandler:nil completionHandler:^(NSMutableDictionary *result) {
         weakSelf.documentPath = nil;
         [alertController dismissViewControllerAnimated:YES completion:^{
             
@@ -632,9 +632,13 @@
             }
             
         }];
-        
     }];
     
+//    [RANetwork request_download:nil  url:url completionHandler:^(NSMutableDictionary *result) {
+//
+//
+//    }];
+    
 //    
 //    // download
 //    dispatch_async(dispatch_get_global_queue(0, 0), ^{

+ 1 - 1
Apex Mobile/Apex Mobile/RANetwork.h

@@ -68,5 +68,5 @@
 +(void) request_retrievepassword : (NSString*) user  email:(NSString*) email completionHandler:(resultHandler)result;
 + (void)request_sendemail:(NSString *)email CC:(NSString *)cc SerialNo:(NSString *)serialNo completionHandler:(resultHandler)result;
 + (void)request_uploadsql:(NSString *)sql completionHandler:(resultHandler)result;
-+ (void)request_download:(NSMutableDictionary *)params url:(NSString *)url completionHandler:(resultHandler)result;
++ (void)request_fastdownload:(NSMutableDictionary *)params url:(NSString *)url completionHandler:(resultHandler)result;
 @end

+ 14 - 2
Apex Mobile/Apex Mobile/RANetwork.m

@@ -2643,15 +2643,27 @@
 //    return resultDic;
 }
 
-+ (void)request_download:(NSMutableDictionary *)params url:(NSString *)url completionHandler:(resultHandler)result
++ (void)request_fastdownload:(NSMutableDictionary *)params url:(NSString *)url completionHandler:(resultHandler)result
 {
     
 #ifdef test_server
     url=URL_REQUEST_RECORDS;
     params[@"prefix"]=URL_REQUEST_RECORDS;
 #endif
+    
+//    if(method.length==0)
+//        method=@"POST";
+//
+//    if(path.length==0)
+//        path=NSTemporaryDirectory();
+    
+//
+//    (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url offset:(NSUInteger)offset method:(NSString *)method storein:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result;
+    
 //    params = [self prepare_addtional_params:params];
-    [self request_file:url parameters:params err_record_url:nil completionHandler:result];
+//    [self request_file:url parameters:params err_record_url:err_record offset:offset method:method storein:path progressHandler:progressHandler completionHandler:result];
+    
+    [self request_file_fast:url parameters:params completionHandler:result];
     
 }
 @end

+ 30 - 3
Apex Mobile/Apex Mobile/Result/Presenter/ApexResultPresenter.m

@@ -385,8 +385,14 @@ static const NSInteger detal = 20;
         [[NSFileManager defaultManager] createDirectoryAtPath:cacheDir withIntermediateDirectories:NO attributes:nil error:nil];
     }
     
-
-    [RANetwork request_download:[params mutableCopy]  url:url completionHandler:^(NSMutableDictionary *result) {
+//    NSString *cacheDir = NSTemporaryDirectory();
+    
+    [RANetwork request_fastdownload:[params mutableCopy] url:url completionHandler:^(NSMutableDictionary *result) {
+        
+//    }
+//
+//    [RANetwork request_download:nil url:url err_record_url:nil offset:0 method:nil storein:cacheDir progressHandler:nil completionHandler:^(NSMutableDictionary *result)
+//     {
         if (self.delegate) {
             [self.delegate onStopLoading];
 
@@ -402,8 +408,29 @@ static const NSInteger detal = 20;
                 NSString *msg = [result objectForKey:@"msg"];
                 [self.delegate onFailed:msg];
             }
-        };;
+        };
     }];
+    
+    
+
+//    [RANetwork request_download:[params mutableCopy]  url:url completionHandler:^(NSMutableDictionary *result) {
+//        if (self.delegate) {
+//            [self.delegate onStopLoading];
+//
+//            self.documentPath = nil;
+//            if ([[result objectForKey:@"result"] intValue] == RESULT_TRUE) {
+//                
+//                NSString *cachePath = [result objectForKey:@"path"];
+//                self.documentPath = cachePath;
+//                [self.delegate onDownloadFile:cachePath];
+//                
+//            } else {
+//                
+//                NSString *msg = [result objectForKey:@"msg"];
+//                [self.delegate onFailed:msg];
+//            }
+//        };;
+//    }];
 
     return;
 //

+ 3 - 2
common/NetworkUtils.h

@@ -27,12 +27,13 @@ typedef void(^dataHandler)(NSData *data);
 
 + (NSURLSessionTask *)upload:(NSData *)filedata  FileName:(NSString*) filename Params:(NSDictionary *)params ToHost:(NSString *)urlString Result:(resultHandler)r Progress:(progressHandler)p DecryptHandler:(id (^)(NSString *))decrypt;
 
-+ (NSURLSessionDataTask *)downloadFileOffset:(NSUInteger)offset Param:(NSDictionary *)param from:(NSString *)url method:(NSString *)method toPath:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result;
++ (NSURLSessionDownloadTask *)downloadFileOffset:(NSUInteger)offset Param:(NSDictionary *)param from:(NSString *)url method:(NSString *)method toPath:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result;
 
 
 // 2020 新增网络访问底层接口,取代get_json
 // 普通请求
 +(void)request_interface : (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result retry:(int) retry;
-+(void)request_file : (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result;
+//+(void)request_file : (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result;
++(void)request_file_fast : (NSString*) url parameters:(NSMutableDictionary *) params completionHandler:(resultHandler)result;
 +(void)request_interface_d : (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result retry:(int) retry;
 @end

+ 123 - 3
common/NetworkUtils.m

@@ -510,7 +510,7 @@ repeat:
     return session;
 }
 
-+ (NSURLSessionDataTask *)downloadFileOffset:(NSUInteger)offset Param:(NSDictionary *)param from:(NSString *)url method:(NSString *)method toPath:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result {
++ (NSURLSessionDownloadTask *)downloadFileOffset:(NSUInteger)offset Param:(NSDictionary *)param from:(NSString *)url method:(NSString *)method toPath:(NSString *)path progressHandler:(progressHandler)progressHandler completionHandler:(resultHandler)result {
     
     NSURLSession *sessoin = [self standSession];
     
@@ -544,7 +544,8 @@ repeat:
         req.HTTPBody = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
     }
 
-    NSURLSessionDataTask *downloadTask = [sessoin dataTaskWithRequest:req];
+    
+    NSURLSessionDownloadTask *downloadTask = [sessoin downloadTaskWithRequest:req];
     
     [downloadTask resume];
         
@@ -1279,7 +1280,7 @@ repeat:
 
 
 
-+(void)request_file : (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result
++(void)request_file_1 : (NSString*) url parameters:(NSMutableDictionary *) params  err_record_url:(NSString* )recorder_url completionHandler:(resultHandler)result
 /* dataHandler:(dataHandler)returndata*/
 {
 //    __block int retry = 0;
@@ -1686,4 +1687,123 @@ repeat:
 //        return responseData;
 //    }
 }
++(void)request_file_fast : (NSString*) url parameters:(NSMutableDictionary *) params completionHandler:(resultHandler)result
+
+
+// Param:(NSDictionary *)param from:(NSString *)url    completionHandler:(resultHandler)result
+/* dataHandler:(dataHandler)returndata*/
+{
+    
+    params = [self prepare_addtional_params:params];
+
+    NSURLSession *urlSession = [self standSession];
+    
+//    RANetworkTaskDelegate *delegate = (RANetworkTaskDelegate *)urlSession.delegate;
+//    delegate.downloadTask = YES;
+//    delegate.fileCachePath = path;
+//    if (progressHandler) {
+//        delegate.p = progressHandler;
+//    }
+//
+//    if (result) {
+//        delegate.r = result;
+//    }
+    
+    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
+    
+    
+    
+    request.HTTPMethod = @"Post";
+//
+//    if (offset > 0) {
+//        [request addValue:[NSString stringWithFormat:@"bytes=%ld-",(unsigned long)offset] forHTTPHeaderField:@"Range"];
+//    }
+    
+    // 拼接参数
+    if (params && params.allValues.count > 0) {
+        __block NSMutableArray *paramArr = [NSMutableArray array];
+        [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
+            [paramArr addObject:[NSString stringWithFormat:@"%@=%@",key,obj]];
+        }];
+        NSString *paramStr = [paramArr componentsJoinedByString:@"&"];
+        
+        request.HTTPBody = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
+    }
+
+    
+    NSURLSessionDownloadTask* downloadTask=[urlSession downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
+        
+        
+        
+        
+
+        
+        NSHTTPURLResponse* urlResponse =response;
+       NSData* data =[NSData dataWithContentsOfURL:location] ;
+        NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
+        if(data==nil)
+        {
+             NSString* err_msg = [error localizedDescription];
+            NSDictionary* error_json = [RAUtils error_dict:error];
+            [ret setObject:error_json forKey:@"err_obj"];
+            NSString* resp_msg= nil;
+
+            if(err_msg.length==0)
+            {
+                err_msg =[NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode];
+                resp_msg = err_msg;
+            }
+            [ret setValue:[NSString stringWithFormat:@"%d",RESULT_NET_ERROR] forKey:@"result"];
+            [ret setValue:err_msg forKey:@"err_msg"];
+            [ret setValue:resp_msg forKey:@"resp_msg"];
+            [ret setValue:[NSString stringWithFormat:@"%ld",(long)urlResponse.statusCode] forKey:@"resp_code"];
+            
+            dispatch_async(dispatch_get_main_queue(), ^{
+            if(result)
+                result(ret);
+            });
+        }
+        else
+        {
+            
+            
+            NSString* path=NSTemporaryDirectory();
+            
+            if (response.suggestedFilename) {
+                path = [path stringByAppendingPathComponent:response.suggestedFilename];
+            } else {
+                path = [path stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
+            }
+            NSError *error = nil;
+            
+            [[NSFileManager defaultManager] copyItemAtPath:location.path toPath:path error:&error];
+            
+//            [[NSFileManager defaultManager] copyItemAtURL:[NSURL URLWithString:location.path] toURL:[NSURL URLWithString:path] error:&error];
+//            NSFileManager
+//                    NSString *cacheDir = NSTemporaryDirectory();
+//
+//                    if (![[NSFileManager defaultManager] fileExistsAtPath:cacheDir]) {
+//                        [[NSFileManager defaultManager] createDirectoryAtPath:cacheDir withIntermediateDirectories:NO attributes:nil error:nil];
+//                    }
+            
+            ret[@"result"]=@"2";
+            ret[@"data"]=data;
+            ret[@"path"]=path;//[location absoluteString];
+            dispatch_async(dispatch_get_main_queue(), ^{
+            if(result)
+                result(ret);
+            });
+        }
+//        if(returndata)
+//        {
+//            returndata(data);
+//        }
+    }];
+    
+    [downloadTask resume];
+    
+    return ;
+
+}
+
 @end