// // SetupServerViewController.m // HMLG Scan Order // // Created by Rui Zhang on 4/1/22. // Copyright © 2022 United Software Applications, Inc. All rights reserved. // #import "SetupServerViewController.h" #import "RADataProvider.h" #import "ScanServerListViewController.h" #import "ZipArchive.h" #import "MainViewController.h" @interface SetupServerViewController () @end @implementation SetupServerViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. if(self.showCancel) self.btnCancel.hidden = false; else self.btnCancel.hidden = true; [self loadServerList]; } - (IBAction)onCancelClick:(id)sender { [self.navigationController popViewControllerAnimated:true]; } - (IBAction)onSaveClick:(id)sender { if(self.editPassword.text.length==0) { [RAUtils message_alert:@"Password can't be empty" title:@"verify Server" controller:self]; return; } __block UIAlertController * waitalert =[RAUtils waiting_alert:self title:@"Verify" completion:nil]; [RADataProvider request_validate_scan_server:self.server[@"url"] name:self.server[@"name"] password:self.editPassword.text completionHandler:^(NSMutableDictionary *result) { NSMutableDictionary* return_json = result; [waitalert dismissViewControllerAnimated:YES completion:^{ if([[return_json valueForKey:@"result"] intValue]==2) { NSUInteger selectedIndex = 1; // 保存信息 NSString *name = self.server[@"name"]; NSString *show_name = self.server[@"show_name"]; NSString *internalAddr = self.server[@"url"]; int price_index = [self.server[@"price_type"] intValue]; // NSString *externalAddr = weakself.externalTextField.text; NSMutableDictionary *addressDic = [NSMutableDictionary dictionary]; if (internalAddr.length) { [addressDic setObject:internalAddr forKey:@"serverAddress"]; } // if (externalAddr.length) { // [addressDic setObject:externalAddr forKey:@"externalAddress"]; // } if (name.length) { [addressDic setObject:name forKey:@"name"]; } if (show_name.length) { [addressDic setObject:show_name forKey:@"show_name"]; } [addressDic setObject:@(price_index) forKey:@"price_index"]; [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setValue:addressDic forKey:@"ScanAddress"]; [userDefaults synchronize]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate.address = [addressDic objectForKey:@"serverAddress"]; // [self updateTemplate]; [ActiveViewController Notify:@"ScanHomeViewController," Message:@"RA_NOTIFICATION_RELOAD_DATA"]; [self.navigationController popViewControllerAnimated:false]; if(self.returnValue) self.returnValue(); } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ; } }]; }]; } - (void)setUserDefaultsValue:(id)value forKey:(NSString *)key { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setValue:value forKey:key]; [userDefaults synchronize]; } //-(void) updateTemplate //{ // //// if(self.internalTextField.text.length>0) // { // 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]; //// NSString* file_name=[appDelegate.downloadurl lastPathComponent]; // // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update local data" completion:^{ // 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:^{ // 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_alert:@"Can not unzip template file, please try download again." title:@"Warrning" controller:self]; // // // // // } // else // { // [zip UnzipCloseFile]; // // // NSFileManager *fileManager = [NSFileManager defaultManager]; // [fileManager removeItemAtPath:path error:nil]; //// [RAUtils message_alert:@"Update template file successful." title:@"Message" controller:self]; // // // // [RADataProvider encrypt_scan_models]; // NSMutableDictionary *templateDic = [NSMutableDictionary dictionary]; // NSString* datetime= [RAUtils current_date_time]; // templateDic[@"updatetime"]= datetime; // [self setUserDefaultsValue:templateDic forKey:@"ScanTemplate"]; // // // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; // [main_vc scanLogin]; // // [OLDataProvider guestLogin]; // [RADataProvider load_scan_models]; // // [self loadTemplate]; // // // [RAUtils message_alert:@"Update template file successful." title:@"Message" controller:self action_handler:^(UIAlertAction * _Nonnull action) { // [self.navigationController popViewControllerAnimated:false]; // } completion:nil]; // // // } // //[zip UnzipCloseFile]; // } // else // { // //打开文件失败,通常是文件路径有问题或密码错误。 // [RAUtils message_alert:@"Can not unzip template file, please contact the Administrator." title:@"Warrning" controller:self]; // // // 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_alert:@"Can not update template file, you can update manually in the setting." title:@"Warrning" controller:self]; // // // // }]; // // // // // cell.imageView.image=[UIImage imageNamed:@"notfound_s"]; // // }); // // }); // }]; // // // // // // //// 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; ; //// }]; // } // //} - (IBAction)onSelectServer:(id)sender { ScanServerListViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"ScanServerListViewController"]; servervc.serverlist = self.serverlist; servervc.returnValue = ^(int index) { self.server = self.serverlist[index]; [self.btnSelectServer setTitle:self.server[@"show_name"] forState:UIControlStateNormal]; }; [self.navigationController pushViewController:servervc animated:true]; } - (IBAction)onreloadList:(id)sender { [self loadServerList]; } -(void) loadServerList { __block UIAlertController * waitalert =[RAUtils waiting_alert:self title:@"Loading Server List" completion:^{ [RADataProvider request_scan_server:^(NSMutableDictionary *result) { NSMutableDictionary* return_json = result; [waitalert dismissViewControllerAnimated:YES completion:^{ if([[return_json valueForKey:@"result"] intValue]==2) { self.serverlist = return_json[@"hostList"]; if(self.serverlist.count>0) { } } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ; } }]; }]; }]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end