ServerSettingViewController.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. //
  2. // LoginSettingViewController.m
  3. // RA Image
  4. //
  5. // Created by Jack on 2017/4/27.
  6. // Copyright © 2017年 USAI. All rights reserved.
  7. //
  8. #import "ServerSettingViewController.h"
  9. //#import "ScannerViewController.h"
  10. #import "RAQRCodeScannerViewController.h"
  11. #import "RAUtils.h"
  12. #import "RAUploadManager.h"
  13. #import "AppDelegate.h"
  14. #import "RADataProvider.h"
  15. #import "ZipArchive.h"
  16. #import "AboutViewController.h"
  17. #import "WebViewController.h"
  18. #import "MainViewController.h"
  19. #import "ChangePasswordViewController.h"
  20. #import "SetupServerViewController.h"
  21. #import "PriceGroupListViewController.h"
  22. @interface ServerSettingViewController ()<UITextFieldDelegate>
  23. @property (strong, nonatomic) IBOutlet UITextField *nameTextField;
  24. //@property (strong, nonatomic) IBOutlet UITextField *internalTextField;
  25. @property (strong, nonatomic) NSString *internalText;
  26. @property (nonatomic,copy) NSString *checkedAddress;
  27. @end
  28. @implementation ServerSettingViewController
  29. - (IBAction)onAboutClick:(id)sender {
  30. AboutViewController * aboutVC =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"AboutViewController"];
  31. // loginvc.delegate = self;
  32. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  33. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:aboutVC] ;
  34. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  35. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  36. [self presentViewController:navi animated:YES completion:^{
  37. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  38. DebugLog(@"about present.........");
  39. // self.btop = false;
  40. // <#code#>
  41. }];
  42. //
  43. //
  44. // UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:aboutVC] ;
  45. //
  46. //
  47. //
  48. //
  49. //
  50. // // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  51. //
  52. // navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  53. // [self presentViewController:navi animated:YES completion:^{
  54. //
  55. // // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  56. //
  57. // DebugLog(@"about present.........");
  58. //
  59. // // self.btop = false;
  60. // // <#code#>
  61. // }];
  62. }
  63. - (IBAction)onPrivacyClick:(id)sender {
  64. NSString* url = URL_PRIVACY;
  65. NSString* title = @"Privacy Policy";
  66. WebViewController *ViewController = [[UIStoryboard storyboardWithName:@"wkweb" bundle:nil] instantiateViewControllerWithIdentifier:@"WebViewController"];
  67. ViewController.url = url;
  68. ViewController.title = title;
  69. [self.navigationController pushViewController:ViewController animated:YES];
  70. }
  71. -(void)viewWillAppear:(BOOL)animated
  72. {
  73. [super viewWillAppear:animated];
  74. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  75. // if(appDelegate.price_group.length>0)
  76. self.labelPriceGroup.text =appDelegate.price_group;
  77. // else
  78. // {
  79. // [RADataProvider default_price_group];
  80. // self.labelPriceGroup.text =appDelegate.price_group;
  81. //
  82. // }
  83. if(appDelegate.user==nil)
  84. self.btnSetPriceGroup.hidden =true;
  85. else
  86. self.btnSetPriceGroup.hidden =false;
  87. self.labelStock.text = [RADataProvider queryStockUpdateTime];
  88. }
  89. - (void)viewDidLoad {
  90. [super viewDidLoad];
  91. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  92. if([RADataProvider scanRtime]&& addressDic!=nil)
  93. {
  94. _btnSetServer.hidden=true;
  95. }
  96. else
  97. _btnSetServer.hidden = false;
  98. // _btnSetServer.hidden = false;
  99. // return;
  100. // Do any additional setup after loading the view.
  101. // UIView *v = [UIView new];
  102. // [self.view insertSubview:v atIndex:0];
  103. // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(doneItemClick:)];
  104. // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Scan" style:UIBarButtonItemStylePlain target:self action:@selector(scanItemClick:)];
  105. //
  106. [self loadAddress];
  107. // [self registListenKeyboard];
  108. RAUPloadManagerConfigure *configure = [[RAUPloadManagerConfigure alloc] init];
  109. self.autoSwitch.on = configure.autoUpload;
  110. // self.autoRmFinishSwitch.on = autoRmFinish;
  111. // self.autoRmErrorSwith.on = autoRmErr;
  112. self.retryCountBox.text = [NSString stringWithFormat:@"%d",configure.retryCount];
  113. self.retryTimeIntervalBox.text = [NSString stringWithFormat:@"%d",(int)configure.retryTimeIntetval];
  114. [self loadTemplate];
  115. }
  116. - (void)didReceiveMemoryWarning {
  117. [super didReceiveMemoryWarning];
  118. // Dispose of any resources that can be recreated.
  119. }
  120. - (id)userDefaultsValue:(NSString *)key {
  121. return [[NSUserDefaults standardUserDefaults] valueForKey:key];
  122. }
  123. - (void)loadAddress {
  124. NSDictionary *addressDic = [self userDefaultsValue:kScanAddress];
  125. if (addressDic) {
  126. NSString* ia=addressDic[@"internalAddress"];
  127. NSString* ea=addressDic[@"externalAddress"];
  128. NSString* sa=addressDic[@"serverAddress"];
  129. if(sa.length==0)
  130. {
  131. if(ea.length>0)
  132. sa=ea;
  133. else if(ia.length>0)
  134. sa=ia;
  135. }
  136. self.internalText= sa;
  137. self.nameTextField.text = [addressDic objectForKey:@"show_name"];
  138. // RASingleton.sharedInstance.price_index =[addressDic[@"price_type"] intValue];
  139. // NSUInteger selectedIndex = [[addressDic objectForKey:@"selectedIndex"] integerValue];
  140. // switch (selectedIndex) {
  141. // case 0:
  142. // case 1: {
  143. // [self internalCheckBtnClick:self.internalCheckBtn];
  144. // }
  145. // break;
  146. // case 2: {
  147. // [self externalCheckBtnClick:self.externalCheckBtn];
  148. // }
  149. // break;
  150. //
  151. // default:
  152. // break;
  153. // }
  154. } else {
  155. // [self internalCheckBtnClick:self.internalCheckBtn];
  156. }
  157. }
  158. - (void)loadTemplate {
  159. NSDictionary *addressDic = [self userDefaultsValue:@"ScanTemplate"];
  160. if (addressDic) {
  161. NSString* updatetime=addressDic[@"updatetime"];
  162. self.labelUpdate.text = updatetime;
  163. }
  164. // NSDictionary *addressDic1 = [self userDefaultsValue:@"ScanStock"];
  165. // if (addressDic1) {
  166. // NSString* updatetime=addressDic1[@"updatetime"];
  167. // self.labelStock.text = updatetime;
  168. // }
  169. //
  170. self.labelStock.text = [RADataProvider queryStockUpdateTime];
  171. }
  172. #pragma mark - Button Click
  173. - (void)doneItemClick:(UIBarButtonItem *)sender {
  174. self.checkedAddress = nil;
  175. NSUInteger selectedIndex = 1;
  176. self.checkedAddress = self.internalText;
  177. // if (self.internalCheckBtn.selected) {
  178. // self.checkedAddress = self.internalTextField.text;
  179. // selectedIndex = 1;
  180. // }
  181. // if (self.externalCheckBtn.selected) {
  182. // self.checkedAddress = self.externalTextField.text;
  183. // selectedIndex = 2;
  184. // }
  185. // if (self.returnValue) {
  186. // self.returnValue(self.nameTextField.text, self.checkedAddress);
  187. // }
  188. __weak typeof(self) weakself = self;
  189. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  190. if (weakself.checkedAddress.length) {
  191. // 保存信息
  192. NSString *name = weakself.nameTextField.text;
  193. NSString *internalAddr = weakself.internalText;
  194. // NSString *externalAddr = weakself.externalTextField.text;
  195. NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
  196. if (internalAddr.length) {
  197. [addressDic setObject:internalAddr forKey:@"serverAddress"];
  198. }
  199. // if (externalAddr.length) {
  200. // [addressDic setObject:externalAddr forKey:@"externalAddress"];
  201. // }
  202. if (name.length) {
  203. [addressDic setObject:name forKey:@"name"];
  204. }
  205. [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"];
  206. [weakself setUserDefaultsValue:addressDic forKey:kScanAddress];
  207. }
  208. }];
  209. }
  210. - (void)setUserDefaultsValue:(id)value forKey:(NSString *)key {
  211. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  212. [userDefaults setValue:value forKey:key];
  213. [userDefaults synchronize];
  214. }
  215. - (IBAction)onUnlockCodeClick:(id)sender {
  216. }
  217. - (IBAction)onSyncStock:(id)sender {
  218. if(self.internalText.length>0)
  219. {
  220. [RADataProvider updateStock:self];
  221. }
  222. else
  223. {
  224. [RAUtils message_box:@"Warrning" message:@"Please setup the server first." completion:nil];
  225. }
  226. }
  227. - (IBAction)onUpdateClick:(id)sender {
  228. if(self.internalText.length>0)
  229. {
  230. [RADataProvider updateTemplate:self];
  231. return;
  232. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  233. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  234. NSString *documents = [paths objectAtIndex:0];
  235. [RAUtils deletefiles:[documents stringByAppendingPathComponent:@"download"]];
  236. RASingleton.sharedInstance.scan_list = nil;
  237. [RADataProvider load_scan_models];
  238. // NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  239. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Update local data" completion:^{
  240. PopWaitAlert* pop =[RAUtils waiting_pop:@"Update local data" completion:nil];
  241. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  242. NSData* download_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  243. dispatch_async(dispatch_get_main_queue(), ^{
  244. // [waitalert dismissViewControllerAnimated:false completion:^{
  245. [pop hide];
  246. if(download_data!=nil)
  247. {
  248. NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  249. NSString* path = [documents stringByAppendingPathComponent:file_name];
  250. [RAUtils saveData:download_data toPath:path];
  251. ZipArchive* zip = [[ZipArchive alloc] init];
  252. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  253. // NSFileManager* fileManager=[NSFileManager defaultManager];
  254. // [fileManager removeItemAtPath:unZipTo error:nil];
  255. // NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation];
  256. //
  257. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  258. // NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  259. //NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
  260. // NSString* unZipTo = img_cache;//[documentPath stringByAppendingString:@"/images"] ;
  261. DebugLog(@"path: %@",unZipTo);
  262. if( [zip UnzipOpenFile:path Password:nil] ){
  263. BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES];
  264. if( NO==result )
  265. {
  266. // int aaa=0;
  267. //解压失败
  268. [zip UnzipCloseFile];
  269. //文件损坏,删除文件重试
  270. NSFileManager* fileManager=[NSFileManager defaultManager];
  271. //debug
  272. // BOOL blDele= [fileManager removeItemAtPath:zipFile error:nil];
  273. [fileManager removeItemAtPath:path error:nil];
  274. [fileManager removeItemAtPath:unZipTo error:nil];
  275. [RAUtils message_box:@"Warrning" message:@"Can not unzip template file, please try download again." completion:nil];
  276. }
  277. else
  278. {
  279. [zip UnzipCloseFile];
  280. NSFileManager *fileManager = [NSFileManager defaultManager];
  281. [fileManager removeItemAtPath:path error:nil];
  282. [RAUtils message_box:@"Message" message:@"Update template file successful." completion:nil];
  283. // [RADataProvider encrypt_scan_models];
  284. NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  285. NSString* datetime= [RAUtils current_date_time];
  286. templateDic[@"updatetime"]= datetime;
  287. [self setUserDefaultsValue:templateDic forKey:@"ScanTemplate"];
  288. [self loadTemplate];
  289. [RADataProvider load_scan_models];
  290. [RADataProvider default_price_group];
  291. }
  292. //[zip UnzipCloseFile];
  293. }
  294. else
  295. {
  296. //打开文件失败,通常是文件路径有问题或密码错误。
  297. [RAUtils message_box:@"Warrning" message:@"Can not unzip template file, please contact the Administrator." completion:nil];
  298. // NSFileManager *fileManager = [NSFileManager defaultManager];
  299. // [fileManager removeItemAtPath:zipFile error:nil];
  300. }
  301. // [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  302. //
  303. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  304. // cell.imageView.image=img;
  305. }
  306. else
  307. [RAUtils message_box:@"Warrning" message:@"Can not download template file. Please check online ERP setting or contact your administrator." completion:nil];
  308. [ActiveViewController Notify:@"ScanSearchViewController" Message:@"Clear"];
  309. // }];
  310. // cell.imageView.image=[UIImage imageNamed:@"notfound_s"];
  311. });
  312. });
  313. // }];
  314. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  315. // [RADataProvider downloadFileOffset:0 Param:nil from:appDelegate.downloadurl method:@"post" toPath:[documents stringByAppendingPathComponent:file_name] progressHandler:nil completionHandler:^(NSMutableDictionary *result) {
  316. // int i=0; ;
  317. // }];
  318. }
  319. else
  320. {
  321. [RAUtils message_box:@"Warrning" message:@"Please setup the server first." completion:nil];
  322. }
  323. }
  324. - (IBAction)onScanClick:(id)sender {
  325. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  326. if(appDelegate.bLogin)
  327. {
  328. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Set Server" message:@"Are you sure you to sign out and setup the server?" preferredStyle:UIAlertControllerStyleAlert];
  329. UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"Sign out and set" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  330. //open exist
  331. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  332. appDelegate.cart_count = 0;//[[jsobj :@"cart_count"] intValue];
  333. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  334. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  335. [appDelegate update_count_mark];
  336. appDelegate.can_show_price =false;
  337. appDelegate.can_see_price =false;
  338. appDelegate.can_create_portfolio =false;
  339. appDelegate.can_create_order =false;
  340. appDelegate.can_cancel_order =false;
  341. appDelegate.can_set_cart_price =false;
  342. appDelegate.can_delete_order =false;
  343. appDelegate.can_submit_order =false;
  344. appDelegate.can_set_tearsheet_price =false;
  345. appDelegate.can_update_contact_info = false;
  346. appDelegate.save_order_logout = false;
  347. appDelegate.submit_order_logout = false;
  348. appDelegate.alert_sold_in_quantities = false;
  349. appDelegate.ipad_perm =nil ;
  350. appDelegate.user_type = USER_ROLE_UNKNOWN;
  351. appDelegate.OrderFilter= nil;
  352. [appDelegate SetSo:nil];
  353. [appDelegate set_main_button_panel];
  354. #ifdef RA_NOTIFICATION
  355. [ActiveViewController Notify:@"SearchViewController" Message:RA_NOTIFICATION_CLEAR_DATA];
  356. #endif
  357. [appDelegate Logout];
  358. SetupServerViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"SetupServerViewController"];
  359. servervc.showCancel = true;
  360. servervc.returnValue = ^{
  361. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  362. if (addressDic) {
  363. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  364. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  365. main_vc.labelServer.text = [addressDic objectForKey:@"show_name"];
  366. self.nameTextField.text = [addressDic objectForKey:@"show_name"];
  367. self.internalText = [addressDic objectForKey:@"serverAddress"];
  368. }
  369. };
  370. [self.navigationController pushViewController:servervc animated:true];
  371. }];
  372. UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  373. //create new;
  374. }];
  375. [alertController addAction:action_1];
  376. [alertController addAction:action_2];
  377. [self presentViewController:alertController animated:YES completion:nil];
  378. }
  379. else
  380. {
  381. SetupServerViewController * servervc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"SetupServerViewController"];
  382. servervc.showCancel = true;
  383. servervc.returnValue = ^{
  384. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];
  385. if (addressDic) {
  386. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  387. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  388. main_vc.labelServer.text = [addressDic objectForKey:@"show_name"];
  389. self.nameTextField.text = [addressDic objectForKey:@"name"];
  390. self.internalText = [addressDic objectForKey:@"serverAddress"];
  391. }
  392. [self onUpdateClick:nil];
  393. };
  394. [self.navigationController pushViewController:servervc animated:true];
  395. }
  396. }
  397. - (IBAction)onSetPriceGroup:(id)sender {
  398. PriceGroupListViewController * listvc =[[UIStoryboard storyboardWithName:@"SO" bundle:nil] instantiateViewControllerWithIdentifier:@"PriceGroupListViewController"];
  399. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  400. NSMutableDictionary* pricegroup=[RADataProvider all_price_group];
  401. NSMutableArray* arrlist = [NSMutableArray new];
  402. for(int i=0;i<appDelegate.available_price_group.count;i++)
  403. {
  404. if(pricegroup[appDelegate.available_price_group[i]]!=nil)
  405. [arrlist addObject:appDelegate.available_price_group[i]];
  406. }
  407. listvc.pricelist = arrlist;//appDelegate.available_price_group;
  408. listvc.returnValue = ^(int index) {
  409. NSString * pricegroup=appDelegate.available_price_group[index];
  410. self.labelPriceGroup.text =pricegroup;
  411. appDelegate.price_group = pricegroup;
  412. [ActiveViewController Notify:@"CartViewController" Message:@"RA_NOTIFICATION_CART_REFRESH"];
  413. // self.server = self.serverlist[index];
  414. // [self.btnSelectServer setTitle:self.server[@"show_name"] forState:UIControlStateNormal];
  415. };
  416. [self.navigationController pushViewController:listvc animated:true];
  417. }
  418. - (void)scanItemClick:(UIBarButtonItem *)sender {
  419. __weak typeof(self) weakself = self;
  420. // ScannerViewController *scannerVC = [[UIStoryboard storyboardWithName:@"cam_scan" bundle:nil] instantiateViewControllerWithIdentifier:@"NewScannerViewController"];
  421. // scannerVC.returnCode = ^(NSString *code) {
  422. // // 扫描成功保存扫描值
  423. // if (weakself) {
  424. // __strong typeof(weakself) strongSelf = weakself;
  425. // [strongSelf processScanResult:code];
  426. // }
  427. // };
  428. // [self presentViewController:scannerVC animated:YES completion:nil];
  429. RAQRCodeScannerViewController *scannerVC = [RAQRCodeScannerViewController viewControllerFromStoryboard];
  430. scannerVC.QRCodeViewControllerDidCompletion = ^(RAQRCodeScannerViewController *qrcodeViewController, NSString *value) {
  431. if (weakself) {
  432. __strong typeof(weakself) strongSelf = weakself;
  433. [strongSelf processScanResult:value];
  434. }
  435. [qrcodeViewController dismissViewControllerAnimated:YES completion:nil];
  436. };
  437. scannerVC.QRCodeViewControllerDidCanceled = ^(RAQRCodeScannerViewController *qrcodeViewController) {
  438. [qrcodeViewController dismissViewControllerAnimated:YES completion:nil];
  439. };
  440. [self presentViewController:scannerVC animated:YES completion:nil];
  441. }
  442. //- (IBAction)internalCheckBtnClick:(UIButton *)sender {
  443. // sender.selected = !sender.selected;
  444. // if (sender.selected) self.externalCheckBtn.selected = NO;
  445. //}
  446. //
  447. //- (IBAction)externalCheckBtnClick:(UIButton *)sender {
  448. // sender.selected = !sender.selected;
  449. // if (sender.selected) self.internalCheckBtn.selected = NO;
  450. //}
  451. #pragma mark - TextField Delegate
  452. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  453. self.currentFirstResponder = textField;
  454. return YES;
  455. }
  456. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  457. return YES;
  458. }
  459. #pragma mark - Private
  460. - (void)processScanResult:(NSString *)result {
  461. if (result.length) {
  462. NSArray *dataArr = [result componentsSeparatedByString:@";"];
  463. if (dataArr.count == 3) {
  464. NSString *name = [dataArr objectAtIndex:0];
  465. NSString *internalAddr = [dataArr objectAtIndex:2];
  466. // NSString *externalAddr = [dataArr objectAtIndex:2];
  467. self.nameTextField.text = name;
  468. self.internalText = internalAddr;
  469. // strongself.externalTextField.text = externalAddr;
  470. }
  471. else if (dataArr.count == 2) {
  472. NSString *name = [dataArr objectAtIndex:0];
  473. NSString *internalAddr = [dataArr objectAtIndex:1];
  474. // NSString *externalAddr = [dataArr objectAtIndex:2];
  475. self.nameTextField.text = name;
  476. self.internalText = internalAddr;
  477. // strongself.externalTextField.text = externalAddr;
  478. }
  479. else {
  480. [RAUtils message_box:@"Can not setup server" message:@"Wrong QR-CODE?" completion:nil];
  481. // [RAUtils alert_view:@"Wrong QR-CODE?" title:@"Can not setup server"];
  482. // strongself.nameTextField.text = nil;
  483. // strongself.internalTextField.text = [dataArr firstObject];
  484. // strongself.externalTextField.text = nil;
  485. }
  486. self.checkedAddress = nil;
  487. NSUInteger selectedIndex = 1;
  488. self.checkedAddress = self.internalText;
  489. // if (self.returnValue) {
  490. // self.returnValue(self.nameTextField.text, self.checkedAddress);
  491. // }
  492. if (self.checkedAddress.length) {
  493. // 保存信息
  494. NSString *name = self.nameTextField.text;
  495. NSString *internalAddr = self.internalText;
  496. // NSString *externalAddr = weakself.externalTextField.text;
  497. NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
  498. if (internalAddr.length) {
  499. [addressDic setObject:internalAddr forKey:@"serverAddress"];
  500. }
  501. // if (externalAddr.length) {
  502. // [addressDic setObject:externalAddr forKey:@"externalAddress"];
  503. // }
  504. if (name.length) {
  505. [addressDic setObject:name forKey:@"name"];
  506. }
  507. [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"];
  508. [self setUserDefaultsValue:addressDic forKey:kScanAddress];
  509. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  510. appDelegate.address = [addressDic objectForKey:@"serverAddress"];
  511. }
  512. }
  513. }
  514. - (IBAction)onNameChange:(id)sender {
  515. self.checkedAddress = nil;
  516. NSUInteger selectedIndex = 1;
  517. self.checkedAddress = self.internalText;
  518. // 保存信息
  519. NSString *name = self.nameTextField.text;
  520. NSString *internalAddr = self.internalText;
  521. // NSString *externalAddr = weakself.externalTextField.text;
  522. NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
  523. if (internalAddr.length) {
  524. [addressDic setObject:internalAddr forKey:@"serverAddress"];
  525. }
  526. // if (externalAddr.length) {
  527. // [addressDic setObject:externalAddr forKey:@"externalAddress"];
  528. // }
  529. if (name.length) {
  530. [addressDic setObject:name forKey:@"name"];
  531. }
  532. [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"];
  533. [self setUserDefaultsValue:addressDic forKey:kScanAddress];
  534. }
  535. - (IBAction)onServerChange:(id)sender {
  536. self.checkedAddress = nil;
  537. NSUInteger selectedIndex = 1;
  538. self.checkedAddress = self.internalText;
  539. // 保存信息
  540. NSString *name = self.nameTextField.text;
  541. NSString *internalAddr = self.internalText;
  542. // NSString *externalAddr = weakself.externalTextField.text;
  543. NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
  544. if (internalAddr.length) {
  545. [addressDic setObject:internalAddr forKey:@"serverAddress"];
  546. }
  547. // if (externalAddr.length) {
  548. // [addressDic setObject:externalAddr forKey:@"externalAddress"];
  549. // }
  550. if (name.length) {
  551. [addressDic setObject:name forKey:@"name"];
  552. }
  553. [addressDic setObject:@(selectedIndex) forKey:@"selectedIndex"];
  554. [self setUserDefaultsValue:addressDic forKey:kScanAddress];
  555. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  556. appDelegate.address = [addressDic objectForKey:@"serverAddress"];
  557. //
  558. // NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  559. // NSString* datetime= [RAUtils current_date_time];
  560. // templateDic[@"updatetime"]= datetime;
  561. //
  562. //
  563. // NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  564. // [userDefaults setValue:templateDic forKey:@"ScanStock"];
  565. }
  566. - (IBAction)onAutoUpload:(id)sender {
  567. BOOL autoUpload = self.autoSwitch.isOn;
  568. [RAUploadManager configureUploadManager:^(RAUPloadManagerConfigure *configure) {
  569. configure.autoUpload = autoUpload;
  570. }];
  571. }
  572. - (IBAction)onRetryChange:(id)sender {
  573. int retryCount = [self.retryCountBox.text intValue];
  574. [RAUploadManager configureUploadManager:^(RAUPloadManagerConfigure *configure) {
  575. configure.retryCount = retryCount;
  576. }];
  577. }
  578. - (IBAction)onWaitingChange:(id)sender {
  579. int retryTimeInterval = [self.retryTimeIntervalBox.text intValue];
  580. [RAUploadManager configureUploadManager:^(RAUPloadManagerConfigure *configure) {
  581. configure.retryTimeIntetval = retryTimeInterval;
  582. }];
  583. }
  584. - (IBAction)onChangePasswordClick:(id)sender {
  585. UIApplication * app = [UIApplication sharedApplication];
  586. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  587. if(appDelegate.user.length==0)
  588. {
  589. [RAUtils message_box:@"Change Password" message:@"You must sign in first." completion:nil];
  590. return;
  591. }
  592. ChangePasswordViewController * vc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChangePasswordViewController"];
  593. // loginvc.delegate = self;
  594. // loginvc.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  595. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc] ;
  596. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  597. navi.modalPresentationStyle = UIModalPresentationFormSheet;//有三种状态,自己看看是哪种
  598. [self presentViewController:navi animated:YES completion:^{
  599. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  600. DebugLog(@"about present.........");
  601. // self.btop = false;
  602. // <#code#>
  603. }];
  604. }
  605. - (IBAction)onDeleteAccountClick:(id)sender {
  606. UIApplication * app = [UIApplication sharedApplication];
  607. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  608. if(appDelegate.user.length==0)
  609. {
  610. [RAUtils message_box:@"Delete Account" message:@"You must sign in first." completion:nil];
  611. return;
  612. }
  613. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Delete Account" message:@"Are you sure you want to permanently delete your account?" preferredStyle:UIAlertControllerStyleAlert];
  614. //增加确定按钮
  615. [alertController addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
  616. //获取第1个输入框;
  617. UITextField *titleTextField = alertController.textFields.firstObject;
  618. if([appDelegate.password isEqualToString:titleTextField.text])
  619. {
  620. NSLog(@"%@", @"run process");
  621. // __block UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Delete Account" completion:^{
  622. PopWaitAlert* pop =[RAUtils waiting_pop:@"Delete Account" completion:nil];
  623. [RANetwork request_delete_account:^(NSMutableDictionary *result) {
  624. NSMutableDictionary* return_json =result;
  625. // [waitalert dismissViewControllerAnimated:YES completion:^{
  626. [pop hide];
  627. if([[return_json valueForKey:@"result"] intValue]==2)
  628. {
  629. // [self hideMenu];
  630. // [self switchToHome:nil];
  631. [appDelegate Logout];
  632. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  633. [main_vc checklogin :false];
  634. [RAUtils message_box:@"Delete Account" message:@"Account deleted." completion:nil];
  635. }
  636. else
  637. {
  638. [RAUtils message_box:@"Delete Account" message:[return_json valueForKey:@"err_msg"] completion:nil];
  639. }
  640. // }];
  641. }];
  642. // }];
  643. }
  644. else
  645. {
  646. [RAUtils message_box:@"Delete Account" message:@"Wrong password." completion:nil];
  647. }
  648. }]];
  649. //增加取消按钮;
  650. [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]];
  651. //定义第一个输入框;
  652. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  653. textField.placeholder = @"Login Password";
  654. textField.textContentType = UITextContentTypePassword;
  655. }];
  656. [self presentViewController:alertController animated:true completion:nil];
  657. }
  658. #pragma mark - RA_NOTIFICAITON
  659. -(void) refresh_ui
  660. {
  661. [self loadTemplate];
  662. }
  663. -(void) reload_data
  664. {
  665. [self refresh_ui];
  666. }
  667. @end