add files apis

This commit is contained in:
lianxiang
2018-08-30 18:07:16 +08:00
parent 54e936535f
commit 3efde8e0ea
68 changed files with 1254 additions and 374 deletions
+81 -25
View File
@@ -9,54 +9,58 @@
#import "GiGaNetManager.h"
#import "AFNetworking.h"
#import "GiGaServerConfig.h"
@interface GiGaNetManager()
@end
@implementation GiGaNetManager
+ (GiGaNetManager *)shareInstance{
static GiGaNetManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
+ (void)request:(GiGaAPIRequest *)api{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURL *baseUrl = [NSURL URLWithString:[GiGaServerConfig getMainUrl]];
AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc] initWithBaseURL:baseUrl sessionConfiguration:configuration];
+ (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 (api.requestType) {
case kRequestTypeGet:
switch (requestMethod) {
case RequestGetMethod:
{
[manager GET:api.fullUrl parameters:api.params progress:^(NSProgress * _Nonnull downloadProgress) {
[manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *outDic = (NSDictionary *)responseObject;
[api callBackFinishedWithDictionary:outDic];
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) {
[api callBackFailed:error];
NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields];
block(nil, headerFields, error);
}];
}
break;
case kRequestTypePost:
case RequestPostMethod:
{
[manager POST:api.fullUrl parameters:api.params progress:^(NSProgress * _Nonnull uploadProgress) {
[manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *outDic = (NSDictionary *)responseObject;
[api callBackFinishedWithDictionary:outDic];
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) {
[api callBackFailed:error];
NSDictionary *headerFields = [(NSHTTPURLResponse *)task.response allHeaderFields];
block(nil, headerFields, error);
}];
}
break;
@@ -67,6 +71,58 @@
}
}
+(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)userLoginrequest:(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