SetupServerViewController.m 12 KB

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