口袋优选

KBCacheHttp.m 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // LDCacheHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/10.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBCacheHttp.h"
  9. #import "ZmzAFNetworking.h"
  10. @implementation KBCacheHttp
  11. + (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json,BOOL isCache))success failure:(void(^)(NSError *error))failure {
  12. YBCacheType cacheType = YBCacheTypeReturnCacheDataThenLoad;
  13. //如果不是第一页数据,就直接忽略缓存,直接请求真正的数据
  14. if (params) {
  15. [params.allKeys containsObject:@"page"];
  16. NSNumber *page = params[@"page"];
  17. if (page.integerValue > 1) {
  18. cacheType = YBCacheTypeReloadIgnoringLocalCacheData;
  19. }
  20. }
  21. ZmzAFNetworking *networking = [[ZmzAFNetworking alloc]init];
  22. [networking requsetWithPath:url withParams:params withCacheType:cacheType withRequestType:NetworkPostType withResult:^(id responseObject, NSError *error,BOOL isCache) {
  23. if (!error) {
  24. NSDictionary *dataDic = (NSDictionary *)responseObject[@"rst"];
  25. success(dataDic,isCache);
  26. }else{
  27. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  28. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  29. NSLog(@"服务器的错误原因:%@",str);
  30. NSLog(@"报错地址:%@",url);
  31. failure(error);
  32. }
  33. }];
  34. }
  35. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json,BOOL isCache))success failure:(void(^)(NSError *error))failure {
  36. YBCacheType cacheType = YBCacheTypeReturnCacheDataThenLoad;
  37. if (params) {
  38. [params.allKeys containsObject:@"page"];
  39. NSNumber *page = params[@"page"];
  40. if (page.integerValue > 1) {
  41. cacheType = YBCacheTypeReloadIgnoringLocalCacheData;
  42. }
  43. }
  44. ZmzAFNetworking *networking = [[ZmzAFNetworking alloc]init];
  45. [networking requsetWithPath:url withParams:params withCacheType:YBCacheTypeReturnCacheDataThenLoad withRequestType:NetworkGetType withResult:^(id responseObject, NSError *error,BOOL isCache) {
  46. if (!error) {
  47. NSDictionary *dataDic = (NSDictionary *)responseObject[@"rst"];
  48. success(dataDic,isCache);
  49. }else{
  50. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  51. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  52. NSLog(@"服务器的错误原因:%@",str);
  53. NSLog(@"报错地址:%@",url);
  54. failure(error);
  55. }
  56. }];
  57. }
  58. @end