Files
nl-blogs/server/handlers/log.go
2026-01-15 13:51:44 +08:00

82 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handlers
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/niangaodev/art-code/repositories"
)
func AdminGetRecentActivities(c *gin.Context) {
// 获取最近10条操作日志
logs, _, err := repositories.GetOperationLogs(1, 10)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get recent activities"})
return
}
// 构建响应
var activities []gin.H
for _, log := range logs {
// 根据HTTP方法设置图标
var icon string
switch log.Method {
case "POST":
icon = ""
case "PUT", "PATCH":
icon = "✏️"
case "DELETE":
icon = "🗑️"
case "GET":
icon = "📋"
case "OPTIONS":
icon = "⚙️"
default:
icon = "📋"
}
// 构建活动文本描述
text := fmt.Sprintf("%s %s", log.Method, log.Path)
activities = append(activities, gin.H{
"id": log.ID,
"icon": icon,
"text": text,
"time": log.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
c.JSON(http.StatusOK, activities)
}
// 获取操作日志列表
func AdminGetOperationLogs(c *gin.Context) {
// 获取分页参数
page := 1
pageSize := 10
// 从查询参数中获取分页信息
if c.Query("page") != "" {
c.ShouldBindQuery(&page)
}
if c.Query("pageSize") != "" {
c.ShouldBindQuery(&pageSize)
}
// 获取操作日志
logs, total, err := repositories.GetOperationLogs(page, pageSize)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get operation logs"})
return
}
c.JSON(http.StatusOK, gin.H{
"list": repositories.BuildOperationLogsResponse(logs),
"total": total,
"page": page,
"size": pageSize,
})
}