addLogObject.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // addLogObject.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/4/11.
  6. //
  7. #import "addLogObject.h"
  8. #import "netWorkManager.h"
  9. #import "connectDeviceManager.h"
  10. #import "fileUploadToFileCenterModel.h"
  11. #import <SSZipArchive/SSZipArchive.h>
  12. @implementation addLogObject
  13. static addLogObject *addlog_ShareInstance = nil;
  14. +(addLogObject *)shareInstance;
  15. {
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. addlog_ShareInstance = [[addLogObject alloc]init];
  19. });
  20. return addlog_ShareInstance;
  21. }
  22. - (id)init
  23. {
  24. self = [super init];
  25. if (self) {
  26. _canUploadNowType = YES;
  27. }
  28. return self;
  29. }
  30. //1.读取日志
  31. //2.上传到文件中心
  32. //3.拿到key上传到盒子服务器
  33. - (void)gotoAddLogFun
  34. {
  35. //HLog(@"hxd beginTime");
  36. if(!_canUploadNowType){
  37. return;
  38. }
  39. //一小时内只能上传一次
  40. NSNumber *preSecondNum = [HWDataManager getNumberWithKey:stringKeyAddSn(@"addLog")];
  41. if(preSecondNum){
  42. long seconds = [iTools getNowTimeStamp];
  43. if(seconds - preSecondNum.longValue <= 60*60){
  44. return;
  45. }
  46. }
  47. _canUploadNowType = NO;
  48. NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch];
  49. NSFileManager *fileManager = [NSFileManager defaultManager];
  50. // 检查文件是否存在
  51. BOOL fileExists = [fileManager fileExistsAtPath:zipPath];
  52. if (fileExists) {
  53. // 删除文件
  54. NSError *error = nil;
  55. BOOL success = [fileManager removeItemAtPath:zipPath error:&error];
  56. if (success) {
  57. HLog(@"文件已成功删除: %@", zipPath);
  58. } else {
  59. HLog(@"删除文件失败: %@", [error localizedDescription]);
  60. }
  61. }
  62. //2.读取日志
  63. //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  64. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch];
  65. NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch];
  66. //3. 将文件压缩成ZIP
  67. NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2];
  68. [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip];
  69. NSData *logData = [NSData dataWithContentsOfFile:zipPath];
  70. HLog(@"%@",logData);
  71. if(!logData || [logData length]==0){
  72. _canUploadNowType = YES;
  73. return;
  74. }
  75. //4.上传到文件中心
  76. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  77. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  78. if(!snStr){
  79. snStr = [iTools getNowTimeString];
  80. }
  81. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  82. [paraDict setValue:filename forKey:@"filename"];
  83. [paraDict setValue:@7 forKey:@"days"];
  84. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  85. HLog(@"%@",responseObject);
  86. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  87. if(model.code == 200){
  88. NSString * fileKey = model.data.fileKey;
  89. if(fileKey && fileKey.length >0){
  90. [self uploadKeyToServerFunBy:fileKey];
  91. }
  92. else{
  93. self->_canUploadNowType = YES;
  94. }
  95. }
  96. else{
  97. self->_canUploadNowType = YES;
  98. }
  99. } faild:^(NSError * _Nonnull error) {
  100. self->_canUploadNowType = YES;
  101. }];
  102. //[self uploadKeyToServerFunBy:@"LowLevelMultipartUpload_43025714606656109698"];
  103. }
  104. //3.拿到key上传到盒子服务器
  105. - (void)uploadKeyToServerFunBy:(NSString*)fileKey
  106. {
  107. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  108. [paraDict setValue:@"ios" forKey:@"type"];
  109. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  110. if(!snStr){
  111. _canUploadNowType = YES;
  112. return;
  113. }
  114. [paraDict setValue:snStr forKey:@"sn"];
  115. [paraDict setValue:fileKey forKey:@"key"];
  116. KWeakSelf
  117. [[netWorkManager shareInstance] CommonPostCallBackCode:addLogFun Parameters:paraDict success:^(id _Nonnull responseObject) {
  118. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  119. if(model.code == 0){
  120. [weakSelf markAddLogTimeFun];
  121. }
  122. else{
  123. self->_canUploadNowType = YES;
  124. }
  125. } failure:^(NSError * _Nonnull error) {
  126. self->_canUploadNowType = YES;
  127. }];
  128. }
  129. - (void)markAddLogTimeFun
  130. {
  131. //
  132. long seconds = [iTools getNowTimeStamp];
  133. NSNumber *secondsNum = [NSNumber numberWithLong:seconds];
  134. if(secondsNum){
  135. [HWDataManager setNumberWithKey:stringKeyAddSn(@"addLog") value:secondsNum];
  136. }
  137. _canUploadNowType = YES;
  138. //HLog(@"hxd endTime");
  139. }
  140. @end