NYTools.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // NYTools.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2024/3/22.
  6. // Copyright © 2024 JCZ. All rights reserved.
  7. //
  8. #import "NYTools.h"
  9. #import "AESCipher.h"
  10. #define nykey @"YojZ7wyaYkgSqf1O"
  11. #define nyIv @"KXh9DfxR0UKCEkH2"
  12. @implementation NYTools
  13. /**
  14. *记录-题目-对错
  15. *userKey 用户id
  16. *carType 车辆类型
  17. *subject 科目几
  18. *exerciseType 题目类型
  19. */
  20. +(void)saveExerciseModel:(RQExerciseModel*)exercise userKey:(NSString*)userKey carType:(RQHomePageCarType)carType subject:(RQHomePageSubjectType)subject exerciseType:(NSUInteger)exerciseType{
  21. NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
  22. YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
  23. if(exerciseType==RQExerciseType_Wrong){
  24. exerciseType = 0;//错题-移除顺序的
  25. }
  26. NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd",carType,subject,exerciseType];
  27. //记录——用户做的题目-答题
  28. // 获取当前题目的ID
  29. NSNumber *exerciseId = @(exercise.ydtQuestionModel.ID);
  30. NSArray *userAnswer = exercise.userAnswer.copy;
  31. NSDictionary *exer_dic = @{
  32. @"ID":exerciseId,//题目id
  33. @"type":@(exercise.ydtQuestionModel.Type),//题目类型
  34. @"answerResultsType":@(exercise.answerResultsType),//是否做对
  35. @"userAnswer":userAnswer,//用户选项
  36. };
  37. NSArray *array = [self readExerciseListUserKey:userKey carType:carType subject:subject exerciseType:exerciseType];
  38. // 转为可变数组准备更新
  39. NSMutableArray *value = [NSMutableArray arrayWithArray:array];
  40. // 查找是否已有该题记录,存在则更新
  41. __block BOOL isUpdated = NO;
  42. [value enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
  43. NSLog(@"tools_id==%@",obj[@"ID"]);
  44. if ([obj[@"ID"] isEqualToNumber:exerciseId]) {
  45. value[idx] = exer_dic; // 替换已有记录
  46. isUpdated = YES;
  47. *stop = YES;
  48. }
  49. }];
  50. // 不存在则添加
  51. if (!isUpdated) {
  52. [value addObject:exer_dic];
  53. }
  54. //根据key写入缓存value
  55. [yyCache setObject:value forKey:NYCocheNameKey withBlock:^{
  56. NSLog(@"setObject sucess");
  57. }];
  58. }
  59. +(void)saveListExerciseModel:(NSMutableArray*)list userKey:(NSString*)userKey carType:(RQHomePageCarType)carType subject:(RQHomePageSubjectType)subject exerciseType:(NSUInteger)exerciseType{
  60. NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
  61. YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
  62. if(exerciseType==RQExerciseType_Wrong){
  63. exerciseType = 0;//错题-移除顺序的
  64. }
  65. NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd",carType,subject,exerciseType];
  66. //根据key写入缓存value
  67. [yyCache setObject:list forKey:NYCocheNameKey withBlock:^{
  68. NSLog(@"setObject sucess");
  69. }];
  70. }
  71. //读取-题目对错
  72. + (NSArray *)readExerciseListUserKey:(NSString *)userKey
  73. carType:(RQHomePageCarType)carType
  74. subject:(RQHomePageSubjectType)subject
  75. exerciseType:(NSUInteger)exerciseType {
  76. NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@", userKey];
  77. YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
  78. NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd", carType, subject, exerciseType];
  79. id obj = [yyCache objectForKey:NYCocheNameKey];
  80. if ([obj isKindOfClass:[NSArray class]]||[obj isKindOfClass:[NSMutableArray class]]) {
  81. return (NSArray *)obj;
  82. }
  83. return @[];
  84. }
  85. //+(NSArray*)readExerciseListUserKey:(NSString*)userKey carType:(RQHomePageCarType)carType subject:(RQHomePageSubjectType)subject exerciseType:(NSUInteger)exerciseType{
  86. // NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
  87. // YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
  88. // NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd",carType,subject,exerciseType];
  89. // id object = [yyCache objectForKey:NYCocheNameKey];
  90. // if ([object isKindOfClass:[NSMutableArray class]]) {
  91. // return (NSArray *)object;
  92. // } else {
  93. // // 返回一个空数组或者其他默认值,视情况而定
  94. // return @[];
  95. // }
  96. //}
  97. +(void)delExerciseListUserKey:(NSString*)userKey
  98. carType:(RQHomePageCarType)carType
  99. subject:(RQHomePageSubjectType)subject
  100. exerciseType:(NSUInteger)exerciseType {
  101. NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@", userKey];
  102. YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
  103. if (exerciseType == RQExerciseType_Wrong) {
  104. exerciseType = 0;
  105. }
  106. NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd", carType, subject, exerciseType];
  107. // 推荐做法:彻底移除
  108. [yyCache removeObjectForKey:NYCocheNameKey withBlock:^(NSString * _Nonnull key) {
  109. NSLog(@"Removed cache for key: %@", key);
  110. }];
  111. }
  112. //删除-题目
  113. +(void)delateUserKey:(NSString*)userKey {
  114. NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
  115. YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
  116. [yyCache removeAllObjects];
  117. }
  118. //保存课堂ID
  119. +(void)saveClassIdStr:(NSString *)classIdStr userKey:(NSString*)userKey{
  120. NSString *NYClassRoomID = [NSString stringWithFormat:@"NYClassRoomID_%@",userKey];
  121. YYCache *yyCache = [YYCache cacheWithName:@"NYClassRoomID"];
  122. //根据key写入缓存value
  123. [yyCache setObject:classIdStr forKey:NYClassRoomID withBlock:^{
  124. NSLog(@"setObject sucess");
  125. }];
  126. }
  127. //读取课堂ID
  128. +(NSString*)readClassIdStr:(NSString*)userKey{
  129. NSString *NYClassRoomID = [NSString stringWithFormat:@"NYClassRoomID_%@",userKey];
  130. YYCache *yyCache = [YYCache cacheWithName:@"NYClassRoomID"];
  131. id object = [yyCache objectForKey:NYClassRoomID];
  132. if ([object isKindOfClass:[NSString class]]) {
  133. return (NSString *)object;
  134. } else {
  135. // 返回一个空数组或者其他默认值,视情况而定
  136. return nil;
  137. }
  138. }
  139. //删除-课堂ID
  140. +(void)delateClassIdStr:(NSString*)userKey {
  141. NSString *NYClassRoomID = [NSString stringWithFormat:@"NYClassRoomID_%@",userKey];
  142. YYCache *yyCache = [YYCache cacheWithName:@"NYClassRoomID"];
  143. [yyCache removeObjectForKey:NYClassRoomID withBlock:^(NSString * _Nonnull key) {
  144. NSLog(@"removeObjectForKey ❤️ sucess");
  145. }];
  146. }
  147. // AES加密方法
  148. + (NSString *)encryptAES:(NSString *)plainText{
  149. NSString *cipherText = aesEncryptString(plainText, nykey);
  150. return cipherText;
  151. }
  152. // AES解密方法
  153. + (NSString *)decryptAES:(id)cipherText{
  154. NSString *decryptedText = aesDecryptString(cipherText, nykey);
  155. return decryptedText;
  156. }
  157. + (BOOL)hasUserWatchedAdToday:(NSString*)keynum {
  158. NSString *kUserLastWatchAdDateKey = [NSString stringWithFormat:@"UserLastWatchAdDateKey%@",keynum];
  159. NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:kUserLastWatchAdDateKey];
  160. // 如果没有记录,返回 NO
  161. if (!lastDate) return NO;
  162. // // 判断是否在今天
  163. // NSCalendar *calendar = [NSCalendar currentCalendar];
  164. // if (![calendar isDateInToday:lastDate]) return NO;
  165. // 判断是否超过 6 小时
  166. NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:lastDate];
  167. NSTimeInterval maxAdTimeInterval = RQ_COMMON_MANAGER.JSJP_APP_MAdTime * 3600; // 6 小时(单位:秒)
  168. return timeInterval <= maxAdTimeInterval;
  169. }
  170. + (void)saveUWatchedAdToday:(NSString*)keynum {
  171. NSString *kUserLastWatchAdDateKey = [NSString stringWithFormat:@"UserLastWatchAdDateKey%@",keynum];
  172. // 记录广告观看时间(建议在广告播放成功的回调中执行)
  173. [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kUserLastWatchAdDateKey];
  174. }
  175. @end