addLogObject.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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)gotoAddLogFunBySuccess:(AddLog_Success)success
  34. {
  35. //HLog(@"hxd beginTime");
  36. if(!_canUploadNowType){
  37. success(@"");
  38. return;
  39. }
  40. //一小时内只能上传一次
  41. NSNumber *preSecondNum = [HWDataManager getNumberWithKey:stringKeyAddSn(@"addLog")];
  42. if(preSecondNum){
  43. long seconds = [iTools getNowTimeStamp];
  44. if(seconds - preSecondNum.longValue <= 60*60){
  45. success(@"");
  46. return;
  47. }
  48. }
  49. _canUploadNowType = NO;
  50. NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch];
  51. NSFileManager *fileManager = [NSFileManager defaultManager];
  52. // 检查文件是否存在
  53. BOOL fileExists = [fileManager fileExistsAtPath:zipPath];
  54. if (fileExists) {
  55. // 删除文件
  56. NSError *error = nil;
  57. BOOL success = [fileManager removeItemAtPath:zipPath error:&error];
  58. if (success) {
  59. HLog(@"文件已成功删除: %@", zipPath);
  60. } else {
  61. HLog(@"删除文件失败: %@", [error localizedDescription]);
  62. }
  63. }
  64. //2.读取日志
  65. //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  66. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch];
  67. NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch];
  68. //3. 将文件压缩成ZIP
  69. NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2];
  70. [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip];
  71. NSData *logData = [NSData dataWithContentsOfFile:zipPath];
  72. HLog(@"%@",logData);
  73. if(!logData || [logData length]==0){
  74. _canUploadNowType = YES;
  75. success(@"");
  76. return;
  77. }
  78. //4.上传到文件中心
  79. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  80. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  81. if(!snStr){
  82. snStr = [iTools getNowTimeString];
  83. }
  84. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  85. [paraDict setValue:filename forKey:@"filename"];
  86. [paraDict setValue:@7 forKey:@"days"];
  87. KWeakSelf
  88. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  89. HLog(@"%@",responseObject);
  90. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  91. if(model.code == 200){
  92. NSString * fileKey = model.data.fileKey;
  93. if(fileKey && fileKey.length >0){
  94. [weakSelf markAddLogTimeFun];
  95. //[self uploadKeyToServerFunBy:fileKey];
  96. success(fileKey);
  97. }
  98. else{
  99. success(@"");
  100. self->_canUploadNowType = YES;
  101. }
  102. }
  103. else{
  104. success(@"");
  105. self->_canUploadNowType = YES;
  106. }
  107. } faild:^(NSError * _Nonnull error) {
  108. success(@"");
  109. self->_canUploadNowType = YES;
  110. }];
  111. //[self uploadKeyToServerFunBy:@"LowLevelMultipartUpload_43025714606656109698"];
  112. }
  113. //3.拿到key上传到盒子服务器
  114. //- (void)uploadKeyToServerFunBy:(NSString*)fileKey
  115. //{
  116. // NSMutableDictionary *paraDict = [NSMutableDictionary new];
  117. // [paraDict setValue:@"ios" forKey:@"type"];
  118. //
  119. // NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  120. // if(!snStr){
  121. // _canUploadNowType = YES;
  122. // return;
  123. // }
  124. // [paraDict setValue:snStr forKey:@"sn"];
  125. // [paraDict setValue:fileKey forKey:@"key"];
  126. //
  127. // KWeakSelf
  128. // [[netWorkManager shareInstance] CommonPostCallBackCode:addLogFun Parameters:paraDict success:^(id _Nonnull responseObject) {
  129. // SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  130. // if(model.code == 0){
  131. // [weakSelf markAddLogTimeFun];
  132. // }
  133. // else{
  134. // self->_canUploadNowType = YES;
  135. // }
  136. //
  137. // } failure:^(NSError * _Nonnull error) {
  138. // self->_canUploadNowType = YES;
  139. // }];
  140. //}
  141. - (void)markAddLogTimeFun
  142. {
  143. //
  144. long seconds = [iTools getNowTimeStamp];
  145. NSNumber *secondsNum = [NSNumber numberWithLong:seconds];
  146. if(secondsNum){
  147. [HWDataManager setNumberWithKey:stringKeyAddSn(@"addLog") value:secondsNum];
  148. }
  149. _canUploadNowType = YES;
  150. //HLog(@"hxd endTime");
  151. }
  152. @end