From e96621f774eaff5845bb6af03d6abd8a91d39bd1 Mon Sep 17 00:00:00 2001 From: haomingming Date: Wed, 24 Dec 2025 17:49:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E6=97=B6=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 +} From 8042b10afeae29ba0bb237f1ca469bb4945e968b Mon Sep 17 00:00:00 2001 From: haomingming Date: Wed, 24 Dec 2025 17:58:08 +0800 Subject: [PATCH 2/2] 12 --- api/service/SystemTime.go | 60 +++++++++++++++------------------------ 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/api/service/SystemTime.go b/api/service/SystemTime.go index 0ec4e82..95e04ed 100644 --- a/api/service/SystemTime.go +++ b/api/service/SystemTime.go @@ -52,44 +52,30 @@ func (r *SystemTimeService) CheckVoteValidStatus() bool { return true } -// CheckVoteValidStatus 检测投票有效期 -// bool true:未结束 false:已结束 +// CheckVoteValidStartStatus 检测投票有效期 +// 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 - } + // 获取配置-时间 + systemTimeDao := dao.SystemTimeDao{} + systemTime, err := systemTimeDao.GetSystemTimeById(1) + if err != nil { + return false } - return true + if systemTime.StartTime == nil { + return false + } + + // 开始时间 + startTime := time.Time(*systemTime.StartTime) + + // 当前时间 + now := time.Now() + + // 如果当前时间在开始时间之后(或等于),说明已开始 + if !now.Before(startTime) { + return true + } + + return false }