F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"ezops/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// LoginLogHandler 登录日志处理器 // Logan.Li
|
|
type LoginLogHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewLoginLogHandler 创建登录日志处理器
|
|
func NewLoginLogHandler(db *gorm.DB) *LoginLogHandler {
|
|
return &LoginLogHandler{db: db}
|
|
}
|
|
|
|
// ListLoginLogs 登录日志列表
|
|
// GET /api/v1/login-logs?username=&status=&page=&page_size=
|
|
func (h *LoginLogHandler) ListLoginLogs(c *gin.Context) {
|
|
var req model.LoginLogQuery
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
if req.Page <= 0 {
|
|
req.Page = 1
|
|
}
|
|
if req.PageSize <= 0 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
query := h.db.Model(&model.LoginLog{})
|
|
if req.Username != "" {
|
|
query = query.Where("username LIKE ?", "%"+req.Username+"%")
|
|
}
|
|
if req.Status != "" {
|
|
query = query.Where("status = ?", req.Status)
|
|
}
|
|
if req.StartTime != "" {
|
|
query = query.Where("created_at >= ?", req.StartTime)
|
|
}
|
|
if req.EndTime != "" {
|
|
query = query.Where("created_at <= ?", req.EndTime)
|
|
}
|
|
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "查询失败"})
|
|
return
|
|
}
|
|
|
|
var logs []model.LoginLog
|
|
if err := query.Order("id DESC").
|
|
Offset((req.Page - 1) * req.PageSize).
|
|
Limit(req.PageSize).
|
|
Find(&logs).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "查询失败"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{
|
|
List: logs,
|
|
Total: total,
|
|
}})
|
|
}
|