天天省钱快报

KBHttp.m 7.5KB

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