No Description

ASHttpTool.m 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // ASHttpTool.m
  3. // ACSION
  4. //
  5. // Created by sunyue on 2019/4/22.
  6. // Copyright © 2019 acsion. All rights reserved.
  7. //
  8. #import "ASHttpTool.h"
  9. @implementation ASHttpTool
  10. + (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
  11. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  12. mgr.requestSerializer.timeoutInterval = 15;
  13. mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
  14. mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
  15. mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  16. NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
  17. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  18. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  19. NSString *urgentString = [NSString stringWithFormat:@"&channel=%@&version=%@",CHANNEL_ID,app_Version];
  20. NSString *mixString = [agentString stringByAppendingString:urgentString];
  21. [mgr.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"];
  22. NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
  23. [mgr.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"];
  24. [mgr.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"];
  25. [mgr.requestSerializer setValue:CHANNEL_ID forHTTPHeaderField:@"cid"];
  26. [mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
  27. NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5";
  28. NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],CHANNEL_ID,@"iOS",sign_key];
  29. [mgr.requestSerializer setValue:[sign md5] forHTTPHeaderField:@"sign"];
  30. [mgr GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  31. id responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  32. if (![responseDict[@"errno"] isEqual:@"0"]) {
  33. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:responseDict[@"err"] preferredStyle:UIAlertControllerStyleAlert];
  34. [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  35. }]];
  36. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  37. NSError *error = nil;
  38. failure(error);
  39. } else {
  40. if (success) {
  41. success(responseDict[@"rst"]);
  42. }
  43. }
  44. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  45. NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  46. NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  47. NSLog(@"服务器的错误原因:%@",str);
  48. if (failure) {
  49. failure(error);
  50. }
  51. }];
  52. }
  53. + (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
  54. AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
  55. mgr.requestSerializer.timeoutInterval = 15;
  56. mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
  57. mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
  58. mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",nil];
  59. NSString *agentString = [mgr.requestSerializer valueForHTTPHeaderField:@"User-Agent"];
  60. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  61. NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  62. NSString *urgentString = [NSString stringWithFormat:@"&channel=%@&version=%@",CHANNEL_ID,app_Version];
  63. NSString *mixString = [agentString stringByAppendingString:urgentString];
  64. [mgr.requestSerializer setValue:mixString forHTTPHeaderField:@"User-Agent"];
  65. NSString *dateTimeInterval = [NSString stringWithFormat:@"%.0f",[[NSDate date]timeIntervalSince1970]];
  66. [mgr.requestSerializer setValue:[NSString stringWithFormat:@"%@",dateTimeInterval] forHTTPHeaderField:@"ttl"];
  67. [mgr.requestSerializer setValue:@"3.0" forHTTPHeaderField:@"version"];
  68. [mgr.requestSerializer setValue:CHANNEL_ID forHTTPHeaderField:@"cid"];
  69. [mgr.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"device"];
  70. NSString *sign_key = @"91f8edf4792c8f63593266f75493a5f5";
  71. NSString *sign = [NSString stringWithFormat:@"ttl=%@&version=3.0&cid=%@&device=%@&sign_key=%@",[NSString stringWithFormat:@"%@",dateTimeInterval],CHANNEL_ID,@"iOS",sign_key];
  72. [mgr.requestSerializer setValue:[sign md5] forHTTPHeaderField:@"sign"];
  73. [mgr POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  74. id responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
  75. if (![responseDict[@"errno"] isEqual:@"0"]) {
  76. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:responseDict[@"err"] preferredStyle:UIAlertControllerStyleAlert];
  77. [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  78. }]];
  79. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
  80. NSError *error = nil;
  81. failure(error);
  82. } else {
  83. if (success) {
  84. success(responseDict[@"rst"]);
  85. }
  86. }
  87. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  88. NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  89. NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  90. NSLog(@"服务器的错误原因:%@",str);
  91. if (failure) {
  92. failure(error);
  93. }
  94. }];
  95. }
  96. + (void)uploadImageWithUrl:(NSString *)url name:(NSString *)name params:(NSDictionary *)params imageData:(NSData *)data imageName:(NSString *)imageName success:(void (^)(id _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
  97. }
  98. @end