RADataProvider.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. @implementation RADataProvider
  10. +(NSArray*)request_scan_model_by_names:(NSString*)names
  11. {
  12. AppDelegate *appDelegate = nil;
  13. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  14. NSMutableArray* models=[NSMutableArray new];
  15. NSArray* arrname=[RAConvertor string2dict:names];
  16. for(int i=0;i<arrname.count;i++)
  17. {
  18. NSString* key = arrname[i];
  19. NSDictionary* m=appDelegate.scan_model[key];
  20. if(m!=nil)
  21. [models addObject:m];
  22. }
  23. return models;
  24. }
  25. +(void)load_scan_models
  26. {
  27. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  28. NSString *documents = [paths objectAtIndex:0];
  29. NSString* unZipTo=[documents stringByAppendingPathComponent:@"download"];
  30. NSString* templatefile = [unZipTo stringByAppendingPathComponent:@"offline_models.json"];
  31. NSString* str = [NSString stringWithContentsOfFile:templatefile encoding:NSUTF8StringEncoding error:nil];
  32. // NSString * str3 = [AESCrypt decrypt:str password:@"usai2010"];
  33. NSMutableDictionary* of = [[RAConvertor string2dict:str] mutableCopy];
  34. // NSData *filedata = [NSData dataWithContentsOfFile:path];
  35. // if(filedata == nil)
  36. // return nil;
  37. // NSError *error = nil;
  38. // NSDictionary *string2dic = [NSJSONSerialization JSONObjectWithData: filedata
  39. // options: NSJSONReadingMutableContainers
  40. // error: &error];
  41. // DebugLog(@"%@",string2dic);
  42. // NSString* str =@"";
  43. // NSMutableDictionary* item = [@{
  44. // @"product_id": @"114",
  45. // @"model": @"1420K-1EK*",
  46. // @"description": @"Eastern King Platform Bed with Footboard Storage",
  47. // @"dimension": @"82 x 91.5 x 59.5H",
  48. // @"stockUom": @"1",
  49. // @"unit_cuft": @"34.602",
  50. // @"port": @"VIET",
  51. // @"origin": @"VCGS",
  52. // @"available": @"Apr",
  53. // @"Fremont_DDP": @"289",
  54. // @"Fremont_WHSE": @"429"
  55. // } mutableCopy];
  56. // NSMutableDictionary* of = [NSMutableDictionary new];
  57. // for(int i=0;i<4000;i++)
  58. // {
  59. // item[@"product_id"]=[NSString stringWithFormat:@"pid_%d",i];
  60. // of[[NSString stringWithFormat:@"model_%d",i]]=item;
  61. // }
  62. //
  63. // NSString* strof=[RAConvertor dict2string:of];
  64. //
  65. //
  66. // NSString* str2 = [AESCrypt encrypt:strof password:@"usai2010"];
  67. // NSString * str3 = [AESCrypt decrypt:str2 password:@"usai2010"];
  68. NSArray* keysort=[self sortedDictionary:of];
  69. AppDelegate *appDelegate = nil;
  70. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  71. appDelegate.scan_model = of;
  72. appDelegate.scan_model_key=keysort;
  73. }
  74. + (NSArray*)sortedDictionary:(NSDictionary *)dict{
  75. //将所有的key放进数组
  76. NSArray *allKeyArray = [dict allKeys];
  77. //序列化器对数组进行排序的block 返回值为排序后的数组
  78. NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id
  79. _Nonnull obj2) {
  80. /**
  81. In the compare: methods, the range argument specifies the
  82. subrange, rather than the whole, of the receiver to use in the
  83. comparison. The range is not applied to the search string. For
  84. example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)]
  85. compares "A" to "ABC", not "A" to "A", and will return
  86. NSOrderedAscending. It is an error to specify a range that is
  87. outside of the receiver's bounds, and an exception may be raised.
  88. - (NSComparisonResult)compare:(NSString *)string;
  89. compare方法的比较原理为,依次比较当前字符串的第一个字母:
  90. 如果不同,按照输出排序结果
  91. 如果相同,依次比较当前字符串的下一个字母(这里是第二个)
  92. 以此类推
  93. 排序结果
  94. NSComparisonResult resuest = [obj1 compare:obj2];为从小到大,即升序;
  95. NSComparisonResult resuest = [obj2 compare:obj1];为从大到小,即降序;
  96. 注意:compare方法是区分大小写的,即按照ASCII排序
  97. */
  98. //排序操作
  99. NSComparisonResult resuest = [obj1 compare:obj2];
  100. return resuest;
  101. }];
  102. return afterSortKeyArray;
  103. // NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
  104. // //通过排列的key值获取value
  105. // NSMutableArray *valueArray = [NSMutableArray array];
  106. // for (NSString *sortsing in afterSortKeyArray) {
  107. // NSString *valueString = [dict objectForKey:sortsing];
  108. // [valueArray addObject:valueString];
  109. // }
  110. // NSLog(@"valueArray:%@",valueArray);
  111. }
  112. +(void) request_scan_news:(resultHandler)result
  113. {
  114. NSMutableDictionary* param = [NSMutableDictionary new];
  115. param[@"_method"]=@"offlineNews";
  116. [self request_interface:URL_SCAN_NEWS parameters:param err_record_url:nil completionHandler:result retry:0];
  117. }
  118. +(void)request_scan_stock:(NSMutableDictionary*)param completionHandler:(resultHandler)result
  119. {
  120. //
  121. param[@"_method"]=@"orderItemCheckStock";
  122. [self request_interface:URL_SCAN_NEWS parameters:param err_record_url:nil completionHandler:result retry:0];
  123. }
  124. +(void) request_scan_server :(resultHandler)result
  125. {
  126. NSMutableDictionary* param = [NSMutableDictionary new];
  127. param[@"_method"]=@"hostList";
  128. [self request_interface:URL_SERVER_LIST parameters:param err_record_url:nil completionHandler:result retry:0];
  129. }
  130. +(void)request_validate_scan_server:(NSString*)url name:(NSString*)name password:(NSString*)password completionHandler:(resultHandler)result
  131. {
  132. NSMutableDictionary* param = [NSMutableDictionary new];
  133. param[@"_method"]=@"hostCheck";
  134. param[@"password"]=password;
  135. param[@"site_name"]=name;
  136. // param[@"url"]=url;
  137. NSString* check_url = [NSString stringWithFormat:@"%@%@",url,URL_SERVER_CHECK];
  138. [self request_interface:check_url parameters:param err_record_url:nil completionHandler:result retry:0];
  139. }
  140. +(NSString*)getSiteName
  141. {
  142. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];;
  143. if (addressDic) {
  144. NSString* ia=addressDic[@"internalAddress"];
  145. NSString* ea=addressDic[@"externalAddress"];
  146. NSString* sa=addressDic[@"serverAddress"];
  147. if(sa.length==0)
  148. {
  149. if(ea.length>0)
  150. sa=ea;
  151. else if(ia.length>0)
  152. sa=ia;
  153. }
  154. return [addressDic objectForKey:@"name"];
  155. }
  156. return @"";
  157. }
  158. +(void)request_change_password:(NSString*) pwd completionHandler:(resultHandler)result
  159. {
  160. AppDelegate *appDelegate = nil;
  161. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  162. NSMutableDictionary* param = [NSMutableDictionary new];
  163. param[@"_method"]=@"changePasswordOfflineOrderUser";
  164. param[@"opsw"]=appDelegate.password;
  165. param[@"npsw"]=pwd;
  166. param[@"user"]=appDelegate.user;
  167. param[@"site_name"]=[self getSiteName];
  168. // param[@"url"]=url;
  169. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_CHANGE_PASSWORD];
  170. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  171. }
  172. +(void)request_sign_up:user email:(NSString*)email password:(NSString*) pwd completionHandler:(resultHandler)result
  173. {
  174. AppDelegate *appDelegate = nil;
  175. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  176. NSMutableDictionary* param = [NSMutableDictionary new];
  177. param[@"_method"]=@"registerOfflineOrderUser";
  178. param[@"password"]=pwd;
  179. param[@"email"]=email;
  180. param[@"user"]=user;
  181. param[@"site_name"]=[self getSiteName];
  182. // param[@"url"]=url;
  183. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP];
  184. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  185. }
  186. +(void)request_change_password:user oldpassword:(NSString*)opwd newpassword:(NSString*) npwd completionHandler:(resultHandler)result
  187. {
  188. AppDelegate *appDelegate = nil;
  189. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  190. NSMutableDictionary* param = [NSMutableDictionary new];
  191. param[@"_method"]=@"changePasswordOfflineOrderUser";
  192. param[@"opsw"]=opwd;
  193. param[@"password"]=opwd;
  194. param[@"npsw"]=npwd;
  195. param[@"user"]=user;
  196. param[@"site_name"]=[self getSiteName];
  197. // param[@"url"]=url;
  198. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP];
  199. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  200. }
  201. +(NSMutableDictionary*)scan_search:(NSString*)keyword
  202. {
  203. // NSMutableArray* arr = [NSMutableArray new];
  204. int count = 0;
  205. NSMutableDictionary * ret = [NSMutableDictionary new];
  206. ret[@"result"]=@2;
  207. AppDelegate *appDelegate = nil;
  208. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  209. // NSArray* keys=[dict allKeys];
  210. for(int i=0;i<appDelegate.scan_model_key.count;i++)
  211. {
  212. NSString* key = appDelegate.scan_model_key[i];
  213. if([key.lowercaseString containsString:keyword.lowercaseString]||keyword.length==0)
  214. {
  215. ret[[NSString stringWithFormat:@"item_%d",count]]=appDelegate.scan_model[key];
  216. // [arr addObject:dict[key]];
  217. count ++;
  218. }
  219. }
  220. ret[@"count"]=@(count);
  221. return ret;
  222. // int a=0;
  223. }
  224. +(void)request_scansearch:(long ) offset limit :(long)limit keywords :(NSString*) keywords completionHandler:(resultHandler)result
  225. {
  226. result([self scan_search:keywords]);
  227. return;
  228. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  229. AppDelegate *appDelegate = nil;
  230. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  231. // if(appDelegate.user!=nil)
  232. // [params setValue:appDelegate.user forKey:@"user"];
  233. // if(appDelegate.contact_id!=nil)
  234. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  235. // if(appDelegate.password!=nil)
  236. // [params setValue:appDelegate.password forKey:@"password"];
  237. // if(appDelegate.order_code!=nil)
  238. // [params setValue:appDelegate.order_code forKey:@"orderCode"];
  239. // [params setValue:[NSString stringWithFormat:@"%d",price_template] forKey:@"price_template"];
  240. // [params setValue:[NSString stringWithFormat:@"%d",customid] forKey:@"custom_id"];
  241. // [params setValue:[NSString stringWithFormat:@"%@",category] forKey:@"category"];
  242. // [params setValue:[NSString stringWithFormat:@"%d",sort_by] forKey:@"sort_by"];
  243. // [params setValue:filter forKey:@"filter"];
  244. [params setValue:keywords forKey:@"keyword"];
  245. [params setValue:@"search" forKey:@"_method"];
  246. params[@"site_name"]=[self getSiteName];
  247. [params setValue:[NSString stringWithFormat:@"%ld",limit] forKey:@"limit"];
  248. [params setValue:[NSString stringWithFormat:@"%ld",offset] forKey:@"offset"];
  249. [self request_interface:URL_SEARCH parameters:params err_record_url:nil completionHandler:result retry:0];
  250. // if(appDelegate.offline_mode)
  251. // {
  252. // return [OLDataProvider offline_search:params];
  253. // }
  254. // if(![self IsNetworkAvailable])
  255. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  256. //
  257. // NSData* json=[self get_json:URL_SEARCH parameters:params];
  258. // if(json==nil)
  259. // return nil;
  260. // NSError *error=nil;
  261. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  262. // return jsobj;
  263. }
  264. +(void)request_editor:(NSString*) request_url params:(NSMutableDictionary*)params completionHandler:(resultHandler)result
  265. {
  266. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  267. AppDelegate *appDelegate = nil;
  268. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  269. if(appDelegate.user!=nil)
  270. [params setValue:appDelegate.user forKey:@"user"];
  271. // if(![appDelegate.order_customer_id isEqualToString: appDelegate.contact_id]&& appDelegate.order_customer_id!=nil)
  272. // [params setValue:appDelegate.order_customer_id forKey:@"contactId"];
  273. // else
  274. if(appDelegate.contact_id!=nil)
  275. [params setValue:appDelegate.contact_id forKey:@"contactId"];
  276. if(appDelegate.password!=nil)
  277. [params setValue:appDelegate.password forKey:@"password"];
  278. if(appDelegate.order_code!=nil)
  279. [params setValue:appDelegate.order_code forKey:@"orderCode"];
  280. bool can_create_backorder=appDelegate.can_create_backorder;
  281. // [params setValue:sourceid forKey:@"sourceid"];
  282. // [params setValue:editor forKey:@"editor"];
  283. DebugLog(@"request editor url:%@",request_url);
  284. if([request_url isEqualToString:URL_NEW_CUSTOMER])
  285. {
  286. [params setObject:ScreenCodeNewCustomer forKey:kScreenName];
  287. }
  288. else if([request_url isEqualToString:URL_EDIT_CUSTOMER])
  289. {
  290. [params setObject:ScreenCodeEditCustomer forKey:kScreenName];
  291. }
  292. else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH])
  293. {
  294. [params setObject:ScreenCodeAdvanceSearch forKey:kScreenName];
  295. }
  296. else if([request_url isEqualToString:URL_CARTDELIVERY])
  297. {
  298. [params setObject:ScreenCodePlaceOrder forKey:kScreenName];
  299. }
  300. else if([request_url isEqualToString:URL_ADDRESS_EDOTOR])
  301. {
  302. [params setObject:ScreenCodeNewAddress forKey:kScreenName];
  303. }
  304. else if ([request_url isEqualToString:URL_DM_PARAMS]){
  305. [params setObject:ScreenCodeCreatePortfolio forKey:kScreenName];
  306. }
  307. if(appDelegate.offline_mode)
  308. {
  309. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  310. NSData* json = nil;
  311. if([request_url isEqualToString:URL_NEW_CUSTOMER])
  312. {
  313. json= [OLDataProvider offline_createContact:params];
  314. }
  315. else if([request_url isEqualToString:URL_EDIT_CUSTOMER])
  316. {
  317. json= [OLDataProvider offline_editContact:params];
  318. }
  319. else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH])
  320. {
  321. json= [OLDataProvider offline_contactAdvancedSearch:params];
  322. }
  323. else if([request_url isEqualToString:URL_CARTDELIVERY])
  324. {
  325. params[@"contact_id"]=params[@"contactId"];
  326. params[@"can_create_backorder"]=@(can_create_backorder);
  327. json= [OLDataProvider offline_placeorder:params];
  328. }
  329. else if([request_url isEqualToString:URL_ADDRESS_EDOTOR])
  330. {
  331. json= [OLDataProvider offline_addreditor:params];
  332. }
  333. else if ([request_url isEqualToString:URL_DM_PARAMS]){
  334. json = [OLDataProvider offline_createTearSheet:params];
  335. }
  336. dispatch_async(dispatch_get_main_queue(), ^{
  337. if(json==nil)
  338. result( [[OLDataProvider offline_notimpl] mutableCopy]);
  339. else
  340. result([[RAConvertor data2dict:json] mutableCopy]);
  341. });
  342. });
  343. return;
  344. }
  345. else
  346. {
  347. if([request_url isEqualToString:URL_CARTDELIVERY])
  348. {
  349. NSString * uuid= [[NSUUID UUID] UUIDString];
  350. params[@"resultSerial"]=uuid;
  351. __block bool bbreak = false;
  352. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  353. for(int i=0;i<150;i++)
  354. {
  355. // NSMutableDictionary* result_params = [[NSMutableDictionary alloc]init];
  356. // result_params[@"resultSerial"]=uuid;
  357. //json=[self get_json:request_url parameters:params];
  358. if(bbreak)
  359. break;
  360. dispatch_async(dispatch_get_main_queue(), ^{
  361. NSLog(@"request cart delivery %d",i);
  362. [self request_interface:request_url parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *resulti) {
  363. if(resulti!=nil)
  364. {
  365. if([resulti[@"result"] intValue]==2 || [resulti[@"result"] intValue] == 8)
  366. {
  367. if(!bbreak) //此判断保证result只调用一次。
  368. {
  369. result(resulti);
  370. NSLog(@"result called at %d",i);
  371. }
  372. bbreak=true;
  373. }
  374. }
  375. } retry:3];
  376. });
  377. // [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0];
  378. sleep(2);
  379. }
  380. });
  381. }
  382. else
  383. [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0];
  384. }
  385. // if(json!=nil)
  386. // {
  387. // NSError *error=nil;
  388. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  389. // return jsobj;
  390. // }
  391. // else
  392. // return nil;
  393. }
  394. +(void)request_commoneditor_partialrefresh: (NSMutableDictionary*)params url:(NSString*)url completionHandler:(resultHandler)result
  395. {
  396. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  397. AppDelegate *appDelegate = nil;
  398. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  399. if(appDelegate.user!=nil)
  400. [params setValue:appDelegate.user forKey:@"user"];
  401. if(appDelegate.contact_id!=nil)
  402. [params setValue:appDelegate.contact_id forKey:@"contactId"];
  403. if(appDelegate.password!=nil)
  404. [params setValue:appDelegate.password forKey:@"password"];
  405. // if(appDelegate.order_code!=nil)
  406. // [params setValue:appDelegate.order_code forKey:@"orderCode"];
  407. // NSDictionary* customerinfo= appDelegate.customerInfo;
  408. //
  409. // NSArray* keys=[customerinfo allKeys];
  410. // for(int i=0;i<keys.count;i++)
  411. // {
  412. // [params setValue:[customerinfo valueForKey:(NSString*)keys[i]] forKey:keys[i] ];
  413. // }
  414. //
  415. // [params setValue:sourceid forKey:@"sourceid"];
  416. // [params setValue:editor forKey:@"editor"];
  417. [params setObject:ScreenCodeModelInfo forKey:kScreenName];
  418. if(appDelegate.offline_mode)
  419. {
  420. params[@"offline_Command"]=url;
  421. [OLDataProvider offline_commoneditor_partialrefresh:params completionHandler:result];
  422. //
  423. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  424. // NSMutableDictionary* json = [[OLDataProvider offline_commoneditor_partialrefresh:params] mutableCopy];
  425. // dispatch_async(dispatch_get_main_queue(), ^{
  426. // result(json);
  427. // });
  428. // });
  429. return;
  430. }
  431. [self request_interface:url parameters:params err_record_url:nil completionHandler:result retry:0];
  432. //
  433. // if(appDelegate.offline_mode)
  434. // {
  435. // params[@"offline_Command"]=url;
  436. // return [OLDataProvider offline_commoneditor_partialrefresh:params];
  437. // }
  438. //
  439. // if(![self IsNetworkAvailable])
  440. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  441. //
  442. // NSData* json=[self get_json:url parameters:params];
  443. // if(json!=nil)
  444. // {
  445. // NSError *error=nil;
  446. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  447. // if([[jsobj valueForKey:@"result"]intValue]==2)
  448. // [appDelegate SetSo:nil];
  449. //
  450. //
  451. // return jsobj;
  452. // }
  453. // else
  454. // return nil;
  455. }
  456. +(void)SaveEditor:(NSMutableDictionary*)params completionHandler:(resultHandler)result
  457. {
  458. }
  459. @end