// // ASHttpTool.m // ACSION // // Created by sunyue on 2019/4/22. // Copyright © 2019 acsion. All rights reserved. // #import "ASHttpTool.h" @implementation ASHttpTool + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure { AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; mgr.requestSerializer.timeoutInterval = 15; mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil]; NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"]; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; NSString *urgentString = [NSString stringWithFormat:@"&channel=%@&version=%@",CHANNEL_ID,app_Version]; NSString *mixString = [agentString stringByAppendingString:urgentString]; [mgr.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"]; NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]]; [mgr.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"]; [mgr.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"]; [mgr.requestSerializer setValue:CHANNEL_ID forHTTPHeaderField:@"cid"]; [mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"]; NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5"; NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],CHANNEL_ID,@"iOS",sign_key]; [mgr.requestSerializer setValue:[sign md5] forHTTPHeaderField:@"sign"]; [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { id responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; if (![responseDict[@"errno"] isEqual:@"0"]) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:responseDict[@"err"] preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; NSError *error = nil; failure(error); } else { if (success) { success(responseDict[@"rst"]); } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"]; NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"服务器的错误原因:%@",str); if (failure) { failure(error); } }]; } + (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure { AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; mgr.requestSerializer.timeoutInterval = 15; mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; mgr.responseSerializer = [AFHTTPResponseSerializer serializer]; mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil]; NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"]; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; NSString *urgentString = [NSString stringWithFormat:@"&channel=%@&version=%@",CHANNEL_ID,app_Version]; NSString *mixString = [agentString stringByAppendingString:urgentString]; [mgr.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"]; NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]]; [mgr.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"]; [mgr.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"]; [mgr.requestSerializer setValue:CHANNEL_ID forHTTPHeaderField:@"cid"]; [mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"]; NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5"; NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],CHANNEL_ID,@"iOS",sign_key]; [mgr.requestSerializer setValue:[sign md5] forHTTPHeaderField:@"sign"]; [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { id responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; if (![responseDict[@"errno"] isEqual:@"0"]) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:responseDict[@"err"] preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; NSError *error = nil; failure(error); } else { if (success) { success(responseDict[@"rst"]); } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"]; NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"服务器的错误原因:%@",str); if (failure) { failure(error); } }]; } + (void)uploadImageWithUrl:(NSString *)url name:(NSString *)name params:(NSDictionary *)params imageData:(NSData *)data imageName:(NSString *)imageName success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure { } @end