优化
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -18,10 +19,11 @@ type Audit struct {
|
||||
service *service.AuditService
|
||||
recorder *service.AuditRecorder
|
||||
logs *service.LogViewerService
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewAudit(service *service.AuditService, recorder *service.AuditRecorder, logs *service.LogViewerService) *Audit {
|
||||
return &Audit{service: service, recorder: recorder, logs: logs}
|
||||
func NewAudit(service *service.AuditService, recorder *service.AuditRecorder, logs *service.LogViewerService, logger *slog.Logger) *Audit {
|
||||
return &Audit{service: service, recorder: recorder, logs: logs, logger: logger}
|
||||
}
|
||||
|
||||
func page(c *gin.Context) (int, int) {
|
||||
@@ -53,8 +55,8 @@ func IDsFromQuery(c *gin.Context) []uint {
|
||||
return ids
|
||||
}
|
||||
|
||||
// auditID accepts both forms used by the GVA web client over time: DELETE
|
||||
// requests may carry the identifier in the query string or in a JSON body.
|
||||
// auditID accepts both DELETE encodings supported by the administration API:
|
||||
// the identifier may be carried in the query string or in a JSON body.
|
||||
func auditID(c *gin.Context) (uint, error) {
|
||||
if raw := c.Query("ID"); raw != "" {
|
||||
value, err := strconv.ParseUint(raw, 10, 64)
|
||||
@@ -68,7 +70,7 @@ func auditID(c *gin.Context) (uint, error) {
|
||||
}
|
||||
|
||||
// auditIDs mirrors auditID for bulk deletes and supports both query-array and
|
||||
// JSON-body encodings used by compatible GVA pages.
|
||||
// JSON-body encodings used by compatible administration pages.
|
||||
func auditIDs(c *gin.Context) ([]uint, error) {
|
||||
if ids := IDsFromQuery(c); len(ids) > 0 {
|
||||
return ids, nil
|
||||
@@ -230,7 +232,7 @@ func (h *Audit) LogDates(c *gin.Context) {
|
||||
}
|
||||
data, err := h.logs.LogDates(c.Request.Context(), month)
|
||||
if err != nil {
|
||||
failLogViewer(c, err)
|
||||
failLogViewer(c, err, h.logger)
|
||||
return
|
||||
}
|
||||
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
|
||||
@@ -243,7 +245,7 @@ func (h *Audit) LogFiles(c *gin.Context) {
|
||||
}
|
||||
data, err := h.logs.LogFiles(c.Request.Context(), date)
|
||||
if err != nil {
|
||||
failLogViewer(c, err)
|
||||
failLogViewer(c, err, h.logger)
|
||||
return
|
||||
}
|
||||
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
|
||||
@@ -267,13 +269,16 @@ func (h *Audit) LogContent(c *gin.Context) {
|
||||
// "日志文件路径不合法" error rather than rejecting them as binding errors.
|
||||
data, err := h.logs.LogContent(c.Request.Context(), date, path, cursor)
|
||||
if err != nil {
|
||||
failLogViewer(c, err)
|
||||
failLogViewer(c, err, h.logger)
|
||||
return
|
||||
}
|
||||
httpx.Write(c, httpx.CodeSuccess, data, "获取成功")
|
||||
}
|
||||
|
||||
func failLogViewer(c *gin.Context, err error) {
|
||||
func failLogViewer(c *gin.Context, err error, logger *slog.Logger) {
|
||||
if logger != nil {
|
||||
logger.ErrorContext(c.Request.Context(), "日志查看失败", "mod", "log-viewer", "error", err)
|
||||
}
|
||||
message := "读取日志失败"
|
||||
switch {
|
||||
case errors.Is(err, biz.ErrInvalidLogMonth):
|
||||
|
||||
@@ -149,6 +149,10 @@ func (h *Dictionary) FindDetail(c *gin.Context) {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.ID == 0 {
|
||||
httpx.Fail(c, "ID值不能为空")
|
||||
return
|
||||
}
|
||||
item, err := h.service.DictionaryDetail(c.Request.Context(), req.ID)
|
||||
if err != nil {
|
||||
httpx.Fail(c, "查询失败")
|
||||
|
||||
Reference in New Issue
Block a user