SetupServerViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. // NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  91. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  92. NSData* download_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  93. dispatch_async(dispatch_get_main_queue(), ^{
  94. if(download_data!=nil)
  95. {
  96. NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  97. NSString* path = [documents stringByAppendingPathComponent:file_name];
  98. [RAUtils saveData:download_data toPath:path];
  99. ZipArchive* zip = [[ZipArchive alloc] init];
  100. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  101. // NSFileManager* fileManager=[NSFileManager defaultManager];
  102. // [fileManager removeItemAtPath:unZipTo error:nil];
  103. // NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation];
  104. //
  105. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  106. // NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  107. //NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
  108. // NSString* unZipTo = img_cache;//[documentPath stringByAppendingString:@"/images"] ;
  109. DebugLog(@"path: %@",unZipTo);
  110. if( [zip UnzipOpenFile:path Password:nil] ){
  111. BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES];
  112. if( NO==result )
  113. {
  114. // int aaa=0;
  115. //解压失败
  116. [zip UnzipCloseFile];
  117. //文件损坏,删除文件重试
  118. NSFileManager* fileManager=[NSFileManager defaultManager];
  119. //debug
  120. // BOOL blDele= [fileManager removeItemAtPath:zipFile error:nil];
  121. [fileManager removeItemAtPath:path error:nil];
  122. [fileManager removeItemAtPath:unZipTo error:nil];
  123. [RAUtils message_alert:@"Can not unzip template file, please try download again." title:@"Warrning" controller:self];
  124. }
  125. else
  126. {
  127. [zip UnzipCloseFile];
  128. NSFileManager *fileManager = [NSFileManager defaultManager];
  129. [fileManager removeItemAtPath:path error:nil];
  130. [RAUtils message_alert:@"Update templete file successful." title:@"Message" controller:self];
  131. NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  132. NSString* datetime= [RAUtils current_date_time];
  133. templateDic[@"updatetime"]= datetime;
  134. [self setUserDefaultsValue:templateDic forKey:@"ScanTemplate"];
  135. [OLDataProvider guestLogin];
  136. // [self loadTemplate];
  137. }
  138. //[zip UnzipCloseFile];
  139. }
  140. else
  141. {
  142. //打开文件失败,通常是文件路径有问题或密码错误。
  143. [RAUtils message_alert:@"Can not unzip template file, please contact the Administrator." title:@"Warrning" controller:self];
  144. // NSFileManager *fileManager = [NSFileManager defaultManager];
  145. // [fileManager removeItemAtPath:zipFile error:nil];
  146. }
  147. // [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  148. //
  149. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  150. // cell.imageView.image=img;
  151. }
  152. else
  153. [RAUtils message_alert:@"Can not update template file, you can update manually in the setting." title:@"Warrning" controller:self];
  154. // cell.imageView.image=[UIImage imageNamed:@"notfound_s"];
  155. });
  156. });
  157. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  158. // [RADataProvider downloadFileOffset:0 Param:nil from:appDelegate.downloadurl method:@"post" toPath:[documents stringByAppendingPathComponent:file_name] progressHandler:nil completionHandler:^(NSMutableDictionary *result) {
  159. // int i=0; ;
  160. // }];
  161. }
  162. }
  163. - (IBAction)onSelectServer:(id)sender {
  164. ScanServerListViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"ScanServerListViewController"];
  165. servervc.serverlist = self.serverlist;
  166. servervc.returnValue = ^(int index) {
  167. self.server = self.serverlist[index];
  168. [self.btnSelectServer setTitle:self.server[@"show_name"] forState:UIControlStateNormal];
  169. };
  170. [self.navigationController pushViewController:servervc animated:true];
  171. }
  172. - (IBAction)onreloadList:(id)sender {
  173. [self loadServerList];
  174. }
  175. -(void) loadServerList
  176. {
  177. __block UIAlertController * waitalert =[RAUtils waiting_alert:self title:@"Loading Server List" completion:^{
  178. [RADataProvider request_scan_server:^(NSMutableDictionary *result) {
  179. NSMutableDictionary* return_json = result;
  180. [waitalert dismissViewControllerAnimated:YES completion:^{
  181. if([[return_json valueForKey:@"result"] intValue]==2)
  182. {
  183. self.serverlist = return_json[@"hostList"];
  184. if(self.serverlist.count>0)
  185. {
  186. }
  187. }
  188. else
  189. {
  190. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ;
  191. }
  192. }];
  193. }];
  194. }];
  195. }
  196. /*
  197. #pragma mark - Navigation
  198. // In a storyboard-based application, you will often want to do a little preparation before navigation
  199. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  200. // Get the new view controller using [segue destinationViewController].
  201. // Pass the selected object to the new view controller.
  202. }
  203. */
  204. @end