ACNetwork.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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=[[RAConvertor 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. NSString* agentName = jsobj[@"agent_name"];
  254. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  255. NSMutableDictionary* user_info = [NSMutableDictionary new];
  256. user_info[@"prefix"]=offPrefix;
  257. user_info[@"agent_name"]= agentName;
  258. [defaults setObject:user_info forKey:user];
  259. // if(offPrefix!=nil)
  260. // {
  261. // [defaults setValue:offPrefix forKey:user];
  262. //
  263. //
  264. // }
  265. // if(agentName!=nil)
  266. // {
  267. // [defaults setValue:agentName forKey:@"agent_name"];
  268. //
  269. //
  270. // }
  271. [defaults synchronize];
  272. // NSDictionary* objheader = [jsobj objectForKey:@"header"];
  273. //
  274. // NSString* required_ver=[jsobj objectForKey:@"min_ver"] ;
  275. // user=[objheader objectForKey:@"username"] ;
  276. // DebugLog(@"required_ver=%@ ",required_ver);
  277. // // NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  278. // // NSString* versionNum =[infoDict valueForKey:@"CFBundleVersion"];
  279. // // versionNum = [NSString stringWithFormat:@"A%@",versionNum];
  280. // int ibadge = [[objheader valueForKey:@"badge"] intValue];
  281. //
  282. // // if([ibadge isEqual:[NSNull null]])
  283. // // strbadge=@"0";
  284. // // if(ibadge==nil)
  285. // // ibadge=@"0";
  286. // // if([strbadge isEqualToString:@"null"])
  287. // // strbadge=@"0";
  288. // app.applicationIconBadgeNumber = ibadge;//[strbadge intValue];
  289. //
  290. // // BOOL bigger = [appDelegate.build compare:required_ver] ;
  291. // if([appDelegate.build intValue]<[required_ver intValue])
  292. // return RESULT_VER_LOW;
  293. // // appDelegate.sessionid = [objheader valueForKey:@"sessionid"];
  294. // appDelegate.user = user;
  295. // appDelegate.password = password;
  296. //// appDelegate.user_type = [[objheader valueForKey:@"user_type"] intValue];
  297. //// appDelegate.user_icon =[objheader valueForKey:@"user_img"] ;
  298. // appDelegate.bLogin = true;
  299. [appDelegate Login:user pwd:password canModify:canModify offPrefix:offPrefix agentName:agentName];
  300. //
  301. // if(appDelegate.user_type==USER_ROLE_CUSTOMER)
  302. // {
  303. // appDelegate.customerInfo = [[objheader objectForKeyedSubscript:@"customerInfo"] mutableCopy];
  304. // appDelegate.contact_id = [objheader valueForKey:@"contact_id"];
  305. // // appDelegate.order_code =[objheader valueForKey:@"orderCode"];
  306. // // appDelegate.order_status =[[objheader valueForKey:@"orderStatus"] intValue];
  307. // // [appDelegate SetSo:[objheader valueForKey:@"soId"]];
  308. // }
  309. // NSString* mode =[jsobj valueForKey:@"mode"] ;
  310. // [appDelegate SetMode:mode];
  311. //
  312. //
  313. // appDelegate.cart_count = [[objheader valueForKey:@"cart_count"] intValue];
  314. // appDelegate.wish_count =[[objheader valueForKey:@"wish_count"] intValue];
  315. // appDelegate.port_count =[[objheader valueForKey:@"portfolio_count"] intValue];
  316. //
  317. // [appDelegate update_count_mark];
  318. //
  319. //
  320. //
  321. // appDelegate.can_show_price =[[objheader valueForKey:@"can_show_price"] boolValue];
  322. // appDelegate.can_see_price =[[objheader valueForKey:@"can_see_price"] boolValue];
  323. // appDelegate.can_create_portfolio =[[objheader valueForKey:@"can_create_portfolio"] boolValue];
  324. // appDelegate.can_create_order =[[objheader valueForKey:@"can_create_order"] boolValue];
  325. //
  326. // appDelegate.can_cancel_order =[[objheader valueForKey:@"can_cancel_order"] boolValue];
  327. // appDelegate.can_set_cart_price =[[objheader valueForKey:@"can_set_cart_price"] boolValue];
  328. // appDelegate.can_delete_order =[[objheader valueForKey:@"can_delete_order"] boolValue];
  329. // appDelegate.can_update_contact_info =[[objheader valueForKey:@"can_update_contact_info"] boolValue];
  330. // appDelegate.can_submit_order =[[objheader valueForKey:@"can_submit_order"] boolValue];
  331. // appDelegate.can_set_tearsheet_price =[[objheader valueForKey:@"can_set_tearsheet_price"] boolValue];
  332. //
  333. // appDelegate.save_order_logout =[[objheader valueForKey:@"save_order_logout"] boolValue];
  334. // appDelegate.submit_order_logout =[[objheader valueForKey:@"submit_order_logout"] boolValue];
  335. //
  336. // appDelegate.alert_sold_in_quantities = [[objheader valueForKey:@"alert_sold_in_quantities"] boolValue];
  337. //
  338. //
  339. //
  340. // appDelegate.ipad_perm =[objheader valueForKey:@"ipad_perm"] ;
  341. //
  342. // NSString* strfilter = [objheader valueForKey:@"statusFilter"] ;
  343. //
  344. //
  345. //
  346. // appDelegate.OrderFilter= [[RAUtils string2dict:strfilter] mutableCopy];
  347. //
  348. //
  349. // [appDelegate set_main_button_panel];
  350. //
  351. // if(appDelegate.user_type==USER_ROLE_CUSTOMER) {
  352. //
  353. // // shop order status filter
  354. // NSString *shopOrderStatusFilterStr = [objheader valueForKey:@"shopOrderFilter"] ;
  355. // [Singleton sharedInstance].shop_order_status_filter = [[RAUtils string2dict:shopOrderStatusFilterStr] mutableCopy];
  356. //
  357. // // specialInstruction
  358. // [Singleton sharedInstance].specialInstruction = [objheader valueForKey:@"specialInstruction"];
  359. //
  360. // //-价格类型
  361. // [Singleton sharedInstance].npd_shop_price_type = [[objheader valueForKey:@"price_type"] integerValue];
  362. //
  363. // // 权限
  364. // [Singleton sharedInstance].permissions_price_setting = [[objheader valueForKey:@"can_set_price_formula"] boolValue];
  365. // [Singleton sharedInstance].permissions_edit_order = [[objheader valueForKey:@"can_place_order"] boolValue];
  366. //
  367. // [Singleton sharedInstance].permissions_submit_order = [[objheader valueForKey:@"can_submit_order"] boolValue];
  368. //
  369. // [Singleton sharedInstance].permissions_merge_order = [[objheader valueForKey:@"can_merge_order"] boolValue];
  370. //
  371. // [Singleton sharedInstance].deliveryString = [objheader valueForKey:@"delivery_price"];
  372. // [[Singleton sharedInstance] resetGlobalLock];
  373. // [Singleton sharedInstance].customer_can_see_sales_Order = [[objheader valueForKey:@"can_see_salesorder"] boolValue];
  374. // }
  375. // DebugLog(@"sessionid=%@ ",appDelegate.sessionid);
  376. // if ([[objheader valueForKey:@"update"] boolValue]==false)
  377. // {
  378. // // no update on the server;
  379. // return RESULT_TRUE;
  380. // }
  381. // int Auth_InfoVer = [[objheader valueForKey:@"ver"] intValue];
  382. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  383. // [defaults removeObjectForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  384. // [defaults setInteger:Auth_InfoVer forKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  385. // [defaults synchronize];
  386. //
  387. // NSDictionary* objfuncs = [jsobj objectForKey:@"functions"];
  388. // NSArray* keys= [objfuncs allKeys];
  389. /*
  390. db.setTransactionSuccessful();
  391. db.endTransaction();
  392. */
  393. return RESULT_TRUE;
  394. }
  395. // NSDictionary *weatherInfo = [jsobj objectForKey:@"weatherinfo"];
  396. // txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
  397. // DebugLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
  398. return RESULT_USERAUTH_ERROR;
  399. }
  400. +(NSDictionary*)err_log:(NSString*) up_params result:(NSString*)result module:(NSString*) module code:(int) code//device:(NSString*) device
  401. {
  402. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  403. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  404. // if(appDelegate.user!=nil)
  405. // [params setValue:appDelegate.user forKey:@"user"];
  406. //
  407. // [params setValue:up_params forKey:@"upparam"];
  408. //
  409. // [params setValue:[NSNumber numberWithInt:code ] forKey:@"code"];
  410. //
  411. // [params setValue:result forKey:@"result"];
  412. // [params setValue:module forKey:@"interfac"];
  413. //
  414. // [params setValue:[RAUtils base64en:[RAUtils dict2string: [RAUtils device_info]]] forKey:@"device"];
  415. //
  416. //
  417. // // [params setValue:module forKey:@"module"];
  418. //
  419. // // [[UIDevice currentDevice] name];
  420. //
  421. // // if(appDelegate.offline_mode)
  422. // // {
  423. // // return [OLDataProvider offline_notimpl];
  424. // // }
  425. // if(![self IsNetworkAvailable])
  426. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  427. //
  428. // NSData* json=[self get_json:URL_ERR_LOG parameters:params];
  429. // if(json==nil)
  430. // return nil;
  431. // NSError *error=nil;
  432. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  433. // return jsobj;
  434. NSMutableDictionary* jsobj = [@{@"result":@"2"} mutableCopy];
  435. return jsobj;
  436. }
  437. +(NSString *) packfiles2zip:(NSMutableArray*) arr_file
  438. {
  439. if(arr_file.count==0)
  440. {
  441. DebugLog(@"packfiles2zip: source list is empty.");
  442. return @"";
  443. }
  444. NSString* serial= [[NSUUID UUID] UUIDString];
  445. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  446. NSString *cachefolder = [paths objectAtIndex:0];
  447. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  448. upfolder = [upfolder stringByAppendingPathComponent:serial];
  449. NSFileManager* fileManager = [NSFileManager defaultManager];
  450. BOOL bdir=YES;
  451. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  452. {
  453. NSError *error = nil;
  454. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  455. if(!bsuccess)
  456. {
  457. DebugLog(@"Create UPLOAD folder failed");
  458. return @"";
  459. }
  460. }
  461. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  462. ZipArchive* zip = [[ZipArchive alloc] init];
  463. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  464. for(int i=0;i<arr_file.count;i++)
  465. {
  466. NSString* file = arr_file[i];
  467. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  468. }
  469. if( ![zip CloseZipFile2] )
  470. {
  471. zippath = @"";
  472. }
  473. return zippath;
  474. }
  475. +(NSString *) prepareUploadFile:(NSString*) file json:(NSMutableDictionary*)json
  476. {
  477. NSString* serial= [[NSUUID UUID] UUIDString];
  478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  479. NSString *cachefolder = [paths objectAtIndex:0];
  480. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  481. upfolder = [upfolder stringByAppendingPathComponent:serial];
  482. NSFileManager* fileManager = [NSFileManager defaultManager];
  483. BOOL bdir=YES;
  484. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  485. {
  486. NSError *error = nil;
  487. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  488. if(!bsuccess)
  489. DebugLog(@"Create UPLOAD folder failed");
  490. }
  491. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  492. // ret[@"contact"]=[self prepareContact:serial];
  493. // ret[@"wishlist"]=[self prepareWishlist:serial];
  494. // ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  495. // ret[@"portfolio"] = [self preparePortfolio:serial];
  496. // ret[@"view_portfolio"] = [self preparePDF:serial];
  497. NSString* str= [RAConvertor dict2string:json];
  498. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  499. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  500. NSError *error=nil;
  501. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  502. // NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  503. ZipArchive* zip = [[ZipArchive alloc] init];
  504. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  505. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  506. result = [zip addFileToZip:jsonpath newname:[jsonpath lastPathComponent]];
  507. // for(int l=0;l<[json[@"count"] intValue];l++)
  508. // {
  509. //
  510. // NSMutableDictionary * page_json = json[[NSString stringWithFormat:@"page_%d",l] ];
  511. //
  512. // for(int p=0;p<[page_json[@"count"] intValue];p++)
  513. // {
  514. // NSMutableDictionary * control_json = page_json[[NSString stringWithFormat:@"control_%d",p] ];
  515. // if([control_json[@"type"] isEqualToString:@"Signature"] )
  516. // {
  517. // NSString* signature_path = control_json[@"value"];
  518. // if(signature_path.length>0)
  519. // result = [zip addFileToZip:signature_path newname:[signature_path lastPathComponent]];
  520. // }
  521. // }
  522. // }
  523. if( ![zip CloseZipFile2] )
  524. {
  525. zippath = @"";
  526. }
  527. return zippath;
  528. }
  529. +(NSDictionary*)request_DocList:(NSMutableDictionary*) params
  530. {
  531. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  532. // if(appDelegate.user!=nil)
  533. // [params setValue:appDelegate.user forKey:@"user"];
  534. //
  535. //
  536. // if(appDelegate.password!=nil)
  537. // [params setValue:appDelegate.password forKey:@"password"];
  538. params[@"_operate"]=@"listIpad";
  539. if(![self IsNetworkAvailable])
  540. {
  541. DebugLog(@"NET ERR");
  542. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  543. // return nil;
  544. }
  545. NSData* json=[self get_json:URL_REQUEST_DOC parameters:params file:nil];
  546. //if(json==nil)
  547. if(json==nil)
  548. return nil;
  549. NSError *error=nil;
  550. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  551. return jsobj;
  552. }
  553. +(NSDictionary *) updateNotes:(NSString*) doc_number notes:(NSString*)notes
  554. {
  555. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  556. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  557. if(appDelegate.user!=nil)
  558. [params setValue:appDelegate.user forKey:@"user"];
  559. if(appDelegate.password!=nil)
  560. [params setValue:appDelegate.password forKey:@"password"];
  561. params[@"doc_number"]=doc_number;
  562. params[@"notes"]=notes;
  563. params[@"_operate"]=@"updateNotes";
  564. if(![self IsNetworkAvailable])
  565. {
  566. DebugLog(@"NET ERR");
  567. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  568. // return nil;
  569. }
  570. NSData* json=[self get_json:URL_UPDATE_NOTES parameters:params file:nil];
  571. //if(json==nil)
  572. if(json==nil)
  573. return nil;
  574. NSError *error=nil;
  575. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  576. int result = [[jsobj valueForKey:@"result"] intValue];
  577. DebugLog(@"new_document result:%d",result);
  578. return jsobj;
  579. }
  580. +(NSString *) getjsonPath:(NSString*) serial
  581. {
  582. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  583. NSString *cachefolder = [paths objectAtIndex:0];
  584. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  585. upfolder = [upfolder stringByAppendingPathComponent:serial];
  586. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  587. return jsonpath;
  588. }
  589. +(NSString *) getPDFPath:(NSString*) file
  590. {
  591. NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  592. NSString *saveDirectory=[paths objectAtIndex:0];
  593. NSString *saveFileName=file ;
  594. NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  595. NSLog(@"%@",newFilePath);
  596. return newFilePath;
  597. }
  598. +(NSString *) offline_docnumber
  599. {
  600. // return @"prefix123";
  601. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  602. if( appDelegate.user==nil)
  603. return nil;
  604. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  605. NSDictionary* user_info = [defaults dictionaryForKey:appDelegate.user];
  606. NSString* prefix=user_info[@"prefix"];
  607. NSString* agentname=user_info[@"agent_name"];
  608. appDelegate.agentName = agentname;
  609. if(prefix==nil)
  610. return nil;
  611. long docint=[defaults integerForKey:prefix];
  612. docint++;
  613. [defaults removeObjectForKey:prefix];
  614. [defaults setInteger:docint forKey:prefix];
  615. [defaults synchronize];
  616. return [NSString stringWithFormat:@"%@%06ld",prefix,docint ];
  617. }
  618. + (int)heartBeat {
  619. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  620. [params setObject:@"getStatus" forKey:@"_operate"];
  621. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  622. // if(appDelegate.user!=nil)
  623. // [params setValue:[AESCrypt AES128Encrypt:appDelegate.user key:@"usai2010"] forKey:@"user"];
  624. // if(appDelegate.password!=nil)
  625. // [params setValue:[AESCrypt AES128Encrypt:appDelegate.password key:@"usai2010"] forKey:@"password"];
  626. if(appDelegate.user!=nil)
  627. [params setValue:appDelegate.user forKey:@"user"];
  628. if(appDelegate.password!=nil)
  629. [params setValue:appDelegate.password forKey:@"password"];
  630. //
  631. // NSString *DeviceToken = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  632. // [params setValue:DeviceToken forKey:@"deviceid"];
  633. NSData* json=[self get_json:URL_HEART_BEAT parameters:params file:nil];
  634. if(json==nil)
  635. return -1;
  636. NSError *error=nil;
  637. NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  638. return [[resultDic objectForKey:@"result"] intValue];
  639. }
  640. @end