123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- //
- // KXHTTP.m
- // QBCS
- //
- // Created by kuxuan on 2017/6/8.
- // Copyright © 2017年 kuxuan. All rights reserved.
- //
- #import "KXHTTP.h"
- #import "KXMD5Manager.h"
- @implementation KXHTTP
- + (void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
-
- AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
- mgr.requestSerializer.timeoutInterval = 15;
- mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
- 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=%@",KXCHANNEL_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:KXCHANNEL_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],KXCHANNEL_ID,@"iOS",sign_key];
- [mgr.requestSerializer setValue:[[KXMD5Manager sharedInstance] md5:sign] forHTTPHeaderField:@"sign"];
-
- // 2.发送请求
- [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];
- }else{
- if (success) {
- success(responseDict[@"rst"]);
- }
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- if (failure) {
- failure(error);
- }
- }];
-
- }
- + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
- // 1.创建请求管理者
- AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
- mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
- mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
- //mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
- mgr.requestSerializer.timeoutInterval = 15;
-
- NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- NSString *urgentString=[NSString stringWithFormat:@"&channel=%@&version=%@",KXCHANNEL_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:KXCHANNEL_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],KXCHANNEL_ID,@"iOS",sign_key];
- [mgr.requestSerializer setValue:[[KXMD5Manager sharedInstance] md5:sign] forHTTPHeaderField:@"sign"];
-
-
-
-
- // 2.发送请求
- [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- // NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
- // for(NSHTTPCookie *cookie in [cookieJar cookies])
- // {
- // NSLog(@"cookie3 -> %@", cookie);
- // }
-
- if (![responseDict[@"errno"] isEqual:@"0"]&&![responseDict[@"errno"] isEqual:@"40044"]&&![responseDict[@"errno"] isEqual:@"90001"]) {
- 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{
-
- NSString *username = [[NSUserDefaults standardUserDefaults]valueForKey:USER_NAME];
- NSString *password = [[NSUserDefaults standardUserDefaults]valueForKey:USER_PASSWORD];
- NSString *token = [[NSUserDefaults standardUserDefaults]valueForKey:USER_TOKEN];
- if ([responseDict[@"errno"] isEqual:@"90001"]) {
- if (username) {
- if (password && ![password isEqualToString:@""]) {
- NSString *urlString=[NSString stringWithFormat:@"%@/user/login",URL];
- NSDictionary *params = @{@"phone":username,@"password":password};
- // 2.发送请求
- [mgr POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id response =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- // NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
- // for(NSHTTPCookie *cookie in [cookieJar cookies])
- // {
- // NSLog(@"cookie3 -> %@", cookie);
- // }
- if (![response[@"errno"] isEqual:@"0"]) {
- [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_NAME];
- [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_PASSWORD];
- [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_TOKEN];
- [[NSUserDefaults standardUserDefaults]synchronize];
- }
- if (success) {
- success(responseDict[@"rst"]);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- if (failure) {
- failure(error);
- }
- }];
- } else {
- if (token && ![token isEqualToString:@""]) {
- NSString *urlString=[NSString stringWithFormat:@"%@/user/loginCode",URL];
- NSDictionary *params = @{@"phone":username,@"code":@"",@"source":KXCHANNEL_ID,@"token":token};
- // 2.发送请求
- [mgr POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id response =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- // NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
- // for(NSHTTPCookie *cookie in [cookieJar cookies])
- // {
- // NSLog(@"cookie3 -> %@", cookie);
- // }
- if (![response[@"errno"] isEqual:@"0"]&&![response[@"errno"] isEqual:@"10023"]) {
- [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_NAME];
- [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_PASSWORD];
- [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_TOKEN];
- [[NSUserDefaults standardUserDefaults]synchronize];
- } else {
- NSDictionary *rst = response[@"rst"];
- NSDictionary *user = rst[@"user"];
- [[NSUserDefaults standardUserDefaults]setObject:user[@"user_token"] forKey:USER_TOKEN];
- }
- if (success) {
- success(responseDict[@"rst"]);
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- if (failure) {
- failure(error);
- }
- }];
- } else {
- if (success) {
- success(responseDict[@"rst"]);
- }
- }
- }
- } else {
- if (success) {
- success(responseDict[@"rst"]);
- }
- }
- }else{
- if (success) {
- success(responseDict[@"rst"]);
- }
- }
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- if (failure) {
- failure(error);
- }
- }];
-
-
- }
- + (void)uploadImageWithUrl:(NSString *)url name:(NSString *)name params:(NSDictionary *)params imageData:(NSData *)data imageName:(NSString *)imageName success:(void(^)(id json))success failure:(void(^)(NSError *error))failure{
-
- //2. 利用时间戳当做图片名字
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- formatter.dateFormat = @"yyyyMMddHHmmss";
- NSString *dateString = [formatter stringFromDate:[NSDate date]];
- NSString *fileName = [NSString stringWithFormat:@"%@%@.jpg",dateString,imageName];
-
- //3. 图片二进制文件
- NSLog(@"upload image size: %ld k", (long)(data.length / 1024));
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/javascript",@"text/json",nil];
- manager.requestSerializer= [AFHTTPRequestSerializer serializer];
- manager.responseSerializer= [AFHTTPResponseSerializer serializer];
- NSString *agentString = [manager.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- NSString *urgentString=[NSString stringWithFormat:@"&channel=%@&version=%@",KXCHANNEL_ID,app_Version];
- NSString *mixString = [agentString stringByAppendingString:urgentString];
- [manager.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"];
- NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
- [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"];
- [manager.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"];
- [manager.requestSerializer setValue:KXCHANNEL_ID forHTTPHeaderField:@"cid"];
- [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
- NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5";
- NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],KXCHANNEL_ID,@"iOS",sign_key];
- [manager.requestSerializer setValue:[[KXMD5Manager sharedInstance] md5:sign] forHTTPHeaderField:@"sign"];
- [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
- //上传的参数(上传图片,以文件流的格式)
- [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/png"];
- } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- success(responseDict);
-
- } 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);
- }
- }];
- }
- @end
|