425 lines
16 KiB
Objective-C
425 lines
16 KiB
Objective-C
//
|
|
// AFNOHeaderHttpTool.m
|
|
// Ifish
|
|
//
|
|
// Created by imac on 16/10/27.
|
|
// Copyright © 2016年 lianxiang. All rights reserved.
|
|
//
|
|
|
|
#import "AFNOHeaderHttpTool.h"
|
|
#import "AFNetworking.h"
|
|
#import "SVProgressHUD.h"
|
|
#define ContentType @"text/html"
|
|
@implementation AFNOHeaderHttpTool
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
// _manager = [AFHTTPSessionManager manager];
|
|
// //请求超时时间为15秒
|
|
// _manager.requestSerializer.timeoutInterval = 15;
|
|
// _manager.requestSerializer = [AFJSONRequestSerializer serializer];
|
|
// _manager.responseSerializer = [AFJSONResponseSerializer serializer];
|
|
// // // 内容类型
|
|
// _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
+ (AFNOHeaderHttpTool *)sharedInstance
|
|
{
|
|
static AFNOHeaderHttpTool *_instance = nil;
|
|
static dispatch_once_t once;
|
|
dispatch_once(&once, ^{
|
|
_instance = [[self alloc] init];
|
|
|
|
});
|
|
return _instance;
|
|
}
|
|
-(NSString *)getRequestPath:(NSString *)urlPath keysArray:(NSArray *)keys valuesArray:(NSArray *)values{
|
|
urlPath = [urlPath stringByAppendingString:@"?"];
|
|
for (int i=0; i<keys.count; i++) {
|
|
NSString *keyValue = [[NSString alloc]initWithFormat:@"%@",values[i]];
|
|
|
|
NSString * newValue = [keyValue stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
|
|
if(i==keys.count-1)
|
|
urlPath = [urlPath stringByAppendingString:[NSString stringWithFormat:@"%@=%@",keys[i],newValue]];
|
|
else
|
|
urlPath = [urlPath stringByAppendingString:[NSString stringWithFormat:@"%@=%@&",keys[i],newValue]];
|
|
|
|
}
|
|
return urlPath;
|
|
}
|
|
-(NSMutableURLRequest *)getUrlRequestWithPath:(NSString *)urlString keysArray:(NSArray *)keys valuesArray:(NSArray *)values{
|
|
|
|
// urlString = [Api_DomainName stringByAppendingString:urlString];
|
|
NSString *newPath = @"";
|
|
if (keys==nil) {
|
|
newPath = urlString;
|
|
}else{
|
|
newPath = [self getRequestPath:urlString keysArray:keys valuesArray:values];
|
|
}
|
|
NSLog(@"requestUrl = %@",newPath);//请求地址
|
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
|
|
[urlRequest setTimeoutInterval:15];//超时时间15秒
|
|
[urlRequest setHTTPMethod:@"POST"];//请求方式post
|
|
NSString *bodyStr;
|
|
if (keys==nil) {
|
|
bodyStr = [newPath substringFromIndex:urlString.length];
|
|
}else
|
|
bodyStr = [newPath substringFromIndex:urlString.length+1];//请求体封装(有?的情况)
|
|
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
|
|
urlRequest.HTTPBody = bodyData;
|
|
return urlRequest;
|
|
}
|
|
|
|
|
|
|
|
//POST请求
|
|
//-(void) postRequestWihtUrl:(NSString *)url
|
|
// keys:(NSArray *)keys
|
|
// values:(NSArray *)values
|
|
// success:(WBCallBackSuccess)success
|
|
// failure:(WBCallBackFailed)failure
|
|
//{
|
|
//
|
|
// NSURLSession *session = [NSURLSession sharedSession];
|
|
// NSMutableURLRequest *urlRequest = [self getUrlRequestWithPath:url keysArray:keys valuesArray:values];
|
|
// NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
// if ([data length] >0 &&
|
|
// error == nil){
|
|
// [SVProgressHUD dismissWithDelay:0.3];
|
|
// id result=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
|
|
// NSLog(@"获取数据 resalut = %@",result);
|
|
// if ([result isKindOfClass:[NSDictionary class]]) {
|
|
//// NSString *resCode = [NSString stringWithFormat:@"%@",[result objectForKey:RESPONSE_CODE]];//状态码
|
|
//// //返回数据正确 ,则解析到数据接收内容
|
|
//// if([resCode integerValue]==kReqSuccess)
|
|
//// {
|
|
// /**
|
|
// 获取后台给定的正确码100,做逻辑处理
|
|
// */
|
|
// dispatch_async(dispatch_get_main_queue(), ^{
|
|
// //直接显示成功信息
|
|
// success(result);
|
|
// });
|
|
//// }else{
|
|
//// /*
|
|
//// 获取后台给定的其他错误码,做逻辑处理(暂无数据等)
|
|
//// */
|
|
//// dispatch_async(dispatch_get_main_queue(), ^{
|
|
//// failure(result);
|
|
//// });
|
|
//// }
|
|
// }else{
|
|
// NSLog(@"json格式错误");
|
|
// }
|
|
// }else if ([data length] == 0 &&
|
|
// error == nil){
|
|
// //请求数据长度为0(服务器返回数据内容问题)
|
|
// [SVProgressHUD dismiss];
|
|
// }else if (error != nil){
|
|
// //反馈错误信息(网络连接失败(服务器关停)等信息)
|
|
// if (error) {
|
|
// [SVProgressHUD dismiss];
|
|
// }
|
|
// }
|
|
// }];
|
|
// [sessionDataTask resume];
|
|
//}
|
|
+(void) requestWihtMethod:(RequestType)
|
|
methodType url : (NSString *)url
|
|
params:(NSDictionary *)params
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError *err))failure
|
|
{
|
|
// NSURL* baseURL = [NSURL URLWithString:DEV_IFISH_SERVER];
|
|
// //获得请求管理者
|
|
// AFHTTPRequestOperationManager*manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
|
|
|
|
AFHTTPRequestOperationManager*manager=[AFHTTPRequestOperationManager manager];
|
|
//此类中接口 无安全校验
|
|
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
|
|
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"text/html", nil];;
|
|
//#ifdef ContentType
|
|
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:ContentType];
|
|
//#endif
|
|
// manager.requestSerializer.HTTPShouldHandleCookies = YES;
|
|
|
|
switch (methodType) {
|
|
case RequestTypeGet:
|
|
{
|
|
//GET请求
|
|
[manager GET:url parameters:params
|
|
success:^(AFHTTPRequestOperation* operation, NSDictionary* responseObj) {
|
|
if (success) {
|
|
success(responseObj);
|
|
}
|
|
} failure:^(AFHTTPRequestOperation* operation, NSError* error) {
|
|
if (failure) {
|
|
failure(error);
|
|
}
|
|
}];
|
|
|
|
}
|
|
break;
|
|
case RequestTypePost:
|
|
{
|
|
//POST请求
|
|
[manager POST:url parameters:params
|
|
success:^(AFHTTPRequestOperation* operation, NSDictionary* responseObj) {
|
|
if (success) {
|
|
[SVProgressHUD dismiss];
|
|
success(responseObj);
|
|
}
|
|
} failure:^(AFHTTPRequestOperation* operation, NSError* error) {
|
|
if (failure){
|
|
[SVProgressHUD dismiss];
|
|
failure(error);
|
|
}
|
|
}];
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
+(void)saveCustomIconWith:(NSNumber*)UserId
|
|
deviceId:(NSString*)deviceId
|
|
customIconName:(NSString*)customIconName
|
|
customShowName:(NSString*)customShowName
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure{
|
|
NSDictionary*para=@{
|
|
@"priId.userId":UserId,
|
|
@"priId.deviceId":deviceId,
|
|
@"customShowName":customShowName,
|
|
@"customIconName":customIconName
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:kSaveCustomIcon params:para success:success failure:failure];
|
|
|
|
}
|
|
|
|
+(void)getLookReportById:(NSString *)reportId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
|
|
@"reportId":reportId
|
|
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishgetLookReportById
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
|
|
|
|
}
|
|
|
|
+(void)deleteDeviceCameraWith:(NSString *)cameraId
|
|
deiviceId:(NSString *)deiviceId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"cameraId":cameraId,
|
|
@"deviceId":deiviceId
|
|
|
|
|
|
};
|
|
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishdeleteDeviceCamera params:para success:success failure:failure];
|
|
|
|
}
|
|
|
|
+(void)kanHuKaiGuanWith:(NSString *)cameraId
|
|
userId:(NSString *)userId
|
|
status:(NSString *)status
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"cameraId":cameraId,
|
|
@"userId":userId,
|
|
@"status":status
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishOnOffLook
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
}
|
|
|
|
+(void)deviceBindCameraWith:(NSString *)cameraId
|
|
userId:(NSNumber *)userId
|
|
deviceId:(NSString *)deviceId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"cameraId":cameraId,
|
|
@"deviceId":deviceId,
|
|
@"userId": userId
|
|
};
|
|
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishdeviceBindCamera params:para success:success failure:failure];
|
|
}
|
|
|
|
+(void)getKanHuListWithShopsId:(NSString *)shopsUserId
|
|
firstResult:(NSString *)firstResult
|
|
pageSize:(NSString *)pageSize
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
|
|
NSDictionary*para=@{
|
|
@"shopsUserId":shopsUserId,
|
|
@"firstResult":firstResult,
|
|
@"pageSize":pageSize
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishlookList
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
}
|
|
|
|
+(void)bindCameraWith:(NSString *)cameraId
|
|
userId:(NSNumber *)userId
|
|
success:(void (^)(id response))success
|
|
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
|
|
NSDictionary*para=@{
|
|
@"cameraId":cameraId,
|
|
@"userId":userId
|
|
};
|
|
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishBindCamera params:para success:success failure:failure];
|
|
|
|
}
|
|
|
|
+(void)setRemindWaterInfWith:(NSString*)deviceId
|
|
waterRemind:(NSString*)waterRemind
|
|
remindCycle:(NSString*)remindCycle
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
|
|
NSDictionary*para=@{
|
|
@"deviceId":deviceId,
|
|
@"waterRemind":waterRemind,
|
|
@"remindCycle":remindCycle
|
|
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:kSetRemindWaterInf params:para success:success failure:failure];
|
|
|
|
|
|
}
|
|
|
|
+(void)getRemindWaterInfwith:(NSString*)deviceId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"deviceId":deviceId,
|
|
|
|
};
|
|
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:kGetRemindWaterInf params:para success:success failure:failure];
|
|
|
|
}
|
|
|
|
+(void)scanCodeActiveCamea:(NSString *)cameraId
|
|
deiviceId:(NSString *)deiviceId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"cameraId":cameraId,
|
|
@"deviceId":deiviceId
|
|
|
|
|
|
};
|
|
|
|
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:JiHuoSheXiangTou
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
|
|
}
|
|
|
|
+(void)getShopsStatus:(NSString *)shopsId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure{
|
|
|
|
NSDictionary*para=@{
|
|
@"shopsId":shopsId
|
|
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishGetShopsStatus
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
|
|
|
|
}
|
|
|
|
+(void)xuanZeKanHuShangJia:(NSString *)shopsPhone
|
|
yongHuId:(NSNumber *)userId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"phoneNumber":shopsPhone,
|
|
@"userId":userId
|
|
|
|
};
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishChoiceShops
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
|
|
}
|
|
|
|
+(void)sendReportWithUserId:(NSString * )userId
|
|
baoGaoHtmlName:(NSString *)fileName
|
|
reportId:(NSString *)reportId
|
|
success:(void (^)(id response))success
|
|
failure:(void (^)(NSError* err))failure
|
|
{
|
|
NSDictionary*para=@{
|
|
@"userId":userId,
|
|
|
|
@"fileName":fileName,
|
|
@"reportId":reportId
|
|
|
|
};
|
|
|
|
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:IfishSendReport
|
|
params:para
|
|
success:success
|
|
failure:failure];
|
|
|
|
}
|
|
|
|
-(NSDictionary *)getErrorDict:( NSError *)error
|
|
{
|
|
NSMutableDictionary * resalut = InitObject(NSMutableDictionary);
|
|
[resalut setObject:[NSString stringWithFormat:@"%ld",error.code] forKey:RESPONSE_CODE];
|
|
[resalut setObject:error.domain forKey:RESPONSE_MESSAGE];
|
|
return resalut;
|
|
}
|
|
@end
|