// // LFWHttp.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LFWHttp.h" #import "LFWLoginViewController.h" @implementation LFWHttp + (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 = 15; 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:sex 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:@"401"]) { //已在其他设备登录 [self loginInOtherDevice]; }else{ NSString *errorStr = responseDict[@"err"]; if (errorStr) { failure(nil); return ; } } NSError *error=nil; failure(error); } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 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"]; if ([AccountTool isLogin]) { NSString *token = [AccountTool account].token; [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"]; } NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey]; [mgr.requestSerializer setValue:sex forHTTPHeaderField:@"sex"]; // 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) { 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)loginInOtherDevice { [AccountTool deleteAccount]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您已在其他设备登录,是否重新登录?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { LFWLoginViewController *login = [[LFWLoginViewController 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