网络请求封装错误码

+(void)taoqipost:(NSString *)url params:(NSMutableDictionary *)params controller:(UIViewController *)VC success:(void (^)(id _Nullable))success errcode:(void (^)(id _Nullable))errcode failure:(void (^)(NSError * _Nullable))failure
{
    
    
    // 1.创建请求管理者
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求数据格式为json
    manager.responseSerializer = [AFJSONResponseSerializer serializer];//响应数据格式
    manager.requestSerializer.timeoutInterval = 10.0;//超时时间10秒
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"application/json", @"text/json" , @"text/html", @"text/plain", @"text/javascript", nil]];
    
    NSDictionary *commonParam = [NSDictionary dictionaryWithObjectsAndKeys:
                                 gDevId,  @"devid",
                                 gToken,  @"token",
                                 [NSNumber numberWithInt:DEV_TYPE], @"devtype",
                                 [NSNumber numberWithInt:APPVER], @"appver",
                                 [NSNumber numberWithInt:APP_ID], @"appid",
                                 nil];
    NSLog(@"token==%@",gToken);
    
    NSMutableDictionary *bodyDic = [NSMutableDictionary dictionaryWithDictionary:commonParam];//字典合并
    for (id key in params) {
        id value=[params objectForKey:key];
        [bodyDic setObject:value forKey:key];
    }
    NSLog(@"拼接的字典%@",bodyDic);
    [manager POST:url parameters:bodyDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
         if ([responseObject[@"errcode"] integerValue]==0) {
             
             if (success) {
                 success(responseObject);//code==0的时候传出去直接解析
             }
         }else{
             if (errcode) {
                 errcode(responseObject[@"errcode"]);//code!=0的时候只要传出错误码就行
             }
         }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);//网络错误等等
        }
        
    }];

你可能感兴趣的:(网络请求封装错误码)