// // LoginSettingViewController.m // RA Image // // Created by Jack on 2017/4/27. // Copyright © 2017年 USAI. All rights reserved. // #import "ServerSettingViewController.h" //#import "ScannerViewController.h" #import "RAQRCodeScannerViewController.h" #import "RAUtils.h" #import "RAUploadManager.h" #import "AppDelegate.h" #import "RADataProvider.h" #import "ZipArchive.h" #import "AboutViewController.h" #import "WebViewController.h" #import "MainViewController.h" #import "ChangePasswordViewController.h" #import "SetupServerViewController.h" #import "PriceGroupListViewController.h" @interface ServerSettingViewController () @property (strong, nonatomic) IBOutlet UITextField *nameTextField; //@property (strong, nonatomic) IBOutlet UITextField *internalTextField; @property (strong, nonatomic) NSString *internalText; @property (nonatomic,copy) NSString *checkedAddress; @end @implementation ServerSettingViewController - (IBAction)onAboutClick:(id)sender { AboutViewController * aboutVC =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"AboutViewController"]; // loginvc.delegate = self; // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种 UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:aboutVC] ; // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种 [self presentViewController:navi animated:YES completion:^{ // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); DebugLog(@"about present........."); // self.btop = false; // <#code#> }]; // // // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:aboutVC] ; // // // // // // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi]; // // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种 // [self presentViewController:navi animated:YES completion:^{ // // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320); // // DebugLog(@"about present........."); // // // self.btop = false; // // <#code#> // }]; } - (IBAction)onPrivacyClick:(id)sender { NSString* url = URL_PRIVACY; NSString* title = @"Privacy Policy"; WebViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"WebViewController"]; ViewController.url = url; ViewController.title = title; [self.navigationController pushViewController:ViewController animated:YES]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // if(appDelegate.price_group.length>0) self.labelPriceGroup.text =appDelegate.price_group; // else // { // [RADataProvider default_price_group]; // self.labelPriceGroup.text =appDelegate.price_group; // // } if(appDelegate.user==nil) self.btnSetPriceGroup.hidden =true; else self.btnSetPriceGroup.hidden =false; } - (void)viewDidLoad { [super viewDidLoad]; NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"]; if([RADataProvider scanRtime:@""]&& addressDic!=nil) { _btnSetServer.hidden=true; } else _btnSetServer.hidden = false; // _btnSetServer.hidden = false; // return; // Do any additional setup after loading the view. // UIView *v = [UIView new]; // [self.view insertSubview:v atIndex:0]; // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneItemClick:)]; // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Scan" style:UIBarButtonItemStylePlain target:self action:@selector(scanItemClick:)]; // [self loadAddress]; // [self registListenKeyboard]; RAUPloadManagerConfigure *configure = [[RAUPloadManagerConfigure alloc] init]; self.autoSwitch.on = configure.autoUpload; // self.autoRmFinishSwitch.on = autoRmFinish; // self.autoRmErrorSwith.on = autoRmErr; self.retryCountBox.text = [NSString stringWithFormat:@"%d",configure.retryCount]; self.retryTimeIntervalBox.text = [NSString stringWithFormat:@"%d",(int)configure.retryTimeIntetval]; [self loadTemplate]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (id)userDefaultsValue:(NSString *)key { return [[NSUserDefaults standardUserDefaults] valueForKey:key]; } - (void)loadAddress { NSDictionary *addressDic = [self userDefaultsValue:kScanAddress]; 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; } self.internalText= sa; self.nameTextField.text = [addressDic objectForKey:@"show_name"]; // RASingleton.sharedInstance.price_index =[addressDic[@"price_type"] intValue]; // NSUInteger selectedIndex = [[addressDic objectForKey:@"selectedIndex"] integerValue]; // switch (selectedIndex) { // case 0: // case 1: { // [self internalCheckBtnClick:self.internalCheckBtn]; // } // break; // case 2: { // [self externalCheckBtnClick:self.externalCheckBtn]; // } // break; // // default: // break; // } } else { // [self internalCheckBtnClick:self.internalCheckBtn]; } } - (void)loadTemplate { NSDictionary *addressDic = [self userDefaultsValue:@"ScanTemplate"]; if (addressDic) { NSString* updatetime=addressDic[@"updatetime"]; self.labelUpdate.text = updatetime; } } #pragma mark - Button Click - (void)doneItemClick:(UIBarButtonItem *)sender { self.checkedAddress = nil; NSUInteger selectedIndex = 1; self.checkedAddress = self.internalText; // if (self.internalCheckBtn.selected) { // self.checkedAddress = self.internalTextField.text; // selectedIndex = 1; // } // if (self.externalCheckBtn.selected) { // self.checkedAddress = self.externalTextField.text; // selectedIndex = 2; // } // if (self.returnValue) { // self.returnValue(self.nameTextField.text, self.checkedAddress); // } __weak typeof(self) weakself = self; [self.navigationController dismissViewControllerAnimated:YES completion:^{ if (weakself.checkedAddress.length) { // 保存信息 NSString *name = weakself.nameTextField.text; NSString *internalAddr = weakself.internalText; // 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"]; } [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"]; [weakself setUserDefaultsValue:addressDic forKey:kScanAddress]; } }]; } - (void)setUserDefaultsValue:(id)value forKey:(NSString *)key { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setValue:value forKey:key]; [userDefaults synchronize]; } - (IBAction)onUnlockCodeClick:(id)sender { } - (IBAction)onUpdateClick:(id)sender { if(self.internalText.length>0) { [RADataProvider updateTemplate:self]; return; 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"]; [self loadTemplate]; [RADataProvider load_scan_models]; [RADataProvider default_price_group]; } //[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 download template file. Please check online ERP setting or contact your administrator." title:@"Warrning" controller:self]; [ActiveViewController Notify:@"ScanSearchViewController" Message:@"Clear"]; }]; // 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; ; // }]; } else { [RAUtils message_alert:@"Please setup the server first." title:@"Warrning" controller:self]; } } - (IBAction)onScanClick:(id)sender { // __weak typeof(self) weakself = self; //// ScannerViewController *scannerVC = [[UIStoryboard storyboardWithName:@"cam_scan" bundle:nil] instantiateViewControllerWithIdentifier:@"NewScannerViewController"]; //// scannerVC.returnCode = ^(NSString *code) { //// // 扫描成功保存扫描值 //// if (weakself) { //// __strong typeof(weakself) strongSelf = weakself; //// [strongSelf processScanResult:code]; //// } //// }; //// [self presentViewController:scannerVC animated:YES completion:nil]; // // RAQRCodeScannerViewController *scannerVC = [[UIStoryboard storyboardWithName:@"QRCode" bundle:nil] instantiateViewControllerWithIdentifier:@"RAQRCodeScannerViewControllerAuto"]; // //// RAQRCodeScannerViewController *scannerVC = [RAQRCodeScannerViewController viewControllerFromStoryboard]; // scannerVC.QRCodeViewControllerDidCompletion = ^(RAQRCodeScannerViewController *qrcodeViewController, NSString *value) { // // if (weakself) { // __strong typeof(weakself) strongSelf = weakself; // [strongSelf processScanResult:value]; // // // } // // [qrcodeViewController dismissViewControllerAnimated:YES completion:nil]; // }; // // scannerVC.QRCodeViewControllerDidCanceled = ^(RAQRCodeScannerViewController *qrcodeViewController) { // // [qrcodeViewController dismissViewControllerAnimated:YES completion:nil]; // }; // // [self presentViewController:scannerVC animated:YES completion:nil]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; if(appDelegate.bLogin) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Set Server" message:@"Are you sure you to sign out and setup the server?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"Sign out and set" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { //open exist AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; appDelegate.cart_count = 0;//[[jsobj :@"cart_count"] intValue]; appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue]; appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue]; [appDelegate update_count_mark]; appDelegate.can_show_price =false; appDelegate.can_see_price =false; appDelegate.can_create_portfolio =false; appDelegate.can_create_order =false; appDelegate.can_cancel_order =false; appDelegate.can_set_cart_price =false; appDelegate.can_delete_order =false; appDelegate.can_submit_order =false; appDelegate.can_set_tearsheet_price =false; appDelegate.can_update_contact_info = false; appDelegate.save_order_logout = false; appDelegate.submit_order_logout = false; appDelegate.alert_sold_in_quantities = false; appDelegate.ipad_perm =nil ; appDelegate.user_type = USER_ROLE_UNKNOWN; appDelegate.OrderFilter= nil; [appDelegate SetSo:nil]; [appDelegate set_main_button_panel]; #ifdef RA_NOTIFICATION [ActiveViewController Notify:@"SearchViewController" Message:RA_NOTIFICATION_CLEAR_DATA]; #endif [appDelegate Logout]; SetupServerViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"SetupServerViewController"]; servervc.showCancel = true; servervc.returnValue = ^{ NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"]; if (addressDic) { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; main_vc.labelServer.text = [addressDic objectForKey:@"show_name"]; self.nameTextField.text = [addressDic objectForKey:@"show_name"]; self.internalText = [addressDic objectForKey:@"serverAddress"]; } }; [self.navigationController pushViewController:servervc animated:true]; }]; UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { //create new; }]; [alertController addAction:action_1]; [alertController addAction:action_2]; [self presentViewController:alertController animated:YES completion:nil]; } else { SetupServerViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"SetupServerViewController"]; servervc.showCancel = true; servervc.returnValue = ^{ NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"]; if (addressDic) { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; main_vc.labelServer.text = [addressDic objectForKey:@"show_name"]; self.nameTextField.text = [addressDic objectForKey:@"name"]; self.internalText = [addressDic objectForKey:@"serverAddress"]; } [self onUpdateClick:nil]; }; [self.navigationController pushViewController:servervc animated:true]; } } - (IBAction)onSetPriceGroup:(id)sender { PriceGroupListViewController * listvc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"PriceGroupListViewController"]; AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSMutableDictionary* pricegroup=[RADataProvider all_price_group]; NSMutableArray* arrlist = [NSMutableArray new]; for(int i=0;i }]; } - (IBAction)onDeleteAccountClick:(id)sender { UIApplication * app = [UIApplication sharedApplication]; AppDelegate *appDelegate = (AppDelegate *)[app delegate]; if(appDelegate.user.length==0) { [RAUtils message_alert:@"You must sign in first." title:@"Delete Account" controller:self]; return; } UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Account" message:@"Are you sure you want to permanently delete your account?" preferredStyle:UIAlertControllerStyleAlert]; //增加确定按钮 [alertController addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){ //获取第1个输入框; UITextField *titleTextField = alertController.textFields.firstObject; if([appDelegate.password isEqualToString:titleTextField.text]) { NSLog(@"%@", @"run process"); __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Delete Account" completion:^{ [RANetwork request_delete_account:^(NSMutableDictionary *result) { NSMutableDictionary* return_json =result; [waitalert dismissViewControllerAnimated:YES completion:^{ if([[return_json valueForKey:@"result"] intValue]==2) { // [self hideMenu]; // [self switchToHome:nil]; [appDelegate Logout]; MainViewController* main_vc=(MainViewController*)appDelegate.main_vc; [main_vc checklogin :false]; [RAUtils message_alert:@"Account deleted." title:nil controller:self]; } else { [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Account" controller:self] ; } }]; }]; }]; } else { [RAUtils message_alert:@"Wrong password." title:nil controller:self]; } }]]; //增加取消按钮; [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]]; //定义第一个输入框; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"Login Password"; textField.textContentType = UITextContentTypePassword; }]; [self presentViewController:alertController animated:true completion:nil]; } @end