90 lines
2.6 KiB
Go
90 lines
2.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// GetAllOperationLogs 管理员查询所有操作日志。
|
|
func GetAllOperationLogs(c *gin.Context) {
|
|
pageInfo := common.GetPageQuery(c)
|
|
action := c.Query("action")
|
|
targetType := c.Query("target_type")
|
|
targetId, _ := strconv.Atoi(c.Query("target_id"))
|
|
username := c.Query("username")
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
|
|
logs, total, err := model.GetOperationLogs(action, targetType, targetId, username, startTimestamp, endTimestamp, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
pageInfo.SetTotal(int(total))
|
|
pageInfo.SetItems(logs)
|
|
common.ApiSuccess(c, pageInfo)
|
|
}
|
|
|
|
// GetUserOperationLogs 用户查询自己的操作日志。
|
|
func GetUserOperationLogs(c *gin.Context) {
|
|
pageInfo := common.GetPageQuery(c)
|
|
userId := c.GetInt("id")
|
|
action := c.Query("action")
|
|
targetType := c.Query("target_type")
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
|
|
logs, total, err := model.GetUserOperationLogs(userId, action, targetType, startTimestamp, endTimestamp, pageInfo.GetStartIdx(), pageInfo.GetPageSize())
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
pageInfo.SetTotal(int(total))
|
|
pageInfo.SetItems(logs)
|
|
common.ApiSuccess(c, pageInfo)
|
|
}
|
|
|
|
// GetOperationLogStats 管理员查看操作日志统计。
|
|
func GetOperationLogStats(c *gin.Context) {
|
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
|
|
|
stats, err := model.GetOperationLogStats(startTimestamp, endTimestamp)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": stats,
|
|
})
|
|
}
|
|
|
|
// DeleteHistoryOperationLogs 清理历史操作日志。
|
|
func DeleteHistoryOperationLogs(c *gin.Context) {
|
|
targetTimestamp, _ := strconv.ParseInt(c.Query("target_timestamp"), 10, 64)
|
|
if targetTimestamp == 0 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": false,
|
|
"message": "target timestamp is required",
|
|
})
|
|
return
|
|
}
|
|
count, err := model.DeleteOldOperationLogs(c.Request.Context(), targetTimestamp, 100)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"success": true,
|
|
"message": "",
|
|
"data": count,
|
|
})
|
|
}
|