fix: operation logs page display and API pagination
- Fix frontend Logs.vue to use correct field names (entity_type, entity_name, old_value, new_value)
- Add user and source_ip columns to logs table
- Add details column showing old_value -> new_value transitions
- Fix backend logs API to support pagination (page, page_size, search)
- Add search support across all log fields (action, entity_type, entity_name, username, values)
- Return proper {list, total, page, page_size} response format
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"lan-manager/server/db"
|
||||
@@ -18,17 +19,55 @@ func NewLogHandler() *LogHandler {
|
||||
func (h *LogHandler) List(c *gin.Context) {
|
||||
action := c.Query("action")
|
||||
date := c.Query("date")
|
||||
search := c.Query("search")
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 || pageSize > 100 {
|
||||
pageSize = 20
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
// Build count query
|
||||
countQuery := `SELECT COUNT(*) FROM operation_logs WHERE 1=1`
|
||||
countArgs := []interface{}{}
|
||||
|
||||
// Build data query
|
||||
query := `SELECT id, action, entity_type, entity_id, entity_name, old_value, new_value, source_ip, username, created_at FROM operation_logs WHERE 1=1`
|
||||
args := []interface{}{}
|
||||
|
||||
if action != "" {
|
||||
countQuery += ` AND action = ?`
|
||||
query += ` AND action = ?`
|
||||
countArgs = append(countArgs, action)
|
||||
args = append(args, action)
|
||||
}
|
||||
if date != "" {
|
||||
countQuery += ` AND DATE(created_at) = ?`
|
||||
query += ` AND DATE(created_at) = ?`
|
||||
countArgs = append(countArgs, date)
|
||||
args = append(args, date)
|
||||
}
|
||||
query += ` ORDER BY created_at DESC LIMIT 1000`
|
||||
if search != "" {
|
||||
searchPattern := "%" + search + "%"
|
||||
countQuery += ` AND (action LIKE ? OR entity_type LIKE ? OR entity_name LIKE ? OR username LIKE ? OR old_value LIKE ? OR new_value LIKE ?)`
|
||||
query += ` AND (action LIKE ? OR entity_type LIKE ? OR entity_name LIKE ? OR username LIKE ? OR old_value LIKE ? OR new_value LIKE ?)`
|
||||
countArgs = append(countArgs, searchPattern, searchPattern, searchPattern, searchPattern, searchPattern, searchPattern)
|
||||
args = append(args, searchPattern, searchPattern, searchPattern, searchPattern, searchPattern, searchPattern)
|
||||
}
|
||||
|
||||
// Get total count
|
||||
var total int
|
||||
if err := db.DB.QueryRow(countQuery, countArgs...).Scan(&total); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Get data with pagination
|
||||
query += ` ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
||||
args = append(args, pageSize, offset)
|
||||
|
||||
rows, err := db.DB.Query(query, args...)
|
||||
if err != nil {
|
||||
@@ -36,6 +75,7 @@ func (h *LogHandler) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
logs := []models.OperationLog{}
|
||||
for rows.Next() {
|
||||
var l models.OperationLog
|
||||
@@ -47,5 +87,11 @@ func (h *LogHandler) List(c *gin.Context) {
|
||||
logs = append(logs, l)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, logs)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"list": logs,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -29,18 +29,26 @@
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="target_type" label="对象" width="100">
|
||||
<el-table-column prop="entity_type" label="对象" width="100">
|
||||
<template #default="{ row }">
|
||||
<el-tag size="small" effect="plain" round>{{ row.target_type }}</el-tag>
|
||||
<el-tag size="small" effect="plain" round>{{ row.entity_type }}</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="target_name" label="对象名称" min-width="160">
|
||||
<el-table-column prop="entity_name" label="对象名称" min-width="160">
|
||||
<template #default="{ row }">
|
||||
<el-icon :size="12" color="var(--accent)"><Monitor /></el-icon>
|
||||
{{ row.target_name }}
|
||||
{{ row.entity_name }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="details" label="详情" min-width="240" show-overflow-tooltip />
|
||||
<el-table-column label="详情" min-width="240" show-overflow-tooltip>
|
||||
<template #default="{ row }">
|
||||
<span v-if="row.old_value">{{ row.old_value }} → {{ row.new_value }}</span>
|
||||
<span v-else-if="row.new_value">{{ row.new_value }}</span>
|
||||
<span v-else>-</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column prop="username" label="用户" width="100" />
|
||||
<el-table-column prop="source_ip" label="来源IP" width="130" />
|
||||
<el-table-column prop="created_at" label="时间" width="170">
|
||||
<template #default="{ row }">
|
||||
<el-icon :size="10"><Clock /></el-icon>
|
||||
|
||||
Reference in New Issue
Block a user