123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- //
- // NYTools.m
- // jiaPei
- //
- // Created by Ning.ge on 2024/3/22.
- // Copyright © 2024 JCZ. All rights reserved.
- //
- #import "NYTools.h"
- #import "AESCipher.h"
- #define nykey @"YojZ7wyaYkgSqf1O"
- #define nyIv @"KXh9DfxR0UKCEkH2"
- @implementation NYTools
- /**
- *记录-题目-对错
- *userKey 用户id
- *carType 车辆类型
- *subject 科目几
- *exerciseType 题目类型
- */
- +(void)saveExerciseModel:(RQExerciseModel*)exercise userKey:(NSString*)userKey carType:(RQHomePageCarType)carType subject:(RQHomePageSubjectType)subject exerciseType:(NSUInteger)exerciseType{
- NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
- YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
- if(exerciseType==RQExerciseType_Wrong){
- exerciseType = 0;//错题-移除顺序的
- }
- NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd",carType,subject,exerciseType];
- //记录——用户做的题目-答题
- // 获取当前题目的ID
- NSNumber *exerciseId = @(exercise.ydtQuestionModel.ID);
- NSArray *userAnswer = exercise.userAnswer.copy;
- NSDictionary *exer_dic = @{
- @"ID":exerciseId,//题目id
- @"type":@(exercise.ydtQuestionModel.Type),//题目类型
- @"answerResultsType":@(exercise.answerResultsType),//是否做对
- @"userAnswer":userAnswer,//用户选项
- };
- NSArray *array = [self readExerciseListUserKey:userKey carType:carType subject:subject exerciseType:exerciseType];
- // 转为可变数组准备更新
- NSMutableArray *value = [NSMutableArray arrayWithArray:array];
- // 查找是否已有该题记录,存在则更新
- __block BOOL isUpdated = NO;
- [value enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
- NSLog(@"tools_id==%@",obj[@"ID"]);
- if ([obj[@"ID"] isEqualToNumber:exerciseId]) {
- value[idx] = exer_dic; // 替换已有记录
- isUpdated = YES;
- *stop = YES;
- }
- }];
- // 不存在则添加
- if (!isUpdated) {
- [value addObject:exer_dic];
- }
- //根据key写入缓存value
- [yyCache setObject:value forKey:NYCocheNameKey withBlock:^{
- NSLog(@"setObject sucess");
- }];
-
- }
- +(void)saveListExerciseModel:(NSMutableArray*)list userKey:(NSString*)userKey carType:(RQHomePageCarType)carType subject:(RQHomePageSubjectType)subject exerciseType:(NSUInteger)exerciseType{
- NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
- YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
- if(exerciseType==RQExerciseType_Wrong){
- exerciseType = 0;//错题-移除顺序的
- }
- NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd",carType,subject,exerciseType];
- //根据key写入缓存value
- [yyCache setObject:list forKey:NYCocheNameKey withBlock:^{
- NSLog(@"setObject sucess");
- }];
- }
- //读取-题目对错
- + (NSArray *)readExerciseListUserKey:(NSString *)userKey
- carType:(RQHomePageCarType)carType
- subject:(RQHomePageSubjectType)subject
- exerciseType:(NSUInteger)exerciseType {
-
- NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@", userKey];
- YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
- NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd", carType, subject, exerciseType];
-
- id obj = [yyCache objectForKey:NYCocheNameKey];
- if ([obj isKindOfClass:[NSArray class]]||[obj isKindOfClass:[NSMutableArray class]]) {
- return (NSArray *)obj;
- }
- return @[];
- }
- //+(NSArray*)readExerciseListUserKey:(NSString*)userKey carType:(RQHomePageCarType)carType subject:(RQHomePageSubjectType)subject exerciseType:(NSUInteger)exerciseType{
- // NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
- // YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
- // NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd",carType,subject,exerciseType];
- // id object = [yyCache objectForKey:NYCocheNameKey];
- // if ([object isKindOfClass:[NSMutableArray class]]) {
- // return (NSArray *)object;
- // } else {
- // // 返回一个空数组或者其他默认值,视情况而定
- // return @[];
- // }
- //}
- +(void)delExerciseListUserKey:(NSString*)userKey
- carType:(RQHomePageCarType)carType
- subject:(RQHomePageSubjectType)subject
- exerciseType:(NSUInteger)exerciseType {
-
- NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@", userKey];
- YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
-
- if (exerciseType == RQExerciseType_Wrong) {
- exerciseType = 0;
- }
- NSString *NYCocheNameKey = [NSString stringWithFormat:@"NYExerciseKey_%zd_%zd_%zd", carType, subject, exerciseType];
- // 推荐做法:彻底移除
- [yyCache removeObjectForKey:NYCocheNameKey withBlock:^(NSString * _Nonnull key) {
- NSLog(@"Removed cache for key: %@", key);
- }];
- }
- //删除-题目
- +(void)delateUserKey:(NSString*)userKey {
- NSString *NYCocheName = [NSString stringWithFormat:@"NYExercise_%@",userKey];
- YYCache *yyCache = [YYCache cacheWithName:NYCocheName];
- [yyCache removeAllObjects];
- }
- //保存课堂ID
- +(void)saveClassIdStr:(NSString *)classIdStr userKey:(NSString*)userKey{
- NSString *NYClassRoomID = [NSString stringWithFormat:@"NYClassRoomID_%@",userKey];
- YYCache *yyCache = [YYCache cacheWithName:@"NYClassRoomID"];
- //根据key写入缓存value
- [yyCache setObject:classIdStr forKey:NYClassRoomID withBlock:^{
- NSLog(@"setObject sucess");
- }];
- }
- //读取课堂ID
- +(NSString*)readClassIdStr:(NSString*)userKey{
- NSString *NYClassRoomID = [NSString stringWithFormat:@"NYClassRoomID_%@",userKey];
- YYCache *yyCache = [YYCache cacheWithName:@"NYClassRoomID"];
- id object = [yyCache objectForKey:NYClassRoomID];
- if ([object isKindOfClass:[NSString class]]) {
- return (NSString *)object;
- } else {
- // 返回一个空数组或者其他默认值,视情况而定
- return nil;
- }
- }
- //删除-课堂ID
- +(void)delateClassIdStr:(NSString*)userKey {
- NSString *NYClassRoomID = [NSString stringWithFormat:@"NYClassRoomID_%@",userKey];
- YYCache *yyCache = [YYCache cacheWithName:@"NYClassRoomID"];
- [yyCache removeObjectForKey:NYClassRoomID withBlock:^(NSString * _Nonnull key) {
- NSLog(@"removeObjectForKey ❤️ sucess");
- }];
- }
- // AES加密方法
- + (NSString *)encryptAES:(NSString *)plainText{
- NSString *cipherText = aesEncryptString(plainText, nykey);
- return cipherText;
- }
- // AES解密方法
- + (NSString *)decryptAES:(id)cipherText{
- NSString *decryptedText = aesDecryptString(cipherText, nykey);
- return decryptedText;
- }
- + (BOOL)hasUserWatchedAdToday:(NSString*)keynum {
- NSString *kUserLastWatchAdDateKey = [NSString stringWithFormat:@"UserLastWatchAdDateKey%@",keynum];
- NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:kUserLastWatchAdDateKey];
- // 如果没有记录,返回 NO
- if (!lastDate) return NO;
-
- // // 判断是否在今天
- // NSCalendar *calendar = [NSCalendar currentCalendar];
- // if (![calendar isDateInToday:lastDate]) return NO;
-
- // 判断是否超过 6 小时
- NSTimeInterval timeInterval = [[NSDate date] timeIntervalSinceDate:lastDate];
- NSTimeInterval maxAdTimeInterval = RQ_COMMON_MANAGER.JSJP_APP_MAdTime * 3600; // 6 小时(单位:秒)
-
- return timeInterval <= maxAdTimeInterval;
- }
- + (void)saveUWatchedAdToday:(NSString*)keynum {
- NSString *kUserLastWatchAdDateKey = [NSString stringWithFormat:@"UserLastWatchAdDateKey%@",keynum];
- // 记录广告观看时间(建议在广告播放成功的回调中执行)
- [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:kUserLastWatchAdDateKey];
- }
- @end
|