省钱达人

DRHttp.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // DRHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRHttp.h"
  9. #import "DRLoginViewController.h"
  10. #import "DRHistoryTool.h"
  11. #import "AccountTool.h"
  12. #define jsonDataPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"jsonData.plist"]
  13. #define JsonDataKey @"JsonData"
  14. @implementation DRHttp
  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:sex 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. if (failure) {
  70. failure(error);
  71. }
  72. }];
  73. }
  74. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure {
  75. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  76. mgr.requestSerializer.timeoutInterval = 15;
  77. mgr.requestSerializer = [AFHTTPRequestSerializer serializer]; // 解析为data
  78. mgr.responseSerializer=[AFHTTPResponseSerializer serializer];
  79. mgr.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  80. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  81. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  82. [mgr.requestSerializer setValue:app_Version forHTTPHeaderField:@"version"];
  83. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"channel_id"];
  84. [mgr.requestSerializer setValue:Channel_id forHTTPHeaderField:@"source"];
  85. if ([AccountTool isLogin]) {
  86. NSString *token = [AccountTool account].token;
  87. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  88. }
  89. NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  90. [mgr.requestSerializer setValue:sex 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. if ([responseDict[@"errno"] isEqualToString:@"401"]) {
  103. //已在其他设备登录
  104. // [self loginInOtherDevice];
  105. }else{
  106. NSString *errorStr = responseDict[@"err"];
  107. if (errorStr) {
  108. return ;
  109. }
  110. }
  111. NSError *error=nil;
  112. failure(error);
  113. }
  114. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  115. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  116. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  117. NSLog(@"服务器的错误原因:%@",str);
  118. if (failure) {
  119. // NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  120. // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  121. // NSLog(@"服务器的错误原因:%@",str);
  122. failure(error);
  123. }
  124. }];
  125. }
  126. /**
  127. 已在其他设备登录
  128. */
  129. + (void)loginInOtherDeviceWithMsg:(NSString *)msg {
  130. [AccountTool deleteAccount];
  131. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  132. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
  133. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
  134. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  135. DRLoginViewController *login = [[DRLoginViewController alloc] init];
  136. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
  137. }];
  138. [alert addAction:cancel];
  139. [alert addAction:sure];
  140. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  141. }
  142. @end