ACNetwork.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. //
  2. // ACNetwork.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/26/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ACNetwork.h"
  9. #import "AppDelegate.h"
  10. #import "RAUtils.h"
  11. #import "ZipArchive.h"
  12. #import "config.h"
  13. #import "AESCrypt.h"
  14. @implementation ACNetwork
  15. +(NSMutableDictionary*) prepare_addtional_params:(NSMutableDictionary* ) params
  16. {
  17. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  18. if(appDelegate.user!=nil)
  19. [params setValue:[AESCrypt AES128Encrypt:appDelegate.user key:@"usai2010"] forKey:@"user"];
  20. if(appDelegate.password!=nil)
  21. [params setValue:[AESCrypt AES128Encrypt:appDelegate.password key:@"usai2010"] forKey:@"password"];
  22. [params setValue:appDelegate.build forKey:@"app_ver"];
  23. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  24. NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  25. [params setValue:short_version forKey:@"app_short_ver"];
  26. #if TARGET_IPHONE_SIMULATOR//模拟器
  27. [params setValue:@"simulator_uuid" forKey:@"deviceid"];
  28. #elif TARGET_OS_IPHONE//真机
  29. UIDevice * dev = [UIDevice currentDevice];
  30. NSUUID* uuid =dev.identifierForVendor;
  31. [params setValue:uuid.UUIDString forKey:@"deviceid"];
  32. #endif
  33. return params;
  34. }
  35. +(NSData*)get_json : (NSString*) url parameters:(NSMutableDictionary *) params file:(NSString *) file //delegate:(id < NSURLConnectionDelegate >)delegate
  36. {
  37. // NSString* e=[AESCrypt AES128Encrypt:@"密码学中的高级加密标准(Advanced Encryption Standard,AES),又称 高级加密标准Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院 (NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。 " key:@"usai2010"];
  38. // DebugLog(e);
  39. // NSString* d=[AESCrypt decrypt:@"VzB4+lENfbzWX7ggHh2Os1P69l5YBzKmM51yqA37AfoKgPY8bJgdK8M4WvF+wDyOsLWP\/o8H5+bHMivQp1u8wM2QV7SYzgzPwV85QKtYWIzEgLe9T0HO69EdutWz7k1rdAT\/mftsRin0Hy5SHk7txTZT\/zPH5X+FQiExCTFm5Zus39HYvp+VxCX4+kYeymn8B63AUd3mRQyscUvDjfgU2olKR\/TP8PV3g+VQMZxpKfol3P1iOtz3XQlHZV0pM6SAf+SwT0sPfrdn6CZFfNQlrKA9QjMrnBdMeSCquSIWXnmLv6okzvnlJnT0SjvJuUeqNdBC5EZ0ACnhI0MrXVZEsq1EuM9al7oPzvU5EZbjZpmx+fSFIkMzTT8bYClTnG1bsL1MCcU4pSODDorUj8zalA==" password:@"usai2010"];
  40. params = [self prepare_addtional_params:params];
  41. return [super get_json:url parameters:params file:(NSString*)file err_recorder:URL_ERR_LOG result_handler:^NSMutableDictionary *(NSMutableDictionary *jsobj) {
  42. int result=[[jsobj valueForKey:@"result"] intValue];
  43. // UIApplication * app = [UIApplication sharedApplication];
  44. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  45. switch (result) {
  46. case 0:
  47. [jsobj setValue:MSG_USERAUTH_ERROR forKey:@"err_msg"];
  48. break;
  49. case 1:
  50. [jsobj setValue:MSG_ERROR forKey:@"err_msg"];
  51. break;
  52. case 2:
  53. [jsobj setValue:MSG_SUCCESS forKey:@"err_msg"];
  54. break;
  55. case 8:
  56. [jsobj setValue:[jsobj valueForKey:@"msg"] forKey:@"err_msg"];
  57. break;
  58. case 9:
  59. [jsobj setValue:[jsobj valueForKey:@"msg"] forKey:@"err_msg"];
  60. break;
  61. case 99:
  62. [jsobj setValue:MSG_TIMEOUT forKey:@"err_msg"];
  63. break;
  64. default:
  65. [jsobj setValue:MSG_ERROR forKey:@"err_msg"];
  66. break;
  67. }
  68. // if([appDelegate.build intValue]< [[jsobj valueForKey:@"min_ver"]intValue])
  69. // {
  70. // [jsobj setValue:@"9" forKey:@"result"];
  71. // [jsobj setValue:MSG_VER_LOW forKey:@"err_msg"];
  72. // }
  73. return jsobj;
  74. } decrypt_handler:^NSMutableDictionary *(NSMutableDictionary *jsobj) {
  75. NSString* base64str = jsobj[@"str"];
  76. NSString* decryptstr=[AESCrypt AES128Decrypt:base64str key:@"usai2010"];
  77. jsobj=[[RAUtils string2dict:decryptstr] mutableCopy];
  78. return jsobj;
  79. }];
  80. /*
  81. */
  82. return nil;
  83. }
  84. +(NSDictionary*)new_document
  85. {
  86. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  87. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  88. if(appDelegate.user!=nil)
  89. [params setValue:appDelegate.user forKey:@"user"];
  90. if(appDelegate.password!=nil)
  91. [params setValue:appDelegate.password forKey:@"password"];
  92. params[@"_operate"]=@"doc_id";
  93. if(![self IsNetworkAvailable])
  94. {
  95. DebugLog(@"NET ERR");
  96. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  97. // return nil;
  98. }
  99. NSData* json=[self get_json:URL_NEW_DOCUMENT parameters:params file:nil];
  100. //if(json==nil)
  101. if(json==nil)
  102. return nil;
  103. NSError *error=nil;
  104. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  105. int result = [[jsobj valueForKey:@"result"] intValue];
  106. DebugLog(@"new_document result:%d",result);
  107. return jsobj;
  108. }
  109. +(NSDictionary*)upload_file:(NSString*)file params:(NSMutableDictionary*) params
  110. {
  111. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  112. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  113. // if(appDelegate.user!=nil)
  114. // [params setValue:appDelegate.user forKey:@"user"];
  115. //
  116. //
  117. // if(appDelegate.password!=nil)
  118. // [params setValue:appDelegate.password forKey:@"password"];
  119. params[@"_operate"]=@"uploadFileIpad";
  120. if(![self IsNetworkAvailable])
  121. {
  122. DebugLog(@"NET ERR");
  123. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  124. // return nil;
  125. }
  126. NSData* json=[self get_json:URL_UPLOAD_PDF parameters:params file:file];
  127. //if(json==nil)
  128. if(json==nil)
  129. return nil;
  130. NSError *error=nil;
  131. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  132. int result = [[jsobj valueForKey:@"result"] intValue];
  133. DebugLog(@"upload_file result:%d",result);
  134. return jsobj;
  135. }
  136. +(NSDictionary*)save_document:(NSString*)file
  137. {
  138. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  139. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  140. // if(appDelegate.user!=nil)
  141. // [params setValue:appDelegate.user forKey:@"user"];
  142. //
  143. //
  144. // if(appDelegate.password!=nil)
  145. // [params setValue:appDelegate.password forKey:@"password"];
  146. params[@"_operate"]=@"deviceMain";
  147. if(![self IsNetworkAvailable])
  148. {
  149. DebugLog(@"NET ERR");
  150. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  151. // return nil;
  152. }
  153. NSData* json=[self get_json:URL_UPLOAD_PDF parameters:params file:file];
  154. //if(json==nil)
  155. if(json==nil)
  156. return nil;
  157. NSError *error=nil;
  158. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  159. int result = [[jsobj valueForKey:@"result"] intValue];
  160. DebugLog(@"new_document result:%d",result);
  161. return jsobj;
  162. }
  163. +(NSDictionary*)logout
  164. {
  165. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  166. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  167. if(appDelegate.user!=nil)
  168. [params setValue:appDelegate.user forKey:@"user"];
  169. if(appDelegate.password!=nil)
  170. [params setValue:appDelegate.password forKey:@"password"];
  171. params[@"_operate"]=@"logout_zip";
  172. if(![self IsNetworkAvailable])
  173. {
  174. DebugLog(@"NET ERR");
  175. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  176. // return nil;
  177. }
  178. NSData* json=[self get_json:URL_LOGOUT parameters:params file:nil];
  179. //if(json==nil)
  180. if(json==nil)
  181. return nil;
  182. NSError *error=nil;
  183. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  184. int result = [[jsobj valueForKey:@"result"] intValue];
  185. if (result==2)
  186. {
  187. [appDelegate Logout];
  188. }
  189. return jsobj;
  190. }
  191. +(int)Authorize : (NSString*) user password:(NSString*) password{
  192. // if(![ApexMobileNetwork IsHostAvailable:URL_UPDATE_AUTH])
  193. // return RESULT_NET_ERROR;
  194. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  195. long ver =[defaults integerForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  196. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  197. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  198. // if(appDelegate.duid !=nil)
  199. // [params setValue:appDelegate.duid forKey:@"udid"];
  200. // [params setValue:@"handset_login" forKey:@"action"];
  201. // [headers setValue:[NSString stringWithFormat:@"%d",dataLength] forKey:@"Content-Length"];
  202. [params setValue:[AESCrypt AES128Encrypt:user key:@"usai2010"] forKey:@"user"];
  203. [params setValue:[AESCrypt AES128Encrypt:password key:@"usai2010"] forKey:@"password"];
  204. [params setValue:[NSString stringWithFormat:@"%ld",ver] forKey:@"auth_ver"];
  205. [params setValue:@"login" forKey:@"_operate"];
  206. //#ifdef TEST
  207. // [params setValue:@"true" forKey:@"is_debug"];
  208. //#else
  209. // [params setValue:@"false" forKey:@"is_debug"];
  210. //#endif
  211. NSData* json= nil;
  212. // if(appDelegate.offline_mode)
  213. // {
  214. // json= [OLDataProvider offline_login:params] ;
  215. //
  216. // }
  217. // else
  218. if(![self IsNetworkAvailable])
  219. return RESULT_NET_NOTAVAILABLE;
  220. json =[self get_json:URL_UPDATE_AUTH parameters:params file:nil];
  221. if(json==nil)
  222. return RESULT_NET_ERROR;
  223. int ret = [self parse_authinfo:json user:user password:password];
  224. DebugLog(@"parse_authinfo return %d ",ret);
  225. return ret;
  226. }
  227. +(int)parse_authinfo : (NSData *) json user:(NSString*) user password:(NSString*) password
  228. {
  229. DebugLog(@"parse_authinfo");
  230. NSError *error=nil;
  231. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  232. if(jsobj)
  233. {
  234. UIApplication * app = [UIApplication sharedApplication];
  235. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  236. int result = [[jsobj valueForKey:@"result"] intValue];
  237. if (result<0)
  238. return result;
  239. if(result!=AP_USER_AUTH&& result!=9)
  240. {
  241. DebugLog(@"USER NOT AUTHORIZED CODE=%d ",result);
  242. [appDelegate Logout];
  243. if(result==8)
  244. {
  245. return RESULT_LOGIN_DEVICE;
  246. }
  247. return RESULT_FALSE;
  248. }
  249. // NSDictionary* objheader = [jsobj objectForKey:@"header"];
  250. //
  251. // NSString* required_ver=[jsobj objectForKey:@"min_ver"] ;
  252. // user=[objheader objectForKey:@"username"] ;
  253. // DebugLog(@"required_ver=%@ ",required_ver);
  254. // // NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  255. // // NSString* versionNum =[infoDict valueForKey:@"CFBundleVersion"];
  256. // // versionNum = [NSString stringWithFormat:@"A%@",versionNum];
  257. // int ibadge = [[objheader valueForKey:@"badge"] intValue];
  258. //
  259. // // if([ibadge isEqual:[NSNull null]])
  260. // // strbadge=@"0";
  261. // // if(ibadge==nil)
  262. // // ibadge=@"0";
  263. // // if([strbadge isEqualToString:@"null"])
  264. // // strbadge=@"0";
  265. // app.applicationIconBadgeNumber = ibadge;//[strbadge intValue];
  266. //
  267. // // BOOL bigger = [appDelegate.build compare:required_ver] ;
  268. // if([appDelegate.build intValue]<[required_ver intValue])
  269. // return RESULT_VER_LOW;
  270. // // appDelegate.sessionid = [objheader valueForKey:@"sessionid"];
  271. // appDelegate.user = user;
  272. // appDelegate.password = password;
  273. //// appDelegate.user_type = [[objheader valueForKey:@"user_type"] intValue];
  274. //// appDelegate.user_icon =[objheader valueForKey:@"user_img"] ;
  275. // appDelegate.bLogin = true;
  276. [appDelegate Login:user pwd:password];
  277. //
  278. // if(appDelegate.user_type==USER_ROLE_CUSTOMER)
  279. // {
  280. // appDelegate.customerInfo = [[objheader objectForKeyedSubscript:@"customerInfo"] mutableCopy];
  281. // appDelegate.contact_id = [objheader valueForKey:@"contact_id"];
  282. // // appDelegate.order_code =[objheader valueForKey:@"orderCode"];
  283. // // appDelegate.order_status =[[objheader valueForKey:@"orderStatus"] intValue];
  284. // // [appDelegate SetSo:[objheader valueForKey:@"soId"]];
  285. // }
  286. // NSString* mode =[jsobj valueForKey:@"mode"] ;
  287. // [appDelegate SetMode:mode];
  288. //
  289. //
  290. // appDelegate.cart_count = [[objheader valueForKey:@"cart_count"] intValue];
  291. // appDelegate.wish_count =[[objheader valueForKey:@"wish_count"] intValue];
  292. // appDelegate.port_count =[[objheader valueForKey:@"portfolio_count"] intValue];
  293. //
  294. // [appDelegate update_count_mark];
  295. //
  296. //
  297. //
  298. // appDelegate.can_show_price =[[objheader valueForKey:@"can_show_price"] boolValue];
  299. // appDelegate.can_see_price =[[objheader valueForKey:@"can_see_price"] boolValue];
  300. // appDelegate.can_create_portfolio =[[objheader valueForKey:@"can_create_portfolio"] boolValue];
  301. // appDelegate.can_create_order =[[objheader valueForKey:@"can_create_order"] boolValue];
  302. //
  303. // appDelegate.can_cancel_order =[[objheader valueForKey:@"can_cancel_order"] boolValue];
  304. // appDelegate.can_set_cart_price =[[objheader valueForKey:@"can_set_cart_price"] boolValue];
  305. // appDelegate.can_delete_order =[[objheader valueForKey:@"can_delete_order"] boolValue];
  306. // appDelegate.can_update_contact_info =[[objheader valueForKey:@"can_update_contact_info"] boolValue];
  307. // appDelegate.can_submit_order =[[objheader valueForKey:@"can_submit_order"] boolValue];
  308. // appDelegate.can_set_tearsheet_price =[[objheader valueForKey:@"can_set_tearsheet_price"] boolValue];
  309. //
  310. // appDelegate.save_order_logout =[[objheader valueForKey:@"save_order_logout"] boolValue];
  311. // appDelegate.submit_order_logout =[[objheader valueForKey:@"submit_order_logout"] boolValue];
  312. //
  313. // appDelegate.alert_sold_in_quantities = [[objheader valueForKey:@"alert_sold_in_quantities"] boolValue];
  314. //
  315. //
  316. //
  317. // appDelegate.ipad_perm =[objheader valueForKey:@"ipad_perm"] ;
  318. //
  319. // NSString* strfilter = [objheader valueForKey:@"statusFilter"] ;
  320. //
  321. //
  322. //
  323. // appDelegate.OrderFilter= [[RAUtils string2dict:strfilter] mutableCopy];
  324. //
  325. //
  326. // [appDelegate set_main_button_panel];
  327. //
  328. // if(appDelegate.user_type==USER_ROLE_CUSTOMER) {
  329. //
  330. // // shop order status filter
  331. // NSString *shopOrderStatusFilterStr = [objheader valueForKey:@"shopOrderFilter"] ;
  332. // [Singleton sharedInstance].shop_order_status_filter = [[RAUtils string2dict:shopOrderStatusFilterStr] mutableCopy];
  333. //
  334. // // specialInstruction
  335. // [Singleton sharedInstance].specialInstruction = [objheader valueForKey:@"specialInstruction"];
  336. //
  337. // //-价格类型
  338. // [Singleton sharedInstance].npd_shop_price_type = [[objheader valueForKey:@"price_type"] integerValue];
  339. //
  340. // // 权限
  341. // [Singleton sharedInstance].permissions_price_setting = [[objheader valueForKey:@"can_set_price_formula"] boolValue];
  342. // [Singleton sharedInstance].permissions_edit_order = [[objheader valueForKey:@"can_place_order"] boolValue];
  343. //
  344. // [Singleton sharedInstance].permissions_submit_order = [[objheader valueForKey:@"can_submit_order"] boolValue];
  345. //
  346. // [Singleton sharedInstance].permissions_merge_order = [[objheader valueForKey:@"can_merge_order"] boolValue];
  347. //
  348. // [Singleton sharedInstance].deliveryString = [objheader valueForKey:@"delivery_price"];
  349. // [[Singleton sharedInstance] resetGlobalLock];
  350. // [Singleton sharedInstance].customer_can_see_sales_Order = [[objheader valueForKey:@"can_see_salesorder"] boolValue];
  351. // }
  352. // DebugLog(@"sessionid=%@ ",appDelegate.sessionid);
  353. // if ([[objheader valueForKey:@"update"] boolValue]==false)
  354. // {
  355. // // no update on the server;
  356. // return RESULT_TRUE;
  357. // }
  358. // int Auth_InfoVer = [[objheader valueForKey:@"ver"] intValue];
  359. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  360. // [defaults removeObjectForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  361. // [defaults setInteger:Auth_InfoVer forKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  362. // [defaults synchronize];
  363. //
  364. // NSDictionary* objfuncs = [jsobj objectForKey:@"functions"];
  365. // NSArray* keys= [objfuncs allKeys];
  366. /*
  367. db.setTransactionSuccessful();
  368. db.endTransaction();
  369. */
  370. return RESULT_TRUE;
  371. }
  372. // NSDictionary *weatherInfo = [jsobj objectForKey:@"weatherinfo"];
  373. // txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
  374. // DebugLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
  375. return RESULT_USERAUTH_ERROR;
  376. }
  377. +(NSDictionary*)err_log:(NSString*) up_params result:(NSString*)result module:(NSString*) module code:(int) code//device:(NSString*) device
  378. {
  379. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  380. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  381. // if(appDelegate.user!=nil)
  382. // [params setValue:appDelegate.user forKey:@"user"];
  383. //
  384. // [params setValue:up_params forKey:@"upparam"];
  385. //
  386. // [params setValue:[NSNumber numberWithInt:code ] forKey:@"code"];
  387. //
  388. // [params setValue:result forKey:@"result"];
  389. // [params setValue:module forKey:@"interfac"];
  390. //
  391. // [params setValue:[RAUtils base64en:[RAUtils dict2string: [RAUtils device_info]]] forKey:@"device"];
  392. //
  393. //
  394. // // [params setValue:module forKey:@"module"];
  395. //
  396. // // [[UIDevice currentDevice] name];
  397. //
  398. // // if(appDelegate.offline_mode)
  399. // // {
  400. // // return [OLDataProvider offline_notimpl];
  401. // // }
  402. // if(![self IsNetworkAvailable])
  403. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  404. //
  405. // NSData* json=[self get_json:URL_ERR_LOG parameters:params];
  406. // if(json==nil)
  407. // return nil;
  408. // NSError *error=nil;
  409. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  410. // return jsobj;
  411. NSMutableDictionary* jsobj = [@{@"result":@"2"} mutableCopy];
  412. return jsobj;
  413. }
  414. +(NSString *) packfiles2zip:(NSMutableArray*) arr_file
  415. {
  416. if(arr_file.count==0)
  417. {
  418. DebugLog(@"packfiles2zip: source list is empty.");
  419. return @"";
  420. }
  421. NSString* serial= [[NSUUID UUID] UUIDString];
  422. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  423. NSString *cachefolder = [paths objectAtIndex:0];
  424. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  425. upfolder = [upfolder stringByAppendingPathComponent:serial];
  426. NSFileManager* fileManager = [NSFileManager defaultManager];
  427. BOOL bdir=YES;
  428. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  429. {
  430. NSError *error = nil;
  431. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  432. if(!bsuccess)
  433. {
  434. DebugLog(@"Create UPLOAD folder failed");
  435. return @"";
  436. }
  437. }
  438. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  439. ZipArchive* zip = [[ZipArchive alloc] init];
  440. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  441. for(int i=0;i<arr_file.count;i++)
  442. {
  443. NSString* file = arr_file[i];
  444. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  445. }
  446. if( ![zip CloseZipFile2] )
  447. {
  448. zippath = @"";
  449. }
  450. return zippath;
  451. }
  452. +(NSString *) prepareUploadFile:(NSString*) file json:(NSMutableDictionary*)json
  453. {
  454. NSString* serial= [[NSUUID UUID] UUIDString];
  455. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  456. NSString *cachefolder = [paths objectAtIndex:0];
  457. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  458. upfolder = [upfolder stringByAppendingPathComponent:serial];
  459. NSFileManager* fileManager = [NSFileManager defaultManager];
  460. BOOL bdir=YES;
  461. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  462. {
  463. NSError *error = nil;
  464. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  465. if(!bsuccess)
  466. DebugLog(@"Create UPLOAD folder failed");
  467. }
  468. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  469. // ret[@"contact"]=[self prepareContact:serial];
  470. // ret[@"wishlist"]=[self prepareWishlist:serial];
  471. // ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  472. // ret[@"portfolio"] = [self preparePortfolio:serial];
  473. // ret[@"view_portfolio"] = [self preparePDF:serial];
  474. NSString* str= [RAUtils dict2string:json];
  475. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  476. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  477. NSError *error=nil;
  478. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  479. // NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  480. ZipArchive* zip = [[ZipArchive alloc] init];
  481. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  482. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  483. result = [zip addFileToZip:jsonpath newname:[jsonpath lastPathComponent]];
  484. if( ![zip CloseZipFile2] )
  485. {
  486. zippath = @"";
  487. }
  488. return zippath;
  489. }
  490. +(NSDictionary*)request_DocList:(NSMutableDictionary*) params
  491. {
  492. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  493. // if(appDelegate.user!=nil)
  494. // [params setValue:appDelegate.user forKey:@"user"];
  495. //
  496. //
  497. // if(appDelegate.password!=nil)
  498. // [params setValue:appDelegate.password forKey:@"password"];
  499. params[@"_operate"]=@"listIpad";
  500. if(![self IsNetworkAvailable])
  501. {
  502. DebugLog(@"NET ERR");
  503. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  504. // return nil;
  505. }
  506. NSData* json=[self get_json:URL_REQUEST_DOC parameters:params file:nil];
  507. //if(json==nil)
  508. if(json==nil)
  509. return nil;
  510. NSError *error=nil;
  511. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  512. return jsobj;
  513. }
  514. +(NSDictionary *) updateNotes:(NSString*) doc_number notes:(NSString*)notes
  515. {
  516. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  517. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  518. if(appDelegate.user!=nil)
  519. [params setValue:appDelegate.user forKey:@"user"];
  520. if(appDelegate.password!=nil)
  521. [params setValue:appDelegate.password forKey:@"password"];
  522. params[@"doc_number"]=doc_number;
  523. params[@"notes"]=notes;
  524. params[@"_operate"]=@"updateNotes";
  525. if(![self IsNetworkAvailable])
  526. {
  527. DebugLog(@"NET ERR");
  528. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  529. // return nil;
  530. }
  531. NSData* json=[self get_json:URL_UPDATE_NOTES parameters:params file:nil];
  532. //if(json==nil)
  533. if(json==nil)
  534. return nil;
  535. NSError *error=nil;
  536. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  537. int result = [[jsobj valueForKey:@"result"] intValue];
  538. DebugLog(@"new_document result:%d",result);
  539. return jsobj;
  540. }
  541. @end