RADataProvider.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. +(void) request_scan_server :(resultHandler)result
  11. {
  12. NSMutableDictionary* param = [NSMutableDictionary new];
  13. param[@"_method"]=@"hostList";
  14. [self request_interface:URL_SERVER_LIST parameters:param err_record_url:nil completionHandler:result retry:0];
  15. }
  16. +(void)request_validate_scan_server:(NSString*)url name:(NSString*)name password:(NSString*)password completionHandler:(resultHandler)result
  17. {
  18. NSMutableDictionary* param = [NSMutableDictionary new];
  19. param[@"_method"]=@"hostCheck";
  20. param[@"password"]=password;
  21. param[@"site_name"]=name;
  22. // param[@"url"]=url;
  23. NSString* check_url = [NSString stringWithFormat:@"%@%@",url,URL_SERVER_CHECK];
  24. [self request_interface:check_url parameters:param err_record_url:nil completionHandler:result retry:0];
  25. }
  26. +(NSString*)getSiteName
  27. {
  28. NSDictionary *addressDic = [[NSUserDefaults standardUserDefaults] valueForKey:@"ScanAddress"];;
  29. if (addressDic) {
  30. NSString* ia=addressDic[@"internalAddress"];
  31. NSString* ea=addressDic[@"externalAddress"];
  32. NSString* sa=addressDic[@"serverAddress"];
  33. if(sa.length==0)
  34. {
  35. if(ea.length>0)
  36. sa=ea;
  37. else if(ia.length>0)
  38. sa=ia;
  39. }
  40. return [addressDic objectForKey:@"name"];
  41. }
  42. return @"";
  43. }
  44. +(void)request_change_password:(NSString*) pwd completionHandler:(resultHandler)result
  45. {
  46. AppDelegate *appDelegate = nil;
  47. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  48. NSMutableDictionary* param = [NSMutableDictionary new];
  49. param[@"_method"]=@"changePasswordOfflineOrderUser";
  50. param[@"opsw"]=appDelegate.password;
  51. param[@"npsw"]=pwd;
  52. param[@"user"]=appDelegate.user;
  53. param[@"site_name"]=[self getSiteName];
  54. // param[@"url"]=url;
  55. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_CHANGE_PASSWORD];
  56. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  57. }
  58. +(void)request_sign_up:user email:(NSString*)email password:(NSString*) pwd completionHandler:(resultHandler)result
  59. {
  60. AppDelegate *appDelegate = nil;
  61. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  62. NSMutableDictionary* param = [NSMutableDictionary new];
  63. param[@"_method"]=@"registerOfflineOrderUser";
  64. param[@"password"]=pwd;
  65. param[@"email"]=email;
  66. param[@"user"]=user;
  67. param[@"site_name"]=[self getSiteName];
  68. // param[@"url"]=url;
  69. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP];
  70. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  71. }
  72. +(void)request_change_password:user oldpassword:(NSString*)opwd newpassword:(NSString*) npwd completionHandler:(resultHandler)result
  73. {
  74. AppDelegate *appDelegate = nil;
  75. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  76. NSMutableDictionary* param = [NSMutableDictionary new];
  77. param[@"_method"]=@"changePasswordOfflineOrderUser";
  78. param[@"opsw"]=opwd;
  79. param[@"password"]=opwd;
  80. param[@"npsw"]=npwd;
  81. param[@"user"]=user;
  82. param[@"site_name"]=[self getSiteName];
  83. // param[@"url"]=url;
  84. NSString* url = [appDelegate.address stringByAppendingPathComponent:URL_SIGN_UP];
  85. [self request_interface:url parameters:param err_record_url:nil completionHandler:result retry:0];
  86. }
  87. +(void)request_scansearch:(long ) offset limit :(long)limit keywords :(NSString*) keywords completionHandler:(resultHandler)result
  88. {
  89. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  90. AppDelegate *appDelegate = nil;
  91. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  92. // if(appDelegate.user!=nil)
  93. // [params setValue:appDelegate.user forKey:@"user"];
  94. // if(appDelegate.contact_id!=nil)
  95. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  96. // if(appDelegate.password!=nil)
  97. // [params setValue:appDelegate.password forKey:@"password"];
  98. // if(appDelegate.order_code!=nil)
  99. // [params setValue:appDelegate.order_code forKey:@"orderCode"];
  100. // [params setValue:[NSString stringWithFormat:@"%d",price_template] forKey:@"price_template"];
  101. // [params setValue:[NSString stringWithFormat:@"%d",customid] forKey:@"custom_id"];
  102. // [params setValue:[NSString stringWithFormat:@"%@",category] forKey:@"category"];
  103. // [params setValue:[NSString stringWithFormat:@"%d",sort_by] forKey:@"sort_by"];
  104. // [params setValue:filter forKey:@"filter"];
  105. [params setValue:keywords forKey:@"keyword"];
  106. [params setValue:@"search" forKey:@"_method"];
  107. params[@"site_name"]=[self getSiteName];
  108. [params setValue:[NSString stringWithFormat:@"%ld",limit] forKey:@"limit"];
  109. [params setValue:[NSString stringWithFormat:@"%ld",offset] forKey:@"offset"];
  110. [self request_interface:URL_SEARCH parameters:params err_record_url:nil completionHandler:result retry:0];
  111. // if(appDelegate.offline_mode)
  112. // {
  113. // return [OLDataProvider offline_search:params];
  114. // }
  115. // if(![self IsNetworkAvailable])
  116. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  117. //
  118. // NSData* json=[self get_json:URL_SEARCH parameters:params];
  119. // if(json==nil)
  120. // return nil;
  121. // NSError *error=nil;
  122. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  123. // return jsobj;
  124. }
  125. +(void)request_editor:(NSString*) request_url params:(NSMutableDictionary*)params completionHandler:(resultHandler)result
  126. {
  127. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  128. AppDelegate *appDelegate = nil;
  129. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  130. if(appDelegate.user!=nil)
  131. [params setValue:appDelegate.user forKey:@"user"];
  132. // if(![appDelegate.order_customer_id isEqualToString: appDelegate.contact_id]&& appDelegate.order_customer_id!=nil)
  133. // [params setValue:appDelegate.order_customer_id forKey:@"contactId"];
  134. // else
  135. if(appDelegate.contact_id!=nil)
  136. [params setValue:appDelegate.contact_id forKey:@"contactId"];
  137. if(appDelegate.password!=nil)
  138. [params setValue:appDelegate.password forKey:@"password"];
  139. if(appDelegate.order_code!=nil)
  140. [params setValue:appDelegate.order_code forKey:@"orderCode"];
  141. bool can_create_backorder=appDelegate.can_create_backorder;
  142. // [params setValue:sourceid forKey:@"sourceid"];
  143. // [params setValue:editor forKey:@"editor"];
  144. DebugLog(@"request editor url:%@",request_url);
  145. if([request_url isEqualToString:URL_NEW_CUSTOMER])
  146. {
  147. [params setObject:ScreenCodeNewCustomer forKey:kScreenName];
  148. }
  149. else if([request_url isEqualToString:URL_EDIT_CUSTOMER])
  150. {
  151. [params setObject:ScreenCodeEditCustomer forKey:kScreenName];
  152. }
  153. else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH])
  154. {
  155. [params setObject:ScreenCodeAdvanceSearch forKey:kScreenName];
  156. }
  157. else if([request_url isEqualToString:URL_CARTDELIVERY])
  158. {
  159. [params setObject:ScreenCodePlaceOrder forKey:kScreenName];
  160. }
  161. else if([request_url isEqualToString:URL_ADDRESS_EDOTOR])
  162. {
  163. [params setObject:ScreenCodeNewAddress forKey:kScreenName];
  164. }
  165. else if ([request_url isEqualToString:URL_DM_PARAMS]){
  166. [params setObject:ScreenCodeCreatePortfolio forKey:kScreenName];
  167. }
  168. if(appDelegate.offline_mode)
  169. {
  170. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  171. NSData* json = nil;
  172. if([request_url isEqualToString:URL_NEW_CUSTOMER])
  173. {
  174. json= [OLDataProvider offline_createContact:params];
  175. }
  176. else if([request_url isEqualToString:URL_EDIT_CUSTOMER])
  177. {
  178. json= [OLDataProvider offline_editContact:params];
  179. }
  180. else if([request_url isEqualToString:URL_CUSTOMER_ADV_SEARCH])
  181. {
  182. json= [OLDataProvider offline_contactAdvancedSearch:params];
  183. }
  184. else if([request_url isEqualToString:URL_CARTDELIVERY])
  185. {
  186. params[@"contact_id"]=params[@"contactId"];
  187. params[@"can_create_backorder"]=@(can_create_backorder);
  188. json= [OLDataProvider offline_placeorder:params];
  189. }
  190. else if([request_url isEqualToString:URL_ADDRESS_EDOTOR])
  191. {
  192. json= [OLDataProvider offline_addreditor:params];
  193. }
  194. else if ([request_url isEqualToString:URL_DM_PARAMS]){
  195. json = [OLDataProvider offline_createTearSheet:params];
  196. }
  197. dispatch_async(dispatch_get_main_queue(), ^{
  198. if(json==nil)
  199. result( [[OLDataProvider offline_notimpl] mutableCopy]);
  200. else
  201. result([[RAConvertor data2dict:json] mutableCopy]);
  202. });
  203. });
  204. return;
  205. }
  206. else
  207. {
  208. if([request_url isEqualToString:URL_CARTDELIVERY])
  209. {
  210. NSString * uuid= [[NSUUID UUID] UUIDString];
  211. params[@"resultSerial"]=uuid;
  212. __block bool bbreak = false;
  213. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  214. for(int i=0;i<150;i++)
  215. {
  216. // NSMutableDictionary* result_params = [[NSMutableDictionary alloc]init];
  217. // result_params[@"resultSerial"]=uuid;
  218. //json=[self get_json:request_url parameters:params];
  219. if(bbreak)
  220. break;
  221. dispatch_async(dispatch_get_main_queue(), ^{
  222. NSLog(@"request cart delivery %d",i);
  223. [self request_interface:request_url parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *resulti) {
  224. if(resulti!=nil)
  225. {
  226. if([resulti[@"result"] intValue]==2 || [resulti[@"result"] intValue] == 8)
  227. {
  228. if(!bbreak) //此判断保证result只调用一次。
  229. {
  230. result(resulti);
  231. NSLog(@"result called at %d",i);
  232. }
  233. bbreak=true;
  234. }
  235. }
  236. } retry:3];
  237. });
  238. // [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0];
  239. sleep(2);
  240. }
  241. });
  242. }
  243. else
  244. [self request_interface:request_url parameters:params err_record_url:nil completionHandler:result retry:0];
  245. }
  246. // if(json!=nil)
  247. // {
  248. // NSError *error=nil;
  249. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  250. // return jsobj;
  251. // }
  252. // else
  253. // return nil;
  254. }
  255. +(void)request_commoneditor_partialrefresh: (NSMutableDictionary*)params url:(NSString*)url completionHandler:(resultHandler)result
  256. {
  257. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  258. AppDelegate *appDelegate = nil;
  259. appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
  260. if(appDelegate.user!=nil)
  261. [params setValue:appDelegate.user forKey:@"user"];
  262. if(appDelegate.contact_id!=nil)
  263. [params setValue:appDelegate.contact_id forKey:@"contactId"];
  264. if(appDelegate.password!=nil)
  265. [params setValue:appDelegate.password forKey:@"password"];
  266. // if(appDelegate.order_code!=nil)
  267. // [params setValue:appDelegate.order_code forKey:@"orderCode"];
  268. // NSDictionary* customerinfo= appDelegate.customerInfo;
  269. //
  270. // NSArray* keys=[customerinfo allKeys];
  271. // for(int i=0;i<keys.count;i++)
  272. // {
  273. // [params setValue:[customerinfo valueForKey:(NSString*)keys[i]] forKey:keys[i] ];
  274. // }
  275. //
  276. // [params setValue:sourceid forKey:@"sourceid"];
  277. // [params setValue:editor forKey:@"editor"];
  278. [params setObject:ScreenCodeModelInfo forKey:kScreenName];
  279. if(appDelegate.offline_mode)
  280. {
  281. params[@"offline_Command"]=url;
  282. [OLDataProvider offline_commoneditor_partialrefresh:params completionHandler:result];
  283. //
  284. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  285. // NSMutableDictionary* json = [[OLDataProvider offline_commoneditor_partialrefresh:params] mutableCopy];
  286. // dispatch_async(dispatch_get_main_queue(), ^{
  287. // result(json);
  288. // });
  289. // });
  290. return;
  291. }
  292. [self request_interface:url parameters:params err_record_url:nil completionHandler:result retry:0];
  293. //
  294. // if(appDelegate.offline_mode)
  295. // {
  296. // params[@"offline_Command"]=url;
  297. // return [OLDataProvider offline_commoneditor_partialrefresh:params];
  298. // }
  299. //
  300. // if(![self IsNetworkAvailable])
  301. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  302. //
  303. // NSData* json=[self get_json:url parameters:params];
  304. // if(json!=nil)
  305. // {
  306. // NSError *error=nil;
  307. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  308. // if([[jsobj valueForKey:@"result"]intValue]==2)
  309. // [appDelegate SetSo:nil];
  310. //
  311. //
  312. // return jsobj;
  313. // }
  314. // else
  315. // return nil;
  316. }
  317. +(void)SaveEditor:(NSMutableDictionary*)params completionHandler:(resultHandler)result
  318. {
  319. }
  320. @end