From 39cda0b3411284b32a6bbf119d1d42ee845eecb7 Mon Sep 17 00:00:00 2001 From: xueyognwei Date: Wed, 28 Dec 2016 14:07:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8B=A5=E6=9C=89=E7=BC=93=E5=AD=98=E4=B8=94?= =?UTF-8?q?=E6=9C=AA=E8=BF=87=E6=9C=9F=EF=BC=8C=E4=B8=8D=E5=86=8D=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9B=B4=E6=8E=A5=E7=BD=91=E7=BB=9C=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=EF=BC=8C=E8=BF=94=E5=9B=9E=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PPNetworkHelper/PPNetworkHelper.h | 17 +++- .../PPNetworkHelper/PPNetworkHelper.m | 83 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.h b/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.h index 1036cc7..99bb9f2 100755 --- a/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.h +++ b/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.h @@ -182,7 +182,22 @@ typedef void(^NetworkStatus)(PPNetworkStatus status); responseCache:(HttpRequestCache)responseCache success:(HttpRequestSuccess)success failure:(HttpRequestFailed)failure; - +/** + * POST请求,根据[过期时间],来返回缓存,或请求数据 + * 如果已有缓存不再发起请求 + * + * @param URL 请求地址 + * @param parameters 请求参数 + * @param parameters 是否刷新数据(不管是否有缓存,只要网络数据) + * @param success 请求成功的回调 + * @param failure 请求失败的回调 + * + * @return 返回的对象可取消请求,调用cancel方法 + */ ++ (NSURLSessionTask *)POSTRquest:(NSString *)URL + parameters:(NSDictionary *)parameters refresh:(BOOL)refresh + success:(HttpRequestSuccess)success + failure:(HttpRequestFailed)failure; /** * 上传图片文件 * diff --git a/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.m b/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.m index 808de06..833d5cf 100755 --- a/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.m +++ b/PPNetworkHelper/PPNetworkHelper/PPNetworkHelper.m @@ -189,7 +189,64 @@ + (NSURLSessionTask *)POST:(NSString *)URL sessionTask ? [[self allSessionTask] addObject:sessionTask] : nil ; return sessionTask; } +#pragma mark - POST请求傻瓜缓存 +/* + 此种缓存需要server配合,在head传入过期时间,此处默认格式:yyyy-MM-dd HH:mm:ss(与服务器协商格式,自行改变过期检测方法) + */ ++ (NSURLSessionTask *)POSTRquest:(NSString *)URL + parameters:(NSDictionary *)parameters refresh:(BOOL)refresh + success:(HttpRequestSuccess)success + failure:(HttpRequestFailed)failure +{ + if (!refresh) { + NSDictionary *localCache =[PPNetworkCache httpCacheForURL:URL parameters:parameters]; + if (localCache) {//本地有数据,检查过期时间 + PPLog(@"本地有数据,检查有效期.."); + NSString *cacheTime = [localCache objectForKey:@"Expires"]; + if (![self hasExpires:cacheTime]) {//缓存还未过期,加载本地数据后直接返回 + PPLog(@"未过期,加载本地数据后直接返回"); + success(localCache[@"responseObject"]); + return nil; + } + PPLog(@"本地数据已过期"); + } + } + PPLog(@"发起网络请求.."); + //发起网络请求 + NSURLSessionTask *sessionTask = [_sessionManager POST:URL parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) { + + } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + [[self allSessionTask] removeObject:task]; + success ? success(responseObject) : nil; + //对数据进行异步缓存 + PPLog(@"网络请求返回数据,对新数据进行处理.."); + //加工网络数据,并缓存到本地 + if (responseObject) { + PPLog(@"准备生成需要缓存的有效数据.."); + if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {//获取是否缓存,缓存过期时间 + NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response; + NSString *Expires = [response.allHeaderFields objectForKey:@"Expires"]; + NSMutableDictionary *dataToCache = [NSMutableDictionary new]; + PPLog(@"准备生成有效期.."); + [dataToCache setObject:Expires.length>0?Expires:[self defaultTime] forKey:@"Expires"]; + [dataToCache setObject:responseObject forKey:@"responseObject"]; + PPLog(@"已缓存到本地"); + [PPNetworkCache setHttpCache:dataToCache URL:URL parameters:parameters]; + } + } + + PPLog(@"responseObject = %@",[self jsonToString:responseObject]); + + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + [[self allSessionTask] removeObject:task]; + failure ? failure(error) : nil; + PPLog(@"error = %@",error); + }]; + // 添加最新的sessionTask到数组 + sessionTask ? [[self allSessionTask] addObject:sessionTask] : nil ; + return sessionTask; +} #pragma mark - 上传图片文件 + (NSURLSessionTask *)uploadWithURL:(NSString *)URL @@ -351,6 +408,32 @@ + (void)openNetworkActivityIndicator:(BOOL)open [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:open]; } +//检查是否过期 ++(BOOL)hasExpires:(NSString *)dataStr +{ + NSDateFormatter* formater = [[NSDateFormatter alloc] init]; + NSLocale* local =[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; + [formater setLocale:local]; + [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; + NSDate* date = [formater dateFromString:dataStr]; + NSTimeInterval passtime = [date timeIntervalSinceNow]; + if (passtime>=0) {//还没到这个时间 + return NO; + } + return YES; +} +//获取默认过期时间:(此处默认可用100s) ++(NSString *)defaultTime +{ + NSDate *date = [NSDate dateWithTimeIntervalSinceNow:100]; + NSDateFormatter *formatter=[[NSDateFormatter alloc] init]; + //定义格式 + formatter.dateFormat=@"yyyy-MM-dd HH:mm:ss"; + //时间转化为字符串 + NSString *dateString = [formatter stringFromDate:date]; + return [dateString copy]; +} + @end