123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // ZBHTTP.m
- // ZBProject
- //
- // Created by 学丽 on 2019/3/26.
- // Copyright © 2019 ZB. All rights reserved.
- //
- #import "ZBHTTP.h"
- #import <AdSupport/AdSupport.h>
- #import "AccountTool.h"
- @implementation ZBHTTP
- + (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 = 10;
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
- [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
- [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
- if ([AccountTool isLogin]) {
- NSString *token = [AccountTool account].token;
-
- [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
- }
-
- NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
- [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
- // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
- [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
-
-
- // 2.发送请求
- [mgr 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 (message &&![@"" isEqualToString:message]&& ![url isEqualToString:getOrderListURL]) {
- [XHToast showCenterWithText:message];
- }
-
- if([responseDict[@"errno"] isEqualToString:@"10009"]) {
-
- }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
- //让用户退出
- [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
- }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
- //让用户退出
- [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
- }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 showTip:@"服务器开小差"];
- if (failure) {
- failure(error);
- }
- }];
-
- }
- + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))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];
- NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
- NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
- [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
- [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"channel_id"];
- [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
- [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
- if ([AccountTool isLogin]) {
- NSString *token = [AccountTool account].token;
- [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
- }
- NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
- NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
- [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
- [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
-
- // jcytesttoken
- // 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"] isEqualToString:@"0"]) {
- if (!responseDict[@"rst"]) {
- success(@{});
- }else{
- success(responseDict[@"rst"]);
- }
- }else{
-
- NSString *message=responseDict[@"err"];
- if (message &&![@"" isEqualToString:message] && ![url isEqualToString:getOrderListURL]) {//首页订单列表不需要错误提示
- [XHToast showCenterWithText:message];
- }
-
- if([responseDict[@"errno"] isEqualToString:@"10009"]) {
-
- }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
- //让用户退出
- [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
- }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
- //让用户退出
- [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
- }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);
- [MBProgressHUD showTip:@"服务器开小差"];
- if (failure) {
- // NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
- // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- // NSLog(@"服务器的错误原因:%@",str);
-
- failure(error);
- }
- }];
-
- }
- /**
- 已在其他设备登录
- */
- + (void)loginInOtherDeviceWithMsg:(NSString *)msg {
-
- [AccountTool deleteAccount];
- // [YBCacheTool clearCache];
- // [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
- UINavigationController *login = [[UINavigationController alloc]initWithRootViewController:[[ZBLoginViewController alloc] init]];
- [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
- // UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
- // UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
- // UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- //
- // }];
- // [alert addAction:cancel];
- // [alert addAction:sure];
- //
- //
- // [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
-
- }
- @end
|