ACNetwork.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. @implementation ACNetwork
  14. +(NSMutableDictionary*) prepare_addtional_params:(NSMutableDictionary* ) params
  15. {
  16. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  17. if(appDelegate.user!=nil)
  18. [params setValue:appDelegate.user forKey:@"user"];
  19. if(appDelegate.password!=nil)
  20. [params setValue:appDelegate.password forKey:@"password"];
  21. [params setValue:appDelegate.build forKey:@"app_ver"];
  22. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  23. NSString* short_version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  24. [params setValue:short_version forKey:@"app_short_ver"];
  25. UIDevice * dev = [UIDevice currentDevice];
  26. NSUUID* uuid =dev.identifierForVendor;
  27. #if TARGET_IPHONE_SIMULATOR//模拟器
  28. [params setValue:@"simulator_uuid" forKey:@"deviceid"];
  29. #elif TARGET_OS_IPHONE//真机
  30. [params setValue:uuid.UUIDString forKey:@"deviceid"];
  31. #endif
  32. return params;
  33. }
  34. +(NSData*)get_json : (NSString*) url parameters:(NSMutableDictionary *) params //delegate:(id < NSURLConnectionDelegate >)delegate
  35. {
  36. params = [self prepare_addtional_params:params];
  37. return [super get_json:url parameters:params err_recorder:URL_ERR_LOG result_handler:^NSMutableDictionary *(NSMutableDictionary *jsobj) {
  38. int result=[[jsobj valueForKey:@"result"] intValue];
  39. // UIApplication * app = [UIApplication sharedApplication];
  40. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  41. switch (result) {
  42. case 0:
  43. [jsobj setValue:MSG_USERAUTH_ERROR forKey:@"err_msg"];
  44. break;
  45. case 1:
  46. [jsobj setValue:MSG_ERROR forKey:@"err_msg"];
  47. break;
  48. case 2:
  49. [jsobj setValue:MSG_SUCCESS forKey:@"err_msg"];
  50. break;
  51. case 8:
  52. [jsobj setValue:[jsobj valueForKey:@"msg"] forKey:@"err_msg"];
  53. break;
  54. case 9:
  55. [jsobj setValue:[jsobj valueForKey:@"msg"] forKey:@"err_msg"];
  56. break;
  57. case 99:
  58. [jsobj setValue:MSG_TIMEOUT forKey:@"err_msg"];
  59. break;
  60. default:
  61. [jsobj setValue:MSG_ERROR forKey:@"err_msg"];
  62. break;
  63. }
  64. // if([appDelegate.build intValue]< [[jsobj valueForKey:@"min_ver"]intValue])
  65. // {
  66. // [jsobj setValue:@"9" forKey:@"result"];
  67. // [jsobj setValue:MSG_VER_LOW forKey:@"err_msg"];
  68. // }
  69. return jsobj;
  70. }];
  71. /*
  72. */
  73. return nil;
  74. }
  75. +(NSDictionary*)logout
  76. {
  77. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  78. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  79. if(appDelegate.user!=nil)
  80. [params setValue:appDelegate.user forKey:@"user"];
  81. if(appDelegate.password!=nil)
  82. [params setValue:appDelegate.password forKey:@"password"];
  83. params[@"_operate"]=@"logout_zip";
  84. if(![self IsNetworkAvailable])
  85. {
  86. DebugLog(@"NET ERR");
  87. return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  88. // return nil;
  89. }
  90. NSData* json=[self get_json:URL_LOGOUT parameters:params];
  91. //if(json==nil)
  92. if(json==nil)
  93. return nil;
  94. NSError *error=nil;
  95. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  96. int result = [[jsobj valueForKey:@"result"] intValue];
  97. if (result==2)
  98. {
  99. [appDelegate Logout];
  100. }
  101. return jsobj;
  102. }
  103. +(int)Authorize : (NSString*) user password:(NSString*) password{
  104. // if(![ApexMobileNetwork IsHostAvailable:URL_UPDATE_AUTH])
  105. // return RESULT_NET_ERROR;
  106. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  107. long ver =[defaults integerForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  108. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  109. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  110. // if(appDelegate.duid !=nil)
  111. // [params setValue:appDelegate.duid forKey:@"udid"];
  112. // [params setValue:@"handset_login" forKey:@"action"];
  113. // [headers setValue:[NSString stringWithFormat:@"%d",dataLength] forKey:@"Content-Length"];
  114. [params setValue:user forKey:@"user"];
  115. [params setValue:password forKey:@"password"];
  116. [params setValue:[NSString stringWithFormat:@"%ld",ver] forKey:@"auth_ver"];
  117. [params setValue:@"login" forKey:@"_operate"];
  118. //#ifdef TEST
  119. // [params setValue:@"true" forKey:@"is_debug"];
  120. //#else
  121. // [params setValue:@"false" forKey:@"is_debug"];
  122. //#endif
  123. NSData* json= nil;
  124. // if(appDelegate.offline_mode)
  125. // {
  126. // json= [OLDataProvider offline_login:params] ;
  127. //
  128. // }
  129. // else
  130. if(![self IsNetworkAvailable])
  131. return RESULT_NET_NOTAVAILABLE;
  132. json =[self get_json:URL_UPDATE_AUTH parameters:params];
  133. if(json==nil)
  134. return RESULT_NET_ERROR;
  135. int ret = [self parse_authinfo:json user:user password:password];
  136. DebugLog(@"parse_authinfo return %d ",ret);
  137. return ret;
  138. }
  139. +(int)parse_authinfo : (NSData *) json user:(NSString*) user password:(NSString*) password
  140. {
  141. DebugLog(@"parse_authinfo");
  142. NSError *error=nil;
  143. NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  144. if(jsobj)
  145. {
  146. UIApplication * app = [UIApplication sharedApplication];
  147. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  148. int result = [[jsobj valueForKey:@"result"] intValue];
  149. if (result<0)
  150. return result;
  151. if(result!=AP_USER_AUTH&& result!=9)
  152. {
  153. DebugLog(@"USER NOT AUTHORIZED CODE=%d ",result);
  154. [appDelegate Logout];
  155. if(result==8)
  156. {
  157. return RESULT_LOGIN_DEVICE;
  158. }
  159. return RESULT_FALSE;
  160. }
  161. // NSDictionary* objheader = [jsobj objectForKey:@"header"];
  162. //
  163. // NSString* required_ver=[jsobj objectForKey:@"min_ver"] ;
  164. // user=[objheader objectForKey:@"username"] ;
  165. // DebugLog(@"required_ver=%@ ",required_ver);
  166. // // NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  167. // // NSString* versionNum =[infoDict valueForKey:@"CFBundleVersion"];
  168. // // versionNum = [NSString stringWithFormat:@"A%@",versionNum];
  169. // int ibadge = [[objheader valueForKey:@"badge"] intValue];
  170. //
  171. // // if([ibadge isEqual:[NSNull null]])
  172. // // strbadge=@"0";
  173. // // if(ibadge==nil)
  174. // // ibadge=@"0";
  175. // // if([strbadge isEqualToString:@"null"])
  176. // // strbadge=@"0";
  177. // app.applicationIconBadgeNumber = ibadge;//[strbadge intValue];
  178. //
  179. // // BOOL bigger = [appDelegate.build compare:required_ver] ;
  180. // if([appDelegate.build intValue]<[required_ver intValue])
  181. // return RESULT_VER_LOW;
  182. // // appDelegate.sessionid = [objheader valueForKey:@"sessionid"];
  183. // appDelegate.user = user;
  184. // appDelegate.password = password;
  185. //// appDelegate.user_type = [[objheader valueForKey:@"user_type"] intValue];
  186. //// appDelegate.user_icon =[objheader valueForKey:@"user_img"] ;
  187. // appDelegate.bLogin = true;
  188. [appDelegate Login:user pwd:password];
  189. //
  190. // if(appDelegate.user_type==USER_ROLE_CUSTOMER)
  191. // {
  192. // appDelegate.customerInfo = [[objheader objectForKeyedSubscript:@"customerInfo"] mutableCopy];
  193. // appDelegate.contact_id = [objheader valueForKey:@"contact_id"];
  194. // // appDelegate.order_code =[objheader valueForKey:@"orderCode"];
  195. // // appDelegate.order_status =[[objheader valueForKey:@"orderStatus"] intValue];
  196. // // [appDelegate SetSo:[objheader valueForKey:@"soId"]];
  197. // }
  198. // NSString* mode =[jsobj valueForKey:@"mode"] ;
  199. // [appDelegate SetMode:mode];
  200. //
  201. //
  202. // appDelegate.cart_count = [[objheader valueForKey:@"cart_count"] intValue];
  203. // appDelegate.wish_count =[[objheader valueForKey:@"wish_count"] intValue];
  204. // appDelegate.port_count =[[objheader valueForKey:@"portfolio_count"] intValue];
  205. //
  206. // [appDelegate update_count_mark];
  207. //
  208. //
  209. //
  210. // appDelegate.can_show_price =[[objheader valueForKey:@"can_show_price"] boolValue];
  211. // appDelegate.can_see_price =[[objheader valueForKey:@"can_see_price"] boolValue];
  212. // appDelegate.can_create_portfolio =[[objheader valueForKey:@"can_create_portfolio"] boolValue];
  213. // appDelegate.can_create_order =[[objheader valueForKey:@"can_create_order"] boolValue];
  214. //
  215. // appDelegate.can_cancel_order =[[objheader valueForKey:@"can_cancel_order"] boolValue];
  216. // appDelegate.can_set_cart_price =[[objheader valueForKey:@"can_set_cart_price"] boolValue];
  217. // appDelegate.can_delete_order =[[objheader valueForKey:@"can_delete_order"] boolValue];
  218. // appDelegate.can_update_contact_info =[[objheader valueForKey:@"can_update_contact_info"] boolValue];
  219. // appDelegate.can_submit_order =[[objheader valueForKey:@"can_submit_order"] boolValue];
  220. // appDelegate.can_set_tearsheet_price =[[objheader valueForKey:@"can_set_tearsheet_price"] boolValue];
  221. //
  222. // appDelegate.save_order_logout =[[objheader valueForKey:@"save_order_logout"] boolValue];
  223. // appDelegate.submit_order_logout =[[objheader valueForKey:@"submit_order_logout"] boolValue];
  224. //
  225. // appDelegate.alert_sold_in_quantities = [[objheader valueForKey:@"alert_sold_in_quantities"] boolValue];
  226. //
  227. //
  228. //
  229. // appDelegate.ipad_perm =[objheader valueForKey:@"ipad_perm"] ;
  230. //
  231. // NSString* strfilter = [objheader valueForKey:@"statusFilter"] ;
  232. //
  233. //
  234. //
  235. // appDelegate.OrderFilter= [[RAUtils string2dict:strfilter] mutableCopy];
  236. //
  237. //
  238. // [appDelegate set_main_button_panel];
  239. //
  240. // if(appDelegate.user_type==USER_ROLE_CUSTOMER) {
  241. //
  242. // // shop order status filter
  243. // NSString *shopOrderStatusFilterStr = [objheader valueForKey:@"shopOrderFilter"] ;
  244. // [Singleton sharedInstance].shop_order_status_filter = [[RAUtils string2dict:shopOrderStatusFilterStr] mutableCopy];
  245. //
  246. // // specialInstruction
  247. // [Singleton sharedInstance].specialInstruction = [objheader valueForKey:@"specialInstruction"];
  248. //
  249. // //-价格类型
  250. // [Singleton sharedInstance].npd_shop_price_type = [[objheader valueForKey:@"price_type"] integerValue];
  251. //
  252. // // 权限
  253. // [Singleton sharedInstance].permissions_price_setting = [[objheader valueForKey:@"can_set_price_formula"] boolValue];
  254. // [Singleton sharedInstance].permissions_edit_order = [[objheader valueForKey:@"can_place_order"] boolValue];
  255. //
  256. // [Singleton sharedInstance].permissions_submit_order = [[objheader valueForKey:@"can_submit_order"] boolValue];
  257. //
  258. // [Singleton sharedInstance].permissions_merge_order = [[objheader valueForKey:@"can_merge_order"] boolValue];
  259. //
  260. // [Singleton sharedInstance].deliveryString = [objheader valueForKey:@"delivery_price"];
  261. // [[Singleton sharedInstance] resetGlobalLock];
  262. // [Singleton sharedInstance].customer_can_see_sales_Order = [[objheader valueForKey:@"can_see_salesorder"] boolValue];
  263. // }
  264. // DebugLog(@"sessionid=%@ ",appDelegate.sessionid);
  265. // if ([[objheader valueForKey:@"update"] boolValue]==false)
  266. // {
  267. // // no update on the server;
  268. // return RESULT_TRUE;
  269. // }
  270. // int Auth_InfoVer = [[objheader valueForKey:@"ver"] intValue];
  271. // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  272. // [defaults removeObjectForKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  273. // [defaults setInteger:Auth_InfoVer forKey:[NSString stringWithFormat:@"%@_Auth_InfoVer",user]];
  274. // [defaults synchronize];
  275. //
  276. // NSDictionary* objfuncs = [jsobj objectForKey:@"functions"];
  277. // NSArray* keys= [objfuncs allKeys];
  278. /*
  279. db.setTransactionSuccessful();
  280. db.endTransaction();
  281. */
  282. return RESULT_TRUE;
  283. }
  284. // NSDictionary *weatherInfo = [jsobj objectForKey:@"weatherinfo"];
  285. // txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
  286. // DebugLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
  287. return RESULT_USERAUTH_ERROR;
  288. }
  289. +(NSDictionary*)err_log:(NSString*) up_params result:(NSString*)result module:(NSString*) module code:(int) code//device:(NSString*) device
  290. {
  291. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  292. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  293. // if(appDelegate.user!=nil)
  294. // [params setValue:appDelegate.user forKey:@"user"];
  295. //
  296. // [params setValue:up_params forKey:@"upparam"];
  297. //
  298. // [params setValue:[NSNumber numberWithInt:code ] forKey:@"code"];
  299. //
  300. // [params setValue:result forKey:@"result"];
  301. // [params setValue:module forKey:@"interfac"];
  302. //
  303. // [params setValue:[RAUtils base64en:[RAUtils dict2string: [RAUtils device_info]]] forKey:@"device"];
  304. //
  305. //
  306. // // [params setValue:module forKey:@"module"];
  307. //
  308. // // [[UIDevice currentDevice] name];
  309. //
  310. // // if(appDelegate.offline_mode)
  311. // // {
  312. // // return [OLDataProvider offline_notimpl];
  313. // // }
  314. // if(![self IsNetworkAvailable])
  315. // return [RAUtils error_json:RESULT_NET_NOTAVAILABLE err_msg:nil];
  316. //
  317. // NSData* json=[self get_json:URL_ERR_LOG parameters:params];
  318. // if(json==nil)
  319. // return nil;
  320. // NSError *error=nil;
  321. // NSDictionary *jsobj = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error];
  322. // return jsobj;
  323. NSMutableDictionary* jsobj = [@{@"result":@"2"} mutableCopy];
  324. return jsobj;
  325. }
  326. +(NSString *) prepareUploadFile:(NSString*) file json:(NSMutableDictionary*)json
  327. {
  328. NSString* serial= [[NSUUID UUID] UUIDString];
  329. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  330. NSString *cachefolder = [paths objectAtIndex:0];
  331. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  332. upfolder = [upfolder stringByAppendingPathComponent:serial];
  333. NSFileManager* fileManager = [NSFileManager defaultManager];
  334. BOOL bdir=YES;
  335. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  336. {
  337. NSError *error = nil;
  338. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  339. if(!bsuccess)
  340. DebugLog(@"Create UPLOAD folder failed");
  341. }
  342. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  343. // ret[@"contact"]=[self prepareContact:serial];
  344. // ret[@"wishlist"]=[self prepareWishlist:serial];
  345. // ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  346. // ret[@"portfolio"] = [self preparePortfolio:serial];
  347. // ret[@"view_portfolio"] = [self preparePDF:serial];
  348. NSString* str= [RAUtils dict2string:json];
  349. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  350. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  351. NSError *error=nil;
  352. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  353. // NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  354. ZipArchive* zip = [[ZipArchive alloc] init];
  355. BOOL result = [zip CreateZipFile2:zippath Password:nil];
  356. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  357. result = [zip addFileToZip:jsonpath newname:[jsonpath lastPathComponent]];
  358. if( ![zip CloseZipFile2] )
  359. {
  360. zippath = @"";
  361. }
  362. return zippath;
  363. }
  364. @end