SetupServerViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // SetupServerViewController.m
  3. // HMLG Scan Order
  4. //
  5. // Created by Rui Zhang on 4/1/22.
  6. // Copyright © 2022 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SetupServerViewController.h"
  9. #import "RADataProvider.h"
  10. #import "ScanServerListViewController.h"
  11. #import "ZipArchive.h"
  12. #import "MainViewController.h"
  13. @interface SetupServerViewController ()
  14. @end
  15. @implementation SetupServerViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. // Do any additional setup after loading the view.
  19. if(self.showCancel)
  20. self.btnCancel.hidden = false;
  21. else
  22. self.btnCancel.hidden = true;
  23. [self loadServerList];
  24. }
  25. - (IBAction)onCancelClick:(id)sender {
  26. [self.navigationController popViewControllerAnimated:true];
  27. }
  28. - (IBAction)onSaveClick:(id)sender {
  29. if(self.editPassword.text.length==0)
  30. {
  31. [RAUtils message_alert:@"Password can't be empty" title:@"verify Server" controller:self];
  32. return;
  33. }
  34. __block UIAlertController * waitalert =[RAUtils waiting_alert:self title:@"Verify" completion:nil];
  35. [RADataProvider request_validate_scan_server:self.server[@"url"] name:self.server[@"name"] password:self.editPassword.text completionHandler:^(NSMutableDictionary *result) {
  36. NSMutableDictionary* return_json = result;
  37. [waitalert dismissViewControllerAnimated:YES completion:^{
  38. if([[return_json valueForKey:@"result"] intValue]==2)
  39. {
  40. NSUInteger selectedIndex = 1;
  41. // 保存信息
  42. NSString *name = self.server[@"name"];
  43. NSString *show_name = self.server[@"show_name"];
  44. NSString *internalAddr = self.server[@"url"];
  45. int price_index = [self.server[@"price_type"] intValue];
  46. // NSString *externalAddr = weakself.externalTextField.text;
  47. NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
  48. if (internalAddr.length) {
  49. [addressDic setObject:internalAddr forKey:@"serverAddress"];
  50. }
  51. // if (externalAddr.length) {
  52. // [addressDic setObject:externalAddr forKey:@"externalAddress"];
  53. // }
  54. if (name.length) {
  55. [addressDic setObject:name forKey:@"name"];
  56. }
  57. if (show_name.length) {
  58. [addressDic setObject:show_name forKey:@"show_name"];
  59. }
  60. [addressDic setObject:@(price_index) forKey:@"price_index"];
  61. [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"];
  62. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  63. [userDefaults setValue:addressDic forKey:@"ScanAddress"];
  64. [userDefaults synchronize];
  65. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  66. appDelegate.address = [addressDic objectForKey:@"serverAddress"];
  67. [self updateTemplate];
  68. if(self.returnValue)
  69. self.returnValue();
  70. [self.navigationController popViewControllerAnimated:true];
  71. }
  72. else
  73. {
  74. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ;
  75. }
  76. }];
  77. }];
  78. }
  79. - (void)setUserDefaultsValue:(id)value forKey:(NSString *)key {
  80. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  81. [userDefaults setValue:value forKey:key];
  82. [userDefaults synchronize];
  83. }
  84. -(void) updateTemplate
  85. {
  86. // if(self.internalTextField.text.length>0)
  87. {
  88. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  89. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  90. NSString *documents = [paths objectAtIndex:0];
  91. [RAUtils deletefiles:[documents stringByAppendingPathComponent:@"download"]];
  92. // NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  93. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  94. NSData* download_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  95. dispatch_async(dispatch_get_main_queue(), ^{
  96. if(download_data!=nil)
  97. {
  98. NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  99. NSString* path = [documents stringByAppendingPathComponent:file_name];
  100. [RAUtils saveData:download_data toPath:path];
  101. ZipArchive* zip = [[ZipArchive alloc] init];
  102. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  103. // NSFileManager* fileManager=[NSFileManager defaultManager];
  104. // [fileManager removeItemAtPath:unZipTo error:nil];
  105. // NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation];
  106. //
  107. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  108. // NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  109. //NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
  110. // NSString* unZipTo = img_cache;//[documentPath stringByAppendingString:@"/images"] ;
  111. DebugLog(@"path: %@",unZipTo);
  112. if( [zip UnzipOpenFile:path Password:nil] ){
  113. BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES];
  114. if( NO==result )
  115. {
  116. // int aaa=0;
  117. //解压失败
  118. [zip UnzipCloseFile];
  119. //文件损坏,删除文件重试
  120. NSFileManager* fileManager=[NSFileManager defaultManager];
  121. //debug
  122. // BOOL blDele= [fileManager removeItemAtPath:zipFile error:nil];
  123. [fileManager removeItemAtPath:path error:nil];
  124. [fileManager removeItemAtPath:unZipTo error:nil];
  125. [RAUtils message_alert:@"Can not unzip template file, please try download again." title:@"Warrning" controller:self];
  126. }
  127. else
  128. {
  129. [zip UnzipCloseFile];
  130. NSFileManager *fileManager = [NSFileManager defaultManager];
  131. [fileManager removeItemAtPath:path error:nil];
  132. [RAUtils message_alert:@"Update template file successful." title:@"Message" controller:self];
  133. // [RADataProvider encrypt_scan_models];
  134. NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  135. NSString* datetime= [RAUtils current_date_time];
  136. templateDic[@"updatetime"]= datetime;
  137. [self setUserDefaultsValue:templateDic forKey:@"ScanTemplate"];
  138. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  139. [main_vc scanLogin];
  140. // [OLDataProvider guestLogin];
  141. [RADataProvider load_scan_models];
  142. // [self loadTemplate];
  143. }
  144. //[zip UnzipCloseFile];
  145. }
  146. else
  147. {
  148. //打开文件失败,通常是文件路径有问题或密码错误。
  149. [RAUtils message_alert:@"Can not unzip template file, please contact the Administrator." title:@"Warrning" controller:self];
  150. // NSFileManager *fileManager = [NSFileManager defaultManager];
  151. // [fileManager removeItemAtPath:zipFile error:nil];
  152. }
  153. // [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  154. //
  155. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  156. // cell.imageView.image=img;
  157. }
  158. else
  159. [RAUtils message_alert:@"Can not update template file, you can update manually in the setting." title:@"Warrning" controller:self];
  160. // cell.imageView.image=[UIImage imageNamed:@"notfound_s"];
  161. });
  162. });
  163. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  164. // [RADataProvider downloadFileOffset:0 Param:nil from:appDelegate.downloadurl method:@"post" toPath:[documents stringByAppendingPathComponent:file_name] progressHandler:nil completionHandler:^(NSMutableDictionary *result) {
  165. // int i=0; ;
  166. // }];
  167. }
  168. }
  169. - (IBAction)onSelectServer:(id)sender {
  170. ScanServerListViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"ScanServerListViewController"];
  171. servervc.serverlist = self.serverlist;
  172. servervc.returnValue = ^(int index) {
  173. self.server = self.serverlist[index];
  174. [self.btnSelectServer setTitle:self.server[@"show_name"] forState:UIControlStateNormal];
  175. };
  176. [self.navigationController pushViewController:servervc animated:true];
  177. }
  178. - (IBAction)onreloadList:(id)sender {
  179. [self loadServerList];
  180. }
  181. -(void) loadServerList
  182. {
  183. __block UIAlertController * waitalert =[RAUtils waiting_alert:self title:@"Loading Server List" completion:^{
  184. [RADataProvider request_scan_server:^(NSMutableDictionary *result) {
  185. NSMutableDictionary* return_json = result;
  186. [waitalert dismissViewControllerAnimated:YES completion:^{
  187. if([[return_json valueForKey:@"result"] intValue]==2)
  188. {
  189. self.serverlist = return_json[@"hostList"];
  190. if(self.serverlist.count>0)
  191. {
  192. }
  193. }
  194. else
  195. {
  196. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ;
  197. }
  198. }];
  199. }];
  200. }];
  201. }
  202. /*
  203. #pragma mark - Navigation
  204. // In a storyboard-based application, you will often want to do a little preparation before navigation
  205. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  206. // Get the new view controller using [segue destinationViewController].
  207. // Pass the selected object to the new view controller.
  208. }
  209. */
  210. @end