SetupServerViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. [ActiveViewController Notify:@"ScanHomeViewController," Message:@"RA_NOTIFICATION_RELOAD_DATA"];
  69. [self.navigationController popViewControllerAnimated:false];
  70. if(self.returnValue)
  71. self.returnValue();
  72. }
  73. else
  74. {
  75. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ;
  76. }
  77. }];
  78. }];
  79. }
  80. - (void)setUserDefaultsValue:(id)value forKey:(NSString *)key {
  81. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  82. [userDefaults setValue:value forKey:key];
  83. [userDefaults synchronize];
  84. }
  85. //-(void) updateTemplate
  86. //{
  87. //
  88. //// if(self.internalTextField.text.length>0)
  89. // {
  90. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  91. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  92. // NSString *documents = [paths objectAtIndex:0];
  93. //
  94. // [RAUtils deletefiles:[documents stringByAppendingPathComponent:@"download"]];
  95. // RASingleton.sharedInstance.scan_list = nil;
  96. // [RADataProvider load_scan_models];
  97. //// NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  98. //
  99. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update local data" completion:^{
  100. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  101. //
  102. //
  103. //
  104. //
  105. //
  106. // NSData* download_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  107. // dispatch_async(dispatch_get_main_queue(), ^{
  108. // [waitalert dismissViewControllerAnimated:false completion:^{
  109. // if(download_data!=nil)
  110. // {
  111. // NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  112. // NSString* path = [documents stringByAppendingPathComponent:file_name];
  113. // [RAUtils saveData:download_data toPath:path];
  114. //
  115. // ZipArchive* zip = [[ZipArchive alloc] init];
  116. // NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  117. // // NSFileManager* fileManager=[NSFileManager defaultManager];
  118. // // [fileManager removeItemAtPath:unZipTo error:nil];
  119. // // NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation];
  120. // //
  121. // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  122. // // NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  123. // //NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
  124. // // NSString* unZipTo = img_cache;//[documentPath stringByAppendingString:@"/images"] ;
  125. //
  126. // DebugLog(@"path: %@",unZipTo);
  127. // if( [zip UnzipOpenFile:path Password:nil] ){
  128. // BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES];
  129. // if( NO==result )
  130. // {
  131. // // int aaa=0;
  132. // //解压失败
  133. //
  134. // [zip UnzipCloseFile];
  135. // //文件损坏,删除文件重试
  136. //
  137. //
  138. // NSFileManager* fileManager=[NSFileManager defaultManager];
  139. // //debug
  140. // // BOOL blDele= [fileManager removeItemAtPath:zipFile error:nil];
  141. // [fileManager removeItemAtPath:path error:nil];
  142. // [fileManager removeItemAtPath:unZipTo error:nil];
  143. //
  144. // [RAUtils message_alert:@"Can not unzip template file, please try download again." title:@"Warrning" controller:self];
  145. //
  146. //
  147. //
  148. //
  149. // }
  150. // else
  151. // {
  152. // [zip UnzipCloseFile];
  153. //
  154. //
  155. // NSFileManager *fileManager = [NSFileManager defaultManager];
  156. // [fileManager removeItemAtPath:path error:nil];
  157. //// [RAUtils message_alert:@"Update template file successful." title:@"Message" controller:self];
  158. //
  159. //
  160. // // [RADataProvider encrypt_scan_models];
  161. // NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  162. // NSString* datetime= [RAUtils current_date_time];
  163. // templateDic[@"updatetime"]= datetime;
  164. // [self setUserDefaultsValue:templateDic forKey:@"ScanTemplate"];
  165. //
  166. //
  167. // MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  168. // [main_vc scanLogin];
  169. // // [OLDataProvider guestLogin];
  170. // [RADataProvider load_scan_models];
  171. // // [self loadTemplate];
  172. //
  173. //
  174. // [RAUtils message_alert:@"Update template file successful." title:@"Message" controller:self action_handler:^(UIAlertAction * _Nonnull action) {
  175. // [self.navigationController popViewControllerAnimated:false];
  176. // } completion:nil];
  177. //
  178. //
  179. // }
  180. // //[zip UnzipCloseFile];
  181. // }
  182. // else
  183. // {
  184. // //打开文件失败,通常是文件路径有问题或密码错误。
  185. // [RAUtils message_alert:@"Can not unzip template file, please contact the Administrator." title:@"Warrning" controller:self];
  186. //
  187. // // NSFileManager *fileManager = [NSFileManager defaultManager];
  188. // // [fileManager removeItemAtPath:zipFile error:nil];
  189. // }
  190. //
  191. //
  192. // // [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  193. // //
  194. // // UIImage * img =[UIImage imageWithData:downloadimg_data];
  195. // // cell.imageView.image=img;
  196. // }
  197. // else
  198. // [RAUtils message_alert:@"Can not update template file, you can update manually in the setting." title:@"Warrning" controller:self];
  199. //
  200. //
  201. //
  202. // }];
  203. //
  204. //
  205. //
  206. // // cell.imageView.image=[UIImage imageNamed:@"notfound_s"];
  207. //
  208. // });
  209. //
  210. // });
  211. // }];
  212. //
  213. //
  214. //
  215. //
  216. //
  217. //
  218. //// NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  219. //// [RADataProvider downloadFileOffset:0 Param:nil from:appDelegate.downloadurl method:@"post" toPath:[documents stringByAppendingPathComponent:file_name] progressHandler:nil completionHandler:^(NSMutableDictionary *result) {
  220. //// int i=0; ;
  221. //// }];
  222. // }
  223. //
  224. //}
  225. - (IBAction)onSelectServer:(id)sender {
  226. ScanServerListViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"ScanServerListViewController"];
  227. servervc.serverlist = self.serverlist;
  228. servervc.returnValue = ^(int index) {
  229. self.server = self.serverlist[index];
  230. [self.btnSelectServer setTitle:self.server[@"show_name"] forState:UIControlStateNormal];
  231. };
  232. [self.navigationController pushViewController:servervc animated:true];
  233. }
  234. - (IBAction)onreloadList:(id)sender {
  235. [self loadServerList];
  236. }
  237. -(void) loadServerList
  238. {
  239. __block UIAlertController * waitalert =[RAUtils waiting_alert:self title:@"Loading Server List" completion:^{
  240. [RADataProvider request_scan_server:^(NSMutableDictionary *result) {
  241. NSMutableDictionary* return_json = result;
  242. [waitalert dismissViewControllerAnimated:YES completion:^{
  243. if([[return_json valueForKey:@"result"] intValue]==2)
  244. {
  245. self.serverlist = return_json[@"hostList"];
  246. if(self.serverlist.count>0)
  247. {
  248. }
  249. }
  250. else
  251. {
  252. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Load Server List" controller:self] ;
  253. }
  254. }];
  255. }];
  256. }];
  257. }
  258. /*
  259. #pragma mark - Navigation
  260. // In a storyboard-based application, you will often want to do a little preparation before navigation
  261. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  262. // Get the new view controller using [segue destinationViewController].
  263. // Pass the selected object to the new view controller.
  264. }
  265. */
  266. @end