口袋版本的一折买

YZMAHttp.m 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // YZMAHttp.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "YZMAHttp.h"
  9. #import "YZMALoginViewController.h"
  10. #import "YZMAHistoryTool.h"
  11. #import "AccountTool.h"
  12. #define jsonDataPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"jsonData.plist"]
  13. #define JsonDataKey @"JsonData"
  14. @implementation YZMAHttp
  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. [mgr.requestSerializer setValue:@"ios" forHTTPHeaderField:@"platform"];
  27. if ([AccountTool isLogin]) {
  28. NSString *token = [AccountTool account].token;
  29. [mgr.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  30. }
  31. // NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  32. [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  33. //测试
  34. // 2.发送请求
  35. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  36. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  37. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  38. if (!responseDict[@"rst"]) {
  39. success(@{});
  40. }else{
  41. success(responseDict[@"rst"]);
  42. }
  43. }else{
  44. if([responseDict[@"errno"] isEqualToString:@"10009"]) {
  45. NSDictionary *errorDic= responseDict[@"rst"];
  46. if (errorDic.count>0) {
  47. NSString *message = errorDic[@"msg"];
  48. [MBProgressHUD showTip:message];
  49. }
  50. }else if([responseDict[@"errno"] isEqualToString:@"4001"]) {
  51. //让用户退出
  52. [self loginInOtherDeviceWithMsg:@"登录信息过期,请重新登录"];
  53. }else if([responseDict[@"errno"] isEqualToString:@"4002"]) {
  54. //让用户退出
  55. [self loginInOtherDeviceWithMsg:@"您已在其他设备登录,是否重新登录"];
  56. }else{
  57. NSString *message=responseDict[@"err"];
  58. if (message &&![@"" isEqualToString:message]) {
  59. [MBProgressHUD showTip:message];
  60. }
  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 *sex = [[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey];
  93. [mgr.requestSerializer setValue:@"0" forHTTPHeaderField:@"sex"];
  94. // jcytesttoken
  95. // 2.发送请求
  96. [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  97. id responseDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  98. if ([responseDict[@"errno"] isEqualToString:@"0"]) {
  99. if (!responseDict[@"rst"]) {
  100. success(@{});
  101. }else{
  102. success(responseDict[@"rst"]);
  103. }
  104. }else{
  105. if ([responseDict[@"errno"] isEqualToString:@"401"]) {
  106. //已在其他设备登录
  107. // [self loginInOtherDevice];
  108. }else{
  109. NSString *errorStr = responseDict[@"err"];
  110. if (errorStr) {
  111. return ;
  112. }
  113. }
  114. NSError *error=nil;
  115. failure(error);
  116. }
  117. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  118. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  119. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  120. NSLog(@"服务器的错误原因:%@",str);
  121. NSLog(@"报错地址:%@",url);
  122. if (failure) {
  123. // NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  124. // NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  125. // NSLog(@"服务器的错误原因:%@",str);
  126. failure(error);
  127. }
  128. }];
  129. }
  130. /**
  131. 已在其他设备登录
  132. */
  133. + (void)loginInOtherDeviceWithMsg:(NSString *)msg {
  134. [AccountTool deleteAccount];
  135. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeSex object:nil];
  136. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
  137. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
  138. UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  139. YZMALoginViewController *login = [[YZMALoginViewController alloc] init];
  140. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:login animated:YES completion:nil];
  141. }];
  142. [alert addAction:cancel];
  143. [alert addAction:sure];
  144. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
  145. }
  146. @end