//
//  NetworkRequest.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2019/11/18.
//  Copyright © 2019 雷俊博. All rights reserved.
//

#import "NetworkRequest.h"
#import "NSString+Timestamp.h"

#define PREFIX @"--"
#define BOUNDARY @"Boundary+7B5F9DCA8D60931B"
#define LINE_END @"  "
@implementation NetworkRequest

+ (void)PostEssResult:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
//    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
//    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
//    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:params error:nil];
    request.timeoutInterval = 10.f;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    //[request setValue:[[MyMD5 md5:[self GetStringBuilderTime:timeString params:params]] lowercaseString] forHTTPHeaderField:@"sign"];
    //[request setValue:timeString forHTTPHeaderField:@"timestamp"];
    NSString *essCardToken =  [UserDefault valueForKey:@"essCardToken"];
    if ([UserDefault valueForKey:@"essCardToken"] && essCardToken.length > 0) {
        [request setValue:essCardToken forHTTPHeaderField:@"token"];
    }
    NSString *user=[NSString stringWithFormat:@"%@#%@#kksh",@"iOS",[Tools getVersionCode]];
    //[request setValue:user forHTTPHeaderField:@"user-agent"];
    
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        NSLog(@"-----responseObject===%@+++++",responseObject);
        if (!error) {
            success(responseObject);
        } else {
            NSLog(@"请求失败error=%@", error);
            failure(error);
        }
    }];

    [task resume];
}

+ (void)PostGasStation:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:params error:nil];
    request.timeoutInterval = 10.f;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:[[MyMD5 md5:[self GetStringBuilderTime:timeString params:params]] lowercaseString] forHTTPHeaderField:@"sign"];
    [request setValue:timeString forHTTPHeaderField:@"timestamp"];
    NSString *GasToken =  [UserDefault valueForKey:@"GasToken"];
    if ([UserDefault valueForKey:@"GasToken"] && GasToken.length > 0) {
        [request setValue:GasToken forHTTPHeaderField:@"token"];
    }
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        NSLog(@"-----responseObject===%@+++++",responseObject);
        if (!error) {
            success(responseObject);
        } else {
            NSLog(@"请求失败error=%@", error);
            failure(error);
            
        }
        
    }];

    [task resume];
}
//post请求
+ (void)PostFomart:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/xml",@"text/html", @"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 10.f;
    [manager.requestSerializer setValue:[[MyMD5 md5:[self GetStringBuilderTime:timeString params:params]] lowercaseString] forHTTPHeaderField:@"sign"];
    [manager.requestSerializer setValue:timeString forHTTPHeaderField:@"timestamp"];
    [manager POST:urlString parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *data= [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
        
        [formData appendPartWithFormData:data name:@"Data"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
            NSLog(@"-----responseObject===%@+++++",dic);
            success(dic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}
+ (void)PostNomal:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:params error:nil];
    request.timeoutInterval = 10.f;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:[[MyMD5 md5:[self GetStringBuilderTime:timeString params:params]] lowercaseString] forHTTPHeaderField:@"sign"];
    [request setValue:timeString forHTTPHeaderField:@"timestamp"];
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        NSLog(@"-----responseObject===%@+++++",responseObject);
        if (!error) {
            success(responseObject);
        } else {
            NSLog(@"请求失败error=%@", error);
            failure(error);
            
        }
        
    }];

    [task resume];
}
+ (void)PostNo:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure{
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:params error:nil];
    request.timeoutInterval = 10.f;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        NSLog(@"-----responseObject===%@+++++",responseObject);
        if (!error) {
            success(responseObject);
        } else {
            NSLog(@"请求失败error=%@", error);
            failure(error);
            
        }
        
    }];

    [task resume];
}

+ (NSString *)GetStringBuilderTime:(NSString *)timeString  params:(NSDictionary *)params{
    NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];
    [paramDic addEntriesFromDictionary:params];
    [paramDic  setValue:timeString forKey:@"timestamp"];

    NSArray *result = [paramDic.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
             
         NSLog(@"%@~%@",obj1,obj2); //3~4 2~1 3~1 3~2
         
         return [obj1 compare:obj2]; //升序
         
     }];
   NSString *StringBuilder = @"KkweFace95271124";
   for (int i = 0; i < result.count; i++) {
     NSString *key = result[i];
     StringBuilder = [NSString stringWithFormat:@"%@%@",StringBuilder,paramDic[key]];
   }
   StringBuilder = [NSString stringWithFormat:@"%@%@",StringBuilder,@"KkweFace95271124"];
   return  StringBuilder;
}
//post请求
+ (void)SBAuthPost:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure {
    //AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    AFHTTPSessionManager *manager = nil;
    
    if ([urlString containsString:@"https://socialsecurity.weface.com.cn:8066"]) {
        manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"https://socialsecurity.weface.com.cn:8066"]];
        //[manager setSecurityPolicy:[NetworkRequest customSecurityPolicy]] ;
    }else{
        manager = [AFHTTPSessionManager manager];
        
    }

    NSMutableDictionary* newParams = [NSMutableDictionary dictionaryWithDictionary:params];
    
    
    /*
    //加密请求设置
    
    [manager.requestSerializer setValue:@"wefacev2" forHTTPHeaderField:@"version"];
    [manager.requestSerializer setValue:[NSString getNowTimeTimestamp3] forHTTPHeaderField:@"timestamp"];
    //random
    NSInteger randomInt = arc4random() % 10001;
    NSString* timeStamp = [NSString getNowTimeTimestamp3];
    
    NSString* nonce = [NSString stringWithFormat:@"%@%@",timeStamp,@(randomInt)];
    
    [manager.requestSerializer setValue:nonce forHTTPHeaderField:@"nonce"];
    [manager.requestSerializer setValue:timeStamp forHTTPHeaderField:@"timestamp"];
    [manager.requestSerializer setValue:[UserDefault objectForKey:@"loginToken"] forHTTPHeaderField:@"token"];
    NSLog(@"loginToken:%@",[UserDefault objectForKey:@"loginToken"]);
//    [manager.requestSerializer setValue:[UserDefault objectForKey:@"loginToken"] forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:[Tools getDeviceUUID] forHTTPHeaderField:@"unid"];
    
    //[manager.requestSerializer setValue:@"application/json" forHTT
     
     
     
     PHeaderField:@"Content-Type"];

    if (params.allKeys.count > 0) {
        
        
        for (NSString* key in newParams.allKeys) {
            if ([key isEqualToString:@"now_address"] || [key isEqualToString:@"phone_number"] || [key isEqualToString:@"province_name"] || [key isEqualToString:@"city_name"] || [key isEqualToString:@"county_name"] || [key isEqualToString:@"town_name"] || [key isEqualToString:@"village_name"] || [key isEqualToString:@"company_name"] ||[key isEqualToString:@"province_id"] || [key isEqualToString:@"city_id"] || [key isEqualToString:@"county_id"] || [key isEqualToString:@"town_id"] || [key isEqualToString:@"telephone"] || [key isEqualToString:@"telphone"] || [key isEqualToString:@"validateCode"] || [key isEqualToString:@"content_text"] || [key isEqualToString:@"cus_name"]) {
                NSString* value = [NSString stringWithFormat:@"%@",newParams[key]];
                if (![Tools isBlankString:value]) {
                    value = [GMSm4Utils ecbEncryptText:value key:SMKey].lowercaseString;
                    [newParams setObject:value forKey:key];
                }
                
            }
        }
        
        
        NSArray *allKeyArray = [newParams allKeys];
        //序列化器对数组进行排序的block 返回值为排序后的数组
        NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSComparisonResult resuest = [obj1 compare:obj2];
            return resuest;
        }];
        NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
        
        NSString* versionsign = nil;
        
        for (NSString *sortsing in afterSortKeyArray) {
            
            if (![Tools dx_isNullOrNilWithObject:newParams[sortsing]]) {
                if (versionsign == nil) {
                    versionsign = [NSString stringWithFormat:@"%@=%@",sortsing,newParams[sortsing]];
                }else{
                    versionsign = [NSString stringWithFormat:@"%@&%@=%@",versionsign,sortsing,newParams[sortsing]];
                }
            }
            
        }
        
        versionsign = [NSString stringWithFormat:@"%@&%@&%@&%@",timeStamp,nonce,versionsign,ParamKey];
        
        NSLog(@"加密前:%@",versionsign);
        
         versionsign = [GMSm3Utils hashWithString:versionsign].lowercaseString;

        
        NSLog(@"versionsign:%@",versionsign);
        
        [manager.requestSerializer setValue:versionsign forHTTPHeaderField:@"versionsign"];
        
    }
   
    
    
    */
    manager.requestSerializer.timeoutInterval = 8;
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    
    
    
    [manager POST:urlString parameters:newParams headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject isKindOfClass:[NSData class]]) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            if ([dic isKindOfClass:[NSDictionary class]]) {
                NSString *code = [NSString stringWithFormat:@"%@",dic[@"code"]];
                if ([code isEqualToString:@"3000"]) {
                    NSLog(@"token失效url:%@",urlString);
                    [[NSNotificationCenter defaultCenter] postNotificationName:KKSBLoginTokenOutTimeNotificationName object:nil];
                }
                
            }
            success(dic);
        }else{
            if ([responseObject isKindOfClass:[NSDictionary class]]) {
                NSString *code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                if ([code isEqualToString:@"3000"]) {
                    NSLog(@"token失效url:%@",urlString);
                    [[NSNotificationCenter defaultCenter] postNotificationName:KKSBLoginTokenOutTimeNotificationName object:nil];
                }
                
            }
            
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}
+ (void)NewsPost:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    NSError *error;
    
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:nil error:nil];
    
    //设置超时时长

    request.timeoutInterval= 30;

    //设置上传数据type

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    //设置接受数据type

   // [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    
    //将对象设置到requestbody中 ,主要是这不操作
    
    

    if (params) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
        
        NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    }
    

    NSURLSessionDataTask* dataTask =[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
        
    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
        
    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        if (error) {
            failure(error);
        }else{
            if ([responseObject isKindOfClass:[NSData class]]) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                success(dic);
            }else{
                success(responseObject);
            }
        }
    }];
    
    [dataTask resume];
    
}

+ (void)Post:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *strHeader = [NSString stringWithFormat:@"%@#%@",[IPTool getSystemVersion],buildAndVersion];
    NSLog(@"strHeader:%@",strHeader);
    [manager.requestSerializer setValue:strHeader forHTTPHeaderField:@"User-Agent"];
    
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)Post2:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = nil;
    
    if ([urlString containsString:@"https://socialsecurity.weface.com.cn:8066"]) {
        manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:urlString]];
        //[manager setSecurityPolicy:[NetworkRequest customSecurityPolicy]] ;
    }else{
        manager = [AFHTTPSessionManager manager];
        
    }
    
    [manager.requestSerializer setValue:[Tools getDeviceUUID] forHTTPHeaderField:@"unid"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            success(dic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         failure(error);
    }];
}


+ (void)Post3:(NSString *)urlString Params:(NSDictionary *)params Token:(NSString *)token Success:( void (^)(id responseObject))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"tuiyiban_1" forHTTPHeaderField:@"from-App"];

    [manager POST:urlString parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSString *key in [params allKeys])
        {
            if ([[params objectForKey:key] isKindOfClass:[NSString class]]) {
                [formData appendPartWithFormData:[[params objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else if ([[params objectForKey:key] isKindOfClass:[NSArray class]]){
                NSArray *array = [params objectForKey:key];
                for (int i =0; i < array.count; i++) {
                    NSString *str = array[i];
                    [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
                }
                
            }else if ([[params objectForKey:key] isKindOfClass:[NSNumber class]]){
                NSString *str = [NSString stringWithFormat:@"%@",[params objectForKey:key]];
                [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else{
                [formData appendPartWithFormData:[params objectForKey:key] name:key];
            }
            
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject)
        {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}


+ (void)Post4:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    
    manager.requestSerializer.timeoutInterval = 5;
    
     [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
                   NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                   success(dic);
               }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)Post5:(NSString *)urlString Params:(NSDictionary *)params Token:(NSString *)token Success:( void (^)(id responseObject))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}

+ (void)Post6:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    
    NSError *error;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];

    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

    //AES加密
    jsonString = [EnercryTool AES256Encrypt:jsonString key:@"abcdtnfo23255625"];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:nil error:nil];

    //设置超时时长

    request.timeoutInterval= 30;

    //设置上传数据type

    [request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];

    //设置接受数据type

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];


    //将对象设置到requestbody中 ,主要是这不操作

    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDataTask* dataTask =[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {

    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {

    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

        if (error) {
            failure(error);
        }else{
            success(responseObject);
        }
    }];

    [dataTask resume];
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//
//
//    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        success(responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        failure(error);
//    }];

    
}

+ (void)Post66:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    
    NSError *error;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];

    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

    //AES加密
    jsonString = [EnercryTool AES256Encrypt:jsonString key:@"abcdtnfo23255625"];

    AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:nil error:nil];

    //设置超时时长

    request.timeoutInterval= 30;

    //设置上传数据type

    [request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];

    //设置接受数据type

    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];


    //将对象设置到requestbody中 ,主要是这不操作

    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDataTask* dataTask =[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {

    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {

    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {

        if (error) {
            failure(error);
        }else{
            success(responseObject);
        }
    }];

    [dataTask resume];
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//
//
//    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        success(responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        failure(error);
//    }];

    
}


+ (void)Post7:(NSString *)urlString Params:(NSDictionary *)params Token:(NSString *)token Success:( void (^)(id responseObject))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"kksh" forHTTPHeaderField:@"from-App"];

    [manager POST:urlString parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSString *key in [params allKeys])
        {
            if ([[params objectForKey:key] isKindOfClass:[NSString class]]) {
                [formData appendPartWithFormData:[[params objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else if ([[params objectForKey:key] isKindOfClass:[NSArray class]]){
                NSArray *array = [params objectForKey:key];
                for (int i =0; i < array.count; i++) {
                    NSString *str = array[i];
                    [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
                }
                
            }else if ([[params objectForKey:key] isKindOfClass:[NSNumber class]]){
                NSString *str = [NSString stringWithFormat:@"%@",[params objectForKey:key]];
                [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else{
                [formData appendPartWithFormData:[params objectForKey:key] name:key];
            }
            
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject)
        {

                   
                   success(responseObject);
               }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}
+ (void)Post8:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//      [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
//      manager.requestSerializer = [AFHTTPRequestSerializer serializer];
 //     manager.responseSerializer = [AFHTTPResponseSerializer serializer];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
    
    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject isKindOfClass:[NSDictionary class]]) {
                   success(responseObject);
               }else if ([responseObject isKindOfClass:[NSData class]]){
                   NSDictionary*dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                   success(dic);
               }else{
                   success(responseObject);
               }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)Post9:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//      [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
      manager.requestSerializer = [AFHTTPRequestSerializer serializer];
      manager.responseSerializer = [AFHTTPResponseSerializer serializer];
     //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
    
    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         if ([responseObject isKindOfClass:[NSDictionary class]]) {
                   success(responseObject);
               }else if ([responseObject isKindOfClass:[NSData class]]){
                   NSDictionary*dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                   success(dic);
               }else{
                   success(responseObject);
               }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

//喜马拉雅
+ (void)Post10:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    
    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

+ (void)Post11:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"kksh" forHTTPHeaderField:@"from-App"];

    [manager POST:urlString parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSString *key in [params allKeys])
        {
            if ([[params objectForKey:key] isKindOfClass:[NSString class]]) {
                [formData appendPartWithFormData:[[params objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else if ([[params objectForKey:key] isKindOfClass:[NSArray class]]){
                NSArray *array = [params objectForKey:key];
                for (int i =0; i < array.count; i++) {
                    NSString *str = array[i];
                    [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
                }
                
            }else if ([[params objectForKey:key] isKindOfClass:[NSNumber class]]){
                NSString *str = [NSString stringWithFormat:@"%@",[params objectForKey:key]];
                [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else{
                [formData appendPartWithFormData:[params objectForKey:key] name:key];
            }
            
        }
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject)
        {
            NSLog(@"=====%@",responseObject);
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}

+ (void)Post12:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager.requestSerializer setValue:[UserDefault objectForKey:@"loginToken"] forHTTPHeaderField:@"token"];
    
    
    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL];
            success(dic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}


//金币
+(void)POST13:(NSString *)url params:(NSDictionary *)params completionHandler:(void (^)(id responseObject, NSError *error))complete
{
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"image/jpeg",@"text/plain", nil];
   [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    if ([url containsString:@"score/increateScore"]) {
        NSString* oldAgent =  [manager.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        app_Version = [app_Version stringByReplacingOccurrencesOfString:@"." withString:@""];
        NSString*newAgent = [NSString stringWithFormat:@"kkLifeIos#%@#%@",app_Version,oldAgent];
        [manager.requestSerializer setValue:newAgent forHTTPHeaderField:@"User-Agent"];
    }
    
    NSLog(@"url:%@",url);
    NSLog(@"prams:%@",params);
    
    
    
    [manager POST:url parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"JSON:%@",responseObject);
        
       
      
        
        complete(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Error: %@",error);
        complete(nil,error);
    }];
}

+ (void)Post14:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject isKindOfClass:[NSData class]]) {
            NSDictionary*dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            success(dic);
        }else{
            success(responseObject);
        }
       
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
}


+ (void)Post15:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
//
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//
//    [manager POST:urlString parameters:params headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        if ([responseObject isKindOfClass:[NSData class]]) {
//            NSDictionary*dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
//            success(dic);
//        }else{
//            success(responseObject);
//        }
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        failure(error);
//    }];
    
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:params error:nil];
    request.timeoutInterval = 10.f;
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        NSLog(@"-----responseObject===%@+++++",responseObject);
        if (!error) {
            success(responseObject);
        } else {
            NSLog(@"请求失败error=%@", error);
            failure(error);
            
        }
        
    }];

    [task resume];

}
//app列表信息
+(void)getAppInfo{
    NSString *urlString = @"https://web.kanface.com:444/app/install/appInstallSave";
    NSDictionary *params = @{
        @"oaid":[Tools getDeviceOaid],
        @"deviceNo":[Tools getDeviceUUID],
        @"terminalType":@(2),
        @"appTag":@"kksh",
        @"userId":@([getPeopleID PeopleID]),
        @"telephone": [EnercryTool AES256Encrypt:[getPeopleID PeopleIphoneNmber] key:DESKEY],
        @"appCategoryInfo":@"",
        @"deviceBrand":[IPTool getCurrentDeviceModel],
        @"deviceBrandType":[IPTool getCurrentDeviceType],
        @"totalSpace":[Tools getGBString:[Tools getAllSpace]],
        @"availableSpace":[Tools getGBString:[Tools getCanUseSpace]]
    };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject:%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
    
}
//获得int版本号
+(int) getVersionCode:(NSString *)version{
    NSString *value=[version stringByReplacingOccurrencesOfString:@"." withString:@""];
    int versionCode=[NSString stringWithFormat:@"%@",value].intValue;
    return versionCode;
}

//登录日志
+(void)appLogin:(int) loginType{
    NSString *urlString = @"https://web.kanface.com:444/appAdditionInfo/appLoginLog/saveLoginLog";
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSDictionary *params = @{
        @"oaId":[Tools getDeviceOaid],
        @"uid":@([getPeopleID PeopleID]),
        @"mobile": [EnercryTool AES256Encrypt:[getPeopleID PeopleIphoneNmber] key:DESKEY],
        @"loginType":@(loginType),
        @"terminal":@"iOS",
        @"appTag":@"kksh",
        @"appVersion":@([self getVersionCode:app_Version]),
        @"creatTime":[Tools getCurrentTimes],
        @"phoneBrand":[IPTool getCurrentDeviceModel],
        @"brandType":[IPTool getCurrentDeviceType]
    };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject:%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

//app打开日志
+(void)appOpen:(NSString *)adTag adSuccess:(int) adSuccess openTime:(long)openTime{
    NSString *urlString = @"https://web.kanface.com:444/appAdditionInfo/appStartLog/saveStartLog";
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSDictionary *params = @{
        @"oaId":[Tools getDeviceOaid],
        @"appTag":@"kksh",
        @"terminal":@"iOS",
        @"appVersion":@([self getVersionCode:app_Version]),
        @"advertiseTag":adTag,
        @"advertiseIsSuccess":@(adSuccess),
        @"openScreenDuration":@(openTime),
        @"appStartTime":[Tools getCurrentTimes],
        @"uid":@([getPeopleID PeopleID]),
        @"mobile": [EnercryTool AES256Encrypt:[getPeopleID PeopleIphoneNmber] key:DESKEY],
        @"phoneBrand":[IPTool getCurrentDeviceModel],
        @"brandType":[IPTool getCurrentDeviceType]
    };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject:%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:NULL]);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}

//统计
+ (void)statisticsWithEventName:(NSString *)eventName
{
    NSLog(@"🎁 统计事件被触发 - 事件名称: %@", eventName);
    NSLog(@"🎁 统计时间: %@", [NSDate date]);
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSString *urlString = @"http://kk.weface.com.cn/statistic/event";
    NSDictionary *params = @{
        @"eventName":eventName,
        @"unTel": [getPeopleID PeopleUnPhoneNmber],
//        @"unTel": [EnercryTool AES256Encrypt:[getPeopleID PeopleIphoneNmber] key:DESKEY],
        @"deviceId":[Tools getDeviceUUID],
        @"eventTime":[Tools getCurrentTimes],
        @"appName":[IPTool getBundleId],
        @"fromModel":@(2),
        @"version":app_Version
    };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}


//get请求

//base请求
+ (void)baseGet:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure {
    
    AFHTTPSessionManager *manager = nil;
    
//    if ([urlString containsString:@"https://socialsecurity.weface.com.cn:8066"]) {
//        manager = [[AFHTTPSessionManager alloc]initWithBaseURL:[NSURL URLWithString:@"https://socialsecurity.weface.com.cn:8066"]];
//        [manager setSecurityPolicy:[NetworkRequest customSecurityPolicy]] ;
//    }else{
//        manager = [AFHTTPSessionManager manager];
//
//    }
    
    manager = [AFHTTPSessionManager manager];
    NSMutableDictionary* newParams = [NSMutableDictionary dictionaryWithDictionary:params];
    /*
    [manager.requestSerializer setValue:@"wefacev2" forHTTPHeaderField:@"version"];
    [manager.requestSerializer setValue:[NSString getNowTimeTimestamp3] forHTTPHeaderField:@"timestamp"];
    //random
    NSInteger randomInt = arc4random() % 10001;
    NSString* timeStamp = [NSString getNowTimeTimestamp3];
    
    NSString* nonce = [NSString stringWithFormat:@"%@%@",timeStamp,@(randomInt)];
    
    [manager.requestSerializer setValue:nonce forHTTPHeaderField:@"nonce"];
    [manager.requestSerializer setValue:timeStamp forHTTPHeaderField:@"timestamp"];
    [manager.requestSerializer setValue:[UserDefault objectForKey:@"loginToken"] forHTTPHeaderField:@"token"];
//    [manager.requestSerializer setValue:[UserDefault objectForKey:@"loginToken"] forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:[Tools getDeviceUUID] forHTTPHeaderField:@"unid"];
    
    
   
    
    
    
    if (params.allKeys.count > 0) {
        
        
        for (NSString* key in newParams.allKeys) {
            if ([key isEqualToString:@"now_address"] || [key isEqualToString:@"phone_number"] || [key isEqualToString:@"province_name"] || [key isEqualToString:@"city_name"] || [key isEqualToString:@"county_name"] || [key isEqualToString:@"town_name"] || [key isEqualToString:@"village_name"] || [key isEqualToString:@"company_name"] ||[key isEqualToString:@"province_id"] || [key isEqualToString:@"city_id"] || [key isEqualToString:@"county_id"] || [key isEqualToString:@"town_id"] || [key isEqualToString:@"telephone"] || [key isEqualToString:@"telphone"] || [key isEqualToString:@"validateCode"] || [key isEqualToString:@"content_text"] || [key isEqualToString:@"cus_name"]) {
                NSString* value = [NSString stringWithFormat:@"%@",newParams[key]];
                if (![Tools isBlankString:value]) {
                    value = [GMSm4Utils ecbEncryptText:value key:SMKey].lowercaseString;
                    [newParams setObject:value forKey:key];
                }
                
            }
        }
        
        
        NSArray *allKeyArray = [newParams allKeys];
        //序列化器对数组进行排序的block 返回值为排序后的数组
        NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSComparisonResult resuest = [obj1 compare:obj2];
            return resuest;
        }];
        NSLog(@"afterSortKeyArray:%@",afterSortKeyArray);
        
        NSString* versionsign = nil;
        
        for (NSString *sortsing in afterSortKeyArray) {
            
            if (![Tools dx_isNullOrNilWithObject:newParams[sortsing]]) {
                if (versionsign == nil) {
                    versionsign = [NSString stringWithFormat:@"%@=%@",sortsing,newParams[sortsing]];
                }else{
                    versionsign = [NSString stringWithFormat:@"%@&%@=%@",versionsign,sortsing,newParams[sortsing]];
                }
            }
            
        }
        
        versionsign = [NSString stringWithFormat:@"%@&%@&%@&%@",timeStamp,nonce,versionsign,ParamKey];
        
        NSLog(@"加密前:%@",versionsign);
        
         versionsign = [GMSm3Utils hashWithString:versionsign].lowercaseString;

        
        NSLog(@"versionsign:%@",versionsign);
        
        [manager.requestSerializer setValue:versionsign forHTTPHeaderField:@"versionsign"];
        
    }
   
    
    */
    
 
    
    
    
   
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    
    [manager GET:urlString parameters:newParams headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if ([responseObject isKindOfClass:[NSData class]]) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            success(dic);
        }else{
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}
+ (void)Get:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *strHeader = [NSString stringWithFormat:@"%@#%@",[IPTool getSystemVersion],buildAndVersion];
    NSLog(@"strHeader:%@",strHeader);
    [manager.requestSerializer setValue:strHeader forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:[[MyMD5 md5:[self GetStringBuilderTime:timeString params:params]] lowercaseString] forHTTPHeaderField:@"sign"];
    [manager.requestSerializer setValue:timeString forHTTPHeaderField:@"timestamp"];
    [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}
+ (void)Get2:(NSString *)urlString Params:(NSDictionary *)params Token:(NSString *)token Success:( void (^)(id responseObject))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    
    
    [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}
+ (void)Get3:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(NSError *error))failure
{
      AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
       
       [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           if (responseObject) {
               success(responseObject);
           }
       } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
           if (error) {
               failure(error);
           }
       }];
}

+ (void)Get4:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
       
   [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
                  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                  success(dic);
        }
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       if (error) {
           failure(error);
       }
   }];
}

+ (void)Get5:(NSString *)urlString Params:(NSDictionary *)params Token:(NSString *)token Success:( void (^)(id responseObject))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"tuiyiban_1" forHTTPHeaderField:@"from-App"];
    
    [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}

+ (void)Get6:(NSString *)urlString Params:(NSDictionary *)params Token:(NSString *)token Success:( void (^)(id responseObject))success Failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [manager.requestSerializer setValue:@"kksh" forHTTPHeaderField:@"from-App"];
    
    [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failure(error);
        }
    }];
}

+ (void)Get7:(NSString *)urlString Params:(NSDictionary *)params Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *strHeader = [NSString stringWithFormat:@"%@#%@",[IPTool getSystemVersion],buildAndVersion];
    NSLog(@"strHeader:%@",strHeader);
    [manager.requestSerializer setValue:strHeader forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"kksh" forHTTPHeaderField:@"from-App"];
    
    [manager GET:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

//上传单图
+ (void)uploadImage:(NSString *)urlString Params:(NSDictionary *)params Image:(NSData *)imageData ImageName:(NSString *)imageName ImageParam:(NSString *)imagePara Success:( void (^)(id))success Failure:(void (^)(id))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    [manager POST:urlString parameters:nil headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
       
        
        for (NSString *key in [params allKeys]) {
            if ([[params objectForKey:key] isKindOfClass:[NSString class]]) {
                           [formData appendPartWithFormData:[[params objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }else if ([[params objectForKey:key] isKindOfClass:[NSNumber class]]){
                NSString *str = [NSString stringWithFormat:@"%@",[params objectForKey:key]];
                [formData appendPartWithFormData:[str dataUsingEncoding:NSUTF8StringEncoding] name:key];
            }
        }
        
        //上传图片,以文件流的格式
        if (imageData) {
            [formData appendPartWithFileData:imageData name:imagePara fileName:imageName mimeType:@"image/jpg/png/jpeg"];
        }
               
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
}

//上传多图
+ (void)uploadMoreImage:(NSString *)urlString Params:(NSDictionary *)params ImageArray:(NSArray *)imageArray ImageParam:(NSString *)imagePara Success:(void (^)(id _Nonnull))success Failure:(void (^)(id _Nonnull))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //显示进度
    [manager POST:urlString parameters:params headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //上传多张图片
        for (NSInteger i = 0; i < imageArray.count; i++) {
            NSData *imageData = [imageArray objectAtIndex:i];
            //上传的参数名
            NSString *Name = [NSString stringWithFormat:@"%@%zi",@"task_image",i+1];
            //上传filename
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg",Name];
            [formData appendPartWithFileData:imageData name:imagePara fileName:fileName mimeType:@"image/jpeg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

+ (AFSecurityPolicy*)customSecurityPolicy
{
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"public" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    NSSet <NSData *>* certSet = [[NSSet alloc] initWithObjects:certData, nil];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    securityPolicy.allowInvalidCertificates = YES;//是否信任一个失效或过期的SSL证书的服务器。默认为NO。
    securityPolicy.validatesDomainName = YES;//验证域名是否在证书的CN字段中。默认是YES。
    [securityPolicy setPinnedCertificates:certSet];
    return securityPolicy;
}

@end
