用户签到 :
逻辑实现 :



代码实现 :
@Override
public Result sign() {
Long userId = UserHolder.getUser().getId();
LocalDateTime now = LocalDateTime.now();
String keySuffix = now.format(DateTimeFormatter.ofPattern("yyyy:MM"));
String key = "sign:" + userId + keySuffix;
int dayOfMonth = now.getDayOfMonth();
stringRedisTemplate.opsForValue().setBit(key,dayOfMonth - 1,true);
return Result.ok();
}
签到统计 :
统计连续签到天数 :
@Override
public Result signCount() {
Long userId = UserHolder.getUser().getId();
LocalDateTime now = LocalDateTime.now();
String keySuffix = now.format(DateTimeFormatter.ofPattern("yyyy:MM"));
String key = "sign:" + userId + keySuffix;
int dayOfMonth = now.getDayOfMonth();
List<Long> list = stringRedisTemplate.opsForValue().bitField(
key,
BitFieldSubCommands.create()
.get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth)).valueAt(0)
);
if (list == null || list.isEmpty()) {
return Result.ok(0);
}
Long num = list.get(0);
if (num == null || num == 0){
return Result.ok(0);
}
int count = 0;
while (true) {
if ((num & 1) == 0) {
break;
}else{
count++;
}
num >>>= 1;
}
return Result.ok(count);
}