dkahgld

ZBHTTP.m 8.3KB

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