No Description

KXHTTP.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // KXHTTP.m
  3. // QBCS
  4. //
  5. // Created by kuxuan on 2017/6/8.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXHTTP.h"
  9. #import "KXMD5Manager.h"
  10. @implementation KXHTTP
  11. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
  12. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  13. mgr.requestSerializer.timeoutInterval = 15;
  14. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  15. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  16. mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  17. NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
  18. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  19. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  20. NSString *urgentString=[NSString stringWithFormat:@"&channel=%@&version=%@",KXCHANNEL_ID,app_Version];
  21. NSString *mixString = [agentString stringByAppendingString:urgentString];
  22. [mgr.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"];
  23. NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
  24. [mgr.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"];
  25. [mgr.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"];
  26. [mgr.requestSerializer setValue:KXCHANNEL_ID forHTTPHeaderField:@"cid"];
  27. [mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
  28. NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5";
  29. NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],KXCHANNEL_ID,@"iOS",sign_key];
  30. [mgr.requestSerializer setValue:[[KXMD5Manager sharedInstance] md5:sign] forHTTPHeaderField:@"sign"];
  31. // 2.发送请求
  32. [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  33. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  34. if (![responseDict[@"errno"] isEqual:@"0"]) {
  35. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:responseDict[@"err"] preferredStyle:UIAlertControllerStyleAlert];
  36. [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  37. }]];
  38. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  39. }else{
  40. if (success) {
  41. success(responseDict[@"rst"]);
  42. }
  43. }
  44. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  45. if (failure) {
  46. failure(error);
  47. }
  48. }];
  49. }
  50. + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
  51. // 1.创建请求管理者
  52. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  53. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  54. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  55. //mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  56. mgr.requestSerializer.timeoutInterval = 15;
  57. NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
  58. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  59. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  60. NSString *urgentString=[NSString stringWithFormat:@"&channel=%@&version=%@",KXCHANNEL_ID,app_Version];
  61. NSString *mixString = [agentString stringByAppendingString:urgentString];
  62. [mgr.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"];
  63. NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
  64. [mgr.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"];
  65. [mgr.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"];
  66. [mgr.requestSerializer setValue:KXCHANNEL_ID forHTTPHeaderField:@"cid"];
  67. [mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
  68. NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5";
  69. NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],KXCHANNEL_ID,@"iOS",sign_key];
  70. [mgr.requestSerializer setValue:[[KXMD5Manager sharedInstance] md5:sign] forHTTPHeaderField:@"sign"];
  71. // 2.发送请求
  72. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  73. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  74. // NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  75. // for(NSHTTPCookie *cookie in [cookieJar cookies])
  76. // {
  77. // NSLog(@"cookie3 -> %@", cookie);
  78. // }
  79. if (![responseDict[@"errno"] isEqual:@"0"]&&![responseDict[@"errno"] isEqual:@"40044"]&&![responseDict[@"errno"] isEqual:@"90001"]) {
  80. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:responseDict[@"err"] preferredStyle:UIAlertControllerStyleAlert];
  81. [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  82. }]];
  83. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  84. NSError *error = nil;
  85. failure(error);
  86. }else{
  87. NSString *username = [[NSUserDefaults standardUserDefaults]valueForKey:USER_NAME];
  88. NSString *password = [[NSUserDefaults standardUserDefaults]valueForKey:USER_PASSWORD];
  89. NSString *token = [[NSUserDefaults standardUserDefaults]valueForKey:USER_TOKEN];
  90. if ([responseDict[@"errno"] isEqual:@"90001"]) {
  91. if (username) {
  92. if (password && ![password isEqualToString:@""]) {
  93. NSString *urlString=[NSString stringWithFormat:@"%@/user/login",URL];
  94. NSDictionary *params = @{@"phone":username,@"password":password};
  95. // 2.发送请求
  96. [mgr POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  97. id response =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  98. // NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  99. // for(NSHTTPCookie *cookie in [cookieJar cookies])
  100. // {
  101. // NSLog(@"cookie3 -> %@", cookie);
  102. // }
  103. if (![response[@"errno"] isEqual:@"0"]) {
  104. [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_NAME];
  105. [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_PASSWORD];
  106. [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_TOKEN];
  107. [[NSUserDefaults standardUserDefaults]synchronize];
  108. }
  109. if (success) {
  110. success(responseDict[@"rst"]);
  111. }
  112. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  113. if (failure) {
  114. failure(error);
  115. }
  116. }];
  117. } else {
  118. if (token && ![token isEqualToString:@""]) {
  119. NSString *urlString=[NSString stringWithFormat:@"%@/user/loginCode",URL];
  120. NSDictionary *params = @{@"phone":username,@"code":@"",@"source":KXCHANNEL_ID,@"token":token};
  121. // 2.发送请求
  122. [mgr POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  123. id response =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  124. // NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  125. // for(NSHTTPCookie *cookie in [cookieJar cookies])
  126. // {
  127. // NSLog(@"cookie3 -> %@", cookie);
  128. // }
  129. if (![response[@"errno"] isEqual:@"0"]&&![response[@"errno"] isEqual:@"10023"]) {
  130. [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_NAME];
  131. [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_PASSWORD];
  132. [[NSUserDefaults standardUserDefaults]setValue:nil forKey:USER_TOKEN];
  133. [[NSUserDefaults standardUserDefaults]synchronize];
  134. } else {
  135. NSDictionary *rst = response[@"rst"];
  136. NSDictionary *user = rst[@"user"];
  137. [[NSUserDefaults standardUserDefaults]setObject:user[@"user_token"] forKey:USER_TOKEN];
  138. }
  139. if (success) {
  140. success(responseDict[@"rst"]);
  141. }
  142. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  143. if (failure) {
  144. failure(error);
  145. }
  146. }];
  147. } else {
  148. if (success) {
  149. success(responseDict[@"rst"]);
  150. }
  151. }
  152. }
  153. } else {
  154. if (success) {
  155. success(responseDict[@"rst"]);
  156. }
  157. }
  158. }else{
  159. if (success) {
  160. success(responseDict[@"rst"]);
  161. }
  162. }
  163. }
  164. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  165. if (failure) {
  166. failure(error);
  167. }
  168. }];
  169. }
  170. + (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{
  171. //2. 利用时间戳当做图片名字
  172. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  173. formatter.dateFormat = @"yyyyMMddHHmmss";
  174. NSString *dateString = [formatter stringFromDate:[NSDate date]];
  175. NSString *fileName = [NSString stringWithFormat:@"%@%@.jpg",dateString,imageName];
  176. //3. 图片二进制文件
  177. NSLog(@"upload image size: %ld k", (long)(data.length / 1024));
  178. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  179. manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/javascript",@"text/json",nil];
  180. manager.requestSerializer= [AFHTTPRequestSerializer serializer];
  181. manager.responseSerializer= [AFHTTPResponseSerializer serializer];
  182. NSString *agentString = [manager.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
  183. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  184. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  185. NSString *urgentString=[NSString stringWithFormat:@"&channel=%@&version=%@",KXCHANNEL_ID,app_Version];
  186. NSString *mixString = [agentString stringByAppendingString:urgentString];
  187. [manager.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"];
  188. NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
  189. [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"];
  190. [manager.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"];
  191. [manager.requestSerializer setValue:KXCHANNEL_ID forHTTPHeaderField:@"cid"];
  192. [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
  193. NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5";
  194. NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],KXCHANNEL_ID,@"iOS",sign_key];
  195. [manager.requestSerializer setValue:[[KXMD5Manager sharedInstance] md5:sign] forHTTPHeaderField:@"sign"];
  196. [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
  197. //上传的参数(上传图片,以文件流的格式)
  198. [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/png"];
  199. } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  200. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  201. success(responseDict);
  202. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  203. // NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  204. // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  205. // NSLog(@"服务器的错误原因:%@",str);
  206. if (failure) {
  207. failure(error);
  208. }
  209. }];
  210. }
  211. @end