// // RADataProvider.m // iSales-USAI // // Created by Rui Zhang on 11/11/21. // Copyright © 2021 United Software Applications, Inc. All rights reserved. // #import "RADataProvider.h" #import "ZipArchive.h" #import "ActiveViewController.h" #import "ScanRefreshStock.h" @implementation RADataProvider +(void)request_autocomplete:(NSMutableDictionary*)param completionHandler:(resultHandler)result { param[@"site_name"]=[self getSiteName]; // param[@"_method"]=@"customerID"; AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; NSString* full_url ; if([param[@"full_url"] boolValue]) full_url =param[@"url"]; else { if(appDelegate.address==nil||[param[@"url"] stringValue].length==0) { NSMutableDictionary* ret=[[NSMutableDictionary alloc]init]; ret[@"result"]=@"8"; ret[@"err_msg"]=@"Missing query url, please contact administrator."; } full_url = [appDelegate.address stringByAppendingPathComponent:[param[@"url"] stringValue]]; } [self request_interface:full_url parameters:param err_record_url:nil completionHandler:result retry:0]; } +(NSString*)getSiteName { NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];; if (addressDic) { NSString* ia=addressDic[@"internalAddress"]; NSString* ea=addressDic[@"externalAddress"]; NSString* sa=addressDic[@"serverAddress"]; if(sa.length==0) { if(ea.length>0) sa=ea; else if(ia.length>0) sa=ia; } return [addressDic objectForKey:@"name"]; } return @""; } #ifdef SCANNER_ORDER +(NSMutableDictionary*)scan_search:(NSString*)keyword { // NSMutableArray* arr = [NSMutableArray new]; if(keyword.length==0) keyword=@""; int count = 0; NSMutableDictionary * ret = [NSMutableDictionary new]; ret[@"result"]=@2; AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; // NSArray* keys=[dict allKeys]; for(int i=0;i0) { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [paths objectAtIndex:0]; [RAUtils deletefiles:[documents stringByAppendingPathComponent:@"download"]]; RASingleton.sharedInstance.scan_list = nil; [RADataProvider load_scan_models]; // __block UIAlertController * waitalert = [RAUtils waiting_alert:vc title:@"Update local data" completion:^{ PopWaitAlert * pop = [RAUtils waiting_pop:@"Update local data" completion:nil]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSData* download_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]]; dispatch_async(dispatch_get_main_queue(), ^{ // [waitalert dismissViewControllerAnimated:false completion:^{ [pop hide]; if(download_data!=nil) { NSString* file_name=[appDelegate.downloadurl lastPathComponent]; NSString* path = [documents stringByAppendingPathComponent:file_name]; [RAUtils saveData:download_data toPath:path]; ZipArchive* zip = [[ZipArchive alloc] init]; NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"]; // NSFileManager* fileManager=[NSFileManager defaultManager]; // [fileManager removeItemAtPath:unZipTo error:nil]; // NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation]; // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; //NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ; // NSString* unZipTo = img_cache;//[documentPath stringByAppendingString:@"/images"] ; DebugLog(@"path: %@",unZipTo); if( [zip UnzipOpenFile:path Password:nil] ){ BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES]; if( NO==result ) { // int aaa=0; //解压失败 [zip UnzipCloseFile]; //文件损坏,删除文件重试 NSFileManager* fileManager=[NSFileManager defaultManager]; //debug // BOOL blDele= [fileManager removeItemAtPath:zipFile error:nil]; [fileManager removeItemAtPath:path error:nil]; [fileManager removeItemAtPath:unZipTo error:nil]; [RAUtils message_box:@"Warrning" message:@"Can not unzip template file, please try download again." completion:nil]; } else { [zip UnzipCloseFile]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:path error:nil]; [RAUtils message_box:@"Message" message:@"Update template file successful." completion:nil]; // [RADataProvider encrypt_scan_models]; NSMutableDictionary *templateDic = [NSMutableDictionary dictionary]; NSString* datetime= [RAUtils current_date_time]; templateDic[@"updatetime"]= datetime; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setValue:templateDic forKey:@"ScanTemplate"]; [userDefaults synchronize]; [RADataProvider load_scan_models]; if([self get_price_group:appDelegate.price_group]==nil ) [RADataProvider default_price_group]; // [self loadTemplate]; } //[zip UnzipCloseFile]; } else { //打开文件失败,通常是文件路径有问题或密码错误。 [RAUtils message_box:@"Warrning" message:@"Can not unzip template file, please contact the Administrator." completion:nil]; // NSFileManager *fileManager = [NSFileManager defaultManager]; // [fileManager removeItemAtPath:zipFile error:nil]; } // [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url]; // // UIImage * img =[UIImage imageWithData:downloadimg_data]; // cell.imageView.image=img; } else [RAUtils message_box:@"Warrning" message:@"Can not update template file, you can update manually in the setting." completion:nil]; // cell.imageView.image=[UIImage imageNamed:@"notfound_s"]; [ActiveViewController Notify:@"ScanSearchViewController" Message:@"Clear"]; [ActiveViewController Notify:@"ServerSettingViewController" Message:RA_NOTIFICATION_REFRESH_UI]; // }]; }); }); // }]; // NSString* file_name=[appDelegate.downloadurl lastPathComponent]; // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]]; // [RADataProvider downloadFileOffset:0 Param:nil from:appDelegate.downloadurl method:@"post" toPath:[documents stringByAppendingPathComponent:file_name] progressHandler:nil completionHandler:^(NSMutableDictionary *result) { // int i=0; ; // }]; } } +(void)load_scan_models { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [paths objectAtIndex:0]; NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"]; NSString* templatefile = [unZipTo stringByAppendingPathComponent:@"offline_models.json"]; NSString* str = [NSString stringWithContentsOfFile:templatefile encoding:NSUTF8StringEncoding error:nil]; NSString * str3 = [AESCrypt AES128Decrypt:str key:@"usai2010"]; NSMutableDictionary* of = [[RAConvertor string2dict:str3] mutableCopy]; NSArray* keysort=[self sortedDictionary:of]; AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate.scan_model = of; appDelegate.scan_model_key=keysort; } +(NSMutableDictionary*)all_price_group { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [paths objectAtIndex:0]; NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"]; AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; NSString* templatefile1 = [unZipTo stringByAppendingPathComponent:@"offline_price_group.json"]; NSString* str1 = [NSString stringWithContentsOfFile:templatefile1 encoding:NSUTF8StringEncoding error:nil]; NSMutableDictionary* pg = [[RAConvertor string2dict:str1] mutableCopy]; return pg; } +(NSDictionary*)get_price_group:(NSString*) groupname { NSMutableDictionary* pg = [self all_price_group]; return pg[groupname]; } +(void)default_price_group { AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; // 如果用户有设置价格组,则默认为第一个,如果没有设置,则用服务器设置的默认价格组。 if(appDelegate.available_price_group.count!=0) { appDelegate.price_group=nil; // 账号price group 要检查合法性 for(int i=0;i