164 lines
4.6 KiB
Objective-C
164 lines
4.6 KiB
Objective-C
//
|
|
// GiGaBaseAPiRequest.m
|
|
// GIGA
|
|
//
|
|
// Created by lianxiang on 2018/8/29.
|
|
// Copyright © 2018年 com.giga.ios. All rights reserved.
|
|
//
|
|
|
|
#import "GiGaBaseAPiRequest.h"
|
|
#import "GiGaServerConfig.h"
|
|
#import <JSONModel/JSONModel.h>
|
|
#import "GiGaNetManager.h"
|
|
|
|
@implementation GiGaBaseAPiRequest
|
|
|
|
- (NSString *)serviceUrl
|
|
{
|
|
return [GiGaServerConfig getMainUrl];
|
|
}
|
|
|
|
- (id)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
_needToken = NO;
|
|
self.requestMethod = RequestGetMethod;
|
|
|
|
}
|
|
return self;
|
|
}
|
|
|
|
+(instancetype)initWithRequestPath:(NSString *)path method:(RequestMethod)method parms:(NSDictionary *)parms
|
|
{
|
|
NSString *absoluteURLString = [NSString stringWithFormat:@"%@%@", [GiGaServerConfig getMainUrl], path];
|
|
return [self initWithRequestAbsoluteURLString:absoluteURLString
|
|
method:method
|
|
parms:parms];
|
|
}
|
|
|
|
+ (instancetype)initWithRequestAbsoluteURLString:(NSString *)absoluteString
|
|
method:(RequestMethod)method
|
|
parms:(NSDictionary *)parms
|
|
{
|
|
GiGaBaseAPiRequest *request = [[self alloc] init];
|
|
request.fullUrl = absoluteString;
|
|
request.requestMethod = method;
|
|
|
|
[request buildRequestDictWithDepedency:parms];
|
|
return request;
|
|
}
|
|
|
|
+ (NSArray *)mergeOriginalArray:(NSArray *)orgArr withOtherArray:(NSArray *)otherArr;
|
|
{
|
|
if (otherArr.count == 0) {
|
|
return orgArr;
|
|
}
|
|
|
|
NSMutableArray *array = [NSMutableArray arrayWithArray:orgArr];
|
|
for (int i = 0; i < otherArr.count; i++) {
|
|
id object = [otherArr objectAtIndex:i];
|
|
if (!orgArr || [orgArr indexOfObject:object] == NSNotFound) {
|
|
[array addObject:object];
|
|
}
|
|
}
|
|
|
|
return array;
|
|
}
|
|
|
|
- (NSMutableDictionary *)buildRequestDict
|
|
{
|
|
if (!_params) {
|
|
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
|
|
_params = dict;
|
|
}
|
|
return _params;
|
|
}
|
|
- (NSMutableDictionary *)buildRequestDictWithDepedency:(NSDictionary *)dict
|
|
{
|
|
self.params = dict.mutableCopy;
|
|
return self.params;
|
|
}
|
|
|
|
- (void)requstDataWithResult:(GiGaRequestResultBlock)result
|
|
{
|
|
self.blockresult = result;
|
|
[self checkNetwork];
|
|
if (self.statu == NetStatusUnKown) {
|
|
return;
|
|
}
|
|
NSLog(@" %@", _fullUrl);
|
|
// NSMutableDictionary *postDict = [self buildRequestDict];
|
|
|
|
[GiGaNetManager requestMethod:self.requestMethod Url:self.fullUrl params:self.params responseBlock:^(NSDictionary *responseDict, NSDictionary *responseHeaderFields, NSError *error) {
|
|
|
|
[self analyseResponseInfo:responseDict headerFileds:responseHeaderFields error:error];
|
|
}];
|
|
|
|
}
|
|
|
|
- (void)analyseResponseInfo:(NSDictionary *)responseData error:(NSError *)error
|
|
{
|
|
[self analyseResponseInfo:responseData
|
|
headerFileds:nil
|
|
error:error];
|
|
}
|
|
|
|
- (void)analyseResponseInfo:(NSDictionary *)responseData headerFileds:(NSDictionary *)headerFields error:(NSError *)error
|
|
{
|
|
self.responseHeaderFields = headerFields;
|
|
NSString *token = headerFields[@"access-token"];
|
|
if (token && token.length > 0) {
|
|
//更新token
|
|
//[FFBMSConfig shareInstance].token = token;
|
|
}
|
|
|
|
GILog(@"headerFileds%@",headerFields);
|
|
if (error) {
|
|
GILog(@"err:%@",error.localizedDescription);
|
|
[[UIApplication sharedApplication].keyWindow makeToast:@"请求错误" duration:1 position:CSToastPositionCenter];
|
|
self.blockresult(nil);
|
|
return;
|
|
}
|
|
|
|
self.resultDict =responseData;
|
|
GiGaAPIResult *res = [[GiGaAPIResult alloc] initWithDictionary:responseData];
|
|
if (self.blockresult) {
|
|
self.blockresult(res);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)checkNetwork{
|
|
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
|
|
[manager startMonitoring];
|
|
[manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
|
|
|
switch (status) {
|
|
case AFNetworkReachabilityStatusReachableViaWWAN:
|
|
{
|
|
GILog(@"2/3/4G");
|
|
self.statu = NetStatusWWAN;
|
|
}
|
|
break;
|
|
case AFNetworkReachabilityStatusReachableViaWiFi:
|
|
{
|
|
GILog(@"WiFi");
|
|
self.statu = NetStatusWifi;
|
|
}
|
|
break;
|
|
default:
|
|
[[UIApplication sharedApplication].keyWindow makeToast:@"网络异常" duration:1.0 position:CSToastPositionCenter];
|
|
self.statu = NetStatusUnKown;
|
|
GILog(@"Unkown Net");
|
|
break;
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
@end
|