RANetwork.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 "RANetwork.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 RANetwork
  16. //+(NSMutableDictionary*) prepare_additional_params:(NSMutableDictionary* ) params
  17. +(NSMutableDictionary*) prepare_additional_params:(NSMutableDictionary* ) params
  18. {
  19. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  20. if(RASingleton.sharedInstance.user!=nil)
  21. [params setValue:[AESCrypt AES128Encrypt:RASingleton.sharedInstance.user key:@"usai2010"] forKey:@"user"];
  22. if(RASingleton.sharedInstance.password!=nil)
  23. [params setValue:[AESCrypt AES128Encrypt:RASingleton.sharedInstance.password key:@"usai2010"] forKey:@"password"];
  24. [params setValue:[RAUtils device_info][@"build"] forKey:@"app_ver"];
  25. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  26. NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  27. [params setValue:short_version forKey:@"app_short_ver"];
  28. #if TARGET_IPHONE_SIMULATOR//模拟器
  29. [params setValue:@"simulator_uuid" forKey:@"deviceid"];
  30. #elif TARGET_OS_IPHONE//真机
  31. UIDevice * dev = [UIDevice currentDevice];
  32. NSUUID* uuid =dev.identifierForVendor;
  33. [params setValue:uuid.UUIDString forKey:@"deviceid"];
  34. #endif
  35. return params;
  36. }
  37. +(NSData*)get_json : (NSString*) url parameters:(NSMutableDictionary *) params file:(NSString *) file //delegate:(id < NSURLConnectionDelegate >)delegate
  38. {
  39. // NSString* e=[AESCrypt AES128Encrypt:@"密码学中的高级加密标准(Advanced Encryption Standard,AES),又称 高级加密标准Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院 (NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。 " key:@"usai2010"];
  40. // DebugLog(e);
  41. // NSString* d=[AESCrypt decrypt:@"VzB4+lENfbzWX7ggHh2Os1P69l5YBzKmM51yqA37AfoKgPY8bJgdK8M4WvF+wDyOsLWP\/o8H5+bHMivQp1u8wM2QV7SYzgzPwV85QKtYWIzEgLe9T0HO69EdutWz7k1rdAT\/mftsRin0Hy5SHk7txTZT\/zPH5X+FQiExCTFm5Zus39HYvp+VxCX4+kYeymn8B63AUd3mRQyscUvDjfgU2olKR\/TP8PV3g+VQMZxpKfol3P1iOtz3XQlHZV0pM6SAf+SwT0sPfrdn6CZFfNQlrKA9QjMrnBdMeSCquSIWXnmLv6okzvnlJnT0SjvJuUeqNdBC5EZ0ACnhI0MrXVZEsq1EuM9al7oPzvU5EZbjZpmx+fSFIkMzTT8bYClTnG1bsL1MCcU4pSODDorUj8zalA==" password:@"usai2010"];
  42. params = [self prepare_additional_params:params];
  43. return [super get_json:url parameters:params file:(NSString*)file err_recorder:URL_ERR_LOG result_handler:^NSMutableDictionary *(NSMutableDictionary *jsobj) {
  44. int result=[[jsobj valueForKey:@"result"] intValue];
  45. // UIApplication * app = [UIApplication sharedApplication];
  46. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  47. switch (result) {
  48. case 0:
  49. [jsobj setValue:MSG_USERAUTH_ERROR forKey:@"err_msg"];
  50. break;
  51. case 1:
  52. [jsobj setValue:MSG_ERROR forKey:@"err_msg"];
  53. break;
  54. case 2:
  55. [jsobj setValue:MSG_SUCCESS forKey:@"err_msg"];
  56. break;
  57. case 8:
  58. [jsobj setValue:[jsobj valueForKey:@"msg"] forKey:@"err_msg"];
  59. break;
  60. case 9:
  61. [jsobj setValue:[jsobj valueForKey:@"msg"] forKey:@"err_msg"];
  62. break;
  63. case 99:
  64. [jsobj setValue:MSG_TIMEOUT forKey:@"err_msg"];
  65. break;
  66. default:
  67. [jsobj setValue:MSG_ERROR forKey:@"err_msg"];
  68. break;
  69. }
  70. // if([appDelegate.build intValue]< [[jsobj valueForKey:@"min_ver"]intValue])
  71. // {
  72. // [jsobj setValue:@"9" forKey:@"result"];
  73. // [jsobj setValue:MSG_VER_LOW forKey:@"err_msg"];
  74. // }
  75. return jsobj;
  76. } decrypt_handler:^NSMutableDictionary *(NSMutableDictionary *jsobj) {
  77. NSString* base64str = jsobj[@"str"];
  78. NSString* decryptstr=[AESCrypt AES128Decrypt:base64str key:@"usai2010"];
  79. jsobj=[[RAConvertor string2dict:decryptstr] mutableCopy];
  80. return jsobj;
  81. }];
  82. /*
  83. */
  84. return nil;
  85. }
  86. //+(NSDictionary*)new_document
  87. //{
  88. //
  89. //
  90. //
  91. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  92. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  93. // if(RASingleton.sharedInstance.user!=nil)
  94. // [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  95. //
  96. //
  97. // if(RASingleton.sharedInstance.password!=nil)
  98. // [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  99. // params[@"_operate"]=@"doc_id";
  100. //
  101. // if(![self IsNetworkAvailable])
  102. // {
  103. // DebugLog(@"NET ERR");
  104. //
  105. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  106. // // return nil;
  107. // }
  108. // NSData* json=[self get_json:URL_NEW_DOCUMENT parameters:params file:nil];
  109. // //if(json==nil)
  110. //
  111. //
  112. // if(json==nil)
  113. // return nil;
  114. // NSError *error=nil;
  115. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  116. // int result = [[jsobj valueForKey:@"result"] intValue];
  117. // DebugLog(@"new_document result:%d",result);
  118. //
  119. // return jsobj;
  120. //}
  121. //+(NSDictionary*)upload_file:(NSString*)file params:(NSMutableDictionary*) params
  122. //{
  123. //
  124. //
  125. //
  126. // // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  127. // // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  128. // // if(appDelegate.user!=nil)
  129. // // [params setValue:appDelegate.user forKey:@"user"];
  130. // //
  131. // //
  132. // // if(appDelegate.password!=nil)
  133. // // [params setValue:appDelegate.password forKey:@"password"];
  134. // params[@"_operate"]=@"uploadFileIpad";
  135. //
  136. // if(![self IsNetworkAvailable])
  137. // {
  138. // DebugLog(@"NET ERR");
  139. //
  140. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  141. // // return nil;
  142. // }
  143. // NSData* json=[self get_json:URL_UPLOAD_PDF parameters:params file:file];
  144. // //if(json==nil)
  145. //
  146. //
  147. // if(json==nil)
  148. // return nil;
  149. // NSError *error=nil;
  150. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  151. // int result = [[jsobj valueForKey:@"result"] intValue];
  152. //
  153. // DebugLog(@"upload_file result:%d",result);
  154. // return jsobj;
  155. //}
  156. //+(NSDictionary*)save_document:(NSString*)file isnew:(bool)isNewDocument
  157. //{
  158. //
  159. //
  160. //
  161. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  162. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  163. //// if(appDelegate.user!=nil)
  164. //// [params setValue:appDelegate.user forKey:@"user"];
  165. ////
  166. ////
  167. //// if(appDelegate.password!=nil)
  168. //// [params setValue:appDelegate.password forKey:@"password"];
  169. // params[@"_operate"]=@"deviceMain";
  170. // params[@"modify_document"]=[NSNumber numberWithBool:!isNewDocument];
  171. // if(![self IsNetworkAvailable])
  172. // {
  173. // DebugLog(@"NET ERR");
  174. //
  175. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  176. // // return nil;
  177. // }
  178. // NSData* json=[self get_json:URL_UPLOAD_PDF parameters:params file:file];
  179. // //if(json==nil)
  180. //
  181. //
  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. //
  188. // DebugLog(@"new_document result:%d",result);
  189. // return jsobj;
  190. //}
  191. //+(NSDictionary*)logout
  192. //{
  193. //
  194. //
  195. //
  196. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  197. ////
  198. // if(RASingleton.sharedInstance.user!=nil)
  199. // [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  200. //
  201. //
  202. // if(RASingleton.sharedInstance.password!=nil)
  203. // [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  204. // params[@"_operate"]=@"logout_zip";
  205. //
  206. // if(![self IsNetworkAvailable])
  207. // {
  208. // DebugLog(@"NET ERR");
  209. //
  210. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  211. // // return nil;
  212. // }
  213. // NSData* json=[self get_json:URL_LOGOUT parameters:params file:nil];
  214. // //if(json==nil)
  215. //
  216. //
  217. // if(json==nil)
  218. // return nil;
  219. // NSError *error=nil;
  220. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  221. // int result = [[jsobj valueForKey:@"result"] intValue];
  222. //
  223. // if (result==2)
  224. // {
  225. //
  226. // dispatch_async(dispatch_get_main_queue(), ^{
  227. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  228. // [appDelegate Logout];
  229. // });
  230. //
  231. // }
  232. // return jsobj;
  233. //}
  234. //+(int)Authorize : (NSString*) user password:(NSString*) password{
  235. //
  236. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  237. // long ver =[defaults integerForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  238. //
  239. //
  240. //
  241. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  242. //
  243. //
  244. //
  245. // [params setValue:[AESCrypt AES128Encrypt:user key:@"usai2010"] forKey:@"user"];
  246. // [params setValue:[AESCrypt AES128Encrypt:password key:@"usai2010"] forKey:@"password"];
  247. // [params setValue:[NSString stringWithFormat:@"%ld",ver] forKey:@"auth_ver"];
  248. // [params setValue:@"login" forKey:@"_operate"];
  249. //
  250. // NSData* json= nil;
  251. //
  252. // if(![self IsNetworkAvailable])
  253. // return RESULT_NET_NOTAVAILABLE;
  254. // json =[self get_json:URL_UPDATE_AUTH parameters:params file:nil];
  255. //
  256. //
  257. //
  258. // if(json==nil)
  259. // return RESULT_NET_ERROR;
  260. //
  261. //
  262. // int ret = [self parse_authinfo:json user:user password:password];
  263. // DebugLog(@"parse_authinfo return %d ",ret);
  264. //
  265. //
  266. // return ret;
  267. //
  268. //
  269. //}
  270. //+(int)parse_authinfo : (NSData *) json user:(NSString*) user password:(NSString*) password
  271. //{
  272. // DebugLog(@"parse_authinfo");
  273. // NSError *error=nil;
  274. //
  275. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  276. // if(jsobj)
  277. // {
  278. //
  279. //
  280. // int result = [[jsobj valueForKey:@"result"] intValue];
  281. //
  282. // if (result<0)
  283. // return result;
  284. // if(result!=AP_USER_AUTH&& result!=9)
  285. // {
  286. //
  287. //
  288. // DebugLog(@"USER NOT AUTHORIZED CODE=%d ",result);
  289. //// [appDelegate Logout];
  290. // if(result==8)
  291. // {
  292. // return RESULT_LOGIN_DEVICE;
  293. // }
  294. // return RESULT_FALSE;
  295. //
  296. // }
  297. //
  298. // NSString* offPrefix = jsobj[@"randcode"];
  299. // bool skip_verify = [jsobj[@"skip_save_verify"] boolValue];
  300. //
  301. //
  302. //
  303. // NSString* agentName = jsobj[@"agent_name"];
  304. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  305. //
  306. //
  307. //// NSString* skip_save_verify = jsobj[@"skip_save_verify"];
  308. //
  309. //
  310. //
  311. // NSMutableDictionary* user_info = [NSMutableDictionary new];
  312. // user_info[@"prefix"]=offPrefix;
  313. // user_info[@"agent_name"]= agentName;
  314. //
  315. //
  316. //
  317. // [defaults setObject:user_info forKey:user];
  318. //
  319. //
  320. // [defaults synchronize];
  321. //
  322. //
  323. // dispatch_async(dispatch_get_main_queue(), ^{
  324. //
  325. // UIApplication * app = [UIApplication sharedApplication];
  326. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  327. // [appDelegate Login:user pwd:password skip_verify:skip_verify offPrefix:offPrefix agentName:agentName];
  328. //
  329. // });
  330. //
  331. //
  332. //
  333. // return RESULT_TRUE;
  334. //
  335. // }
  336. // // NSDictionary *weatherInfo = [jsobj objectForKey:@"weatherinfo"];
  337. // // txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
  338. // // DebugLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
  339. //
  340. //
  341. //
  342. // return RESULT_USERAUTH_ERROR;
  343. //}
  344. +(NSDictionary*)err_log:(NSString*) up_params result:(NSString*)result module:(NSString*) module code:(int) code//device:(NSString*) device
  345. {
  346. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  347. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  348. // if(appDelegate.user!=nil)
  349. // [params setValue:appDelegate.user forKey:@"user"];
  350. //
  351. // [params setValue:up_params forKey:@"upparam"];
  352. //
  353. // [params setValue:[NSNumber numberWithInt:code ] forKey:@"code"];
  354. //
  355. // [params setValue:result forKey:@"result"];
  356. // [params setValue:module forKey:@"interfac"];
  357. //
  358. // [params setValue:[RAUtils base64en:[RAUtils dict2string: [RAUtils device_info]]] forKey:@"device"];
  359. //
  360. //
  361. // // [params setValue:module forKey:@"module"];
  362. //
  363. // // [[UIDevice currentDevice] name];
  364. //
  365. // // if(appDelegate.offline_mode)
  366. // // {
  367. // // return [OLDataProvider offline_notimpl];
  368. // // }
  369. // if(![self IsNetworkAvailable])
  370. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  371. //
  372. // NSData* json=[self get_json:URL_ERR_LOG parameters:params];
  373. // if(json==nil)
  374. // return nil;
  375. // NSError *error=nil;
  376. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  377. // return jsobj;
  378. NSMutableDictionary* jsobj = [@{@"result":@"2"} mutableCopy];
  379. return jsobj;
  380. }
  381. +(NSString *) packfiles2zip:(NSMutableArray*) arr_file
  382. {
  383. if(arr_file.count==0)
  384. {
  385. DebugLog(@"packfiles2zip: source list is empty.");
  386. return @"";
  387. }
  388. NSString* serial= [[NSUUID UUID] UUIDString];
  389. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  390. NSString *cachefolder = [paths objectAtIndex:0];
  391. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  392. upfolder = [upfolder stringByAppendingPathComponent:serial];
  393. NSFileManager* fileManager = [NSFileManager defaultManager];
  394. BOOL bdir=YES;
  395. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  396. {
  397. NSError *error = nil;
  398. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  399. if(!bsuccess)
  400. {
  401. DebugLog(@"Create UPLOAD folder failed");
  402. return @"";
  403. }
  404. }
  405. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  406. ZipArchive* zip = [[ZipArchive alloc] init];
  407. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  408. for(int i=0;i<arr_file.count;i++)
  409. {
  410. NSString* file = arr_file[i];
  411. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  412. }
  413. if( ![zip CloseZipFile2] )
  414. {
  415. zippath = @"";
  416. }
  417. return zippath;
  418. }
  419. +(NSString *) prepareUploadFile:(NSString*) file json:(NSMutableDictionary*)json
  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. DebugLog(@"Create UPLOAD folder failed");
  434. }
  435. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  436. // ret[@"contact"]=[self prepareContact:serial];
  437. // ret[@"wishlist"]=[self prepareWishlist:serial];
  438. // ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  439. // ret[@"portfolio"] = [self preparePortfolio:serial];
  440. // ret[@"view_portfolio"] = [self preparePDF:serial];
  441. NSString* str= [RAConvertor dict2string:json];
  442. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  443. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  444. NSError *error=nil;
  445. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  446. // NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  447. ZipArchive* zip = [[ZipArchive alloc] init];
  448. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  449. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  450. result = [zip addFileToZip:jsonpath newname:[jsonpath lastPathComponent]];
  451. // for(int l=0;l<[json[@"count"] intValue];l++)
  452. // {
  453. //
  454. // NSMutableDictionary * page_json = json[[NSString stringWithFormat:@"page_%d",l] ];
  455. //
  456. // for(int p=0;p<[page_json[@"count"] intValue];p++)
  457. // {
  458. // NSMutableDictionary * control_json = page_json[[NSString stringWithFormat:@"control_%d",p] ];
  459. // if([control_json[@"type"] isEqualToString:@"Signature"] )
  460. // {
  461. // NSString* signature_path = control_json[@"value"];
  462. // if(signature_path.length>0)
  463. // result = [zip addFileToZip:signature_path newname:[signature_path lastPathComponent]];
  464. // }
  465. // }
  466. // }
  467. if( ![zip CloseZipFile2] )
  468. {
  469. zippath = @"";
  470. }
  471. return zippath;
  472. }
  473. //
  474. //+(NSDictionary*)request_DocList:(NSMutableDictionary*) params
  475. //{
  476. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  477. //// if(appDelegate.user!=nil)
  478. //// [params setValue:appDelegate.user forKey:@"user"];
  479. ////
  480. ////
  481. //// if(appDelegate.password!=nil)
  482. //// [params setValue:appDelegate.password forKey:@"password"];
  483. // params[@"_operate"]=@"listIpad";
  484. //
  485. // if(![self IsNetworkAvailable])
  486. // {
  487. // DebugLog(@"NET ERR");
  488. //
  489. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  490. // // return nil;
  491. // }
  492. // NSData* json=[self get_json:URL_REQUEST_DOC parameters:params file:nil];
  493. // //if(json==nil)
  494. //
  495. //
  496. // if(json==nil)
  497. // return nil;
  498. // NSError *error=nil;
  499. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  500. //
  501. // return jsobj;
  502. //}
  503. //+(NSDictionary *) updateNotes:(NSString*) doc_number notes:(NSString*)notes
  504. //{
  505. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  506. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  507. // if(RASingleton.sharedInstance.user!=nil)
  508. // [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  509. //
  510. //
  511. // if(RASingleton.sharedInstance.password!=nil)
  512. // [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  513. //
  514. // params[@"doc_number"]=doc_number;
  515. // params[@"notes"]=notes;
  516. //
  517. // params[@"_operate"]=@"updateNotes";
  518. //
  519. // if(![self IsNetworkAvailable])
  520. // {
  521. // DebugLog(@"NET ERR");
  522. //
  523. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  524. // // return nil;
  525. // }
  526. // NSData* json=[self get_json:URL_UPDATE_NOTES parameters:params file:nil];
  527. // //if(json==nil)
  528. //
  529. //
  530. // if(json==nil)
  531. // return nil;
  532. // NSError *error=nil;
  533. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  534. // int result = [[jsobj valueForKey:@"result"] intValue];
  535. // DebugLog(@"new_document result:%d",result);
  536. //
  537. // return jsobj;
  538. //
  539. //}
  540. +(NSString *) getjsonPath:(NSString*) serial
  541. {
  542. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  543. NSString *cachefolder = [paths objectAtIndex:0];
  544. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  545. upfolder = [upfolder stringByAppendingPathComponent:serial];
  546. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  547. return jsonpath;
  548. }
  549. +(NSString *) getPDFPath:(NSString*) file
  550. {
  551. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  552. NSString *saveDirectory=[paths objectAtIndex:0];
  553. NSString *saveFileName=file ;
  554. NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  555. DebugLog(@"%@",newFilePath);
  556. return newFilePath;
  557. }
  558. +(NSString *) offline_docnumber
  559. {
  560. // return @"prefix123";
  561. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  562. if( RASingleton.sharedInstance.user==nil)
  563. return nil;
  564. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  565. NSDictionary* user_info = [defaults dictionaryForKey:RASingleton.sharedInstance.user];
  566. NSString* prefix=user_info[@"prefix"];
  567. NSString* agentname=user_info[@"agent_name"];
  568. RASingleton.sharedInstance.agentName = agentname;
  569. if(prefix==nil)
  570. return nil;
  571. long docint=[defaults integerForKey:prefix];
  572. docint++;
  573. [defaults removeObjectForKey:prefix];
  574. [defaults setInteger:docint forKey:prefix];
  575. [defaults synchronize];
  576. return [NSString stringWithFormat:@"%@%06ld",prefix,docint ];
  577. }
  578. //+ (int)heartBeat {
  579. //
  580. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  581. //
  582. // [params setObject:@"getStatus" forKey:@"_operate"];
  583. //
  584. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  585. //
  586. //// if(appDelegate.user!=nil)
  587. //// [params setValue:[AESCrypt AES128Encrypt:appDelegate.user key:@"usai2010"] forKey:@"user"];
  588. //// if(appDelegate.password!=nil)
  589. //// [params setValue:[AESCrypt AES128Encrypt:appDelegate.password key:@"usai2010"] forKey:@"password"];
  590. //
  591. // if(RASingleton.sharedInstance.user!=nil)
  592. // [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  593. // if(RASingleton.sharedInstance.password!=nil)
  594. // [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  595. //
  596. ////
  597. //// NSString *DeviceToken = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  598. //// [params setValue:DeviceToken forKey:@"deviceid"];
  599. //
  600. //
  601. // NSData* json=[self get_json:URL_HEART_BEAT parameters:params file:nil];
  602. // if(json==nil)
  603. // return -1;
  604. // NSError *error=nil;
  605. // NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  606. //
  607. // return [[resultDic objectForKey:@"result"] intValue];
  608. //}
  609. #pragma mark new network
  610. +(void)request_new_document : (NSMutableDictionary*) params completionHandler:(resultHandler)result
  611. {
  612. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  613. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  614. // if(RASingleton.sharedInstance.user!=nil)
  615. // [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  616. //
  617. //
  618. // if(RASingleton.sharedInstance.password!=nil)
  619. // [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  620. params[@"_operate"]=@"doc_id";
  621. // if(![self IsNetworkAvailable])
  622. // {
  623. // DebugLog(@"NET ERR");
  624. //
  625. // result ([[RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil] mutableCopy]);
  626. //
  627. // return;
  628. // // return nil;
  629. // }
  630. [self request_interface:URL_NEW_DOCUMENT parameters:params err_record_url:nil completionHandler:result retry:0];
  631. // NSData* json=[self get_json:URL_NEW_DOCUMENT parameters:params file:nil];
  632. //if(json==nil)
  633. //
  634. // if(json==nil)
  635. // return nil;
  636. // NSError *error=nil;
  637. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  638. // int result = [[jsobj valueForKey:@"result"] intValue];
  639. // DebugLog(@"new_document result:%d",result);
  640. //
  641. // return jsobj;
  642. }
  643. +(void)request_login : (NSString*) user password:(NSString*) password completionHandler:(resultHandler)result
  644. {
  645. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  646. long ver =[defaults integerForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  647. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  648. [params setValue:[AESCrypt AES128Encrypt:user key:@"usai2010"] forKey:@"user"];
  649. [params setValue:[AESCrypt AES128Encrypt:password key:@"usai2010"] forKey:@"password"];
  650. [params setValue:[NSString stringWithFormat:@"%ld",ver] forKey:@"auth_ver"];
  651. [params setValue:@"login" forKey:@"_operate"];
  652. [self request_interface:URL_UPDATE_AUTH parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *chresult) {
  653. int ret = [self parse_logininfo:chresult user:user password:password] ;
  654. NSMutableDictionary * retjson = [NSMutableDictionary new];
  655. retjson[@"result"]= [NSNumber numberWithInt:ret];
  656. result(retjson);
  657. } retry:0];
  658. }
  659. +(int)parse_logininfo: (NSMutableDictionary *) jsobj user:(NSString*) user password:(NSString*) password
  660. {
  661. DebugLog(@"parse_authinfo");
  662. // NSError *error=nil;
  663. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  664. if(jsobj)
  665. {
  666. int result = [[jsobj valueForKey:@"result"] intValue];
  667. if (result<0)
  668. return result;
  669. if(result!=AP_USER_AUTH&& result!=9)
  670. {
  671. DebugLog(@"USER NOT AUTHORIZED CODE=%d ",result);
  672. // [appDelegate Logout];
  673. if(result==8)
  674. {
  675. return RESULT_LOGIN_DEVICE;
  676. }
  677. return RESULT_FALSE;
  678. }
  679. NSString* offPrefix = jsobj[@"randcode"];
  680. bool skip_verify = [jsobj[@"skip_save_verify"] boolValue];
  681. NSString* agentName = jsobj[@"agent_name"];
  682. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  683. // NSString* skip_save_verify = jsobj[@"skip_save_verify"];
  684. NSMutableDictionary* user_info = [NSMutableDictionary new];
  685. user_info[@"prefix"]=offPrefix;
  686. user_info[@"agent_name"]= agentName;
  687. [defaults setObject:user_info forKey:user];
  688. [defaults synchronize];
  689. UIApplication * app = [UIApplication sharedApplication];
  690. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  691. [appDelegate Login:user pwd:password skip_verify:skip_verify offPrefix:offPrefix agentName:agentName];
  692. return RESULT_TRUE;
  693. }
  694. // NSDictionary *weatherInfo = [jsobj objectForKey:@"weatherinfo"];
  695. // txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
  696. // DebugLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
  697. return RESULT_USERAUTH_ERROR;
  698. }
  699. +(void)request_logout:(resultHandler)result
  700. {
  701. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  702. //
  703. if(RASingleton.sharedInstance.user!=nil)
  704. [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  705. if(RASingleton.sharedInstance.password!=nil)
  706. [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  707. params[@"_operate"]=@"logout_zip";
  708. [self request_interface:URL_LOGOUT parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *resulti) {
  709. NSMutableDictionary* jsobj=resulti;
  710. int result_code = [[jsobj valueForKey:@"result"] intValue];
  711. if (result_code==2)
  712. {
  713. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  714. [appDelegate Logout];
  715. }
  716. result(resulti);
  717. } retry:0];
  718. }
  719. +(void)request_doc_list:(NSMutableDictionary*) params completionHandler:(resultHandler)result
  720. {
  721. params[@"_operate"]=@"listIpad";
  722. [self request_interface:URL_REQUEST_DOC parameters:params err_record_url:nil completionHandler:^(NSMutableDictionary *chresult) {
  723. NSString* base64str = chresult[@"str"];
  724. NSString* decryptstr=[AESCrypt AES128Decrypt:base64str key:@"usai2010"];
  725. chresult=[[RAConvertor string2dict:decryptstr] mutableCopy];
  726. result(chresult);
  727. } retry:0];
  728. }
  729. +(void)request_save_document:(NSString*)file isnew:(bool)isNewDocument completionHandler:(resultHandler)result
  730. {
  731. // file
  732. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  733. params[@"_operate"]=@"deviceMain";
  734. params[@"modify_document"]=[NSNumber numberWithBool:!isNewDocument];
  735. params=[self prepare_additional_params:params];
  736. // [self request_interface:URL_UPLOAD_PDF parameters:params err_record_url:nil completionHandler:result retry:0];
  737. NSData* data = [NSData dataWithContentsOfFile:file];
  738. NSString* filename = [file lastPathComponent];
  739. [self request_upload_fast:data FileName:filename server:URL_UPLOAD_PDF parameters:params completionHandler:result];
  740. //if(json==nil)
  741. }
  742. +(void) request_update_notes:(NSString*) doc_number notes:(NSString*)notes completionHandler:(resultHandler)result
  743. {
  744. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  745. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  746. if(RASingleton.sharedInstance.user!=nil)
  747. [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  748. if(RASingleton.sharedInstance.password!=nil)
  749. [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  750. params[@"doc_number"]=doc_number;
  751. params[@"notes"]=notes;
  752. params[@"_operate"]=@"updateNotes";
  753. [self request_interface:URL_UPDATE_NOTES parameters:params err_record_url:nil completionHandler:result retry:0];
  754. //if(json==nil)
  755. }
  756. + (void)request_heart_beat:(resultHandler)result
  757. {
  758. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  759. [params setObject:@"getStatus" forKey:@"_operate"];
  760. if(RASingleton.sharedInstance.user!=nil)
  761. [params setValue:RASingleton.sharedInstance.user forKey:@"user"];
  762. if(RASingleton.sharedInstance.password!=nil)
  763. [params setValue:RASingleton.sharedInstance.password forKey:@"password"];
  764. [self request_interface:URL_HEART_BEAT parameters:params err_record_url:nil completionHandler:result retry:0];
  765. }
  766. + (void)request_upload_file:(NSString*)file params:(NSMutableDictionary*) params completionHandler:(resultHandler)result
  767. {
  768. params[@"_operate"]=@"uploadFileIpad";
  769. params=[self prepare_additional_params:params];
  770. // [self request_interface:URL_UPLOAD_PDF parameters:params err_record_url:nil completionHandler:result retry:0];
  771. NSData* data = [NSData dataWithContentsOfFile:file];
  772. NSString* filename = [file lastPathComponent];
  773. [self request_upload_fast:data FileName:filename server:URL_UPLOAD_PDF parameters:params completionHandler:result];
  774. }
  775. @end