省钱达人

DRCacheHttp.m 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // DRCacheHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/10.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRCacheHttp.h"
  9. #import "ZmzAFNetworking.h"
  10. @implementation DRCacheHttp
  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. failure(error);
  31. }
  32. }];
  33. }
  34. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json,BOOL isCache))success failure:(void(^)(NSError *error))failure {
  35. YBCacheType cacheType = YBCacheTypeReturnCacheDataThenLoad;
  36. if (params) {
  37. [params.allKeys containsObject:@"page"];
  38. NSNumber *page = params[@"page"];
  39. if (page.integerValue > 1) {
  40. cacheType = YBCacheTypeReloadIgnoringLocalCacheData;
  41. }
  42. }
  43. ZmzAFNetworking *networking = [[ZmzAFNetworking alloc]init];
  44. [networking requsetWithPath:url withParams:params withCacheType:YBCacheTypeReturnCacheDataThenLoad withRequestType:NetworkGetType withResult:^(id responseObject, NSError *error,BOOL isCache) {
  45. if (!error) {
  46. NSDictionary *dataDic = (NSDictionary *)responseObject[@"rst"];
  47. success(dataDic,isCache);
  48. }else{
  49. failure(error);
  50. }
  51. }];
  52. }
  53. @end