接口访问次数限制
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package com.sqx.common.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.Objects;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
@@ -117,7 +113,7 @@ public class ApiAccessLimitUtil {
|
||||
redisUtils.set(redisKey, 1, expireAt);
|
||||
return true;
|
||||
}
|
||||
if ((int) countObj < count) {
|
||||
if (Integer.parseInt(countObj.toString()) < count) {
|
||||
// 访问次数未达上限,次数加1
|
||||
redisUtils.incr(redisKey);
|
||||
return true;
|
||||
@@ -131,17 +127,19 @@ public class ApiAccessLimitUtil {
|
||||
}
|
||||
|
||||
private static long calculateExpireAt(String timePeriod) {
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate expireDate = null;
|
||||
Date now = DateUtil.beginOfDay(DateUtil.date());
|
||||
Date expireDate = null;
|
||||
if ("day".equals(timePeriod)) {
|
||||
expireDate = now.plusDays(1).atStartOfDay().toLocalDate();
|
||||
expireDate = DateUtil.endOfDay(now);
|
||||
} else if ("week".equals(timePeriod)) {
|
||||
expireDate = now.plusWeeks(0).with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));
|
||||
expireDate = DateUtil.endOfWeek(now);
|
||||
} else if ("month".equals(timePeriod)) {
|
||||
expireDate = now.plusMonths(1).withDayOfMonth(1).minusDays(1);
|
||||
expireDate = DateUtil.endOfMonth(now);
|
||||
} else if ("year".equals(timePeriod)) {
|
||||
expireDate = now.plusYears(1).withDayOfYear(1).minusDays(1);
|
||||
expireDate = DateUtil.endOfYear(now);
|
||||
}
|
||||
return Objects.requireNonNull(expireDate).atTime(23, 59, 59).toEpochSecond(ZoneOffset.UTC);
|
||||
long endTimeStamp = DateUtil.endOfDay(expireDate).getTime() / 1000L;
|
||||
long currentTimeStamp = DateUtil.currentSeconds();
|
||||
return endTimeStamp - currentTimeStamp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user