123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- //
- // NetworkManager.m
- // LNManager
- //
- // Created by EchoShacolee on 2017/4/6.
- // Copyright © 2017年 lee. All rights reserved.
- //
- #import "NetworkManager.h"
- #import "NetHeader.h"
- #import <AFNetworking.h>
- #import <RealReachability/RealReachability.h>
- #import "NSString+ex.h"
- #import <MBProgressHUD.h>
- @implementation NetworkManager
- + (BOOL) connectedToNetWork{
-
- [GLobalRealReachability startNotifier];
- ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
- [GLobalRealReachability stopNotifier];
-
- // return status ? YES : NO;
-
- return YES;//@lee-mark 2017/9/30 临时处理,似乎没有网络判断的必要,并且如果为no的时候会阻止请求失败的一些处理;
- }
- + (void)requestWithMethod:(NSString *)method
- parameters:(id)parameters
- type:(HttpRequestType)type
- handler:(GetDataHandler)handler;{
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.requestSerializer = [AFJSONRequestSerializer serializer];
- manager.responseSerializer = [AFHTTPResponseSerializer serializer];
- manager.requestSerializer.timeoutInterval = 15;
-
- // manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript", nil];
-
- //生成请求加密后的url
- NSString *URLString = [NetworkManager getURLWithDict:parameters urlPre:method];
-
- switch (type) {
- case HttpRequestTypeGet:
- {
- [manager GET:URLString parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- if (handler) {
- if (responseObject == nil) {
- handler(nil,@"无数据");
- return;
- }
- NSError * error;
- NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error:&error];
- if (error) {
- handler(nil,@"数据解析异常");
- // NSLog(@"数据解析异常-%@<%@>error:%@",parameters,method,error);
- }else{
- handler(dic,nil);
- // NSLog(@"%@<%@>success:%@",parameters,method,dic);
- }
-
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- if (handler) {
- if ([error.localizedDescription isEqualToString:@"The request timed out."]) {
- handler(nil,@"请求超时");
- }else{
- handler(nil,@"请求失败");
- // NSLog(@"post请求失败-%@\n%ld\n%@\n%@\n%@\n本地恢复建议%@\n获取恢复建议%@",
- // parameters,
- // (long)error.code,
- // error.localizedDescription,
- // error.userInfo[@"NSErrorFailingURLStringKey"],
- // error.localizedFailureReason,
- // error.localizedRecoveryOptions,
- // error.localizedRecoverySuggestion);
- }
-
- }
- }];
- }
- break;
- case HttpRequestTypePost:
- {
- [manager POST:URLString parameters:parameters headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
- if (handler) {
- if (responseObject == nil) {
- handler(nil,@"无数据");
- return;
- }
- NSError * error;
- NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error:&error];
- if (error) {
- handler(nil,@"数据解析异常");
- NSLog(@"数据解析异常-%@<%@>error:%@",parameters,URLString,error);
- }else{
- handler(dic,nil);
- NSLog(@"%@<%@>success:%@",parameters,URLString,dic);
- }
-
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- if (handler) {
- if ([error.localizedDescription isEqualToString:@"The request timed out."]) {
- handler(nil,@"请求超时");
- }else{
- handler(nil,@"请求失败");
- NSLog(@"post请求失败-%@\n%ld\n%@\n%@\n%@\n本地恢复建议%@\n获取恢复建议%@",
- parameters,
- (long)error.code,
- error.localizedDescription,
- error.userInfo[@"NSErrorFailingURLKey"],
- error.localizedFailureReason,
- error.localizedRecoveryOptions,
- error.localizedRecoverySuggestion);
- }
-
- }
- }];
- }
- break;
- }
- }
- //生成带有签名的URL
- + (NSString *)getURLWithDict:(NSDictionary *)dic urlPre:(NSString *)urlP
- {
-
- NSString *httpUrl = defaultHttp;
- if (!isOfficial) {
- httpUrl = TestHttp;
- }
-
- NSString *urlDefault = defaultHttpUrl;
-
- NSTimeInterval timeI = [[NSDate date] timeIntervalSince1970] * 1000;//获取时间戳 单位:s *1000毫秒
- NSString *urlS = @"";
- if (dic == nil) {
- urlS = [NSString stringWithFormat:@"%@%@%@?ts=%@&user=ios&v=ln",httpUrl,urlDefault,urlP,[NSString stringWithFormat:@"%.0f",timeI]];
- }else{
-
- //添加key字段
- NSMutableString *contentString = [NSMutableString stringWithString:[self getSignStringWithDictionary:dic]];
- [contentString appendFormat:@"key=%@", [NSString stringWithFormat:@"%.0f",timeI]];
-
- urlS = [NSString stringWithFormat:@"%@%@%@?ts=%@&sign=%@&user=ios&v=ln",httpUrl,urlDefault,urlP,[NSString stringWithFormat:@"%.0f",timeI],[[contentString md5Encrypt] uppercaseString]];
-
- }
- return urlS;
- }
- + (NSString *)getSignStringWithDictionary:(NSDictionary *)dic
- {
- NSMutableString *contentString =[NSMutableString string];
- NSDictionary* dict = [NSDictionary dictionaryWithDictionary:dic];
- NSArray *keys = [dict allKeys];
- //按字母顺序排序
- NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
- return [obj1 compare:obj2 options:NSNumericSearch];
- }];
- //拼接字符串
- for (NSString *categoryId in sortedArray) {
-
- if (![categoryId isEqualToString:@"sign"] && ![categoryId isEqualToString:@"timestamp"]){
-
- if([categoryId isEqualToString:@"biz_content"]){
- NSError *error = nil;
- NSDictionary* bizDict = [dict objectForKey:@"biz_content"];
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:bizDict options:NSJSONWritingPrettyPrinted error: &error];
- NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
- NSString* jsonString1 = [[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding];
- NSString *jsonString2 = [jsonString1 stringByReplacingOccurrencesOfString:@" : " withString:@":"];
- [contentString appendFormat:@"biz_content=%@&",jsonString2];
- }else{
- [contentString appendFormat:@"%@=%@&", categoryId, [dict valueForKey:categoryId]];
- }
- }
- }
-
- return contentString;
- }
- @end
|