// // 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" @implementation RADataProvider #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;i0) sa=ea; else if(ia.length>0) sa=ia; } return [addressDic objectForKey:@"name"]; } return @""; } +(bool)getSiteHasERP { AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; if( [appDelegate.user isEqualToString:@"GUEST"]) return false; NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];; if (addressDic) { return [addressDic[@"has_erp_site"] boolValue]; } return false; } #endif +(void)request_change_password:(NSString*) pwd completionHandler:(resultHandler)result { AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; NSMutableDictionary* param = [NSMutableDictionary new]; param[@"_method"]=@"changePasswordOfflineOrderUser"; param[@"opsw"]=appDelegate.password; param[@"npsw"]=pwd; param[@"user"]=appDelegate.user; param[@"site_name"]=[self getSiteName]; // param[@"url"]=url; NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_CHANGE_PASSWORD]; [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0]; } +(void)request_sign_up:user email:(NSString*)email password:(NSString*) pwd completionHandler:(resultHandler)result { AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; NSMutableDictionary* param = [NSMutableDictionary new]; param[@"_method"]=@"registerOfflineOrderUser"; param[@"password"]=pwd; param[@"email"]=email; param[@"user"]=user; param[@"site_name"]=[self getSiteName]; // param[@"url"]=url; NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP]; [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0]; } +(void)request_change_password:user oldpassword:(NSString*)opwd newpassword:(NSString*) npwd completionHandler:(resultHandler)result { AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; NSMutableDictionary* param = [NSMutableDictionary new]; param[@"_method"]=@"changePasswordOfflineOrderUser"; param[@"opsw"]=opwd; param[@"password"]=opwd; param[@"npsw"]=npwd; param[@"user"]=user; param[@"site_name"]=[self getSiteName]; // param[@"url"]=url; NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP]; [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0]; } +(void)request_editor:(NSString*) request_url params:(NSMutableDictionary*)params completionHandler:(resultHandler)result { // NSMutableDictionary* params = [[NSMutableDictionary alloc] init]; AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.user!=nil) [params setValue:appDelegate.user forKey:@"user"]; // if(![appDelegate.order_customer_id isEqualToString: appDelegate.contact_id]&& appDelegate.order_customer_id!=nil) // [params setValue:appDelegate.order_customer_id forKey:@"contactId"]; // else if(appDelegate.contact_id!=nil) [params setValue:appDelegate.contact_id forKey:@"contactId"]; if(appDelegate.password!=nil) [params setValue:appDelegate.password forKey:@"password"]; if(appDelegate.order_code!=nil) [params setValue:appDelegate.order_code forKey:@"orderCode"]; bool can_create_backorder=appDelegate.can_create_backorder; // [params setValue:sourceid forKey:@"sourceid"]; // [params setValue:editor forKey:@"editor"]; DebugLog(@"request editor url:%@",request_url); if([request_url isEqualToString:URL_NEW_CUSTOMER]) { [params setObject:ScreenCodeNewCustomer forKey:kScreenName]; } else if([request_url isEqualToString:URL_EDIT_CUSTOMER]) { [params setObject:ScreenCodeEditCustomer forKey:kScreenName]; } else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH]) { [params setObject:ScreenCodeAdvanceSearch forKey:kScreenName]; } else if([request_url isEqualToString:URL_CARTDELIVERY]) { [params setObject:ScreenCodePlaceOrder forKey:kScreenName]; } else if([request_url isEqualToString:URL_ADDRESS_EDOTOR]) { [params setObject:ScreenCodeNewAddress forKey:kScreenName]; } else if ([request_url isEqualToString:URL_DM_PARAMS]){ [params setObject:ScreenCodeCreatePortfolio forKey:kScreenName]; } if(appDelegate.offline_mode) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* json = nil; if([request_url isEqualToString:URL_NEW_CUSTOMER]) { json= [OLDataProvider offline_createContact:params]; } else if([request_url isEqualToString:URL_EDIT_CUSTOMER]) { json= [OLDataProvider offline_editContact:params]; } else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH]) { json= [OLDataProvider offline_contactAdvancedSearch:params]; } else if([request_url isEqualToString:URL_CARTDELIVERY]) { params[@"contact_id"]=params[@"contactId"]; params[@"can_create_backorder"]=@(can_create_backorder); json= [OLDataProvider offline_placeorder:params]; } else if([request_url isEqualToString:URL_ADDRESS_EDOTOR]) { json= [OLDataProvider offline_addreditor:params]; } else if ([request_url isEqualToString:URL_DM_PARAMS]){ json = [OLDataProvider offline_createTearSheet:params]; } dispatch_async(dispatch_get_main_queue(), ^{ if(json==nil) result( [[OLDataProvider offline_notimpl] mutableCopy]); else result([[RAConvertor data2dict:json] mutableCopy]); }); }); return; } else { if([request_url isEqualToString:URL_CARTDELIVERY]) { NSString * uuid= [[NSUUID UUID] UUIDString]; params[@"resultSerial"]=uuid; __block bool bbreak = false; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ for(int i=0;i<150;i++) { // NSMutableDictionary* result_params = [[NSMutableDictionary alloc]init]; // result_params[@"resultSerial"]=uuid; //json=[self get_json:request_url parameters:params]; if(bbreak) break; dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"request cart delivery %d",i); [self request_interface:request_url parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *resulti) { if(resulti!=nil) { if([resulti[@"result"] intValue]==2 || [resulti[@"result"] intValue] == 8) { if(!bbreak) //此判断保证result只调用一次。 { result(resulti); NSLog(@"result called at %d",i); } bbreak=true; } } } retry:3]; }); // [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0]; sleep(2); } }); } else [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0]; } // if(json!=nil) // { // NSError *error=nil; // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error]; // return jsobj; // } // else // return nil; } +(void)request_commoneditor_partialrefresh: (NSMutableDictionary*)params url:(NSString*)url completionHandler:(resultHandler)result { // NSMutableDictionary* params = [[NSMutableDictionary alloc] init]; AppDelegate *appDelegate = nil; appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate]; 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"]; // if(appDelegate.order_code!=nil) // [params setValue:appDelegate.order_code forKey:@"orderCode"]; // NSDictionary* customerinfo= appDelegate.customerInfo; // // NSArray* keys=[customerinfo allKeys]; // for(int i=0;i