// // RequestApi.m // yiliantiPersonal // // Created by lirenbo on 2016/12/13. // Copyright © 2016年 ping. All rights reserved. // #import "RequestApi.h" //全局方法 #import "GlobalMethod.h" //#import "GlobalMethod+Version.h" //请求单例 #import "RequestInstance.h" //登陆页面 #import "xingchuangke-Swift.h" @implementation RequestApi #pragma mark - 网络请求 + (void)postUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure { [self postUrl:URL delegate:delegate parameters:parameters returnALL:false isGet:false isJson:false constructingBodyWithBlock:nil success:success failure:failure]; } // post + (void)postJsonUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure{ [self postUrl:URL delegate:delegate parameters:parameters returnALL:false isGet:false isJson:true constructingBodyWithBlock:nil success:success failure:failure]; } + (void)getUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure { [self postUrl:URL delegate:delegate parameters:parameters returnALL:false isGet:true isJson:false constructingBodyWithBlock:nil success:success failure:failure]; } //上传图片 + (void)postUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id formData))block success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure { [self postUrl:URL delegate:delegate parameters:parameters returnALL:true isGet:false isJson:false constructingBodyWithBlock:block success:success failure:failure]; } #pragma mark - 网络请求 返回全部 // post + (void)postUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters returnALL:(BOOL)returnAll success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure{ [self postUrl:URL delegate:delegate parameters:parameters returnALL:returnAll isGet:false isJson:false constructingBodyWithBlock:nil success:success failure:failure]; } // post + (void)postJsonUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters returnALL:(BOOL)returnAll success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure{ [self postUrl:URL delegate:delegate parameters:parameters returnALL:returnAll isGet:false isJson:false constructingBodyWithBlock:nil success:success failure:failure]; } //get + (void)getUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters returnALL:(BOOL)returnAll success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure{ [self postUrl:URL delegate:delegate parameters:parameters returnALL:returnAll isGet:false isJson:false constructingBodyWithBlock:nil success:success failure:failure]; } //上传图片 + (void)postUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters returnALL:(BOOL)returnAll constructingBodyWithBlock:(void (^)(id formData))block success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure{ [self postUrl:URL delegate:delegate parameters:parameters returnALL:returnAll isGet:false isJson:false constructingBodyWithBlock:block success:success failure:failure]; } + (void)postUrl:(NSString *)URL delegate:(id )delegate parameters:(NSDictionary *)parameters returnALL:(BOOL)returnAll isGet:(BOOL)isGet isJson:(BOOL)isJson constructingBodyWithBlock:(void (^)(id formData))block success:(void (^)(NSDictionary * response, id mark))success failure:(void (^)(NSString * errorStr, id mark))failure { //设置请求头 parameters = [self setInitHead:parameters]; //拼接参数 NSString * urlNew =[URL hasPrefix:@"http"]?URL:[NSString stringWithFormat:@"%@%@",URL_HEAD,URL]; //走回调 隐藏progress [GlobalMethod performSelector:@"protocolWillRequest" delegate:delegate]; //选择请求方式 [self switchRequest:urlNew parameters:parameters isGet:isGet isJson:isJson constructingBodyWithBlock:block success:^(NSURLSessionDataTask *task, id responseObject) { //上拉 下拉 刷新 [self endRefresh:delegate]; //返回数据 NSDictionary * dicResponse = nil; if ([responseObject isKindOfClass:[NSDictionary class]]) { dicResponse = responseObject; }else{ dicResponse = [GlobalMethod exchangeDataToDic:responseObject]; } if (dicResponse == nil ) { //走回调 隐藏progress [self requestFailDelegate:delegate errorStr:@"数据请求失败" errorCode:@"0" failure:failure]; return ; } //判断请求状态 if([@([dicResponse[RESPONSE_CODE] integerValue]) isEqualToNumber:RESPONSE_CODE_200]){ [self requestSuccessDelegate:delegate responseDic:dicResponse success:success]; return; } if([@([dicResponse[RESPONSE_CODE] integerValue]) isEqualToNumber:RESPONSE_CODE_NEGATIVE100]){ //重新登陆 [GlobalMethod relogin]; return; } if (![GlobalMethod isLoginSuccess]) { [GB_Nav pushViewController:[LoginViewController new] animated:true]; } { //走回调 请求失败 [self requestFailDelegate:delegate errorStr:dicResponse[RESPONSE_MESSAGE] errorCode:isNum(dicResponse[RESPONSE_CODE])?[NSString stringWithFormat:@"%@",dicResponse[RESPONSE_CODE]]:@"0" failure:failure]; return; } } failure:^(NSURLSessionDataTask *task, NSError *error){ //上拉 下拉 刷新 [self endRefresh:delegate]; NSLog(@"%@",error.userInfo); //走回调 网络连接失败 [self requestFailDelegate:delegate errorStr:@"服务器接口出错" errorCode:@"0" failure:failure]; }]; } #pragma mark - success + (void)requestSuccessDelegate:(id)delegate responseDic:(NSDictionary *)responseDic success:(void (^)(NSDictionary * response, id mark))success{ //走回调 请求成功 [GlobalMethod performSelector:@"protocolDidRequestSuccess" delegate:delegate]; if (success != nil) { success(responseDic,nil); } [self showNoResult:delegate]; } #pragma mark - fail + (void)requestFailDelegate:(id)delegate errorStr:(NSString *)strError errorCode:(NSString *)errorCode failure:(void (^)(NSString * errorStr, id mark))failure{ [GlobalMethod performSelector:@"protocolDidRequestFailure:" delegate:delegate object:strError isHasReturn:false]; if (failure != nil) { failure(strError,errorCode); [GlobalMethod showAlert:strError]; } [self showNoResult:delegate]; } #pragma mark - 上拉 下拉刷新 + (void)endRefresh:(id)delegate{ if (delegate != nil && [delegate respondsToSelector:@selector(endRefreshing)]) { [delegate performSelector:@selector(endRefreshing)]; } } #pragma mark - 展示无数据 + (void)showNoResult:(id)delegate{ [GlobalMethod performSelector:@"showNoResult" delegate:delegate]; } #pragma mark - 选择请求 + (void)switchRequest:(NSString *)URLString parameters:(id)parameters isGet:(BOOL)isGet isJson:(BOOL)isJson constructingBodyWithBlock:(void (^)(id formData))block success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure{ if (isJson) { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ; manager.requestSerializer = [AFJSONRequestSerializer serializer]; NSString *token =[GlobalData sharedInstance].GB_Key; if ([GlobalMethod isLoginSuccess]) { [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@", token] forHTTPHeaderField:@"userId"]; } [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"phoneType"]; [manager.requestSerializer setValue:[GlobalMethod getVersion] forHTTPHeaderField:@"apkVersion"]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/html",@"text/plain",@"text/javascript",nil]; manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; [manager POST:URLString parameters:parameters progress:nil success:success failure:failure]; // [manager POST:URLString parameters:parameters progress:nil success:success failure:failure]; }else{ NSURLSessionDataTask * task; AFHTTPSessionManager *manager = [RequestInstance sharedInstance]; manager.responseSerializer = [AFJSONResponseSerializer serializer];//申明返回的结果是json类型 NSString *token =[GlobalData sharedInstance].GB_Key; if ([GlobalMethod isLoginSuccess]) { [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@", token] forHTTPHeaderField:@"userId"]; } [manager.requestSerializer setValue:@"1" forHTTPHeaderField:@"phoneType"]; [manager.requestSerializer setValue:[GlobalMethod getVersion] forHTTPHeaderField:@"apkVersion"]; if (isGet == true) { task = [[RequestInstance sharedInstance] GET:URLString parameters:parameters progress:nil success:success failure:failure]; } else if (block == nil) { task = [[RequestInstance sharedInstance] POST:URLString parameters:parameters progress:nil success:success failure:failure]; } else { task = [[RequestInstance sharedInstance] POST:URLString parameters:parameters constructingBodyWithBlock:block progress:nil success:success failure:failure]; } } } #pragma mark - 拼接基础头字符串 + (NSMutableDictionary *)setInitHead:(NSDictionary *)dicParameters { if (dicParameters == nil){ dicParameters = [NSDictionary dictionary]; } NSMutableDictionary * muDic = [NSMutableDictionary dictionaryWithDictionary:dicParameters]; [self fetchSystem:muDic]; return muDic; } #pragma mark - 拼接头数据 //拼接苹果系统数据 + (void)fetchSystem:(NSMutableDictionary *)muDic{ // [muDic setObject:@"1" forKey:@"phoneType"]; // NSString *currentVersion =[GlobalMethod getVersion]; // [muDic setObject:currentVersion forKey:@"apkVersion"]; } @end