diff --git a/common/constants.go b/common/constants.go index 7c1ff298..3a8f6d77 100644 --- a/common/constants.go +++ b/common/constants.go @@ -25,6 +25,7 @@ var OptionMap map[string]string var OptionMapRWMutex sync.RWMutex var ItemsPerPage = 10 +var MaxRecentItems = 100 var PasswordLoginEnabled = true var PasswordRegisterEnabled = true diff --git a/controller/log.go b/controller/log.go new file mode 100644 index 00000000..88d2ed1f --- /dev/null +++ b/controller/log.go @@ -0,0 +1,86 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "one-api/common" + "one-api/model" + "strconv" +) + +func GetAllLogs(c *gin.Context) { + p, _ := strconv.Atoi(c.Query("p")) + if p < 0 { + p = 0 + } + logType, _ := strconv.Atoi(c.Query("type")) + logs, err := model.GetAllLogs(logType, p*common.ItemsPerPage, common.ItemsPerPage) + if err != nil { + c.JSON(200, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + c.JSON(200, gin.H{ + "success": true, + "message": "", + "data": logs, + }) +} + +func GetUserLogs(c *gin.Context) { + p, _ := strconv.Atoi(c.Query("p")) + if p < 0 { + p = 0 + } + userId := c.GetInt("id") + logType, _ := strconv.Atoi(c.Query("type")) + logs, err := model.GetUserLogs(userId, logType, p*common.ItemsPerPage, common.ItemsPerPage) + if err != nil { + c.JSON(200, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + c.JSON(200, gin.H{ + "success": true, + "message": "", + "data": logs, + }) +} + +func SearchAllLogs(c *gin.Context) { + keyword := c.Query("keyword") + logs, err := model.SearchAllLogs(keyword) + if err != nil { + c.JSON(200, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + c.JSON(200, gin.H{ + "success": true, + "message": "", + "data": logs, + }) +} + +func SearchUserLogs(c *gin.Context) { + keyword := c.Query("keyword") + userId := c.GetInt("id") + logs, err := model.SearchUserLogs(userId, keyword) + if err != nil { + c.JSON(200, gin.H{ + "success": false, + "message": err.Error(), + }) + return + } + c.JSON(200, gin.H{ + "success": true, + "message": "", + "data": logs, + }) +} diff --git a/model/log.go b/model/log.go new file mode 100644 index 00000000..179955b7 --- /dev/null +++ b/model/log.go @@ -0,0 +1,44 @@ +package model + +import "one-api/common" + +type Log struct { + Id int `json:"id"` + UserId int `json:"user_id" gorm:"index"` + CreatedAt int64 `json:"created_at" gorm:"bigint"` + Type int `json:"type" gorm:"index"` + Content string `json:"content"` +} + +func RecordLog(userId int, logType int, content string) { + log := &Log{ + UserId: userId, + CreatedAt: common.GetTimestamp(), + Type: logType, + Content: content, + } + err := DB.Create(log).Error + if err != nil { + common.SysError("failed to record log: " + err.Error()) + } +} + +func GetAllLogs(logType int, startIdx int, num int) (logs []*Log, err error) { + err = DB.Where("type = ?", logType).Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error + return logs, err +} + +func GetUserLogs(userId int, logType int, startIdx int, num int) (logs []*Log, err error) { + err = DB.Where("user_id = ? and type = ?", userId, logType).Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error + return logs, err +} + +func SearchAllLogs(keyword string) (logs []*Log, err error) { + err = DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error + return logs, err +} + +func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) { + err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error + return logs, err +} diff --git a/router/api-router.go b/router/api-router.go index abd4d23b..de8249b1 100644 --- a/router/api-router.go +++ b/router/api-router.go @@ -93,5 +93,10 @@ func SetApiRouter(router *gin.Engine) { redemptionRoute.PUT("/", controller.UpdateRedemption) redemptionRoute.DELETE("/:id", controller.DeleteRedemption) } + logRoute := apiRouter.Group("/log") + logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs) + logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs) + logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs) + logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs) } }