口袋优选

KBHttp.m 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // KBHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBHttp.h"
  9. #import "KBLoginViewController.h"
  10. #import "KBHistoryTool.h"
  11. #import "AccountTool.h"
  12. #import <AdSupport/AdSupport.h>
  13. #define jsonDataPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"jsonData.plist"]
  14. #define JsonDataKey @"JsonData"
  15. @implementation KBHttp
  16. + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
  17. // 1.创建请求管理者
  18. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  19. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  20. mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
  21. mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  22. mgr.requestSerializer.timeoutInterval = 10;
  23. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  24. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  25. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  26. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  27. [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  28. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  29. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  30. [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  31. if ([AccountTool isLogin]) {
  32. NSString *token = [AccountTool account].token;
  33. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  34. }
  35. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  36. [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  37. //测试
  38. // 2.发送请求
  39. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  40. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  41. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  42. if (!responseDict[@"rst"]) {
  43. success(@{});
  44. }else{
  45. success(responseDict[@"rst"]);
  46. }
  47. }else{
  48. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  49. NSDictionary *errorDic= responseDict[@"rst"];
  50. if (errorDic.count>0) {
  51. NSString *message = errorDic[@"msg"];
  52. [MBProgressHUD showTip:message];
  53. }
  54. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  55. //让用户退出
  56. [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
  57. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  58. //让用户退出
  59. [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
  60. }else{
  61. NSString *message=responseDict[@"err"];
  62. if (message &&![@"" isEqualToString:message]) {
  63. [MBProgressHUD showTip:message];
  64. }
  65. }
  66. if (failure) {
  67. failure(nil);
  68. }
  69. }
  70. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  71. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  72. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  73. NSLog(@"服务器的错误原因:%@",str);
  74. NSLog(@"报错地址:%@",url);
  75. if (failure) {
  76. failure(error);
  77. }
  78. }];
  79. }
  80. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure {
  81. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  82. mgr.requestSerializer.timeoutInterval = 15;
  83. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  84. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  85. mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  86. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  87. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  88. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  89. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"channel_id"];
  90. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  91. [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  92. // NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  93. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  94. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  95. [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  96. if ([AccountTool isLogin]) {
  97. NSString *token = [AccountTool account].token;
  98. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  99. }
  100. [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  101. // jcytesttoken
  102. // 2.发送请求
  103. [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  104. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  105. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  106. if (!responseDict[@"rst"]) {
  107. success(@{});
  108. }else{
  109. success(responseDict[@"rst"]);
  110. }
  111. }else{
  112. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  113. NSDictionary *errorDic= responseDict[@"rst"];
  114. if (errorDic.count>0) {
  115. NSString *message = errorDic[@"msg"];
  116. [MBProgressHUD showTip:message];
  117. }
  118. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  119. //让用户退出
  120. [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
  121. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  122. //让用户退出
  123. [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
  124. }else{
  125. NSString *message=responseDict[@"err"];
  126. if (message &&![@"" isEqualToString:message]) {
  127. [MBProgressHUD showTip:message];
  128. }
  129. }
  130. if (failure) {
  131. failure(nil);
  132. }
  133. }
  134. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  135. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  136. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  137. NSLog(@"服务器的错误原因:%@",str);
  138. NSLog(@"报错地址:%@",url);
  139. if (failure) {
  140. // NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  141. // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  142. // NSLog(@"服务器的错误原因:%@",str);
  143. failure(error);
  144. }
  145. }];
  146. }
  147. /**
  148. 已在其他设备登录
  149. */
  150. + (void)loginInOtherDeviceWithMsg:(NSString *)msg {
  151. [AccountTool deleteAccount];
  152. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  153. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
  154. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
  155. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  156. KBLoginViewController *login = [[KBLoginViewController alloc] init];
  157. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
  158. }];
  159. [alert addAction:cancel];
  160. [alert addAction:sure];
  161. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  162. }
  163. @end