From e96621f774eaff5845bb6af03d6abd8a91d39bd1 Mon Sep 17 00:00:00 2001 From: haomingming Date: Wed, 24 Dec 2025 17:49:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=80=E5=A7=8B=E6=97=B6?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controller/Video.go | 7 +++++++ api/service/SystemTime.go | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/api/controller/Video.go b/api/controller/Video.go index 28fe3c6..d344664 100644 --- a/api/controller/Video.go +++ b/api/controller/Video.go @@ -295,6 +295,13 @@ func (r *Video) AddVideoVote(c *gin.Context) { // 检测投票有效期 systemTimeService := service.SystemTimeService{} + isStartValid := systemTimeService.CheckVoteValidStartStatus() + if isStartValid == false { + responses.FailWithMessage("投票未开始", c) + return + } + + // 检测投票有效期 isValid := systemTimeService.CheckVoteValidStatus() if isValid == false { responses.FailWithMessage("投票已结束", c) diff --git a/api/service/SystemTime.go b/api/service/SystemTime.go index ded55b4..0ec4e82 100644 --- a/api/service/SystemTime.go +++ b/api/service/SystemTime.go @@ -51,3 +51,45 @@ func (r *SystemTimeService) CheckVoteValidStatus() bool { return true } + +// CheckVoteValidStatus 检测投票有效期 +// bool true:未结束 false:已结束 +func (r *SystemTimeService) CheckVoteValidStartStatus() bool { + redisKey := "VoteSystemTime" + res, _ := global.Redis.Get(context.Background(), redisKey).Result() + if res == "" { + // 获取配置-时间 + systemTimeDao := dao.SystemTimeDao{} + systemTime, err := systemTimeDao.GetSystemTimeById(1) + if err != nil { + return false + } + + if systemTime.StartTime == nil { + return false + } + + // 结束时间 + startTime := time.Time(*systemTime.StartTime) + + // 当前时间 + now := time.Now() + + duration := startTime.Sub(now) + if duration > 0 { + return false + } + + if duration > 5*time.Minute { + duration = 5 * time.Minute + } + + // 添加缓存 + _, err = global.Redis.Set(context.Background(), redisKey, "1", duration).Result() + if err != nil { + return false + } + } + + return true +}