Browse Source

160831
Add upload logic

Ray Zhang 9 years ago
parent
commit
e6d73dfef1

BIN
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/Ray.xcuserdatad/UserInterfaceState.xcuserstate


File diff suppressed because it is too large
+ 0 - 1192
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/Ray.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist


+ 4 - 2
RedAnt ERP Mobile/common/Functions/offline/OLDataProvider.h

@@ -76,8 +76,10 @@
 
 
 
-// sync 
-+(void) prepareUpdata:(NSArray*) arr_order;
+// sync
++(NSArray*) enumOfflineOrder;
++(NSString*) prepareUpload:(NSArray*) arr_order;
++(void) uploadFile:(NSString*) file;
 //  category filter, contact advanced search, item search, search,wishlist,
 
 //+(NSDictionary*) model_img :(int)product_id;

+ 202 - 3
RedAnt ERP Mobile/common/Functions/offline/OLDataProvider.m

@@ -11,21 +11,220 @@
 #import "RAUtils.h"
 #import "AESCrypt.h"
 #import "AppDelegate.h"
-
+#import "ZipArchive.h"
+#import "UIProgressView+AFNetworking.h"
+#import "AFHTTPSessionManager.h"
 
 @interface OLDataProvider ()
 
 @end
 
 @implementation OLDataProvider
-+(void) prepareUpdata:(NSArray*) arr_order
++(NSArray*) enumOfflineOrder
+{
+    
+    NSMutableArray* ret = [[NSMutableArray alloc] init];
+    sqlite3 *db = [iSalesDB get_db];
+    
+    NSString *sqlQuery =@"select so_id from offline_order where order_id is null";
+    sqlite3_stmt * statement;
+    
+    int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
+    if ( dbresult== SQLITE_OK)
+    {
+        
+        
+        while (sqlite3_step(statement) == SQLITE_ROW)
+        {
+            
+            
+
+            char *so_id = (char*)sqlite3_column_text(statement, 0);
+            NSString* nsso_id=nil;
+            if(so_id!=nil)
+            {
+                nsso_id= [[NSString alloc]initWithUTF8String:so_id];
+                
+            }
+            
+            [ret addObject:nsso_id];
+            
+        }
+        
+        
+     
+        sqlite3_finalize(statement);
+    }
+    
+ 
+    
+    [iSalesDB close_db:db];
+    
+    
+    return ret;
+}
++(void) uploadFile:(NSString*) file
+{
+    NSData* data = [NSData dataWithContentsOfFile: file];
+    UIApplication * app = [UIApplication sharedApplication];
+    AppDelegate *appDelegate = (AppDelegate *)[app delegate];
+
+    
+    AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
+    
+    
+    
+    NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
+    
+    if(appDelegate.user!=nil)
+        [params setValue:appDelegate.user forKey:@"user"];
+    //    if(appDelegate.contact_id!=nil)
+    //        [params setValue:appDelegate.contact_id forKey:@"contactId"];
+    if(appDelegate.password!=nil)
+        [params setValue:appDelegate.password forKey:@"password"];
+    
+    
+    NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
+        [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
+    } error:nil];
+    
+    
+
+    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
+    
+    NSProgress *progress = nil;
+    
+    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
+        
+        
+        
+        if (error) {
+            
+            NSString* err_msg = [error localizedDescription];
+            NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
+            
+            
+            NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
+            
+            NSLog(@"data string: %@",str);
+            [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
+            
+        } else {
+            NSLog(@"response ");
+            
+            
+            
+            NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
+            
+            // 再将NSData转为字符串
+            NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+            
+            //            NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
+            
+            NSLog(@"data string: %@",jsonStr);
+            
+            NSDictionary* json = responseObject;
+            
+            
+            if([[json valueForKey:@"result"] intValue]==2)
+            {
+//                NSString* img_url_down = json[@"img_url_aname"];
+//                NSString* img_url_up = json[@"img_url"];
+                
+
+            }
+            else
+            {
+                [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
+            }
+            
+            
+        }
+        //  [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
+    }];
+    
+    
+    
+    
+  //  [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
+    //
+    
+    
+    
+    
+    
+    
+    [uploadTask resume];
+    
+}
++(NSString*) prepareUpload:(NSArray*) arr_order
 {
     NSString* serial= [[NSUUID UUID] UUIDString];
+    
+    
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+    NSString *cachefolder = [paths objectAtIndex:0];
+    
+    NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
+    upfolder = [upfolder stringByAppendingPathComponent:serial];
+    
+    NSFileManager* fileManager = [NSFileManager defaultManager];
+    BOOL bdir=YES;
+    if(!  [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
+    {
+        
+        NSError *error = nil;
+        bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
+        
+        if(!bsuccess)
+            DebugLog(@"Create UPLOAD folder failed");
+        
+    }
+    
     NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
     ret[@"contact"]=[self prepareContact:serial];
     ret[@"wishlist"]=[self prepareWishlist:serial];
     ret[@"order"]=[self prepareOrder:serial soid:arr_order];
     
+    NSString* str= [RAUtils dict2string:ret];
+    
+    
+
+    
+    
+    NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
+    
+    NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
+    
+    
+    NSError *error=nil;
+    
+    [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
+    
+    NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
+    
+    ZipArchive* zip = [[ZipArchive alloc] init];
+    
+    
+    BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
+    
+    for(int i=0;i<arr_files.count;i++)
+    {
+        NSString* file=arr_files[i];
+        
+        result = [zip addFileToZip:file newname:[file lastPathComponent]];
+    }
+
+        if( ![zip CloseZipFile2] )
+        {
+            zippath = @"";
+        }
+    
+    
+
+    
+    
+    return zippath;
 }
 + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
 {
@@ -66,7 +265,7 @@
     NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
     sqlite3 *db = [iSalesDB get_db];
     
-    NSString *sqlQuery =@"select _id,product_id,item_id,qty,create_time";
+    NSString *sqlQuery =@"select _id,product_id,item_id,qty,create_time from wishlist";
     sqlite3_stmt * statement;
     
     

+ 1 - 1
RedAnt ERP Mobile/common/RAUtils.h

@@ -37,7 +37,7 @@ typedef void (^add_to_cart_Handler)(void);
 +(NSString*) base64de:(NSString*) string;
 +(NSDictionary*) device_info;
 +(NSDictionary*) error_dict:(NSError*)error;
-
++ (NSArray*) allFilesAtPath:(NSString*) dirString;
 
 +(NSDictionary*) dictfromfile:(NSString*) path;
 

+ 30 - 1
RedAnt ERP Mobile/common/RAUtils.m

@@ -24,7 +24,36 @@
     return string2dic;
     
 }
-
++ (NSArray*) allFilesAtPath:(NSString*) dirString
+{
+    
+    NSMutableArray* array = [NSMutableArray arrayWithCapacity:10];
+    
+    NSFileManager* fileMgr = [NSFileManager defaultManager];
+    
+    NSArray* tempArray = [fileMgr contentsOfDirectoryAtPath:dirString error:nil];
+    
+    for (NSString* fileName in tempArray) {
+        
+        BOOL flag = YES;
+        
+        NSString* fullPath = [dirString stringByAppendingPathComponent:fileName];
+        
+        if ([fileMgr fileExistsAtPath:fullPath isDirectory:&flag]) {
+            
+            if (!flag) {
+                
+                [array addObject:fullPath];
+                
+            }
+            
+        }
+        
+    }
+    
+    return array;
+    
+}
 + (bool)mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir error:(NSError**)err {
     
     NSLog(@"- mergeContentsOfPath: %@\n intoPath: %@", srcDir, dstDir);

+ 46 - 7
RedAnt ERP Mobile/iSales-NPD/AppDelegate.m

@@ -12,6 +12,8 @@
 #import "PDFViewController.h"
 #import "ZipArchive.h"
 #import "DefaultAppearance.h"
+#import "OLDataProvider.h"
+
 
 #define UNZIP_OPEN_FAILED 0
 #define UNZIP_SUCCESS 1
@@ -428,6 +430,26 @@
 {
     [DefaultAppearance init_appearance];
     
+    
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+    NSString *cachefolder = [paths objectAtIndex:0];
+    
+    NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
+ //   upfolder = [upfolder stringByAppendingPathComponent:serial];
+    
+    NSFileManager* fileManager = [NSFileManager defaultManager];
+    BOOL bdir=YES;
+    if(!  [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
+    {
+        
+        NSError *error = nil;
+        bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
+        
+        if(!bsuccess)
+            DebugLog(@"Create UPLOAD folder failed");
+        
+    }
+    
     //    if ([[NSFileManager defaultManager] fileExistsAtPath:dist_path]) {
     //
     //        NSLog(@"文件已经存在了");
@@ -748,19 +770,36 @@
 }
 -(void) check_offline:(NSString*) ver useInternalAddress:(bool)buseinternaladdress
 {
+    
+
+    
+    
     if(self.downloading)
         return;
     
-    self.downloading=true;
+    
+       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+    
+    [self add_downloadlog:@"Begin sync, please wait."];
+    [self add_downloadlog:@"Prepare upload data."];
+    NSArray* arr_order=[OLDataProvider enumOfflineOrder];
+    NSString* upfile=[OLDataProvider prepareUpload:arr_order];
+    
+           [OLDataProvider uploadFile:upfile];
+           
+           
+    [self.settingUI onSyncFinish];
+    return;
+     self.downloading=true;
     NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
     
-    NSString*  vid = [defaults stringForKey:@"vid"] ;
+    NSString*  pvid = [defaults stringForKey:@"vid"] ;
     
-    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+
         
-        [self add_downloadlog:@"Checking offline data, this will take a few minute, Please wait."];
+        [self add_downloadlog:@"Checking offline data, this will take a few minute, please wait."];
         
-        NSMutableDictionary* json=[[iSalesNetwork  check_Offline:ver useInternalAddress:buseinternaladdress vid:vid] mutableCopy];
+        NSMutableDictionary* json=[[iSalesNetwork  check_Offline:ver useInternalAddress:buseinternaladdress vid:pvid] mutableCopy];
         
         
         if([json[@"result"] intValue]!=2)
@@ -778,7 +817,7 @@
         NSMutableDictionary* download_json=nil;
         
 //        NSString* vid=@"31f4d59d-c229-47e3-9270-e15425777186";
-        NSString* vid=[json valueForKey:@"downSerial"];
+        NSString* downSerial=[json valueForKey:@"downSerial"];
         //                        sleep(3);
         //UIAlertView * waitalert1 = [RAUtils waiting_alert:@"Please wait..." title:@"Server is preparing data for you"];
         
@@ -792,7 +831,7 @@
             
             
             count++;
-            download_json=[[iSalesNetwork  download_Offline:vid] mutableCopy];
+            download_json=[[iSalesNetwork  download_Offline:downSerial] mutableCopy];
             
            // int aaa=0;
             

File diff suppressed because it is too large
+ 144 - 144
RedAnt ERP Mobile/iSales-NPD/Base.lproj/Main.storyboard


+ 3 - 0
RedAnt ERP Mobile/iSales-NPD/config.h

@@ -120,10 +120,13 @@
 
 #define  URL_UPLOAD_IMG  @"http://192.168.0.126:8080/site/isales/uploadImg.htm"
 
+
 #define  URL_NPD_POLICY  @"http://192.168.0.126:8080/site/isales/generalPolicy.htm"
 
 
 #define  URL_CHECK_OFFLINE  @"http://192.168.0.126:8080/site/offline/initDatas.htm"
+#define  URL_UPLOAD_OFFLINE  @"http://192.168.0.126:8080/site/offline/uploadOfflineData.htm"
+
 
 #define  URL_DOWNLOAD_OFFLINE  @"http://192.168.0.126:8080/site/offline/downloadDatas.htm"
 

Some files were not shown because too many files changed in this diff