NetworkManager.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // NetworkManager.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/4/6.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "NetworkManager.h"
  9. #import "NetHeader.h"
  10. #import <AFNetworking.h>
  11. #import <RealReachability/RealReachability.h>
  12. #import "NSString+ex.h"
  13. #import <MBProgressHUD.h>
  14. @implementation NetworkManager
  15. + (BOOL) connectedToNetWork{
  16. [GLobalRealReachability startNotifier];
  17. ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
  18. [GLobalRealReachability stopNotifier];
  19. // return status ? YES : NO;
  20. return YES;//@lee-mark 2017/9/30 临时处理,似乎没有网络判断的必要,并且如果为no的时候会阻止请求失败的一些处理;
  21. }
  22. + (void)requestWithMethod:(NSString *)method
  23. parameters:(id)parameters
  24. type:(HttpRequestType)type
  25. handler:(GetDataHandler)handler;{
  26. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  27. manager.requestSerializer = [AFJSONRequestSerializer serializer];
  28. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  29. manager.requestSerializer.timeoutInterval = 15;
  30. // manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
  31. //生成请求加密后的url
  32. NSString *URLString = [NetworkManager getURLWithDict:parameters urlPre:method];
  33. switch (type) {
  34. case HttpRequestTypeGet:
  35. {
  36. [manager GET:URLString parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  37. if (handler) {
  38. if (responseObject == nil) {
  39. handler(nil,@"无数据");
  40. return;
  41. }
  42. NSError * error;
  43. NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error:&error];
  44. if (error) {
  45. handler(nil,@"数据解析异常");
  46. // NSLog(@"数据解析异常-%@<%@>error:%@",parameters,method,error);
  47. }else{
  48. handler(dic,nil);
  49. // NSLog(@"%@<%@>success:%@",parameters,method,dic);
  50. }
  51. }
  52. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  53. if (handler) {
  54. if ([error.localizedDescription isEqualToString:@"The request timed out."]) {
  55. handler(nil,@"请求超时");
  56. }else{
  57. handler(nil,@"请求失败");
  58. // NSLog(@"post请求失败-%@\n%ld\n%@\n%@\n%@\n本地恢复建议%@\n获取恢复建议%@",
  59. // parameters,
  60. // (long)error.code,
  61. // error.localizedDescription,
  62. // error.userInfo[@"NSErrorFailingURLStringKey"],
  63. // error.localizedFailureReason,
  64. // error.localizedRecoveryOptions,
  65. // error.localizedRecoverySuggestion);
  66. }
  67. }
  68. }];
  69. }
  70. break;
  71. case HttpRequestTypePost:
  72. {
  73. [manager POST:URLString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  74. if (handler) {
  75. if (responseObject == nil) {
  76. handler(nil,@"无数据");
  77. return;
  78. }
  79. NSError * error;
  80. NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error:&error];
  81. if (error) {
  82. handler(nil,@"数据解析异常");
  83. NSLog(@"数据解析异常-%@<%@>error:%@",parameters,URLString,error);
  84. }else{
  85. handler(dic,nil);
  86. NSLog(@"%@<%@>success:%@",parameters,URLString,dic);
  87. }
  88. }
  89. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  90. if (handler) {
  91. if ([error.localizedDescription isEqualToString:@"The request timed out."]) {
  92. handler(nil,@"请求超时");
  93. }else{
  94. handler(nil,@"请求失败");
  95. NSLog(@"post请求失败-%@\n%ld\n%@\n%@\n%@\n本地恢复建议%@\n获取恢复建议%@",
  96. parameters,
  97. (long)error.code,
  98. error.localizedDescription,
  99. error.userInfo[@"NSErrorFailingURLKey"],
  100. error.localizedFailureReason,
  101. error.localizedRecoveryOptions,
  102. error.localizedRecoverySuggestion);
  103. }
  104. }
  105. }];
  106. }
  107. break;
  108. }
  109. }
  110. //生成带有签名的URL
  111. + (NSString *)getURLWithDict:(NSDictionary *)dic urlPre:(NSString *)urlP
  112. {
  113. NSString *httpUrl = defaultHttp;
  114. if (!isOfficial) {
  115. httpUrl = TestHttp;
  116. }
  117. NSString *urlDefault = defaultHttpUrl;
  118. NSTimeInterval timeI = [[NSDate date] timeIntervalSince1970] * 1000;//获取时间戳 单位:s *1000毫秒
  119. NSString *urlS = @"";
  120. if (dic == nil) {
  121. urlS = [NSString stringWithFormat:@"%@%@%@?ts=%@&user=ios&v=ln",httpUrl,urlDefault,urlP,[NSString stringWithFormat:@"%.0f",timeI]];
  122. }else{
  123. //添加key字段
  124. NSMutableString *contentString = [NSMutableString stringWithString:[self getSignStringWithDictionary:dic]];
  125. [contentString appendFormat:@"key=%@", [NSString stringWithFormat:@"%.0f",timeI]];
  126. urlS = [NSString stringWithFormat:@"%@%@%@?ts=%@&sign=%@&user=ios&v=ln",httpUrl,urlDefault,urlP,[NSString stringWithFormat:@"%.0f",timeI],[[contentString md5Encrypt] uppercaseString]];
  127. }
  128. return urlS;
  129. }
  130. + (NSString *)getSignStringWithDictionary:(NSDictionary *)dic
  131. {
  132. NSMutableString *contentString =[NSMutableString string];
  133. NSDictionary* dict = [NSDictionary dictionaryWithDictionary:dic];
  134. NSArray *keys = [dict allKeys];
  135. //按字母顺序排序
  136. NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  137. return [obj1 compare:obj2 options:NSNumericSearch];
  138. }];
  139. //拼接字符串
  140. for (NSString *categoryId in sortedArray) {
  141. if (![categoryId isEqualToString:@"sign"] && ![categoryId isEqualToString:@"timestamp"]){
  142. if([categoryId isEqualToString:@"biz_content"]){
  143. NSError *error = nil;
  144. NSDictionary* bizDict = [dict objectForKey:@"biz_content"];
  145. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bizDict options:NSJSONWritingPrettyPrinted error: &error];
  146. NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
  147. NSString* jsonString1 = [[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding];
  148. NSString *jsonString2 = [jsonString1 stringByReplacingOccurrencesOfString:@" : " withString:@":"];
  149. [contentString appendFormat:@"biz_content=%@&",jsonString2];
  150. }else{
  151. [contentString appendFormat:@"%@=%@&", categoryId, [dict valueForKey:categoryId]];
  152. }
  153. }
  154. }
  155. return contentString;
  156. }
  157. @end