Initial commit
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// GiGaAPIRequest.h
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/15.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "GiGaAPIResult.h"
|
||||
@class GiGaAPIRequest;
|
||||
#define DEFALUT_APIREQUEST_TIMEOUT 20
|
||||
|
||||
typedef enum GiGaRequestType {
|
||||
kRequestTypeGet,
|
||||
kRequestTypePost,
|
||||
kRequestTypeUpload,
|
||||
kRequestTypeMultiUpload
|
||||
}GiGaRequestTyp;
|
||||
|
||||
typedef enum ApiResultFormat {
|
||||
kApiResultJson,
|
||||
kApiResultXml,
|
||||
}ApiResultFormat;
|
||||
|
||||
@protocol GiGaApiRquestDelegate<NSObject>
|
||||
@optional
|
||||
|
||||
- (void)serverApiFinishedSuccessed:(GiGaAPIRequest *)api result:(GiGaAPIResult *)result;
|
||||
- (void)serverApiRequestFailed:(GiGaAPIRequest *)api error:(NSError *)error;
|
||||
- (void)serverApiFinishedFailed:(GiGaAPIRequest *)api result:(GiGaAPIResult *)result;
|
||||
|
||||
@end
|
||||
|
||||
@interface GiGaAPIRequest : NSObject
|
||||
|
||||
#pragma mark - porperty
|
||||
/** 请求类型 */
|
||||
@property (nonatomic, readonly) GiGaRequestTyp requestType;
|
||||
/** 请求返回的格式 */
|
||||
@property (nonatomic, readonly) ApiResultFormat resultFormat;
|
||||
/** 请求超时时间 */
|
||||
@property (nonatomic, readonly) NSTimeInterval timeout;
|
||||
/** 服务器地址 */
|
||||
@property (nonatomic, readonly) NSString *serviceUrl;
|
||||
/** 请求路径 */
|
||||
@property (nonatomic, readonly) NSString *fullUrl;
|
||||
/** 代理 */
|
||||
@property (nonatomic, weak) id<GiGaApiRquestDelegate> delegate;
|
||||
/** 请求参数数组 */
|
||||
@property (nonatomic, strong) NSMutableArray *params;
|
||||
|
||||
#pragma mark - Method
|
||||
- (id)initWithDelegate:(id<GiGaApiRquestDelegate>)delegate;
|
||||
/**
|
||||
* 拼接公共参数
|
||||
*/
|
||||
- (void)appendBaseParams;
|
||||
|
||||
#pragma mark - APIRequestDelegate
|
||||
/**
|
||||
* 返回数据调用方法
|
||||
*/
|
||||
- (void)callBackFinishedWithDictionary:(NSDictionary *)dic;
|
||||
/**
|
||||
* 返回数据错误
|
||||
*/
|
||||
- (void)callBackFailed:(NSError *)error;
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// GiGaAPIRequest.m
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/15.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import "GiGaAPIRequest.h"
|
||||
#import "GiGaServerConfig.h"
|
||||
@implementation GiGaAPIRequest
|
||||
|
||||
- (id)initWithDelegate:(id<GiGaApiRquestDelegate>)delegate
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.params = [NSMutableArray array];
|
||||
self.delegate = delegate;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
//默认Get访问
|
||||
- (GiGaRequestTyp)requestType
|
||||
{
|
||||
return kRequestTypeGet;
|
||||
}
|
||||
|
||||
//默认超时时间
|
||||
- (NSTimeInterval)timeout
|
||||
{
|
||||
return DEFALUT_APIREQUEST_TIMEOUT;
|
||||
}
|
||||
|
||||
//服务器地址
|
||||
-(NSString *)serviceUrl{
|
||||
return [GiGaServerConfig getMainUrl];
|
||||
}
|
||||
|
||||
/** 拼接的请求地址 */
|
||||
|
||||
//
|
||||
- (NSString *)fullUrl
|
||||
{
|
||||
// 服务器地址 和 接口名
|
||||
NSString *url = [NSString stringWithFormat:@"%@",self.serviceUrl];
|
||||
|
||||
return url;
|
||||
}
|
||||
// 数据请求完成的 回调
|
||||
- (void)callBackFinishedWithDictionary:(NSDictionary *)dic
|
||||
{
|
||||
// 处理 responseObject
|
||||
GiGaAPIResult *resu = [[GiGaAPIResult alloc] initWithDictionary:dic];
|
||||
// error ID = 0
|
||||
if (resu.success) {
|
||||
if (self.delegate && [self.delegate respondsToSelector:@selector(serverApiFinishedSuccessed: result:)]) {
|
||||
//
|
||||
[self.delegate serverApiFinishedSuccessed:self result:resu];
|
||||
}
|
||||
// error ID = 1
|
||||
} else {
|
||||
if (self.delegate && [self.delegate respondsToSelector:@selector(serverApiFinishedFailed:result:)]) {
|
||||
[self.delegate serverApiFinishedFailed:self result:resu];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 数据请求失败的回调
|
||||
- (void)callBackFailed:(NSError *)error
|
||||
{
|
||||
if (self.delegate && [self.delegate respondsToSelector:@selector(serverApiRequestFailed:error:)]) {
|
||||
[self.delegate serverApiRequestFailed:self error:error];
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接的基本参数
|
||||
- (void)appendBaseParams
|
||||
{
|
||||
|
||||
}
|
||||
@end
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// GiGaAPIResult.h
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/15.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface GiGaAPIResult : NSObject
|
||||
|
||||
/** 提示信息 */
|
||||
@property (nonatomic, copy) NSString *message;
|
||||
/** 请求状态 */
|
||||
@property (nonatomic, assign) NSInteger status;
|
||||
/** 请求是否成功 */
|
||||
@property (nonatomic, readonly) BOOL success;
|
||||
/** 接收数据的字典 */
|
||||
@property (nonatomic, strong) NSDictionary *dic;
|
||||
|
||||
- (id)initWithDictionary:(NSDictionary *)dic;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// GiGaAPIResult.m
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/15.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import "GiGaAPIResult.h"
|
||||
|
||||
@implementation GiGaAPIResult
|
||||
|
||||
-(BOOL)success{
|
||||
|
||||
return self.status == 0;
|
||||
|
||||
}
|
||||
|
||||
-(id)initWithDictionary:(NSDictionary *)dic
|
||||
{
|
||||
if (self = [super init]) {
|
||||
@try {
|
||||
if (dic && ![dic isKindOfClass:[NSNull class]]) {
|
||||
self.status = [[dic objectForKey:@"errorcode"] intValue];//待定
|
||||
self.message = [dic objectForKey:@""];//待定
|
||||
NSDictionary *data = [[dic objectForKey:@"result"] objectForKey:@"data"];
|
||||
self.dic = data;
|
||||
|
||||
}else{
|
||||
self.message = @"网络错误";
|
||||
self.dic = nil;
|
||||
self.status = 1;
|
||||
}
|
||||
|
||||
}
|
||||
@catch(NSException *exception) {
|
||||
self.dic = dic;
|
||||
self.status = 0;
|
||||
|
||||
}
|
||||
@finally {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// GiGaNetManager.h
|
||||
// GIGA
|
||||
//
|
||||
// Created by lianxiang on 2018/8/15.
|
||||
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "GiGaAPIRequest.h"
|
||||
@interface GiGaNetManager : NSObject
|
||||
+(GiGaNetManager *)shareInstance;
|
||||
+(void)request:(GiGaAPIRequest*)api;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
+ (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];
|
||||
|
||||
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
|
||||
switch (api.requestType) {
|
||||
case kRequestTypeGet:
|
||||
|
||||
{
|
||||
[manager GET:api.fullUrl parameters:api.params progress:^(NSProgress * _Nonnull downloadProgress) {
|
||||
|
||||
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
||||
NSDictionary *outDic = (NSDictionary *)responseObject;
|
||||
[api callBackFinishedWithDictionary:outDic];
|
||||
|
||||
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||||
[api callBackFailed:error];
|
||||
}];
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case kRequestTypePost:
|
||||
{
|
||||
[manager POST:api.fullUrl parameters:api.params progress:^(NSProgress * _Nonnull uploadProgress) {
|
||||
|
||||
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
||||
NSDictionary *outDic = (NSDictionary *)responseObject;
|
||||
[api callBackFinishedWithDictionary:outDic];
|
||||
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||||
[api callBackFailed:error];
|
||||
}];
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user