// // GiGaNetManager.m // GIGA // // Created by lianxiang on 2018/8/15. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiGaNetManager.h" #import "AFNetworking.h" #import "GiGaServerConfig.h" @interface GiGaNetManager() @end @implementation GiGaNetManager + (void)requestMethod:(RequestMethod)requestMethod Url:(NSString *)url params:(NSDictionary *)params responseBlock:(responseBlock)block{ // NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; // NSURL *baseUrl = [NSURL URLWithString:[GiGaServerConfig getMainUrl]]; // AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] initWithBaseURL:baseUrl sessionConfiguration:configuration]; AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] init]; manager.responseSerializer=[AFHTTPResponseSerializer serializer]; switch (requestMethod) { case RequestGetMethod: { [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@", responseObject); NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields]; block(dict, headerFields, nil); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields]; block(nil, headerFields, error); }]; } break; case RequestPostMethod: { [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@", dict); NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields]; block(dict, headerFields, nil); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields]; block(nil, headerFields, error); }]; } break; default: break; } } +(void)request:(NSString *)url params:(NSDictionary *)param success:(void (^)(id response))success failure:(void (^)(NSError *err))failure { AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] init]; manager.responseSerializer=[AFHTTPResponseSerializer serializer]; [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@", dict); if (success) { success(responseObject); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error); if (failure) { failure(error); } }]; } //登录body传参数 +(void)userbodyRequest:(NSString *)url params:(NSDictionary *)param completionHandler:(nullable void (^)(NSURLResponse *response,NSDictionary *resDic, NSError * _Nullable error))completionHandler { NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURL *baseUrl = [NSURL URLWithString:[GiGaServerConfig getMainUrl]]; AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] initWithBaseURL:baseUrl sessionConfiguration:configuration]; NSMutableURLRequest *requst = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:param error:nil]; requst.timeoutInterval = 10.f; [requst setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [requst setValue:@"application/json" forHTTPHeaderField:@"Accept"]; NSURLSessionDataTask *task = [manager dataTaskWithRequest:requst uploadProgress:^(NSProgress * _Nonnull uploadProgress) { } downloadProgress:^(NSProgress * _Nonnull downloadProgress) { } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { if (completionHandler) { completionHandler(response,responseObject,error); } }]; [task resume]; } @end