浏览代码

1.修改iOS Apex Mobile上传缓存用户数据。

Pen Li 7 年之前
父节点
当前提交
8a80def2a8

+ 4 - 0
Apex Mobile/Apex Mobile/ApexMobileDB.h

@@ -26,4 +26,8 @@
 + (void)jk_query:(NSString *)sql completion:(void (^)(sqlite3_stmt *stmt,long *count)) query failure:(void (^)(NSString *err_msg)) failure;
 + (void)jk_excute:(NSString *)sql completion:(void (^)(BOOL success)) completion;
 
++ (NSString *)prepareUploadSQLOfUser:(NSString *)user withDiviceID:(NSString *)deviceID;
++ (BOOL)isCachedDataForUser:(NSString *)user;
++ (void)cleanCacheDataForUser:(NSString *)user;
+
 @end

+ 166 - 0
Apex Mobile/Apex Mobile/ApexMobileDB.m

@@ -350,4 +350,170 @@
     });
 }
 
++ (NSString *)prepareUploadSQLOfUser:(NSString *)user withDiviceID:(NSString *)deviceID {
+    
+    NSMutableString *sqlStr = [NSMutableString string];
+    sqlite3 *db = [self get_db];
+
+    // favorites
+    NSString *favorites_sql = [NSString stringWithFormat:@"select ifnull(name,''), ifnull(params, ''), ifnull(action, ''), ifnull(module_name, ''), ifnull(user, ''), ifnull(create_time, '')  from favorites where user = '%@';", user];
+    sqlite3_stmt *favorites_statment;
+    if (sqlite3_prepare_v2(db, favorites_sql.UTF8String, -1, &favorites_statment, NULL) == SQLITE_OK) {
+        while (sqlite3_step(favorites_statment) == SQLITE_ROW) {
+            
+            char *ch_name = (char *)sqlite3_column_text(favorites_statment, 0);
+            char *ch_params = (char *)sqlite3_column_text(favorites_statment, 1);
+            char *ch_action = (char *)sqlite3_column_text(favorites_statment, 2);
+            char *ch_module_name = (char *)sqlite3_column_text(favorites_statment, 3);
+            char *ch_user = (char *)sqlite3_column_text(favorites_statment, 4);
+            char *ch_create_time = (char *)sqlite3_column_text(favorites_statment, 5);
+            
+            NSString *str_name = [NSString stringWithUTF8String:ch_name];
+            NSString *str_params = [NSString stringWithUTF8String:ch_params];
+            NSString *str_action = [NSString stringWithUTF8String:ch_action];
+            NSString *str_module_name = [NSString stringWithUTF8String:ch_module_name];
+            NSString *str_user = [NSString stringWithUTF8String:ch_user];
+            NSString *str_create_time = [NSString stringWithUTF8String:ch_create_time];
+            
+            NSString *insert_sql = [NSString stringWithFormat:@"insert into favorites (name, params, action, module_name, user_name, device_id, create_time) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@');", str_name, str_params, str_action, str_module_name, str_user, deviceID, str_create_time];
+            [sqlStr appendString:insert_sql];
+            [sqlStr appendString:@"\r\n"];
+        }
+        sqlite3_finalize(favorites_statment);
+    }
+    
+    // fields_info
+    NSString *fieldsSql = [NSString stringWithFormat:@"select ifnull(name, ''), ifnull(aname, ''), ifnull(field_type, 0), ifnull(function_name, ''), ifnull(behavior, 0), ifnull(priority, 0), ifnull(show, 0), ifnull(user, '') from fields_info where user = '%@';",user];
+    sqlite3_stmt *fields_statment;
+    if (sqlite3_prepare_v2(db, fieldsSql.UTF8String, -1, &fields_statment, NULL) == SQLITE_OK) {
+        while (sqlite3_step(fields_statment) == SQLITE_ROW) {
+            
+            char *ch_name = (char *)sqlite3_column_text(fields_statment, 0);
+            char *ch_aname = (char *)sqlite3_column_text(fields_statment, 1);
+            int field_type = sqlite3_column_int(fields_statment, 2);
+            char *ch_function_name = (char *)sqlite3_column_text(fields_statment, 3);
+            int behavior = sqlite3_column_int(fields_statment, 4);
+            int priority = sqlite3_column_int(fields_statment, 5);
+            int show = sqlite3_column_int(fields_statment, 6);
+            char *ch_user = (char *)sqlite3_column_text(fields_statment, 7);
+            
+            NSString *str_name = [NSString stringWithUTF8String:ch_name];
+            NSString *str_aname = [NSString stringWithUTF8String:ch_aname];
+            NSString *str_function_name =  [NSString stringWithUTF8String:ch_function_name];
+            NSString *str_user = [NSString stringWithUTF8String:ch_user];
+            
+            NSString *insert_sql = [NSString stringWithFormat:@"insert into fields_info (name, aname, field_type, function_name, behavior, priority, show, user_name, device_id) values ('%@', '%@', %d, '%@', %d, %d, %d, '%@', '%@');", str_name, str_aname, field_type, str_function_name, behavior, priority, show, str_user, deviceID];
+            [sqlStr appendString:insert_sql];
+            [sqlStr appendString:@"\r\n"];
+        }
+        sqlite3_finalize(fields_statment);
+    }
+    
+    // history
+    NSString *historySql = [NSString stringWithFormat:@"select ifnull(name, ''), ifnull(params, ''), ifnull(action, ''), ifnull(module_name, ''), ifnull(user, ''), ifnull(create_time, '') from history where user = '%@';",user];
+    sqlite3_stmt *history_statment;
+    if (sqlite3_prepare_v2(db, historySql.UTF8String, -1, &history_statment, NULL) == SQLITE_OK) {
+        while (sqlite3_step(history_statment) == SQLITE_ROW) {
+            char *ch_name = (char *)sqlite3_column_text(history_statment, 0);
+            char *ch_params = (char *)sqlite3_column_text(history_statment, 1);
+            char *ch_action = (char *)sqlite3_column_text(history_statment, 2);
+            char *ch_module_name = (char *)sqlite3_column_text(history_statment, 3);
+            char *ch_user = (char *)sqlite3_column_text(history_statment, 4);
+            char *ch_create_time = (char *)sqlite3_column_text(history_statment, 5);
+            
+            NSString *str_name = [NSString stringWithUTF8String:ch_name];
+            NSString *str_params = [NSString stringWithUTF8String:ch_params];
+            NSString *str_action = [NSString stringWithUTF8String:ch_action];
+            NSString *str_module_name = [NSString stringWithUTF8String:ch_module_name];
+            NSString *str_user = [NSString stringWithUTF8String:ch_user];
+            NSString *str_create_time = [NSString stringWithUTF8String:ch_create_time];
+            
+            NSString *insert_sql = [NSString stringWithFormat:@"insert into history (name, params, action, module_name, user_name, device_id, create_time) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@');", str_name, str_params, str_action, str_module_name, str_user, deviceID, str_create_time];
+            [sqlStr appendString:insert_sql];
+            [sqlStr appendString:@"\r\n"];
+        }
+        
+        sqlite3_finalize(history_statment);
+    }
+    
+    // search history
+    NSString *searchHistorySql = [NSString stringWithFormat:@"select ifnull(h_val, ''), ifnull(h_field, ''), ifnull(level, 0), ifnull(user, ''), ifnull(h_time, '') from search_history where user = '%@';",user];
+    sqlite3_stmt *search_history_statment;
+    if (sqlite3_prepare_v2(db, searchHistorySql.UTF8String, -1, &search_history_statment, NULL) == SQLITE_OK) {
+        while (sqlite3_step(search_history_statment) == SQLITE_ROW) {
+            char *ch_val = (char *)sqlite3_column_text(search_history_statment, 0);
+            char *ch_field = (char *)sqlite3_column_text(search_history_statment, 1);
+            int level = sqlite3_column_int(search_history_statment, 2);
+            char *ch_user = (char *)sqlite3_column_text(search_history_statment, 3);
+            char *ch_time = (char *)sqlite3_column_text(search_history_statment, 4);
+            
+            NSString *str_val = [NSString stringWithUTF8String:ch_val];
+            NSString *str_field = [NSString stringWithUTF8String:ch_field];
+            NSString *str_user = [NSString stringWithUTF8String:ch_user];
+            NSString *str_time = [NSString stringWithUTF8String:ch_time];
+            
+            NSString *insert_sql = [NSString stringWithFormat:@"insert into search_history (h_val, h_field, level, user_name, device_id, h_time) values ('%@', '%@', %d, '%@', '%@', '%@');",str_val, str_field, level, str_user, deviceID, str_time];
+            [sqlStr appendString:insert_sql];
+            [sqlStr appendString:@"\r\n"];
+        }
+        
+        sqlite3_finalize(search_history_statment);
+    }
+    
+    sqlite3_close(db);
+    
+    return sqlStr.copy;
+}
+
++ (BOOL)hasDatabaseFile {
+    
+    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
+    NSString *documents = [paths objectAtIndex:0];
+    NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:database_path]) {
+        return YES;
+    }
+    
+    return NO;
+}
+
++ (BOOL)isCachedDataForUser:(NSString *)user {
+    
+    BOOL hasDatabase = [self hasDatabaseFile];
+    if (!hasDatabase) {
+        return NO;
+    }
+    
+    __block BOOL result = NO;
+    
+    NSString *has_sql = [NSString stringWithFormat:@"select count(0) from (select user from favorites union all select user from fields_info  union all select user from history union all select user from search_history) where user = '%@';", user];
+    
+    sqlite3_stmt * statement;
+    sqlite3 *db = [self get_db];
+    
+    if (sqlite3_prepare_v2(db, [has_sql UTF8String], -1, &statement, nil) == SQLITE_OK) {
+        
+        while (sqlite3_step(statement) == SQLITE_ROW) {
+            int c = sqlite3_column_int(statement, 0);
+            result = c > 0;
+        }
+        
+        sqlite3_finalize(statement);
+    } else {
+        DebugLog(@"excute sql:%@ error: %s",sql,sqlite3_errmsg(db));
+    }
+    
+    sqlite3_close(db);
+    
+    return result;
+}
+
++ (void)cleanCacheDataForUser:(NSString *)user {
+    NSString *sql = [NSString stringWithFormat:@"delete from favorites where user = '%@';delete from fields_info where user = '%@';delete from history where user = '%@';delete from search_history where user = '%@';",user,user,user,user];
+    [self jk_excute:sql completion:^(BOOL success) {
+        
+    }];
+}
+
 @end

+ 3 - 0
Apex Mobile/Apex Mobile/AppDelegate.h

@@ -27,4 +27,7 @@ UIKIT_EXTERN NSString * const APPersonModeChangeNotification;
 
 @property (nonatomic,assign) BOOL personalmode;
 -(void)Logout;
+
+@property (nonatomic,assign) BOOL shouldCheckCache;///<检查数据库是否存有用户数据
+
 @end

+ 152 - 56
Apex Mobile/Apex Mobile/HomeViewController.m

@@ -276,11 +276,98 @@ typedef enum {
     NSLog(@"-dialog show %@",self.progressDialog);
 }
 
-- (void)dismissProgressDialog {
-    [self.progressDialog dismissViewControllerAnimated:YES completion:nil];
+// 2019.1.4 增加参数completion
+// 解决加载完成后在dismiss过程中弹出对话框询问上传Cache时警告:while a presentation is in progress!
+- (void)dismissProgressDialog:(void(^)(void))completion {
+    [self.progressDialog dismissViewControllerAnimated:YES completion:completion];
     NSLog(@"-dialog dismiss %@",self.progressDialog);
 }
 
+#pragma mark - Check Database
+
+- (void)checkDatabase {
+    
+    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+    if (appDelegate.shouldCheckCache) {
+        appDelegate.shouldCheckCache = NO;
+        
+        if ([ApexMobileDB isCachedDataForUser:appDelegate.user]) {
+            
+            __weak typeof(self) weakSelf = self;
+
+            // alert
+            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"there is some cached data need to upload to service, do you allow us to upload it? if you reject we would clean it" preferredStyle:UIAlertControllerStyleAlert];
+            UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"Reject" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+                dispatch_async(dispatch_get_global_queue(0, 0), ^{
+                    [ApexMobileDB cleanCacheDataForUser:appDelegate.user];
+                });
+            }];
+            UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+               
+                [weakSelf uploadCache];
+            }];
+            
+            [alertVC addAction:rejectAction];
+            [alertVC addAction:allowAction];
+            
+            [self presentViewController:alertVC animated:YES completion:nil];
+        }
+    }
+}
+
+- (void)uploadCache {
+    
+    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+
+    [self showProgressDialog];
+    
+    __weak typeof(self) weakSelf = self;
+    // upload/handle data
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+        
+        NSString *sql = [ApexMobileDB prepareUploadSQLOfUser:appDelegate.user withDiviceID:RAUtils.deviceID];
+        
+        // upload
+        NSDictionary *json = [RANetwork uploadUserSql:sql];
+        
+        // handle result
+        int result = [json[@"result"] intValue];
+        dispatch_async(dispatch_get_main_queue(), ^{
+            
+            [weakSelf dismissProgressDialog:^{
+                
+                if (result == RESULT_TRUE) {
+                    
+                    [RAUtils message_alert:nil title:@"upload success" controller:self];
+                    
+                } else {
+                    
+                    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"upload local cached data failed,do you want to rery or cancel? if you choose cancel, then we will clean the cache" preferredStyle:UIAlertControllerStyleAlert];
+                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+                        dispatch_async(dispatch_get_global_queue(0, 0), ^{
+                            [ApexMobileDB cleanCacheDataForUser:appDelegate.user];
+                        });
+                    }];
+                    
+                    UIAlertAction *retryAction = [UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
+                        [weakSelf uploadCache];
+                    }];
+                    
+                    [alertVC addAction:cancelAction];
+                    [alertVC addAction:retryAction];
+                    
+                    [self presentViewController:alertVC animated:YES completion:nil];
+                }
+                
+            }]; // dismiss
+            
+            
+        });// main queue
+        
+    }); // global queue
+    
+}
+
 #pragma mark - Load Data
 
 - (NSMutableArray *)shipArray {
@@ -343,30 +430,36 @@ typedef enum {
             
             [self.ShipLoading stopAnimating];
             
-            [weakSelf dismissProgressDialog];
-            
             int result = [[json objectForKey:@"result"] intValue];
-            if (result == RESULT_TRUE) {
+            
+            [weakSelf dismissProgressDialog:^{
+                
+                if (result == RESULT_TRUE) {
+                    
+                    [self.shipArray removeAllObjects];
+                    [self.shipArray addObjectsFromArray:[json objectForKey:@"container_list"]];
+                    self.iconSelectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
+                    [self showMapAnnotaionWithIndexPath:self.iconSelectedIndexPath];
+                    [self.shipTableView reloadData];
+                    
+                    [self checkDatabase];
+                    
+                } else {
+                    NSString *msg = [json objectForKey:@"err_msg"];
+                    [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
+                }
                 
-                [self.shipArray removeAllObjects];
-                [self.shipArray addObjectsFromArray:[json objectForKey:@"container_list"]];
-                self.iconSelectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
-                [self showMapAnnotaionWithIndexPath:self.iconSelectedIndexPath];
-                [self.shipTableView reloadData];
+                self.ShipNoDataBtn.hidden = self.shipArray.count > 0;
+                if (self.shipArray.count) {
+                    [self.shipTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
+                }
+                weakSelf.recentRefresh = NO;
                 
-            } else {
-                NSString *msg = [json objectForKey:@"err_msg"];
-                [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
-            }
+            }];// dismiss
 
-            self.ShipNoDataBtn.hidden = self.shipArray.count > 0;
-            if (self.shipArray.count) {
-                [self.shipTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
-            }
-            weakSelf.recentRefresh = NO;
-        });
+        }); // main queue
         
-    });
+    }); // global queue
 }
 
 - (void)loadKPIData {
@@ -391,46 +484,49 @@ typedef enum {
             
             [weakSelf.KPILoading stopAnimating];
             
-            [weakSelf dismissProgressDialog];
-            
             int result = [[json objectForKey:@"result"] intValue];
-            if (result == RESULT_TRUE) {
-//                self.currentKPI = 1;
-                weakSelf.monthArray = [json objectForKey:@"month"];
+
+            [weakSelf dismissProgressDialog:^{
                 
-                [weakSelf.KPIArray removeAllObjects];
-                NSArray *kpiArr = [json objectForKey:@"KPI"];
-
-//                [self.KPIArray addObject:[[kpiArr firstObject] mutableCopy]];
-                for (int i = 0; i < kpiArr.count; i++) {
-                    NSMutableDictionary *kpiModel = [[kpiArr objectAtIndex:i] mutableCopy];
-                    NSMutableArray *itemMutArr = [NSMutableArray array];
-                    NSArray *itemArr = [kpiModel objectForKey:@"arr_val"];
-                    for (int j = 0; j < itemArr.count; j++) {
-                        NSMutableDictionary *item = [[itemArr objectAtIndex:j] mutableCopy];
-                        [itemMutArr addObject:item];
+                if (result == RESULT_TRUE) {
+                    //                self.currentKPI = 1;
+                    weakSelf.monthArray = [json objectForKey:@"month"];
+                    
+                    [weakSelf.KPIArray removeAllObjects];
+                    NSArray *kpiArr = [json objectForKey:@"KPI"];
+                    
+                    //                [self.KPIArray addObject:[[kpiArr firstObject] mutableCopy]];
+                    for (int i = 0; i < kpiArr.count; i++) {
+                        NSMutableDictionary *kpiModel = [[kpiArr objectAtIndex:i] mutableCopy];
+                        NSMutableArray *itemMutArr = [NSMutableArray array];
+                        NSArray *itemArr = [kpiModel objectForKey:@"arr_val"];
+                        for (int j = 0; j < itemArr.count; j++) {
+                            NSMutableDictionary *item = [[itemArr objectAtIndex:j] mutableCopy];
+                            [itemMutArr addObject:item];
+                        }
+                        [kpiModel setObject:itemMutArr forKey:@"arr_val"];
+                        
+                        //                    [self.KPIArray addObjectsFromArray:kpiArr];
+                        [weakSelf.KPIArray addObject:kpiModel];
                     }
-                    [kpiModel setObject:itemMutArr forKey:@"arr_val"];
+                    //                [self.KPIArray addObject:[[kpiArr lastObject] mutableCopy]];
                     
-//                    [self.KPIArray addObjectsFromArray:kpiArr];
-                    [weakSelf.KPIArray addObject:kpiModel];
-                }
-//                [self.KPIArray addObject:[[kpiArr lastObject] mutableCopy]];
-                
-                
-                [weakSelf.KPITableView reloadData];
-                
-                if (((UITabBarController *)weakSelf.parentViewController).selectedIndex == 0) {
-                    NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
-                    weakSelf.parentViewController.title = str;
+                    
+                    [weakSelf.KPITableView reloadData];
+                    
+                    if (((UITabBarController *)weakSelf.parentViewController).selectedIndex == 0) {
+                        NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
+                        weakSelf.parentViewController.title = str;
+                    }
+                    
+                } else {
+                    NSString *msg = [json objectForKey:@"err_msg"];
+                    [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
                 }
-                
-            } else {
-                NSString *msg = [json objectForKey:@"err_msg"];
-                [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
-            }
-            weakSelf.KPINoDataBtn.hidden = self.KPIArray.count > 0;
-            weakSelf.kpiRefresh = NO;
+                weakSelf.KPINoDataBtn.hidden = self.KPIArray.count > 0;
+                weakSelf.kpiRefresh = NO;
+            }];
+            
         });
         
     });

+ 2 - 0
Apex Mobile/Apex Mobile/RANetwork.h

@@ -44,4 +44,6 @@
 + (NSDictionary *)requestKPI;
 + (NSDictionary *)sendEmail:(NSString *)email CC:(NSString *)cc SerialNo:(NSString *)serialNo;
 
++ (NSDictionary *)uploadUserSql:(NSString *)sql;
+
 @end

+ 21 - 0
Apex Mobile/Apex Mobile/RANetwork.m

@@ -1368,4 +1368,25 @@
     
 }
 
++ (NSDictionary *)uploadUserSql:(NSString *)sql {
+    
+    NSMutableDictionary *params = [NSMutableDictionary dictionary];
+    [params setObject:@"handset_search" forKey:@"action"];
+    [params setValue:sql forKey:@"sql"];
+    
+    NSData* json=[self get_json:URL_UPLOAD_SQL parameters:params  file:nil];
+    
+    if (json==nil)
+    {
+        return @{
+                 @"result" : @RESULT_NET_ERROR,
+                 @"err_msg" : MSG_NET_ERROR
+                 };
+    }
+    
+    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:nil];
+    
+    return resultDic;
+}
+
 @end

+ 1 - 0
Apex Mobile/Apex Mobile/RootViewController.m

@@ -42,6 +42,7 @@
     loginVC.loginSuccessful = ^(void)
     {
         appDelegate.isLogin = YES;
+        appDelegate.shouldCheckCache = YES;
       // 登陆成功后重新刷新tabbar
         self.viewControllers = self.loginbar;
     };

+ 5 - 0
Apex Mobile/Apex Mobile/config.h

@@ -36,6 +36,8 @@
 #define URL_ERR_LOG             @""
 #define URL_SEND_COMM_EMAIL     @"http://192.168.2.138/Online/Online/main_new.php"
 
+// 2019.1.4
+#define URL_UPLOAD_SQL     @"http://192.168.2.138/Online/Online/main_new.php"
 
 #else
 
@@ -54,6 +56,9 @@
 #define URL_ERR_LOG             @""
 #define URL_SEND_COMM_EMAIL     @"https://ra.apexshipping.com/main_new.php"
 
+// 2019.1.4
+#define URL_UPLOAD_SQL     @"https://ra.apexshipping.com/main_new.php"
+
 #endif
 
 #endif /* config_h */