SetupServerViewController.m 14 KB

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