完成了“后台审计日志”
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package adminaudit
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
type Query struct {
|
||||
ActorID uint64
|
||||
Action string
|
||||
BizType string
|
||||
Limit int
|
||||
}
|
||||
|
||||
type LogDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
ActorType string `json:"actor_type"`
|
||||
ActorID uint64 `json:"actor_id"`
|
||||
ActorUsername string `json:"actor_username"`
|
||||
ActorNickname string `json:"actor_nickname"`
|
||||
Action string `json:"action"`
|
||||
BizType string `json:"biz_type"`
|
||||
BizID *uint64 `json:"biz_id"`
|
||||
IP string `json:"ip"`
|
||||
UserAgent string `json:"user_agent"`
|
||||
Detail datatypes.JSON `json:"detail"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package adminaudit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
query, ok := parseQuery(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
items, err := h.service.List(query)
|
||||
if err != nil {
|
||||
writeAuditError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{"items": items})
|
||||
}
|
||||
|
||||
func parseQuery(c *gin.Context) (Query, bool) {
|
||||
var query Query
|
||||
if raw := c.Query("actor_id"); raw != "" {
|
||||
value, err := strconv.ParseUint(raw, 10, 64)
|
||||
if err != nil || value == 0 {
|
||||
response.BadRequest(c, "管理员 ID 不正确")
|
||||
return query, false
|
||||
}
|
||||
query.ActorID = value
|
||||
}
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
value, err := strconv.Atoi(raw)
|
||||
if err != nil || value <= 0 {
|
||||
response.BadRequest(c, "查询条数不正确")
|
||||
return query, false
|
||||
}
|
||||
query.Limit = value
|
||||
}
|
||||
query.Action = c.Query("action")
|
||||
query.BizType = c.Query("biz_type")
|
||||
return query, true
|
||||
}
|
||||
|
||||
func writeAuditError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, ErrDependencyUnavailable):
|
||||
response.ServiceUnavailable(c, "数据库未连接")
|
||||
default:
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "审计日志服务暂时不可用")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package adminaudit
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) List(query Query) ([]LogDTO, error) {
|
||||
limit := query.Limit
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
}
|
||||
|
||||
db := r.db.Table("audit_logs AS al").
|
||||
Select(`al.id, al.actor_type, al.actor_id, COALESCE(au.username, '') AS actor_username,
|
||||
COALESCE(au.nickname, '') AS actor_nickname, al.action, al.biz_type, al.biz_id,
|
||||
al.ip, al.user_agent, al.detail, al.created_at`).
|
||||
Joins("LEFT JOIN admin_users AS au ON au.id = al.actor_id AND al.actor_type = ?", "admin")
|
||||
|
||||
if query.ActorID > 0 {
|
||||
db = db.Where("al.actor_id = ?", query.ActorID)
|
||||
}
|
||||
if query.Action != "" {
|
||||
db = db.Where("al.action = ?", query.Action)
|
||||
}
|
||||
if query.BizType != "" {
|
||||
db = db.Where("al.biz_type = ?", query.BizType)
|
||||
}
|
||||
|
||||
var rows []auditLogRow
|
||||
if err := db.Order("al.id DESC").Limit(limit).Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]LogDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
items = append(items, row.toDTO())
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
type auditLogRow struct {
|
||||
ID uint64
|
||||
ActorType string
|
||||
ActorID uint64
|
||||
ActorUsername string
|
||||
ActorNickname string
|
||||
Action string
|
||||
BizType string
|
||||
BizID *uint64
|
||||
IP string
|
||||
UserAgent string
|
||||
Detail datatypes.JSON
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (row auditLogRow) toDTO() LogDTO {
|
||||
return LogDTO{
|
||||
ID: row.ID,
|
||||
ActorType: row.ActorType,
|
||||
ActorID: row.ActorID,
|
||||
ActorUsername: row.ActorUsername,
|
||||
ActorNickname: row.ActorNickname,
|
||||
Action: row.Action,
|
||||
BizType: row.BizType,
|
||||
BizID: row.BizID,
|
||||
IP: row.IP,
|
||||
UserAgent: row.UserAgent,
|
||||
Detail: row.Detail,
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package adminaudit
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
|
||||
type Service struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(query Query) ([]LogDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.List(query)
|
||||
}
|
||||
Reference in New Issue
Block a user