ACNetwork.m 30 KB

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