RADataProvider.m 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. //
  2. // RADataProvider.m
  3. // iSales-USAI
  4. //
  5. // Created by Rui Zhang on 11/11/21.
  6. // Copyright © 2021 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RADataProvider.h"
  9. #import "ZipArchive.h"
  10. #import "ActiveViewController.h"
  11. @implementation RADataProvider
  12. +(void)request_autocomplete:(NSMutableDictionary*)param completionHandler:(resultHandler)result
  13. {
  14. param[@"site_name"]=[self getSiteName];
  15. // param[@"_method"]=@"customerID";
  16. AppDelegate *appDelegate = nil;
  17. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  18. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  19. NSString* full_url ;
  20. if([param[@"full_url"] boolValue])
  21. full_url =param[@"url"];
  22. else
  23. {
  24. if(appDelegate.address==nil||[param[@"url"] stringValue].length==0)
  25. {
  26. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  27. ret[@"result"]=@"8";
  28. ret[@"err_msg"]=@"Missing query url, please contact administrator.";
  29. }
  30. full_url = [appDelegate.address stringByAppendingPathComponent:[param[@"url"] stringValue]];
  31. }
  32. [self request_interface:full_url parameters:param err_record_url:nil completionHandler:result retry:0];
  33. }
  34. +(NSString*)getSiteName
  35. {
  36. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];;
  37. if (addressDic) {
  38. NSString* ia=addressDic[@"internalAddress"];
  39. NSString* ea=addressDic[@"externalAddress"];
  40. NSString* sa=addressDic[@"serverAddress"];
  41. if(sa.length==0)
  42. {
  43. if(ea.length>0)
  44. sa=ea;
  45. else if(ia.length>0)
  46. sa=ia;
  47. }
  48. return [addressDic objectForKey:@"name"];
  49. }
  50. return @"";
  51. }
  52. #ifdef SCANNER_ORDER
  53. +(NSMutableDictionary*)scan_search:(NSString*)keyword
  54. {
  55. // NSMutableArray* arr = [NSMutableArray new];
  56. if(keyword.length==0)
  57. keyword=@"";
  58. int count = 0;
  59. NSMutableDictionary * ret = [NSMutableDictionary new];
  60. ret[@"result"]=@2;
  61. AppDelegate *appDelegate = nil;
  62. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  63. // NSArray* keys=[dict allKeys];
  64. for(int i=0;i<appDelegate.scan_model_key.count;i++)
  65. {
  66. NSString* key = appDelegate.scan_model_key[i];
  67. if([key.lowercaseString containsString:keyword.lowercaseString]||keyword.length==0)
  68. {
  69. ret[[NSString stringWithFormat:@"item_%d",count]]=appDelegate.scan_model[key];
  70. // [arr addObject:dict[key]];
  71. count ++;
  72. }
  73. }
  74. ret[@"count"]=@(count);
  75. return ret;
  76. // int a=0;
  77. }
  78. +(void)request_scansearch:(long ) offset limit :(long)limit keywords :(NSString*) keywords completionHandler:(resultHandler)result
  79. {
  80. result([self scan_search:keywords]);
  81. return;
  82. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  83. AppDelegate *appDelegate = nil;
  84. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  85. [params setValue:keywords forKey:@"keyword"];
  86. [params setValue:@"search" forKey:@"_method"];
  87. params[@"site_name"]=[self getSiteName];
  88. [params setValue:[NSString stringWithFormat:@"%ld",limit] forKey:@"limit"];
  89. [params setValue:[NSString stringWithFormat:@"%ld",offset] forKey:@"offset"];
  90. [self request_interface:URL_SEARCH parameters:params err_record_url:nil completionHandler:result retry:0];
  91. }
  92. +(void)encrypt_scan_models
  93. {
  94. return;
  95. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  96. NSString *documents = [paths objectAtIndex:0];
  97. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  98. NSString* templatefile = [unZipTo stringByAppendingPathComponent:@"offline_models.json"];
  99. NSString* str = [NSString stringWithContentsOfFile:templatefile encoding:NSUTF8StringEncoding error:nil];
  100. NSString * stre = [AESCrypt encrypt:str password:@"usai2010"];
  101. // NSString *writePath = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/haha.txt"];
  102. NSError *error;
  103. [stre writeToFile:templatefile atomically:YES encoding:NSUTF8StringEncoding error:&error];
  104. if (error) {
  105. DebugLog(@"导出失败");
  106. }else {
  107. DebugLog(@"导出成功");
  108. }
  109. }
  110. +(NSArray*)request_scan_model_by_names:(NSString*)names
  111. {
  112. AppDelegate *appDelegate = nil;
  113. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  114. NSMutableArray* models=[NSMutableArray new];
  115. NSArray* arrname=[RAConvertor string2dict:names];
  116. for(int i=0;i<arrname.count;i++)
  117. {
  118. NSString* key = arrname[i];
  119. NSDictionary* m=appDelegate.scan_model[key];
  120. if(m!=nil)
  121. [models addObject:m];
  122. }
  123. return models;
  124. }
  125. +(bool) scanRtime
  126. {
  127. // 当前日期小于设定日期,返回true
  128. NSDate *cdate= [NSDate date];
  129. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
  130. [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  131. // 2022-07-14。1.17
  132. // 2022-07-14。1.19
  133. NSDate * ddate =[dateFormatter dateFromString:@"2023-4-18"];
  134. // bool review=[cdate earlierDate:ddate];
  135. NSTimeInterval secondsInterval= [cdate timeIntervalSinceDate:ddate];
  136. int sec =secondsInterval;
  137. //#ifdef DEBUG
  138. // return false;
  139. //#endif
  140. return sec<0;
  141. }
  142. +(NSString*) queryStockUpdateTime
  143. {
  144. NSString* serverpath = [OLDataProvider getServerPath];
  145. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  146. // NSString *documents = [paths objectAtIndex:0];
  147. NSString* path = [serverpath stringByAppendingPathComponent:@"stock.json"];
  148. NSFileManager *fileManager = [NSFileManager defaultManager];
  149. NSError* error;
  150. NSDictionary *fileAttributes =[fileManager attributesOfItemAtPath:path error:&error];
  151. if (fileAttributes != nil) {
  152. NSDate *creationDate;
  153. //NSString *NSFileCreationDate
  154. //文件创建日期
  155. creationDate = [fileAttributes objectForKey:NSFileCreationDate];
  156. NSDateFormatter * df = [[NSDateFormatter alloc] init ];
  157. [df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  158. NSString * na = [df stringFromDate:creationDate];
  159. return na;
  160. }
  161. else {
  162. return @"Last Update Unknown";
  163. }
  164. }
  165. +(NSString*) queryStock:(NSString*)modelname
  166. {
  167. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  168. NSString* user =appDelegate.user;
  169. if(user.length==0||[user.lowercaseString isEqualToString:@"guest"])
  170. return @"Stock:0";
  171. NSString* serverpath = [OLDataProvider getServerPath];
  172. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  173. // NSString *documents = [paths objectAtIndex:0];
  174. NSString* path = [serverpath stringByAppendingPathComponent:@"stock.json"];
  175. NSDictionary* map=[RAUtils dictfromfile:path];
  176. int stock = [map[modelname] intValue];
  177. if(stock<0)
  178. stock=0;
  179. return [NSString stringWithFormat:@"Stock: %d",stock];
  180. }
  181. +(void) updateStock:(UIViewController*) vc
  182. {
  183. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  184. NSString* user =appDelegate.user;
  185. if(user.length==0||[user.lowercaseString isEqualToString:@"guest"])
  186. {
  187. [RAUtils message_box:@"Warrning" message:@"Can not update inventory, you must login first." completion:nil];
  188. return;
  189. }
  190. bool haserp = [self getSiteHasERP];
  191. if(!haserp)
  192. {
  193. [RAUtils message_box:@"Warrning" message:@"Can not update inventory, current site dosen't have ERP." completion:nil];
  194. return;
  195. }
  196. NSString* serverpath = [OLDataProvider getServerPath];
  197. [RAUtils deletefiles:[serverpath stringByAppendingPathComponent:@"stock.json"]];
  198. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Inventory update" message:@"Inventory update will take 60s or longer" preferredStyle:UIAlertControllerStyleAlert];
  199. UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"Yes, proceed" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  200. // __block UIAlertController * waitalert = [RAUtils waiting_alert:vc title:@"Update stock" completion:^{
  201. PopWaitAlert * pop = [RAUtils waiting_pop:@"Update stock" completion:nil];
  202. [RADataProvider request_scan_stock:^(NSMutableDictionary *result) {
  203. NSMutableDictionary* return_json = result;
  204. // [waitalert dismissViewControllerAnimated:false completion:^{
  205. [pop hide];
  206. if([[return_json valueForKey:@"result"] intValue]==2)
  207. {
  208. // NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  209. NSString* path = [serverpath stringByAppendingPathComponent:@"stock.json"];
  210. [RAUtils dicttofile:path dict:result];
  211. // NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  212. // NSString* datetime= [RAUtils current_date_time];
  213. // templateDic[@"updatetime"]= datetime;
  214. //
  215. //
  216. // NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  217. // [userDefaults setValue:templateDic forKey:@"ScanStock"];
  218. // [userDefaults synchronize];
  219. if([vc respondsToSelector:@selector(refreshStock)])
  220. {
  221. [vc performSelector:@selector(refreshStock) withObject:nil afterDelay:0];
  222. }
  223. }
  224. else
  225. [RAUtils message_box:@"Warrning" message:@"Can not update inventory, you can try again later." completion:nil];
  226. [ActiveViewController Notify:@"ScanSearchViewController" Message:@"Clear"];
  227. [ActiveViewController Notify:@"ServerSettingViewController" Message:RA_NOTIFICATION_REFRESH_UI];
  228. // }];
  229. }];
  230. // }];
  231. }];
  232. UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  233. //create new;
  234. }];
  235. [alertController addAction:action_1];
  236. [alertController addAction:action_2];
  237. [vc presentViewController:alertController animated:YES completion:nil];
  238. ;
  239. }
  240. +(void) updateTemplate:(UIViewController*) vc
  241. {
  242. // if(self.internalTextField.text.length>0)
  243. {
  244. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  245. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  246. NSString *documents = [paths objectAtIndex:0];
  247. [RAUtils deletefiles:[documents stringByAppendingPathComponent:@"download"]];
  248. RASingleton.sharedInstance.scan_list = nil;
  249. [RADataProvider load_scan_models];
  250. // __block UIAlertController * waitalert = [RAUtils waiting_alert:vc title:@"Update local data" completion:^{
  251. PopWaitAlert * pop = [RAUtils waiting_pop:@"Update local data" completion:nil];
  252. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  253. NSData* download_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  254. dispatch_async(dispatch_get_main_queue(), ^{
  255. // [waitalert dismissViewControllerAnimated:false completion:^{
  256. [pop hide];
  257. if(download_data!=nil)
  258. {
  259. NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  260. NSString* path = [documents stringByAppendingPathComponent:file_name];
  261. [RAUtils saveData:download_data toPath:path];
  262. ZipArchive* zip = [[ZipArchive alloc] init];
  263. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  264. // NSFileManager* fileManager=[NSFileManager defaultManager];
  265. // [fileManager removeItemAtPath:unZipTo error:nil];
  266. // NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation];
  267. //
  268. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  269. // NSString *documentPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
  270. //NSString* zipFile = [documentPath stringByAppendingString:@"/images.zip"] ;
  271. // NSString* unZipTo = img_cache;//[documentPath stringByAppendingString:@"/images"] ;
  272. DebugLog(@"path: %@",unZipTo);
  273. if( [zip UnzipOpenFile:path Password:nil] ){
  274. BOOL result = [zip UnzipFileTo:unZipTo overWrite:YES];
  275. if( NO==result )
  276. {
  277. // int aaa=0;
  278. //解压失败
  279. [zip UnzipCloseFile];
  280. //文件损坏,删除文件重试
  281. NSFileManager* fileManager=[NSFileManager defaultManager];
  282. //debug
  283. // BOOL blDele= [fileManager removeItemAtPath:zipFile error:nil];
  284. [fileManager removeItemAtPath:path error:nil];
  285. [fileManager removeItemAtPath:unZipTo error:nil];
  286. [RAUtils message_box:@"Warrning" message:@"Can not unzip template file, please try download again." completion:nil];
  287. }
  288. else
  289. {
  290. [zip UnzipCloseFile];
  291. NSFileManager *fileManager = [NSFileManager defaultManager];
  292. [fileManager removeItemAtPath:path error:nil];
  293. [RAUtils message_box:@"Message" message:@"Update template file successful." completion:nil];
  294. // [RADataProvider encrypt_scan_models];
  295. NSMutableDictionary *templateDic = [NSMutableDictionary dictionary];
  296. NSString* datetime= [RAUtils current_date_time];
  297. templateDic[@"updatetime"]= datetime;
  298. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  299. [userDefaults setValue:templateDic forKey:@"ScanTemplate"];
  300. [userDefaults synchronize];
  301. [RADataProvider load_scan_models];
  302. if([self get_price_group:appDelegate.price_group]==nil )
  303. [RADataProvider default_price_group];
  304. // [self loadTemplate];
  305. }
  306. //[zip UnzipCloseFile];
  307. }
  308. else
  309. {
  310. //打开文件失败,通常是文件路径有问题或密码错误。
  311. [RAUtils message_box:@"Warrning" message:@"Can not unzip template file, please contact the Administrator." completion:nil];
  312. // NSFileManager *fileManager = [NSFileManager defaultManager];
  313. // [fileManager removeItemAtPath:zipFile error:nil];
  314. }
  315. // [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  316. //
  317. // UIImage * img =[UIImage imageWithData:downloadimg_data];
  318. // cell.imageView.image=img;
  319. }
  320. else
  321. [RAUtils message_box:@"Warrning" message:@"Can not update template file, you can update manually in the setting." completion:nil];
  322. // cell.imageView.image=[UIImage imageNamed:@"notfound_s"];
  323. [ActiveViewController Notify:@"ScanSearchViewController" Message:@"Clear"];
  324. [ActiveViewController Notify:@"ServerSettingViewController" Message:RA_NOTIFICATION_REFRESH_UI];
  325. // }];
  326. });
  327. });
  328. // }];
  329. // NSString* file_name=[appDelegate.downloadurl lastPathComponent];
  330. // NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:appDelegate.downloadurl]];
  331. // [RADataProvider downloadFileOffset:0 Param:nil from:appDelegate.downloadurl method:@"post" toPath:[documents stringByAppendingPathComponent:file_name] progressHandler:nil completionHandler:^(NSMutableDictionary *result) {
  332. // int i=0; ;
  333. // }];
  334. }
  335. }
  336. +(void)load_scan_models
  337. {
  338. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  339. NSString *documents = [paths objectAtIndex:0];
  340. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  341. NSString* templatefile = [unZipTo stringByAppendingPathComponent:@"offline_models.json"];
  342. NSString* str = [NSString stringWithContentsOfFile:templatefile encoding:NSUTF8StringEncoding error:nil];
  343. NSString * str3 = [AESCrypt AES128Decrypt:str key:@"usai2010"];
  344. NSMutableDictionary* of = [[RAConvertor string2dict:str3] mutableCopy];
  345. NSArray* keysort=[self sortedDictionary:of];
  346. AppDelegate *appDelegate = nil;
  347. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  348. appDelegate.scan_model = of;
  349. appDelegate.scan_model_key=keysort;
  350. }
  351. +(NSMutableDictionary*)all_price_group
  352. {
  353. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  354. NSString *documents = [paths objectAtIndex:0];
  355. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  356. AppDelegate *appDelegate = nil;
  357. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  358. NSString* templatefile1 = [unZipTo stringByAppendingPathComponent:@"offline_price_group.json"];
  359. NSString* str1 = [NSString stringWithContentsOfFile:templatefile1 encoding:NSUTF8StringEncoding error:nil];
  360. NSMutableDictionary* pg = [[RAConvertor string2dict:str1] mutableCopy];
  361. return pg;
  362. }
  363. +(NSDictionary*)get_price_group:(NSString*) groupname
  364. {
  365. NSMutableDictionary* pg = [self all_price_group];
  366. return pg[groupname];
  367. }
  368. +(void)default_price_group
  369. {
  370. AppDelegate *appDelegate = nil;
  371. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  372. // 如果用户有设置价格组,则默认为第一个,如果没有设置,则用服务器设置的默认价格组。
  373. if(appDelegate.available_price_group.count!=0)
  374. {
  375. appDelegate.price_group=nil;
  376. // 账号price group 要检查合法性
  377. for(int i=0;i<appDelegate.available_price_group.count;i++)
  378. {
  379. if([self all_price_group][appDelegate.available_price_group[i]]!=nil)
  380. {
  381. appDelegate.price_group= appDelegate.available_price_group[i];
  382. return;
  383. }
  384. }
  385. //如果账号的price group 全部非法,则用默认价格组。
  386. // if(appDelegate.price_group==nil)
  387. {
  388. NSMutableDictionary* pg = [self all_price_group];
  389. appDelegate.price_group=pg[@"default"];
  390. }
  391. }
  392. else
  393. {
  394. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  395. // NSString *documents = [paths objectAtIndex:0];
  396. // NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  397. //
  398. //
  399. //
  400. //
  401. //
  402. // NSString* templatefile1 = [unZipTo stringByAppendingPathComponent:@"price_group.json"];
  403. //
  404. // NSString* str1 = [NSString stringWithContentsOfFile:templatefile1 encoding:NSUTF8StringEncoding error:nil];
  405. //
  406. // NSMutableDictionary* pg = [[RAConvertor string2dict:str1] mutableCopy];
  407. //
  408. NSMutableDictionary* pg = [self all_price_group];
  409. appDelegate.price_group=pg[@"default"];
  410. if(appDelegate.price_group!=nil)
  411. appDelegate.available_price_group = @[appDelegate.price_group];
  412. }
  413. }
  414. + (NSArray*)sortedDictionary:(NSDictionary *)dict{
  415. //将所有的key放进数组
  416. NSArray *allKeyArray = [dict allKeys];
  417. //序列化器对数组进行排序的block 返回值为排序后的数组
  418. NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id
  419. _Nonnull obj2) {
  420. /**
  421. In the compare: methods, the range argument specifies the
  422. subrange, rather than the whole, of the receiver to use in the
  423. comparison. The range is not applied to the search string. For
  424. example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)]
  425. compares "A" to "ABC", not "A" to "A", and will return
  426. NSOrderedAscending. It is an error to specify a range that is
  427. outside of the receiver's bounds, and an exception may be raised.
  428. - (NSComparisonResult)compare:(NSString *)string;
  429. compare方法的比较原理为,依次比较当前字符串的第一个字母:
  430. 如果不同,按照输出排序结果
  431. 如果相同,依次比较当前字符串的下一个字母(这里是第二个)
  432. 以此类推
  433. 排序结果
  434. NSComparisonResult resuest = [obj1 compare:obj2];为从小到大,即升序;
  435. NSComparisonResult resuest = [obj2 compare:obj1];为从大到小,即降序;
  436. 注意:compare方法是区分大小写的,即按照ASCII排序
  437. */
  438. //排序操作
  439. NSComparisonResult resuest = [obj1 compare:obj2];
  440. return resuest;
  441. }];
  442. return afterSortKeyArray;
  443. // DebugLog(@"afterSortKeyArray:%@",afterSortKeyArray);
  444. // //通过排列的key值获取value
  445. // NSMutableArray *valueArray = [NSMutableArray array];
  446. // for (NSString *sortsing in afterSortKeyArray) {
  447. // NSString *valueString = [dict objectForKey:sortsing];
  448. // [valueArray addObject:valueString];
  449. // }
  450. // DebugLog(@"valueArray:%@",valueArray);
  451. }
  452. +(void) request_scan_stock:(resultHandler)result
  453. {
  454. NSMutableDictionary* param = [NSMutableDictionary new];
  455. param[@"_method"]=@"offlineModelCheckStock";
  456. AppDelegate *appDelegate = nil;
  457. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  458. if(appDelegate.address==nil)
  459. return;
  460. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SCAN_NEWS];
  461. param[@"site_name"]=[RADataProvider getSiteName];
  462. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  463. }
  464. +(void) request_scan_news:(resultHandler)result
  465. {
  466. NSMutableDictionary* param = [NSMutableDictionary new];
  467. param[@"_method"]=@"offlineNews";
  468. AppDelegate *appDelegate = nil;
  469. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  470. if(appDelegate.address==nil)
  471. return;
  472. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SCAN_NEWS];
  473. param[@"site_name"]=[RADataProvider getSiteName];
  474. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  475. }
  476. //+(void)request_scan_cid:(NSMutableDictionary*)param url:(NSString*) url completionHandler:(resultHandler)result
  477. //{
  478. // param[@"site_name"]=[self getSiteName];
  479. // param[@"_method"]=@"customerID";
  480. // AppDelegate *appDelegate = nil;
  481. //
  482. // appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  483. // NSString* full_url = [appDelegate.address stringByAppendingPathComponent:url];
  484. // [self request_interface:full_url parameters:param err_record_url:nil completionHandler:result retry:0];
  485. //}
  486. //+(void)request_scan_shipto_company:(NSMutableDictionary*)param url:(NSString*) url completionHandler:(resultHandler)result
  487. //{
  488. // param[@"site_name"]=[self getSiteName];
  489. // param[@"_method"]=@"shiptoCompany";
  490. // AppDelegate *appDelegate = nil;
  491. //
  492. // appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  493. // NSString* full_url = [appDelegate.address stringByAppendingPathComponent:url];
  494. // [self request_interface:full_url parameters:param err_record_url:nil completionHandler:result retry:0];
  495. //}
  496. +(void)request_scan_stock:(NSMutableDictionary*)param completionHandler:(resultHandler)result
  497. {
  498. param[@"site_name"]=[self getSiteName];
  499. param[@"_method"]=@"orderItemCheckStock";
  500. AppDelegate *appDelegate = nil;
  501. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  502. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_CHECK_STOCK];
  503. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  504. }
  505. +(void) request_scan_server :(resultHandler)result
  506. {
  507. NSMutableDictionary* param = [NSMutableDictionary new];
  508. param[@"_method"]=@"hostList";
  509. [self request_interface:URL_SERVER_LIST parameters:param err_record_url:nil completionHandler:result retry:0];
  510. }
  511. +(void)request_validate_scan_server:(NSString*)url name:(NSString*)name password:(NSString*)password completionHandler:(resultHandler)result
  512. {
  513. NSMutableDictionary* param = [NSMutableDictionary new];
  514. param[@"_method"]=@"hostCheck";
  515. param[@"password"]=password;
  516. param[@"site_name"]=name;
  517. // param[@"url"]=url;
  518. NSString* check_url = [NSString stringWithFormat:@"%@%@",url,URL_SERVER_CHECK];
  519. [self request_interface:check_url parameters:param err_record_url:nil completionHandler:result retry:0];
  520. }
  521. +(bool)getSiteHasERP
  522. {
  523. AppDelegate *appDelegate = nil;
  524. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  525. if( [appDelegate.user isEqualToString:@"GUEST"])
  526. return false;
  527. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];;
  528. if (addressDic) {
  529. return [addressDic[@"has_erp_site"] boolValue];
  530. }
  531. return false;
  532. }
  533. #endif
  534. +(void)request_change_password:(NSString*) pwd completionHandler:(resultHandler)result
  535. {
  536. AppDelegate *appDelegate = nil;
  537. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  538. NSMutableDictionary* param = [NSMutableDictionary new];
  539. param[@"_method"]=@"changePasswordOfflineOrderUser";
  540. param[@"opsw"]=appDelegate.password;
  541. param[@"npsw"]=pwd;
  542. param[@"user"]=appDelegate.user;
  543. param[@"site_name"]=[self getSiteName];
  544. // param[@"url"]=url;
  545. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_CHANGE_PASSWORD];
  546. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  547. }
  548. +(void)request_sign_up:user email:(NSString*)email password:(NSString*) pwd completionHandler:(resultHandler)result
  549. {
  550. AppDelegate *appDelegate = nil;
  551. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  552. NSMutableDictionary* param = [NSMutableDictionary new];
  553. param[@"_method"]=@"registerOfflineOrderUser";
  554. param[@"password"]=pwd;
  555. param[@"email"]=email;
  556. param[@"user"]=user;
  557. param[@"site_name"]=[self getSiteName];
  558. // param[@"url"]=url;
  559. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP];
  560. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  561. }
  562. +(void)request_change_password:user oldpassword:(NSString*)opwd newpassword:(NSString*) npwd completionHandler:(resultHandler)result
  563. {
  564. AppDelegate *appDelegate = nil;
  565. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  566. NSMutableDictionary* param = [NSMutableDictionary new];
  567. param[@"_method"]=@"changePasswordOfflineOrderUser";
  568. param[@"opsw"]=opwd;
  569. param[@"password"]=opwd;
  570. param[@"npsw"]=npwd;
  571. param[@"user"]=user;
  572. param[@"site_name"]=[self getSiteName];
  573. // param[@"url"]=url;
  574. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP];
  575. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  576. }
  577. +(void)request_editor:(NSString*) request_url params:(NSMutableDictionary*)params completionHandler:(resultHandler)result
  578. {
  579. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  580. AppDelegate *appDelegate = nil;
  581. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  582. if(appDelegate.user!=nil)
  583. [params setValue:appDelegate.user forKey:@"user"];
  584. // if(![appDelegate.order_customer_id isEqualToString: appDelegate.contact_id]&& appDelegate.order_customer_id!=nil)
  585. // [params setValue:appDelegate.order_customer_id forKey:@"contactId"];
  586. // else
  587. if(appDelegate.contact_id!=nil)
  588. [params setValue:appDelegate.contact_id forKey:@"contactId"];
  589. if(appDelegate.password!=nil)
  590. [params setValue:appDelegate.password forKey:@"password"];
  591. if(appDelegate.order_code!=nil)
  592. [params setValue:appDelegate.order_code forKey:@"orderCode"];
  593. bool can_create_backorder=appDelegate.can_create_backorder;
  594. // [params setValue:sourceid forKey:@"sourceid"];
  595. // [params setValue:editor forKey:@"editor"];
  596. DebugLog(@"request editor url:%@",request_url);
  597. if([request_url isEqualToString:URL_NEW_CUSTOMER])
  598. {
  599. [params setObject:ScreenCodeNewCustomer forKey:kScreenName];
  600. }
  601. else if([request_url isEqualToString:URL_EDIT_CUSTOMER])
  602. {
  603. [params setObject:ScreenCodeEditCustomer forKey:kScreenName];
  604. }
  605. else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH])
  606. {
  607. [params setObject:ScreenCodeAdvanceSearch forKey:kScreenName];
  608. }
  609. else if([request_url isEqualToString:URL_CARTDELIVERY])
  610. {
  611. [params setObject:ScreenCodePlaceOrder forKey:kScreenName];
  612. }
  613. else if([request_url isEqualToString:URL_ADDRESS_EDOTOR])
  614. {
  615. [params setObject:ScreenCodeNewAddress forKey:kScreenName];
  616. }
  617. else if ([request_url isEqualToString:URL_DM_PARAMS]){
  618. [params setObject:ScreenCodeCreatePortfolio forKey:kScreenName];
  619. }
  620. if(appDelegate.offline_mode)
  621. {
  622. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  623. NSData* json = nil;
  624. if([request_url isEqualToString:URL_NEW_CUSTOMER])
  625. {
  626. json= [OLDataProvider offline_createContact:params];
  627. }
  628. else if([request_url isEqualToString:URL_EDIT_CUSTOMER])
  629. {
  630. json= [OLDataProvider offline_editContact:params];
  631. }
  632. else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH])
  633. {
  634. json= [OLDataProvider offline_contactAdvancedSearch:params];
  635. }
  636. else if([request_url isEqualToString:URL_CARTDELIVERY])
  637. {
  638. params[@"contact_id"]=params[@"contactId"];
  639. params[@"can_create_backorder"]=@(can_create_backorder);
  640. json= [OLDataProvider offline_placeorder:params];
  641. }
  642. else if([request_url isEqualToString:URL_ADDRESS_EDOTOR])
  643. {
  644. json= [OLDataProvider offline_addreditor:params];
  645. }
  646. else if ([request_url isEqualToString:URL_DM_PARAMS]){
  647. json = [OLDataProvider offline_createTearSheet:params];
  648. }
  649. dispatch_async(dispatch_get_main_queue(), ^{
  650. if(json==nil)
  651. result( [[OLDataProvider offline_notimpl] mutableCopy]);
  652. else
  653. result([[RAConvertor data2dict:json] mutableCopy]);
  654. });
  655. });
  656. return;
  657. }
  658. else
  659. {
  660. if([request_url isEqualToString:URL_CARTDELIVERY])
  661. {
  662. NSString * uuid= [[NSUUID UUID] UUIDString];
  663. params[@"resultSerial"]=uuid;
  664. __block bool bbreak = false;
  665. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  666. for(int i=0;i<150;i++)
  667. {
  668. // NSMutableDictionary* result_params = [[NSMutableDictionary alloc]init];
  669. // result_params[@"resultSerial"]=uuid;
  670. //json=[self get_json:request_url parameters:params];
  671. if(bbreak)
  672. break;
  673. dispatch_async(dispatch_get_main_queue(), ^{
  674. DebugLog(@"request cart delivery %d",i);
  675. [self request_interface:request_url parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *resulti) {
  676. if(resulti!=nil)
  677. {
  678. if([resulti[@"result"] intValue]==2 || [resulti[@"result"] intValue] == 8)
  679. {
  680. if(!bbreak) //此判断保证result只调用一次。
  681. {
  682. result(resulti);
  683. DebugLog(@"result called at %d",i);
  684. }
  685. bbreak=true;
  686. }
  687. }
  688. } retry:3];
  689. });
  690. // [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0];
  691. sleep(2);
  692. }
  693. });
  694. }
  695. else
  696. [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0];
  697. }
  698. // if(json!=nil)
  699. // {
  700. // NSError *error=nil;
  701. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  702. // return jsobj;
  703. // }
  704. // else
  705. // return nil;
  706. }
  707. +(void)request_commoneditor_partialrefresh: (NSMutableDictionary*)params url:(NSString*)url completionHandler:(resultHandler)result
  708. {
  709. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  710. AppDelegate *appDelegate = nil;
  711. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  712. if(appDelegate.user!=nil)
  713. [params setValue:appDelegate.user forKey:@"user"];
  714. if(appDelegate.contact_id!=nil)
  715. [params setValue:appDelegate.contact_id forKey:@"contactId"];
  716. if(appDelegate.password!=nil)
  717. [params setValue:appDelegate.password forKey:@"password"];
  718. // if(appDelegate.order_code!=nil)
  719. // [params setValue:appDelegate.order_code forKey:@"orderCode"];
  720. // NSDictionary* customerinfo= appDelegate.customerInfo;
  721. //
  722. // NSArray* keys=[customerinfo allKeys];
  723. // for(int i=0;i<keys.count;i++)
  724. // {
  725. // [params setValue:[customerinfo valueForKey:(NSString*)keys[i]] forKey:keys[i] ];
  726. // }
  727. //
  728. // [params setValue:sourceid forKey:@"sourceid"];
  729. // [params setValue:editor forKey:@"editor"];
  730. [params setObject:ScreenCodeModelInfo forKey:kScreenName];
  731. if(appDelegate.offline_mode)
  732. {
  733. params[@"offline_Command"]=url;
  734. [OLDataProvider offline_commoneditor_partialrefresh:params completionHandler:result];
  735. //
  736. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  737. // NSMutableDictionary* json = [[OLDataProvider offline_commoneditor_partialrefresh:params] mutableCopy];
  738. // dispatch_async(dispatch_get_main_queue(), ^{
  739. // result(json);
  740. // });
  741. // });
  742. return;
  743. }
  744. [self request_interface:url parameters:params err_record_url:nil completionHandler:result retry:0];
  745. //
  746. // if(appDelegate.offline_mode)
  747. // {
  748. // params[@"offline_Command"]=url;
  749. // return [OLDataProvider offline_commoneditor_partialrefresh:params];
  750. // }
  751. //
  752. // if(![self IsNetworkAvailable])
  753. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  754. //
  755. // NSData* json=[self get_json:url parameters:params];
  756. // if(json!=nil)
  757. // {
  758. // NSError *error=nil;
  759. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  760. // if([[jsobj valueForKey:@"result"]intValue]==2)
  761. // [appDelegate SetSo:nil];
  762. //
  763. //
  764. // return jsobj;
  765. // }
  766. // else
  767. // return nil;
  768. }
  769. +(void)SaveEditor:(NSMutableDictionary*)params completionHandler:(resultHandler)result
  770. {
  771. }
  772. @end