parent
e0d0674f81
commit
45e9fd66e7
@ -25,6 +25,7 @@ var OptionMap map[string]string
|
|||||||
var OptionMapRWMutex sync.RWMutex
|
var OptionMapRWMutex sync.RWMutex
|
||||||
|
|
||||||
var ItemsPerPage = 10
|
var ItemsPerPage = 10
|
||||||
|
var MaxRecentItems = 100
|
||||||
|
|
||||||
var PasswordLoginEnabled = true
|
var PasswordLoginEnabled = true
|
||||||
var PasswordRegisterEnabled = true
|
var PasswordRegisterEnabled = true
|
||||||
|
86
controller/log.go
Normal file
86
controller/log.go
Normal file
@ -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,
|
||||||
|
})
|
||||||
|
}
|
44
model/log.go
Normal file
44
model/log.go
Normal file
@ -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
|
||||||
|
}
|
@ -93,5 +93,10 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
redemptionRoute.PUT("/", controller.UpdateRedemption)
|
redemptionRoute.PUT("/", controller.UpdateRedemption)
|
||||||
redemptionRoute.DELETE("/:id", controller.DeleteRedemption)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user