// // HttpRequestHelper.m // xplan-ios // // Created by zu on 2021/9/3. // #import "HttpRequestHelper.h" #import "ApiHost.h" #import "YYUtility.h" #import "AccountInfoStorage.h" #import "YYReachability.h" #import #import @implementation HttpRequestHelper +(AFHTTPSessionManager *)requestManager { 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 (^)(BaseModel *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, @"请检查网络连接。"); NSException * extrion= [NSException exceptionWithName:method reason:@"接口没有网络" userInfo:nil]; [Bugly reportException:extrion]; }); return; } [self configHeaders]; params = [self configBaseParmars:params]; #ifdef DEBUG NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params); #endif AFHTTPSessionManager *manager = [HttpRequestHelper requestManager]; [manager GET:method parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { BaseModel *baseModel = [BaseModel modelWithDictionary:responseObject]; #ifdef DEBUG NSLog(@"\n%@", [baseModel toJSONString]); #endif success(baseModel); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [self handleNetError:error method:method failure:failure]; }]; } + (void)POST:(NSString *)method params:(NSDictionary *)params success:(void (^)(BaseModel *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, @"请检查网络连接。"); NSException * extrion= [NSException exceptionWithName:method reason:@"接口没有网络" userInfo:nil]; [Bugly reportException:extrion]; }); return; } [self configHeaders]; params = [self configBaseParmars:params]; #ifdef DEBUG NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params); #endif AFHTTPSessionManager *manager = [HttpRequestHelper requestManager]; [manager POST:method parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { BaseModel *baseModel = [BaseModel modelWithDictionary:responseObject]; #ifdef DEBUG NSLog(@"\n%@", [baseModel toJSONString]); #endif success(baseModel); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [self handleNetError:error method:method failure:failure]; }]; } + (void)DELETE:(NSString *)method params:(NSDictionary *)params success:(void (^)(BaseModel *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, @"请检查网络连接。"); NSException * extrion= [NSException exceptionWithName:method reason:@"接口没有网络" userInfo:nil]; [Bugly reportException:extrion]; }); return; } [self configHeaders]; params = [self configBaseParmars:params]; #ifdef DEBUG NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params); #endif AFHTTPSessionManager *manager = [HttpRequestHelper requestManager]; [manager DELETE:method parameters:params headers:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { BaseModel *baseModel = [BaseModel modelWithDictionary:responseObject]; #ifdef DEBUG NSLog(@"\n%@", [baseModel toJSONString]); #endif success(baseModel); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [self handleNetError:error method:method failure:failure]; }]; } + (void)request:(NSString *)url method:(HttpRequestHelperMethod)method params:(NSDictionary *)params success:(void (^)(BaseModel *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:^(BaseModel *data) { if (completion) { completion(data, data.code, data.message); } } failure:^(NSInteger resCode, NSString *message) { if (completion) { completion(nil, resCode, message); } }]; } + (void)configHeaders { AFHTTPSessionManager *client = [HttpRequestHelper requestManager]; if ([[AccountInfoStorage instance] getUid].length > 0) { [client.requestSerializer setValue:[[AccountInfoStorage instance] getUid] forHTTPHeaderField:@"pub_uid"]; } else { [client.requestSerializer setValue:nil forHTTPHeaderField:@"pub_uid"]; } if ([[AccountInfoStorage instance] getTicket].length > 0) { [client.requestSerializer setValue:[[AccountInfoStorage instance] getTicket] forHTTPHeaderField:@"pub_ticket"]; }else { [client.requestSerializer setValue:nil forHTTPHeaderField:@"pub_ticket"]; } } + (NSDictionary*)configBaseParmars:(NSDictionary *)parmars { NSDictionary *defaultBasciParame = @{ @"os" : @"iOS", @"osVersion" : [YYUtility systemVersion], @"netType" : ([YYUtility networkStatus] == ReachableViaWiFi) ? @2 : @1, @"ispType" : @([YYUtility carrierIdentifier]), @"channel" : [YYUtility getAppSource] ? : @"", @"model" : [YYUtility modelName], @"deviceId" : [YYUtility deviceUniqueIdentification], @"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)handleNetError:(NSError *)error method:(NSString *)method failure:(void (^)(NSInteger resCode, NSString *message))failure { #ifdef DEBUG NSLog(@"\n%@", error); #endif // 别问,反正 oauth/ticket 接口要通过这种方式取错误码。 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 : @"大鹅开小差中~请稍后再试"); } NSString * code = error.code ? [NSString stringWithFormat:@"%ld", error.code] : @"9999"; NSString * des = error.description.length > 0 ? error.description : @"接口报错信息未知"; NSException *exception = [NSException exceptionWithName:@"229181" reason:method userInfo:@{@"code":code, @"desc":des}]; [Bugly reportException:exception]; } } /// 增加或编辑技能卡专用接口 Post 请求参数放入到 body 里 使用 application/json 类型传递 /// @param path 请求地址 /// @param params 参数 /// @param completion 回调 + (void)postSkillCard:(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 configHeaders]; NSDictionary *baseParams = [self configBaseParmars:nil]; #ifdef DEBUG // NSLog(@"\nmethod:\n%@\nparameter:\n%@", method, params); #endif AFHTTPSessionManager *manager = [HttpRequestHelper requestManager]; 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 ([[AccountInfoStorage instance] getUid].length > 0) { [request setValue:[[AccountInfoStorage instance] getUid] forHTTPHeaderField:@"pub_uid"]; [manager.requestSerializer setValue:[[AccountInfoStorage instance] getUid] forHTTPHeaderField:@"pub_uid"]; } else { [request setValue:nil forHTTPHeaderField:@"pub_uid"]; } if ([[AccountInfoStorage instance] getTicket].length > 0) { [request setValue:[[AccountInfoStorage instance] getTicket] forHTTPHeaderField:@"pub_ticket"]; [manager.requestSerializer setValue:[[AccountInfoStorage instance] getTicket] 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) { BaseModel *baseModel = [BaseModel modelWithDictionary:responseObject]; #ifdef DEBUG NSLog(@"\n%@", [baseModel toJSONString]); #endif if (baseModel.code == 200) { if (completion) { completion(baseModel, 200, nil); } } else { if (completion) { completion(nil, baseModel.code, baseModel.message); } } } if (error) { [self handleNetError:error method:path failure:^(NSInteger resCode, NSString *message) { completion(nil, resCode, message); }]; } }] resume]; } @end