Commit 0a1f258e authored by 朱璇's avatar 朱璇

增加每个请求的唯一标识trace_id

parent 20e500a7
......@@ -213,7 +213,6 @@
6003F587195388D20070C39A /* Frameworks */,
6003F588195388D20070C39A /* Resources */,
E5148730E302727A2A70918A /* [CP] Embed Pods Frameworks */,
8076969C9D6AA6428E01E508 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
......@@ -232,8 +231,6 @@
6003F5AA195388D20070C39A /* Sources */,
6003F5AB195388D20070C39A /* Frameworks */,
6003F5AC195388D20070C39A /* Resources */,
E15697FFEC80A844FFD3D9A1 /* [CP] Embed Pods Frameworks */,
8BA8F52FD87C2DB398B4BB07 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
......@@ -342,58 +339,13 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
8076969C9D6AA6428E01E508 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-GMNetService_Example/Pods-GMNetService_Example-resources.sh\"\n";
showEnvVarsInLog = 0;
};
8BA8F52FD87C2DB398B4BB07 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-GMNetService_Tests/Pods-GMNetService_Tests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
E15697FFEC80A844FFD3D9A1 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-GMNetService_Tests/Pods-GMNetService_Tests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
E5148730E302727A2A70918A /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-GMNetService_Example/Pods-GMNetService_Example-frameworks.sh",
"${PODS_ROOT}/Target Support Files/Pods-GMNetService_Example/Pods-GMNetService_Example-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/AFNetworking/AFNetworking.framework",
"${BUILT_PRODUCTS_DIR}/GMNetService/GMNetService.framework",
);
......@@ -404,7 +356,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-GMNetService_Example/Pods-GMNetService_Example-frameworks.sh\"\n";
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-GMNetService_Example/Pods-GMNetService_Example-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
......
......@@ -14,20 +14,24 @@ PODS:
- AFNetworking/Serialization (3.1.0)
- AFNetworking/UIKit (3.1.0):
- AFNetworking/NSURLSession
- GMNetService (0.1.9):
- GMNetService (1.0.2):
- AFNetworking (= 3.1.0)
DEPENDENCIES:
- GMNetService (from `../`)
SPEC REPOS:
https://github.com/cocoapods/specs.git:
- AFNetworking
EXTERNAL SOURCES:
GMNetService:
:path: ../
:path: "../"
SPEC CHECKSUMS:
AFNetworking: 5e0e199f73d8626b11e79750991f5d173d1f8b67
GMNetService: 09e2c2cea77d0337118f91ff96cd748db8fc05c8
GMNetService: 35d60c9581b4881c986670a722d7b79a2d575d54
PODFILE CHECKSUM: 7ff46b43a8d5593b3f8ac864bad161c3754f2311
COCOAPODS: 1.3.1
COCOAPODS: 1.6.1
......@@ -9,6 +9,7 @@
#import "WMNetService.h"
#import "GMHTTPSessionManager.h"
#import "Utils.h"
#import<CommonCrypto/CommonDigest.h>
@implementation WMNetService
......@@ -42,6 +43,8 @@
failed:(HttpFailedBlock)failed {
NSString *resultUrl = nil;
resultUrl = [url stringByAppendingString:_urlCommonParameters];
// 拼接trace_id
resultUrl = [resultUrl stringByAppendingString:[NSString stringWithFormat:@"&trace_id=%@", [self createTraceId]]];
NSString * URLString = [[NSURL URLWithString:resultUrl relativeToURL:[NSURL URLWithString:_apiHost]] absoluteString];
[self startUploadTaskRequestWithData:data fileName:fileName name:name URLString:URLString mineType:mineType parameters:parameters success:success failed:failed];
}
......@@ -59,7 +62,8 @@
}
NSString *fullUrlString = [NSString stringWithFormat:@"%@%@%@", self.apiHost, URLString, self.urlCommonParameters];
// 拼接trace_id
fullUrlString = [fullUrlString stringByAppendingString:[NSString stringWithFormat:@"&trace_id=%@", [self createTraceId]]];
if (method == HttpMethodGet) {
[networking GET:fullUrlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
[self successWithTask:task responseObject:responseObject success:success];
......@@ -180,4 +184,61 @@
_networking = [GMHTTPSessionManager shareInstanceWithApiHost:_apiHost];
}
// 生成traceId
- (NSString *)createTraceId {
NSString *traceId = @"";
// 1、先取 当前时间 按照 "年/月/日/时分" 转换 作为traceId的上半部分--> 2018/10/25/1821
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
NSTimeInterval timeNow = [date timeIntervalSince1970];
NSString *frontPart = [self getDateStringWithTimeStr:timeNow];
traceId = [traceId stringByAppendingString:frontPart];
// 2、取当前的时间戳 + 随机字符串(长度 32) 组成字符串,将该字符串 MD5哈希,取该哈希值的前 12位 作为图片路径的下半部分 --> caf8f8d86886
NSString *timeNowStr = [NSString stringWithFormat:@"%lld", timeNow];
NSString *radomStr = [self getRadomString];
NSString *secondPart = [timeNowStr stringByAppendingString:radomStr];
NSString *secondPartMD5 = [[self MD5ForLower32Bate:secondPart] substringToIndex:12];
return [NSString stringWithFormat:@"%@/%@", traceId, secondPartMD5];
}
// 时间戳转字符串
- (NSString *)getDateStringWithTimeStr:(float )time{
NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormat:@"yyyy/MM/dd/HHmm"];
NSString *currentDateStr = [dateFormatter stringFromDate: detailDate];
return currentDateStr;
}
// 取随机字符串
- (NSString *)getRadomString {
NSArray *array = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
NSMutableArray *randomArray = [[NSMutableArray alloc] init];
while (randomArray.count < 32) {
int r = arc4random() % [array count];
[randomArray addObject:[array objectAtIndex:r]];
}
return [randomArray componentsJoinedByString:@""];
}
// MD5加密
-(NSString *)MD5ForLower32Bate:(NSString *)str{
//要进行UTF8的转码
const char* input = [str UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(input, (CC_LONG)strlen(input), result);
NSMutableString *digest = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[digest appendFormat:@"%02x", result[i]];
}
return digest;
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment