猎豆优选

LDHttp.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // LDHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDHttp.h"
  9. #import "LDLoginViewController.h"
  10. #import "LDHistoryTool.h"
  11. #import "AccountTool.h"
  12. #import "YBCacheTool.h"
  13. #import <AdSupport/AdSupport.h>
  14. #define jsonDataPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"jsonData.plist"]
  15. #define JsonDataKey @"JsonData"
  16. @implementation LDHttp
  17. + (void)post:(NSString *)url params:(id)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure {
  18. // 1.创建请求管理者
  19. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  20. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  21. mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
  22. mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  23. mgr.requestSerializer.timeoutInterval = 10;
  24. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  25. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  26. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  27. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  28. [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  29. if ([AccountTool isLogin]) {
  30. NSString *token = [AccountTool account].token;
  31. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  32. }
  33. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  34. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  35. [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  36. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  37. [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  38. //测试
  39. // 2.发送请求
  40. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  41. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  42. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  43. if (!responseDict[@"rst"]) {
  44. success(@{});
  45. }else{
  46. success(responseDict[@"rst"]);
  47. }
  48. }else{
  49. NSString *message=responseDict[@"err"];
  50. if (message &&![@"" isEqualToString:message]) {
  51. [XHToast showCenterWithText:message];
  52. }
  53. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  54. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  55. //让用户退出
  56. [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
  57. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  58. //让用户退出
  59. [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
  60. }else{
  61. }
  62. if (failure) {
  63. failure(nil);
  64. }
  65. }
  66. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  67. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  68. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  69. NSLog(@"服务器的错误原因:%@",str);
  70. NSLog(@"报错地址:%@",url);
  71. if (failure) {
  72. failure(error);
  73. }
  74. }];
  75. }
  76. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure {
  77. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  78. mgr.requestSerializer.timeoutInterval = 15;
  79. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  80. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  81. mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  82. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  83. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  84. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  85. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"channel_id"];
  86. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  87. [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  88. if ([AccountTool isLogin]) {
  89. NSString *token = [AccountTool account].token;
  90. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  91. }
  92. NSString *deviceUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
  93. NSString *uuid = [NSString stringWithFormat:@"I-%@",deviceUUID];
  94. [mgr.requestSerializer setValue:uuid forHTTPHeaderField:@"idfa"];
  95. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  96. [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  97. // jcytesttoken
  98. // 2.发送请求
  99. [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  100. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  101. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  102. if (!responseDict[@"rst"]) {
  103. success(@{});
  104. }else{
  105. success(responseDict[@"rst"]);
  106. }
  107. }else{
  108. NSString *message=responseDict[@"err"];
  109. if (message &&![@"" isEqualToString:message]) {
  110. [XHToast showCenterWithText:message];
  111. }
  112. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  113. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  114. //让用户退出
  115. [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
  116. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  117. //让用户退出
  118. [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
  119. }else{
  120. }
  121. if (failure) {
  122. failure(nil);
  123. }
  124. }
  125. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  126. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  127. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  128. NSLog(@"服务器的错误原因:%@",str);
  129. if (failure) {
  130. // NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  131. // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  132. // NSLog(@"服务器的错误原因:%@",str);
  133. failure(error);
  134. }
  135. }];
  136. }
  137. /**
  138. 已在其他设备登录
  139. */
  140. + (void)loginInOtherDeviceWithMsg:(NSString *)msg {
  141. [AccountTool deleteAccount];
  142. [YBCacheTool clearCache];
  143. //判断是否有粘贴。有,则直接退出登录
  144. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  145. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
  146. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
  147. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  148. LDNewLoginVC *login = [[LDNewLoginVC alloc] init];
  149. login.modalPresentationStyle = UIModalPresentationFullScreen;
  150. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
  151. }];
  152. [alert addAction:cancel];
  153. [alert addAction:sure];
  154. alert.modalPresentationStyle = UIModalPresentationFullScreen;
  155. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  156. }
  157. @end