310 lines
12 KiB
Mathematica
310 lines
12 KiB
Mathematica
![]() |
//
|
||
|
// HttpRequestFacilitater.m
|
||
|
// YUMI
|
||
|
//
|
||
|
// Created by zu on 2021/9/3.
|
||
|
//
|
||
|
|
||
|
#import "HttpRequestFacilitater.h"
|
||
|
#import "YYUtility.h"
|
||
|
#import "AccountAbstractStorage.h"
|
||
|
#import "YYReachability.h"
|
||
|
#import <AFNetworking.h>
|
||
|
#import "YUMIMacroUitls.h"
|
||
|
@implementation HttpRequestFacilitater
|
||
|
|
||
|
+(AFHTTPSessionManager *)requisitionExecutive
|
||
|
{
|
||
|
static AFHTTPSessionManager *manager = nil;
|
||
|
static dispatch_once_t onceToken;
|
||
|
dispatch_once(&onceToken, ^{
|
||
|
manager = [[AFHTTPSessionManager manager]initWithBaseURL:[NSURL URLWithString:API_HOST_URL]];
|
||
|
manager.responseSerializer = [AFJSONResponseSerializer serializer];
|
||
|
manager.requestSerializer.HTTPShouldHandleCookies = YES;
|
||
|
|
||
|
manager.securityPolicy.allowInvalidCertificates = YES;
|
||
|
AFSecurityPolicy *securityPolicy= [AFSecurityPolicy defaultPolicy];
|
||
|
manager.securityPolicy = securityPolicy;
|
||
|
|
||
|
[manager.securityPolicy setValidatesDomainName:NO];
|
||
|
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain",@"image/jpeg",@"image/png", nil];
|
||
|
manager.requestSerializer.timeoutInterval = 60;
|
||
|
});
|
||
|
return manager;
|
||
|
}
|
||
|
|
||
|
+ (void)GET:(NSString *)method
|
||
|
params:(NSDictionary *)params
|
||
|
success:(void (^)(BaseMatrix *data))success
|
||
|
failure:(void (^)(NSInteger resCode, NSString *message))failure
|
||
|
{
|
||
|
if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
|
||
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
|
failure(-1, @"请检查网络连接。");
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
[self prohibitipositionIntelligencers];
|
||
|
params = [self prohibitipositionBaseParmars:params];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params);
|
||
|
#endif
|
||
|
|
||
|
AFHTTPSessionManager *manager = [HttpRequestFacilitater requisitionExecutive];
|
||
|
[manager GET:method parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
||
|
BaseMatrix *baseModel = [BaseMatrix mtowardsrixAboutImpactbasket:responseObject];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\n%@", [baseModel toJSONBWSttr]);
|
||
|
#endif
|
||
|
success(baseModel);
|
||
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||
|
[self shankNetMistake:error method:method failure:failure];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (void)POST:(NSString *)method
|
||
|
params:(NSDictionary *)params
|
||
|
success:(void (^)(BaseMatrix *data))success
|
||
|
failure:(void (^)(NSInteger resCode, NSString *message))failure
|
||
|
{
|
||
|
if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
|
||
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
|
failure(-1, @"请检查网络连接。");
|
||
|
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
[self prohibitipositionIntelligencers];
|
||
|
params = [self prohibitipositionBaseParmars:params];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params);
|
||
|
#endif
|
||
|
|
||
|
AFHTTPSessionManager *manager = [HttpRequestFacilitater requisitionExecutive];
|
||
|
[manager POST:method parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
||
|
BaseMatrix *baseModel = [BaseMatrix mtowardsrixAboutImpactbasket:responseObject];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\n%@", [baseModel toJSONBWSttr]);
|
||
|
#endif
|
||
|
success(baseModel);
|
||
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||
|
[self shankNetMistake:error method:method failure:failure];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (void)DELETE:(NSString *)method
|
||
|
params:(NSDictionary *)params
|
||
|
success:(void (^)(BaseMatrix *data))success
|
||
|
failure:(void (^)(NSInteger resCode, NSString *message))failure
|
||
|
{
|
||
|
if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
|
||
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
|
failure(-1, @"请检查网络连接。");
|
||
|
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
[self prohibitipositionIntelligencers];
|
||
|
params = [self prohibitipositionBaseParmars:params];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params);
|
||
|
#endif
|
||
|
|
||
|
AFHTTPSessionManager *manager = [HttpRequestFacilitater requisitionExecutive];
|
||
|
[manager DELETE:method parameters:params headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
|
||
|
BaseMatrix *baseModel = [BaseMatrix mtowardsrixAboutImpactbasket:responseObject];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\n%@", [baseModel toJSONBWSttr]);
|
||
|
#endif
|
||
|
success(baseModel);
|
||
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
|
||
|
[self shankNetMistake:error method:method failure:failure];
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (void)request:(NSString *)url
|
||
|
method:(HttpRequestHelperMethod)method
|
||
|
params:(NSDictionary *)params
|
||
|
success:(void (^)(BaseMatrix *data))success
|
||
|
failure:(void (^)(NSInteger resCode, NSString *message))failure
|
||
|
{
|
||
|
switch (method) {
|
||
|
case HttpRequestHelperMethodGET: {
|
||
|
[self GET:url params:params success:success failure:failure];
|
||
|
}
|
||
|
break;
|
||
|
case HttpRequestHelperMethodPOST:{
|
||
|
[self POST:url params:params success:success failure:failure];
|
||
|
}
|
||
|
break;
|
||
|
case HttpRequestHelperMethodDELETE:{
|
||
|
[self DELETE:url params:params success:success failure:failure];
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
+ (void)request:(NSString *)path
|
||
|
method:(HttpRequestHelperMethod)method
|
||
|
params:(NSDictionary *)params
|
||
|
completion:(HttpRequestHelperCompletion)completion
|
||
|
{
|
||
|
[self request:path method:method params:params success:^(BaseMatrix *data) {
|
||
|
if (completion) {
|
||
|
completion(data, data.code, data.message);
|
||
|
}
|
||
|
} failure:^(NSInteger resCode, NSString *message) {
|
||
|
if (completion) {
|
||
|
completion(nil, resCode, message);
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
+ (void)prohibitipositionIntelligencers
|
||
|
{
|
||
|
AFHTTPSessionManager *client = [HttpRequestFacilitater requisitionExecutive];
|
||
|
if ([[AccountAbstractStorage instance] acquireCid].length > 0) {
|
||
|
[client.requestSerializer setValue:[[AccountAbstractStorage instance] acquireCid] forHTTPHeaderField:@"pub_uid"];
|
||
|
} else {
|
||
|
[client.requestSerializer setValue:nil forHTTPHeaderField:@"pub_uid"];
|
||
|
}
|
||
|
if ([[AccountAbstractStorage instance] acquireTicket].length > 0) {
|
||
|
[client.requestSerializer setValue:[[AccountAbstractStorage instance] acquireTicket] forHTTPHeaderField:@"pub_ticket"];
|
||
|
}else {
|
||
|
[client.requestSerializer setValue:nil forHTTPHeaderField:@"pub_ticket"];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
+ (NSDictionary*)prohibitipositionBaseParmars:(NSDictionary *)parmars
|
||
|
{
|
||
|
NSDictionary *defaultBasciParame = @{
|
||
|
@"os" : @"iOS",
|
||
|
@"osVersion" : [YYUtility systemVersion],
|
||
|
@"netType" : ([YYUtility networkPosition] == ReachableViaWiFi) ? @2 : @1,
|
||
|
@"ispType" : @([YYUtility automobilerierAplictowardsionraiser]),
|
||
|
@"channel" : [YYUtility acquireAplictowardsionProvenience] ? : @"",
|
||
|
@"model" : [YYUtility mtowardsrixConstitute],
|
||
|
@"deviceId" : [YYUtility instentiretytowardsionUniqueIdentifictowardsion],
|
||
|
@"appVersion" : [YYUtility appVersion],
|
||
|
@"app" : [YYUtility appName]
|
||
|
};
|
||
|
if (!parmars||![parmars isKindOfClass:[NSDictionary class]]){
|
||
|
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:defaultBasciParame];
|
||
|
return dic;
|
||
|
}
|
||
|
NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithDictionary:parmars];
|
||
|
for (NSString *parameKey in defaultBasciParame.allKeys) {
|
||
|
[dic setObject:defaultBasciParame[parameKey] forKey:parameKey];
|
||
|
}
|
||
|
return dic;
|
||
|
}
|
||
|
|
||
|
+ (void)shankNetMistake:(NSError *)error method:(NSString *)method
|
||
|
failure:(void (^)(NSInteger resCode, NSString *message))failure
|
||
|
{
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\n%@", error);
|
||
|
#endif
|
||
|
|
||
|
NSHTTPURLResponse *response = error.userInfo[@"com.alamofire.serialization.response.error.response"];
|
||
|
if (response && response.statusCode == 401) {
|
||
|
failure(response.statusCode, @"登录已过期。");
|
||
|
} else {
|
||
|
|
||
|
if (error.code == -1009 || error.code == -1001 || error.code == -1004 || error.code == -1003 || error.code == -1002 || error.code == 3840) {
|
||
|
failure(error.code, @"");
|
||
|
} else {
|
||
|
failure(error.code, error.localizedDescription.length > 0 ? error.localizedDescription : YMLocalizedString(@"Http_Request_Helper_4"));
|
||
|
}
|
||
|
NSString * code = error.code ? [NSString stringWithFormat:@"%ld", error.code] : @"9999";
|
||
|
NSString * des = error.description.length > 0 ? error.description : YMLocalizedString(@"Http_Request_Helper_5");
|
||
|
NSException *exception = [NSException exceptionWithName:@"229181"
|
||
|
reason:method
|
||
|
userInfo:@{@"code":code, @"desc":des}];
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
+ (void)postCommunictowardsionSolitaire:(NSString *)path
|
||
|
params:(NSString *)params
|
||
|
completion:(HttpRequestHelperCompletion)completion{
|
||
|
|
||
|
if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
|
||
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
|
completion(nil, -1, @"请检查网络连接。");
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
[self prohibitipositionIntelligencers];
|
||
|
NSDictionary *baseParams = [self prohibitipositionBaseParmars:nil];
|
||
|
#ifdef DEBUG
|
||
|
#endif
|
||
|
|
||
|
AFHTTPSessionManager *manager = [HttpRequestFacilitater requisitionExecutive];
|
||
|
NSString *url = API_HOST_URL;
|
||
|
NSString *urlPath = [NSString stringWithFormat:@"%@/%@", url ,path];
|
||
|
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"url:%@,parameter:%@",urlPath, baseParams);
|
||
|
#endif
|
||
|
|
||
|
__block NSString *requestUrl = @"";
|
||
|
[baseParams.allKeys enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||
|
NSString *value = baseParams[obj];
|
||
|
requestUrl = [requestUrl stringByAppendingString:[NSString stringWithFormat:@"%@=%@&", obj, value]];
|
||
|
}];
|
||
|
|
||
|
urlPath = [NSString stringWithFormat:@"%@?%@", urlPath, requestUrl];
|
||
|
|
||
|
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlPath parameters:baseParams error:nil];
|
||
|
request.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];
|
||
|
|
||
|
[request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
|
||
|
|
||
|
if ([[AccountAbstractStorage instance] acquireCid].length > 0) {
|
||
|
[request setValue:[[AccountAbstractStorage instance] acquireCid] forHTTPHeaderField:@"pub_uid"];
|
||
|
[manager.requestSerializer setValue:[[AccountAbstractStorage instance] acquireCid] forHTTPHeaderField:@"pub_uid"];
|
||
|
} else {
|
||
|
[request setValue:nil forHTTPHeaderField:@"pub_uid"];
|
||
|
}
|
||
|
if ([[AccountAbstractStorage instance] acquireTicket].length > 0) {
|
||
|
[request setValue:[[AccountAbstractStorage instance] acquireTicket] forHTTPHeaderField:@"pub_ticket"];
|
||
|
[manager.requestSerializer setValue:[[AccountAbstractStorage instance] acquireTicket] forHTTPHeaderField:@"pub_ticket"];
|
||
|
}else {
|
||
|
[request setValue:nil forHTTPHeaderField:@"pub_ticket"];
|
||
|
}
|
||
|
|
||
|
NSError *error;
|
||
|
|
||
|
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
|
||
|
|
||
|
[[manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
|
||
|
if (responseObject) {
|
||
|
BaseMatrix *baseModel = [BaseMatrix mtowardsrixAboutImpactbasket:responseObject];
|
||
|
#ifdef DEBUG
|
||
|
NSLog(@"\n%@", [baseModel toJSONBWSttr]);
|
||
|
#endif
|
||
|
if (baseModel.code == 200) {
|
||
|
if (completion) {
|
||
|
completion(baseModel, 200, nil);
|
||
|
}
|
||
|
} else {
|
||
|
if (completion) {
|
||
|
completion(nil, baseModel.code, baseModel.message);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if (error) {
|
||
|
[self shankNetMistake:error method:path failure:^(NSInteger resCode, NSString *message) {
|
||
|
completion(nil, resCode, message);
|
||
|
}];
|
||
|
}
|
||
|
}] resume];
|
||
|
}
|
||
|
|
||
|
@end
|