123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- //
- // KBHttp.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/16.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "KBHttp.h"
- #import "KBLoginViewController.h"
- #import "KBHistoryTool.h"
- #import "AccountTool.h"
- #define jsonDataPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"jsonData.plist"]
- #define JsonDataKey @"JsonData"
- @implementation KBHttp
- + (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"];
- if ([AccountTool isLogin]) {
- NSString *token = [AccountTool account].token;
- [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
- }
- // 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{
- if([responseDict[@"errno"] isEqualToString:@"10009"]) {
- NSDictionary *errorDic= responseDict[@"rst"];
- if (errorDic.count>0) {
- NSString *message = errorDic[@"msg"];
- [MBProgressHUD showTip:message];
- }
- }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
- //让用户退出
- [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
- }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
- //让用户退出
- [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
- }else{
- NSString *message=responseDict[@"err"];
- if (message &&![@"" isEqualToString:message]) {
- [MBProgressHUD showTip:message];
- }
- }
- 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);
- 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"];
- if ([AccountTool isLogin]) {
- NSString *token = [AccountTool account].token;
- [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
- }
- // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
- [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{
- if ([responseDict[@"errno"] isEqualToString:@"401"]) {
- //已在其他设备登录
- // [self loginInOtherDevice];
- }else{
- NSString *errorStr = responseDict[@"err"];
- if (errorStr) {
-
- return ;
- }
- }
- NSError *error=nil;
- failure(error);
- }
- } 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);
- 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];
- [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object: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) {
- KBLoginViewController *login = [[KBLoginViewController alloc] init];
- [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
- }];
- [alert addAction:cancel];
- [alert addAction:sure];
-
-
- [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
-
- }
- @end
|