@ -8,9 +8,11 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -8,9 +8,11 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.cloud.kicc.common.core.exception.CheckedException ;
import com.cloud.kicc.common.data.entity.KiccUser ;
import com.cloud.kicc.common.security.util.SecurityUtils ;
import com.cloud.kicc.system.api.entity.ImContent ;
import com.cloud.kicc.system.api.entity.OssFile ;
import com.cloud.kicc.system.api.enums.SubscriberEnum ;
import com.cloud.kicc.system.config.OpenAiConfigProperties ;
import com.cloud.kicc.system.mapper.ImContentMapper ;
import com.cloud.kicc.system.service.FileService ;
@ -219,32 +221,23 @@ public class ImContentServiceImpl extends ServiceImpl<ImContentMapper, ImContent
@@ -219,32 +221,23 @@ public class ImContentServiceImpl extends ServiceImpl<ImContentMapper, ImContent
return receiveOssOssFile ;
}
/** 速率限制请求处理 */
private void RateLimitRequestHandler ( ) {
public void rateLimitRequestHandler ( ) {
ZoneId chinaZoneId = ZoneId . of ( "Asia/Shanghai" ) ;
LocalDateTime startTime = LocalDateTime . now ( ) . minusSeconds ( openAiConfigProperties . getRateLimit Duration ( ) ) ;
LocalDateTime startTime = LocalDateTime . now ( ) . minusSeconds ( openAiConfigProperties . getDuration ( ) ) ;
long startTimestamp = startTime . atZone ( chinaZoneId ) . toEpochSecond ( ) ;
// 查询当前用户在指定范围内的发送消息
long maxCount = super . count ( Wrappers . < ImContent > lambdaQuery ( )
. eq ( ImContent : : getSendUserId , SecurityUtils . getCasUser ( ) . getId ( ) )
. between ( ImContent : : getSendTime , startTimestamp , LocalDateTime . now ( ) . atZone ( chinaZoneId ) ) ) ;
. between ( ImContent : : getSendTime , startTimestamp , LocalDateTime . now ( ) . atZone ( chinaZoneId ) . toEpochSecond ( ) ) ) ;
// 检查是否大于发送次数
// NON("0", "非订阅"),
//
// BASIC("1", "基础订阅"),
// 限制当前非订阅用户只能用10次
// 限制当前基础订阅用户只能用10次
// 高级订阅不受限制
if ( StrUtil . equals ( SecurityUtils . < KiccUser > getUser ( ) . getSubscriber ( ) , SubscriberEnum . NON . getValue ( ) ) & & maxCount > openAiConfigProperties . getNonMaxCount ( ) ) {
throw new CheckedException ( "普通订阅用户已超出当前最大速率限制,请过一会在试试!" ) ;
}
if ( StrUtil . equals ( SecurityUtils . < KiccUser > getUser ( ) . getSubscriber ( ) , SubscriberEnum . BASIC . getValue ( ) ) & & maxCount > openAiConfigProperties . getBasicMaxCount ( ) ) {
throw new CheckedException ( "基础订阅用户已超出当前最大速率限制,请过一会在试试!" ) ;
}
}
}