Просмотр исходного кода

1.修改iOS Apex Drivers离线。

Pen Li 7 лет назад
Родитель
Сommit
d64cc68164

+ 3 - 0
Redant Drivers/Apex And Drivers/AppDelegate.m

@@ -169,6 +169,9 @@
     }];
     }];
 #endif
 #endif
     
     
+//    [RADataProvider downloadOfflineData];
+//    [RASingleton sharedInstance].offline = YES;
+        
     // View
     // View
     
     
     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

+ 185 - 65
Redant Drivers/Apex And Drivers/Offline/RAOfflineHandler.m

@@ -153,19 +153,22 @@ static dispatch_semaphore_t _lock;
        
        
         NSMutableDictionary *params = [NSMutableDictionary dictionary];
         NSMutableDictionary *params = [NSMutableDictionary dictionary];
         [self prepareParams:params];
         [self prepareParams:params];
-        
-        [NetworkUtils downloadFileOffset:0 Param:params from:@"" method:@"POST" toPath:self.offlineDir progressHandler:^(NSURLSessionTask *task, double progress) {
-            
+
+        [NetworkUtils downloadFileOffset:0 Param:params from:@"http://192.168.0.130/fake_offline.zip" method:@"POST" toPath:self.offlineDir progressHandler:^(NSURLSessionTask *task, double progress) {
+
         } completionHandler:^(NSMutableDictionary *result) {
         } completionHandler:^(NSMutableDictionary *result) {
-           
+
             int rs = [[result objectForKey:@"result"] intValue];
             int rs = [[result objectForKey:@"result"] intValue];
             NSString *path = [result objectForKey:@"path"];
             NSString *path = [result objectForKey:@"path"];
             if (rs == RESULT_TRUE) {
             if (rs == RESULT_TRUE) {
                 [self handleDownloadFile:path];
                 [self handleDownloadFile:path];
             }
             }
-            
+
         }];
         }];
         
         
+//        NSString *path = [self.offlineDir stringByAppendingPathComponent:@"download.zip"];
+//        [self handleDownloadFile:path];
+//
     });
     });
 }
 }
 
 
@@ -186,6 +189,78 @@ static dispatch_semaphore_t _lock;
     return nil;
     return nil;
 }
 }
 
 
+- (void)_moveFile:(NSString *)filePath toFolder:(NSString *)folder {
+    
+    NSFileManager *fm = [NSFileManager defaultManager];
+    BOOL isDir = NO;
+    if ([fm fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) {
+        
+        BOOL fIsDir = NO;
+        if ([fm fileExistsAtPath:folder isDirectory:&fIsDir] && fIsDir) {
+            
+        } else {
+            NSError *err = nil;
+            [fm createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:&err];
+            if (err) {
+                NSLog(@"create folder error: %@",err);
+                return;
+            }
+        }
+        
+        NSString *to = [folder stringByAppendingPathComponent:filePath.lastPathComponent];
+        
+        NSError *err = nil;
+        [fm moveItemAtPath:filePath toPath:to error:&err];
+        if (err) {
+            NSLog(@"move file error: %@",err);
+        }
+    }
+}
+
+- (void)_moveFolder:(NSString *)srcfolder toFolder:(NSString *)destFolder {
+    
+    NSFileManager *fm = [NSFileManager defaultManager];
+    BOOL isDir = NO;
+    if ([fm fileExistsAtPath:srcfolder isDirectory:&isDir] && isDir) {
+        
+        NSString *dest = [destFolder stringByAppendingPathComponent:srcfolder.lastPathComponent];
+        isDir = NO;
+        if ([fm fileExistsAtPath:dest isDirectory:&isDir] && isDir) {
+            
+        } else {
+            NSError *err = nil;
+            [fm createDirectoryAtPath:dest withIntermediateDirectories:YES attributes:nil error:&err];
+            if (err) {
+                NSLog(@"create folder error: %@",err);
+                return;
+            }
+        }
+        
+        NSArray<NSString *> *contents = [fm contentsOfDirectoryAtPath:srcfolder error:nil];
+        if (contents.count > 0) {
+            
+            [contents enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+               
+                NSString *path = [srcfolder stringByAppendingPathComponent:obj];
+                
+                BOOL fileIsDir = NO;
+                if ([fm fileExistsAtPath:path isDirectory:&fileIsDir]) {
+                    
+                    if (fileIsDir) {
+                        
+                        [self _moveFolder:path toFolder:dest];
+                        
+                    } else {
+                        [self _moveFile:path toFolder:dest];
+                    }
+                    
+                }
+            }];
+            
+        }
+    }
+}
+
 /**
 /**
  * @brief 处理下载的压缩文件
  * @brief 处理下载的压缩文件
  * @param path 压缩文件路径
  * @param path 压缩文件路径
@@ -202,8 +277,10 @@ static dispatch_semaphore_t _lock;
         // 删除旧数据,除了tmp
         // 删除旧数据,除了tmp
         NSArray<NSString *> *items = [fm contentsOfDirectoryAtPath:self.offlineDir error:nil];
         NSArray<NSString *> *items = [fm contentsOfDirectoryAtPath:self.offlineDir error:nil];
         [items enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         [items enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
-            if (![obj isEqualToString:self.offlineTmp] || ![obj.lastPathComponent isEqualToString:@"finish"] || ![obj isEqualToString:self.offlineUploadDir]) {
-                [fm removeItemAtPath:obj error:nil];
+            
+            NSString *path = [self.offlineDir stringByAppendingPathComponent:obj];
+            if (![path isEqualToString:self.offlineTmp] && ![path.lastPathComponent isEqualToString:@"finish"] && ![path isEqualToString:self.offlineUploadDir]) {
+                [fm removeItemAtPath:path error:nil];
             }
             }
         }];
         }];
         
         
@@ -211,9 +288,17 @@ static dispatch_semaphore_t _lock;
         items = [fm contentsOfDirectoryAtPath:self.offlineTmp error:nil];
         items = [fm contentsOfDirectoryAtPath:self.offlineTmp error:nil];
         [items enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         [items enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
             
             
-            NSString *lastComponent = [obj lastPathComponent];
-            NSString *to = [self.offlineDir stringByAppendingPathComponent:lastComponent];
-            [fm moveItemAtPath:obj toPath:to error:nil];
+            NSString *src = [self.offlineTmp stringByAppendingPathComponent:obj];
+            BOOL isDir = NO;
+            if ([fm fileExistsAtPath:src isDirectory:&isDir]) {
+                
+                if (isDir) {
+                    [self _moveFolder:src toFolder:self.offlineDir];
+                } else {
+                    [self _moveFile:src toFolder:self.offlineDir];
+                }
+                
+            }
         }];
         }];
         
         
         // 重置缓存的action
         // 重置缓存的action
@@ -412,7 +497,7 @@ static dispatch_semaphore_t _lock;
  * @param orderId 订单号
  * @param orderId 订单号
  * @return YES/NO
  * @return YES/NO
  */
  */
-- (BOOL)isLastActionForOrder:(NSString *)orderId {
+- (BOOL)isLastActionForOrder:(NSString *)orderId index:(NSInteger)actionIndex {
     if (!orderId) {
     if (!orderId) {
         return NO;
         return NO;
     }
     }
@@ -436,7 +521,14 @@ static dispatch_semaphore_t _lock;
                 findAction = YES;
                 findAction = YES;
                 NSMutableDictionary *mObj = [obj mutableCopy];
                 NSMutableDictionary *mObj = [obj mutableCopy];
                 NSMutableArray<NSDictionary *> *actions = [[mObj objectForKey:@"actions"] mutableCopy];
                 NSMutableArray<NSDictionary *> *actions = [[mObj objectForKey:@"actions"] mutableCopy];
-                if (actions.count == 1) {
+                
+                __block int maxActionIndex = 0;
+                [actions enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+                    
+                    int index = [[obj objectForKey:@"index"] intValue];
+                    maxActionIndex = index;
+                }];
+                if (actionIndex == maxActionIndex) {
                     isLast = YES;
                     isLast = YES;
                 }
                 }
                 *stop = YES;
                 *stop = YES;
@@ -517,7 +609,6 @@ static dispatch_semaphore_t _lock;
     
     
     NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
     NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
     [data writeToFile:path atomically:NO];
     [data writeToFile:path atomically:NO];
-    
 }
 }
 
 
 /**
 /**
@@ -546,6 +637,18 @@ static dispatch_semaphore_t _lock;
     return [formatter stringFromDate:date];
     return [formatter stringFromDate:date];
 }
 }
 
 
+- (NSString *)statusTitleForActionIndex:(NSInteger)actionIndex {
+    
+    Lock();
+    
+    NSString *path = [self.offlineDir stringByAppendingPathComponent:@"status_title.json"];
+    NSDictionary *titleDic = [self _loadCacheData:path];
+    
+    Unlock();
+    
+    return [titleDic objectForKey:[NSString stringWithFormat:@"%ld",(long)actionIndex]];
+}
+
 #pragma mark - Update Data
 #pragma mark - Update Data
 
 
 /**
 /**
@@ -574,7 +677,7 @@ static dispatch_semaphore_t _lock;
     [finishDic setObject:@(actionIdx) forKey:orderId];
     [finishDic setObject:@(actionIdx) forKey:orderId];
     
     
     NSString *path = [self.offlineDir stringByAppendingPathComponent:@"finish"];
     NSString *path = [self.offlineDir stringByAppendingPathComponent:@"finish"];
-    [self writeJson:finishDic toPath:path];
+    [finishDic writeToFile:path atomically:NO];
 }
 }
 
 
 /**
 /**
@@ -615,11 +718,15 @@ static dispatch_semaphore_t _lock;
                                    @"noFile" : @YES,
                                    @"noFile" : @YES,
                                    @"params" : params
                                    @"params" : params
                                    } mutableCopy];
                                    } mutableCopy];
-//    @"file" : photoPath,
-
-    // 提交参数给UploadManager
-    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
-    [appDelegate.uploadManager addTask:task];
+    
+    dispatch_async(dispatch_get_main_queue(), ^{
+        
+        //    @"file" : photoPath,
+        
+        // 提交参数给UploadManager
+        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+        [appDelegate.uploadManager addTask:task];
+    });
     
     
     // New Order
     // New Order
     if (type == RAOrderStatusNew) {
     if (type == RAOrderStatusNew) {
@@ -696,6 +803,8 @@ static dispatch_semaphore_t _lock;
                             
                             
                             // 修改order状态为Processing
                             // 修改order状态为Processing
                             [curOrder setValue:@(RAOrderStatusProcessing) forKey:@"status"];
                             [curOrder setValue:@(RAOrderStatusProcessing) forKey:@"status"];
+                            [curOrder setValue:@(NO) forKey:@"backendFlag"];
+                            [curOrder setValue:[self statusTitleForActionIndex:actionIdx] forKey:@"title"];
                             
                             
                             // 将当前order插入第一个位置
                             // 将当前order插入第一个位置
                             [orders insertObject:curOrder atIndex:0];
                             [orders insertObject:curOrder atIndex:0];
@@ -739,12 +848,7 @@ static dispatch_semaphore_t _lock;
  */
  */
 - (NSDictionary *)updateOrder:(NSString *)orderId action:(NSInteger)actionId title:(NSString *)title index:(NSInteger)idx withParams:(NSDictionary *)params photos:(NSArray<RAEditImageBaseModel *> *)photos cacheDir:(NSString *)dir {
 - (NSDictionary *)updateOrder:(NSString *)orderId action:(NSInteger)actionId title:(NSString *)title index:(NSInteger)idx withParams:(NSDictionary *)params photos:(NSArray<RAEditImageBaseModel *> *)photos cacheDir:(NSString *)dir {
     
     
-    Lock();
-    
     NSFileManager *fm = [NSFileManager defaultManager];
     NSFileManager *fm = [NSFileManager defaultManager];
-    // 生成上传目录
-    NSString *upDir = [self.offlineUploadDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld",orderId,(long)idx]];
-    [fm createDirectoryAtPath:upDir withIntermediateDirectories:NO attributes:nil error:nil];
     
     
     // 修改Params
     // 修改Params
     NSString *time = [self currentDate];
     NSString *time = [self currentDate];
@@ -758,6 +862,13 @@ static dispatch_semaphore_t _lock;
     
     
     NSString *zipF = nil;
     NSString *zipF = nil;
     if (photos.count > 0) {
     if (photos.count > 0) {
+        
+        Lock();
+        
+        // 生成上传目录
+        NSString *upDir = [self.offlineUploadDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld",orderId,(long)idx]];
+        [fm createDirectoryAtPath:upDir withIntermediateDirectories:NO attributes:nil error:nil];
+        
         // 生成图片目录
         // 生成图片目录
         NSString *imageDir = [upDir stringByAppendingPathComponent:@"images"];
         NSString *imageDir = [upDir stringByAppendingPathComponent:@"images"];
         [fm createDirectoryAtPath:imageDir withIntermediateDirectories:NO attributes:nil error:nil];
         [fm createDirectoryAtPath:imageDir withIntermediateDirectories:NO attributes:nil error:nil];
@@ -795,10 +906,10 @@ static dispatch_semaphore_t _lock;
         [fm removeItemAtPath:upDir error:nil];
         [fm removeItemAtPath:upDir error:nil];
         
         
         zipF = zip;
         zipF = zip;
+        
+        Unlock();
     }
     }
     
     
-    Unlock();
-    
     // 创建Task
     // 创建Task
     NSMutableDictionary *task = [@{
     NSMutableDictionary *task = [@{
                                    @"order" : orderId,
                                    @"order" : orderId,
@@ -814,42 +925,46 @@ static dispatch_semaphore_t _lock;
         [task setObject:@(YES) forKey:@"noFile"];
         [task setObject:@(YES) forKey:@"noFile"];
     }
     }
     
     
-    // 提交Task
-    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
-    [appDelegate.uploadManager addTask:task];
+    dispatch_async(dispatch_get_main_queue(), ^{
+        
+        // 提交Task
+        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+        [appDelegate.uploadManager addTask:task];
+    });
     
     
     // 删除Edit文件
     // 删除Edit文件
     [self deleteEditJsonFileForOrder:orderId withActionIndex:idx];
     [self deleteEditJsonFileForOrder:orderId withActionIndex:idx];
     
     
     // 判断是否完成order 所有操作
     // 判断是否完成order 所有操作
-    BOOL finish = [self isLastActionForOrder:orderId];
-    if (finish) {
-        
-        // 将order从Processing中删除
-        // 加载Home
-        NSMutableDictionary *home = [[self requestOfflineHome] mutableCopy];
-        if (home) {
-            NSMutableArray<NSDictionary *> *sections = [[home objectForKey:@"sections"] mutableCopy];
-            if (sections) {
+    BOOL finish = [self isLastActionForOrder:orderId index:idx];
+    
+    // 将order从Processing中删除
+    // 加载Home
+    NSMutableDictionary *home = [[self requestOfflineHome] mutableCopy];
+    if (home) {
+        NSMutableArray<NSDictionary *> *sections = [[home objectForKey:@"sections"] mutableCopy];
+        if (sections) {
+            
+            __block NSDictionary *rmSection = nil; // 若section下没有order,则移除section
+            for (int i = 0; i < sections.count; i++) {
+                NSMutableDictionary *section = [[sections objectAtIndex:i] mutableCopy];
                 
                 
-                __block NSDictionary *rmSection = nil; // 若section下没有order,则移除section
-                for (int i = 0; i < sections.count; i++) {
-                    NSMutableDictionary *section = [[sections objectAtIndex:i] mutableCopy];
+                RAOrderStatus type = [[section objectForKey:@"type"] intValue];
+                if (type == RAOrderStatusProcessing) {
+                    NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
                     
                     
-                    RAOrderStatus type = [[section objectForKey:@"type"] intValue];
-                    if (type == RAOrderStatusProcessing) {
-                        NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
+                    __block NSDictionary *curOrder = nil; // 当前order
+                    [orders enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                         
                         
-                        __block NSDictionary *curOrder = nil; // 当前order
-                        [orders enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
-                            
-                            NSString *orderID = [obj objectForKey:@"orderID"];
-                            if ([orderId isEqualToString:orderID]) {
-                                curOrder = obj;
-                                *stop = YES;
-                            }
-                            
-                        }];
+                        NSString *orderID = [obj objectForKey:@"orderID"];
+                        if ([orderId isEqualToString:orderID]) {
+                            curOrder = obj;
+                            *stop = YES;
+                        }
+                        
+                    }];
+                    
+                    if (finish) {
                         [orders removeObject:curOrder];
                         [orders removeObject:curOrder];
                         
                         
                         if (orders.count > 0) {
                         if (orders.count > 0) {
@@ -861,26 +976,31 @@ static dispatch_semaphore_t _lock;
                             
                             
                             rmSection = [sections objectAtIndex:i];
                             rmSection = [sections objectAtIndex:i];
                         }
                         }
+                    } else {
                         
                         
-                        break;
+                        [curOrder setValue:[self statusTitleForActionIndex:idx] forKey:@"title"];
                     }
                     }
+                    
+                    break;
                 }
                 }
-                
-                if (rmSection) {
-                    [sections removeObject:rmSection];
-                }
-                
-                // 重新生成sections
-                [home setObject:sections forKey:@"sections"];
-                
             }
             }
             
             
-            // 更新Home文件
-            [self updateHome:home];
+            if (rmSection) {
+                [sections removeObject:rmSection];
+            }
+            
+            // 重新生成sections
+            [home setObject:sections forKey:@"sections"];
+            
         }
         }
         
         
+        // 更新Home文件
+        [self updateHome:home];
     }
     }
     
     
+    // 更新Action
+    [self updateLastAction:idx forOrder:orderId];
+    
     return @{
     return @{
              @"result" : @(RESULT_TRUE)
              @"result" : @(RESULT_TRUE)
              };
              };

+ 2 - 0
Redant Drivers/Apex And Drivers/RADataProvider.h

@@ -49,4 +49,6 @@
 
 
 + (NSDictionary *)offlineSubmitOrder:(NSString *)orderId action:(NSInteger)actionId title:(NSString *)title index:(NSInteger)idx withParams:(NSDictionary *)params photos:(NSArray<RAEditImageBaseModel *> *)photos cacheDir:(NSString *)dir;
 + (NSDictionary *)offlineSubmitOrder:(NSString *)orderId action:(NSInteger)actionId title:(NSString *)title index:(NSInteger)idx withParams:(NSDictionary *)params photos:(NSArray<RAEditImageBaseModel *> *)photos cacheDir:(NSString *)dir;
 
 
++ (void)downloadOfflineData;
+
 @end
 @end

+ 4 - 0
Redant Drivers/Apex And Drivers/RADataProvider.m

@@ -463,6 +463,10 @@
     return [self handleJsonData:json];
     return [self handleJsonData:json];
 }
 }
 
 
++ (void)downloadOfflineData {
+    [[RAOfflineHandler defaultHandler] downloadOfflineData];
+}
+
 #pragma mark - Utils
 #pragma mark - Utils
 
 
 + (NSString *)dic2String:(NSDictionary *)dic {
 + (NSString *)dic2String:(NSDictionary *)dic {

+ 19 - 16
Redant Drivers/Apex And Drivers/Update/RAOrderEditViewController.m

@@ -434,27 +434,30 @@
             
             
             NSDictionary *json = [weakSelf offlineUpdate:params photos:photoArr];
             NSDictionary *json = [weakSelf offlineUpdate:params photos:photoArr];
             
             
-            [hud dismiss:^{
-                
-                int result = [[json objectForKey:@"result"] intValue];
+            dispatch_async(dispatch_get_main_queue(), ^{
                 
                 
-                NSString *msg = NSLocalizedString(@"sorry", nil);
-                if (result == RESULT_TRUE) {
+                [hud dismiss:^{
                     
                     
-                    msg = NSLocalizedString(@"offline_update_success", nil);
+                    int result = [[json objectForKey:@"result"] intValue];
                     
                     
-                    [weakSelf submitSuccessWithMsg:msg];
-                    
-                } else {
-                    msg = [json objectForKey:@"err_msg"];
-                    if (msg == nil) {
-                        msg = NSLocalizedString(@"sorry", nil);
+                    NSString *msg = NSLocalizedString(@"sorry", nil);
+                    if (result == RESULT_TRUE) {
+                        
+                        msg = NSLocalizedString(@"offline_update_success", nil);
+                        
+                        [weakSelf submitSuccessWithMsg:msg];
+                        
+                    } else {
+                        msg = [json objectForKey:@"err_msg"];
+                        if (msg == nil) {
+                            msg = NSLocalizedString(@"sorry", nil);
+                        }
+                        
+                        [weakSelf showAlert:msg];
                     }
                     }
                     
                     
-                    [weakSelf showAlert:msg];
-                }
-
-            }];
+                }];
+            });
         }
         }
         else { // 在线
         else { // 在线