初始化
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PublicService struct {
|
||||
}
|
||||
|
||||
// GetUserIP 获取用户ip
|
||||
func (r *PublicService) GetUserIP(h *http.Request) string {
|
||||
forwarded := h.Header.Get("X-FORWARDED-FOR")
|
||||
if forwarded != "" {
|
||||
return forwarded
|
||||
}
|
||||
return h.RemoteAddr
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"vote-admin-api/api/dao"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type SystemTimeService struct {
|
||||
}
|
||||
|
||||
// CheckVoteValidStatus 检测投票有效期
|
||||
// bool true:未结束 false:已结束
|
||||
func (r *SystemTimeService) CheckVoteValidStatus() 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.EndTime == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 结束时间
|
||||
endTime := time.Time(*systemTime.EndTime)
|
||||
|
||||
// 当前时间
|
||||
now := time.Now()
|
||||
|
||||
duration := endTime.Sub(now)
|
||||
if duration < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
// 添加缓存
|
||||
_, err = global.Redis.Set(context.Background(), redisKey, "1", duration).Result()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
"vote-admin-api/config"
|
||||
"vote-admin-api/global"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
}
|
||||
|
||||
// AddUserVoteDayCache 新增用户每日投票记录缓存
|
||||
// t 1:文章 2:视频
|
||||
func (r *UserService) AddUserVoteDayCache(userId, id int64, t int) bool {
|
||||
now := time.Now()
|
||||
|
||||
redisKey := "UserVoteDay" + fmt.Sprintf("%d", userId) + fmt.Sprintf("%d", id) + now.Format("2006-01-02") + fmt.Sprintf("%d", t)
|
||||
|
||||
// 缓存过期时间
|
||||
year, month, day := now.Date()
|
||||
location := now.Location()
|
||||
validTime := time.Date(year, month, day, 23, 59, 59, 0, location)
|
||||
duration := validTime.Sub(now)
|
||||
if duration < 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
if config.C.Env == "dev" {
|
||||
duration = 60 * 5 * time.Second
|
||||
}
|
||||
|
||||
// 添加缓存
|
||||
_, err := global.Redis.Set(context.Background(), redisKey, "1", duration).Result()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// CheckUserVoteDay 检测用户今日是否投票
|
||||
// t 1:文章 2:视频
|
||||
func (r *UserService) CheckUserVoteDay(userId, id int64, t int) bool {
|
||||
if t != 1 && t != 2 {
|
||||
return false
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
redisKey := "UserVoteDay" + fmt.Sprintf("%d", userId) + fmt.Sprintf("%d", id) + now.Format("2006-01-02") + fmt.Sprintf("%d", t)
|
||||
res, _ := global.Redis.Get(context.Background(), redisKey).Result()
|
||||
if res == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if res == "1" {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
//if res == "" {
|
||||
// // 是否已投票(1:已投票 0:未投票)
|
||||
// isVote := "0"
|
||||
// if t == 1 {
|
||||
// // 文章
|
||||
// maps := make(map[string]interface{})
|
||||
// maps["article_id"] = id
|
||||
// maps["user_id"] = userId
|
||||
// maps["voted_at"] = now.Format("2006-01-02")
|
||||
//
|
||||
// articleVoteDayDao := dao.ArticleVoteDayDao{}
|
||||
// articleVoteDay, _ := articleVoteDayDao.GetArticleVoteDayById(id)
|
||||
// if articleVoteDay != nil {
|
||||
// isVote = "1"
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if t == 2 {
|
||||
// // 视频
|
||||
// maps := make(map[string]interface{})
|
||||
// maps["article_id"] = id
|
||||
// maps["user_id"] = userId
|
||||
// maps["voted_at"] = now.Format("2006-01-02")
|
||||
//
|
||||
// videoVoteDayDao := dao.VideoVoteDayDao{}
|
||||
// videoVoteDay, _ := videoVoteDayDao.GetVideoVoteDayById(id)
|
||||
// if videoVoteDay != nil {
|
||||
// isVote = "1"
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 添加缓存
|
||||
// if isVote == "1" {
|
||||
// result := r.AddUserVoteDayCache(userId, id, t)
|
||||
// if result == false {
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// res = isVote
|
||||
//}
|
||||
//
|
||||
//if res == "0" {
|
||||
// return false
|
||||
//} else {
|
||||
// return true
|
||||
//}
|
||||
}
|
||||
Reference in New Issue
Block a user