説明なし

LFWHttp.m 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // LFWHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LFWHttp.h"
  9. #import "LFWLoginViewController.h"
  10. @implementation LFWHttp
  11. + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
  12. // 1.创建请求管理者
  13. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  14. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  15. mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
  16. mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  17. mgr.requestSerializer.timeoutInterval = 15;
  18. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  19. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  20. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  21. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  22. if ([AccountTool isLogin]) {
  23. NSString *token = [AccountTool account].token;
  24. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  25. }
  26. NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  27. [mgr.requestSerializer setValue:sex forHTTPHeaderField:@"sex"];
  28. // 2.发送请求
  29. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  30. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  31. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  32. if (!responseDict[@"rst"]) {
  33. success(@{});
  34. }else{
  35. success(responseDict[@"rst"]);
  36. }
  37. }else{
  38. if ([responseDict[@"errno"] isEqualToString:@"401"]) {
  39. //已在其他设备登录
  40. [self loginInOtherDevice];
  41. }else{
  42. NSString *errorStr = responseDict[@"err"];
  43. if (errorStr) {
  44. failure(nil);
  45. return ;
  46. }
  47. }
  48. NSError *error=nil;
  49. failure(error);
  50. }
  51. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  52. if (failure) {
  53. failure(error);
  54. }
  55. }];
  56. }
  57. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure {
  58. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  59. mgr.requestSerializer.timeoutInterval = 15;
  60. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  61. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  62. mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  63. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  64. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  65. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  66. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"channel_id"];
  67. if ([AccountTool isLogin]) {
  68. NSString *token = [AccountTool account].token;
  69. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  70. }
  71. NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  72. [mgr.requestSerializer setValue:sex forHTTPHeaderField:@"sex"];
  73. // 2.发送请求
  74. [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  75. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  76. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  77. if (!responseDict[@"rst"]) {
  78. success(@{});
  79. }else{
  80. success(responseDict[@"rst"]);
  81. }
  82. }else{
  83. if ([responseDict[@"errno"] isEqualToString:@"401"]) {
  84. //已在其他设备登录
  85. [self loginInOtherDevice];
  86. }else{
  87. NSString *errorStr = responseDict[@"err"];
  88. if (errorStr) {
  89. return ;
  90. }
  91. }
  92. NSError *error=nil;
  93. failure(error);
  94. }
  95. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  96. if (failure) {
  97. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  98. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  99. NSLog(@"服务器的错误原因:%@",str);
  100. failure(error);
  101. }
  102. }];
  103. }
  104. /**
  105. 已在其他设备登录
  106. */
  107. + (void)loginInOtherDevice {
  108. [AccountTool deleteAccount];
  109. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您已在其他设备登录,是否重新登录?" preferredStyle:UIAlertControllerStyleAlert];
  110. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
  111. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  112. LFWLoginViewController *login = [[LFWLoginViewController alloc] init];
  113. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
  114. }];
  115. [alert addAction:cancel];
  116. [alert addAction:sure];
  117. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  118. }
  119. @end