123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- //
- // KDPNetworkRequestHTTP.m
- // KuDianProject
- //
- // Created by 学丽 on 2019/7/4.
- // Copyright © 2019 KDP. All rights reserved.
- //
- #import "KDPNetworkRequestHTTP.h"
- #import <AdSupport/AdSupport.h>
- #import "KDPPhoneLoginViewController.h"
- #import "KDPTabBarVC.h"
- @implementation KDPNetworkRequestHTTP
- + (void)postURL:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure
- {
- // 1.创建请求管理者
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
- manager.requestSerializer.timeoutInterval = 10;
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- [manager.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
- [manager.requestSerializer setValue:SOURCE_ID forHTTPHeaderField:@"source"];
- [manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
- if ([KDPAccountTool isLogin]) {
- NSString *token = [KDPAccountTool account].token;
- [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
- }
-
- NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
- [manager.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
- // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
- [manager.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
-
-
- // 2.发送请求
- [manager 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"] isEqualToString:@"0"]) {
- if (!responseDict[@"rst"]) {
- success(@{});
- }else{
- success(responseDict[@"rst"]);
- }
- }else{
-
- NSString *message=responseDict[@"err"];
-
-
- if([responseDict[@"errno"] isEqualToString:@"10009"]) {
-
- }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
- //让用户退出
- [MBProgressHUD showMessage:@"登录信息过期,请重新登录"];
- [self appOpenLogin];
- }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
- //让用户退出
- [MBProgressHUD showMessage:@"您已在其他设备登录,是否重新登录"];
- [self appOpenLogin];
- }else{
-
- }
-
-
- if (failure) {
- failure(nil);
- }
- }
- } 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);
- NSLog(@"报错地址:%@",url);
- [MBProgressHUD showError:@"服务器开小差"];
- if (failure) {
- failure(error);
- }
- }];
- }
- +(void)appOpenLogin
- {
- [[NSNotificationCenter defaultCenter]postNotificationName:@"exitLogin" object:nil];
- [KDPAccountTool deleteAccount];
- [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[[UINavigationController alloc]initWithRootViewController:[[KDPPhoneLoginViewController alloc]init]] animated:YES completion:^{
- UIViewController *currentVC = [[self alloc] getCurrentVC];
-
- [currentVC.navigationController popToRootViewControllerAnimated:YES];
-
- KDPTabBarVC *tabbar = (KDPTabBarVC *)[UIApplication sharedApplication].keyWindow.rootViewController;
- tabbar.selectedIndex = 0;
- }];
-
- }
- - (UIViewController *)getCurrentVC {
- // Find best view controller
- UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
- return [self findBestViewController:viewController];
- }
- -(UIViewController*) findBestViewController:(UIViewController*)vc {
-
- if (vc.presentedViewController) {
-
- // Return presented view controller
- return [self findBestViewController:vc.presentedViewController];
-
- } else if ([vc isKindOfClass:[UISplitViewController class]]) {
-
- // Return right hand side
- UISplitViewController* svc = (UISplitViewController*) vc;
- if (svc.viewControllers.count > 0)
- return [self findBestViewController:svc.viewControllers.lastObject];
- else
- return vc;
-
- } else if ([vc isKindOfClass:[UINavigationController class]]) {
-
- // Return top view
- UINavigationController* svc = (UINavigationController*) vc;
- if (svc.viewControllers.count > 0)
- return [self findBestViewController:svc.topViewController];
- else
- return vc;
-
- } else if ([vc isKindOfClass:[UITabBarController class]]) {
-
- // Return visible view
- UITabBarController* svc = (UITabBarController*) vc;
- if (svc.viewControllers.count > 0)
- return [self findBestViewController:svc.selectedViewController];
- else
- return vc;
-
- } else {
-
- // Unknown view controller type, return last child view controller
- return vc;
-
- }
- }
- /**
- *普通get请求
- */
- + (void)getURL:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure
- {
- // 1.创建请求管理者
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
- manager.requestSerializer.timeoutInterval = 10;
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- [manager.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
- [manager.requestSerializer setValue:SOURCE_ID forHTTPHeaderField:@"source"];
- [manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
- if ([KDPAccountTool isLogin]) {
- NSString *token = [KDPAccountTool account].token;
-
- [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
- }
- NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
- [manager.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
- // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
- [manager.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
-
-
- // 2.发送请求
- [manager 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"] isEqualToString:@"0"]) {
- if (!responseDict[@"rst"]) {
- success(@{});
- }else{
- success(responseDict[@"rst"]);
- }
- }else{
-
- NSString *message=responseDict[@"err"];
-
-
- if([responseDict[@"errno"] isEqualToString:@"10009"]) {
-
- }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
- //让用户退出
- [MBProgressHUD showMessage:@"登录信息过期,请重新登录"];
- [self appOpenLogin];
-
- }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
- //让用户退出
- [self appOpenLogin];
- [MBProgressHUD showMessage:@"您已在其他设备登录,是否重新登录"];
-
- }else{
-
- }
-
-
- if (failure) {
- failure(nil);
- }
- }
- } 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);
- NSLog(@"报错地址:%@",url);
- [MBProgressHUD showError:@"服务器开小差"];
- if (failure) {
- failure(error);
- }
- }];
- }
- @end
|