酷店

KDPNetworkRequestHTTP.m 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // KDPNetworkRequestHTTP.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPNetworkRequestHTTP.h"
  9. #import <AdSupport/AdSupport.h>
  10. #import "KDPPhoneLoginViewController.h"
  11. #import "KDPTabBarVC.h"
  12. @implementation KDPNetworkRequestHTTP
  13. + (void)postURL:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure
  14. {
  15. // 1.创建请求管理者
  16. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  17. manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  18. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  19. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  20. manager.requestSerializer.timeoutInterval = 10;
  21. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  22. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  23. [manager.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  24. [manager.requestSerializer setValue:SOURCE_ID forHTTPHeaderField:@"source"];
  25. [manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  26. if ([KDPAccountTool isLogin]) {
  27. NSString *token = [KDPAccountTool account].token;
  28. [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  29. }
  30. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  31. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  32. [manager.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  33. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  34. [manager.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  35. // 2.发送请求
  36. [manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  37. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  38. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  39. if (!responseDict[@"rst"]) {
  40. success(@{});
  41. }else{
  42. success(responseDict[@"rst"]);
  43. }
  44. }else{
  45. NSString *message=responseDict[@"err"];
  46. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  47. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  48. //让用户退出
  49. [MBProgressHUD showMessage:@"登录信息过期,请重新登录"];
  50. [self appOpenLogin];
  51. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  52. //让用户退出
  53. [MBProgressHUD showMessage:@"您已在其他设备登录,是否重新登录"];
  54. [self appOpenLogin];
  55. }else{
  56. }
  57. if (failure) {
  58. failure(nil);
  59. }
  60. }
  61. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  62. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  63. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  64. NSLog(@"服务器的错误原因:%@",str);
  65. NSLog(@"报错地址:%@",url);
  66. [MBProgressHUD showError:@"服务器开小差"];
  67. if (failure) {
  68. failure(error);
  69. }
  70. }];
  71. }
  72. +(void)appOpenLogin
  73. {
  74. [[NSNotificationCenter defaultCenter]postNotificationName:@"exitLogin" object:nil];
  75. [KDPAccountTool deleteAccount];
  76. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:[[KDPPhoneLoginViewController alloc]init]] animated:YES completion:^{
  77. UIViewController *currentVC = [[self alloc] getCurrentVC];
  78. [currentVC.navigationController popToRootViewControllerAnimated:YES];
  79. KDPTabBarVC *tabbar = (KDPTabBarVC *)[UIApplication sharedApplication].keyWindow.rootViewController;
  80. tabbar.selectedIndex = 0;
  81. }];
  82. }
  83. - (UIViewController *)getCurrentVC {
  84. // Find best view controller
  85. UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  86. return [self findBestViewController:viewController];
  87. }
  88. -(UIViewController*) findBestViewController:(UIViewController*)vc {
  89. if (vc.presentedViewController) {
  90. // Return presented view controller
  91. return [self findBestViewController:vc.presentedViewController];
  92. } else if ([vc isKindOfClass:[UISplitViewController class]]) {
  93. // Return right hand side
  94. UISplitViewController* svc = (UISplitViewController*) vc;
  95. if (svc.viewControllers.count > 0)
  96. return [self findBestViewController:svc.viewControllers.lastObject];
  97. else
  98. return vc;
  99. } else if ([vc isKindOfClass:[UINavigationController class]]) {
  100. // Return top view
  101. UINavigationController* svc = (UINavigationController*) vc;
  102. if (svc.viewControllers.count > 0)
  103. return [self findBestViewController:svc.topViewController];
  104. else
  105. return vc;
  106. } else if ([vc isKindOfClass:[UITabBarController class]]) {
  107. // Return visible view
  108. UITabBarController* svc = (UITabBarController*) vc;
  109. if (svc.viewControllers.count > 0)
  110. return [self findBestViewController:svc.selectedViewController];
  111. else
  112. return vc;
  113. } else {
  114. // Unknown view controller type, return last child view controller
  115. return vc;
  116. }
  117. }
  118. /**
  119. *普通get请求
  120. */
  121. + (void)getURL:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure
  122. {
  123. // 1.创建请求管理者
  124. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  125. manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  126. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  127. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  128. manager.requestSerializer.timeoutInterval = 10;
  129. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  130. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  131. [manager.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  132. [manager.requestSerializer setValue:SOURCE_ID forHTTPHeaderField:@"source"];
  133. [manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  134. if ([KDPAccountTool isLogin]) {
  135. NSString *token = [KDPAccountTool account].token;
  136. [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  137. }
  138. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  139. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  140. [manager.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  141. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  142. [manager.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  143. // 2.发送请求
  144. [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  145. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  146. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  147. if (!responseDict[@"rst"]) {
  148. success(@{});
  149. }else{
  150. success(responseDict[@"rst"]);
  151. }
  152. }else{
  153. NSString *message=responseDict[@"err"];
  154. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  155. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  156. //让用户退出
  157. [MBProgressHUD showMessage:@"登录信息过期,请重新登录"];
  158. [self appOpenLogin];
  159. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  160. //让用户退出
  161. [self appOpenLogin];
  162. [MBProgressHUD showMessage:@"您已在其他设备登录,是否重新登录"];
  163. }else{
  164. }
  165. if (failure) {
  166. failure(nil);
  167. }
  168. }
  169. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  170. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  171. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  172. NSLog(@"服务器的错误原因:%@",str);
  173. NSLog(@"报错地址:%@",url);
  174. [MBProgressHUD showError:@"服务器开小差"];
  175. if (failure) {
  176. failure(error);
  177. }
  178. }];
  179. }
  180. @end