优化结构

This commit is contained in:
Yvan
2026-08-22 14:12:00 +08:00
parent b27a66e9d3
commit 9dd62c9fb6
37 changed files with 352 additions and 130 deletions
+18 -5
View File
@@ -232,15 +232,24 @@ func failLogViewer(c *gin.Context, err error, logger *slog.Logger) {
}
func (h *Audit) DeleteError(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
if err := h.service.DeleteErrors(c.Request.Context(), []uint{uint(id)}); err != nil {
id, err := positiveUintQuery(c, "ID")
if err != nil {
Fail(c, "错误记录ID非法")
return
}
if err := h.service.DeleteErrors(c.Request.Context(), []uint{id}); err != nil {
Fail(c, "删除失败:"+err.Error())
return
}
Write(c, CodeSuccess, gin.H{}, "删除成功")
}
func (h *Audit) DeleteErrors(c *gin.Context) {
if err := h.service.DeleteErrors(c.Request.Context(), IDsFromQuery(c)); err != nil {
ids, err := IDsFromQuery(c)
if err != nil {
Fail(c, "错误记录ID非法")
return
}
if err := h.service.DeleteErrors(c.Request.Context(), ids); err != nil {
Fail(c, "批量删除失败:"+err.Error())
return
}
@@ -259,8 +268,12 @@ func (h *Audit) UpdateError(c *gin.Context) {
Write(c, CodeSuccess, gin.H{}, "更新成功")
}
func (h *Audit) Error(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.Error(c.Request.Context(), uint(id))
id, err := positiveUintQuery(c, "ID")
if err != nil {
Fail(c, "错误记录ID非法")
return
}
item, err := h.service.Error(c.Request.Context(), id)
if err != nil {
Fail(c, "查询失败:"+err.Error())
return
+5 -1
View File
@@ -142,7 +142,11 @@ func (h *Export) Preview(c *gin.Context) {
Fail(c, "模板ID不能为空")
return
}
params, _ := exportParams(c.Request.URL.Query())
params, err := exportParams(c.Request.URL.Query())
if err != nil {
Fail(c, "解析 params 参数失败")
return
}
sql, err := h.service.Preview(c.Request.Context(), templateID, params)
if err != nil {
Fail(c, "获取失败")
+11 -3
View File
@@ -56,7 +56,11 @@ func (h *Media) Upload(c *gin.Context) {
return
}
defer opened.Close()
category, _ := strconv.Atoi(c.DefaultPostForm("classId", "0"))
category, parseErr := strconv.Atoi(c.DefaultPostForm("classId", "0"))
if parseErr != nil || category < 0 {
Fail(c, "文件分类 ID 非法")
return
}
save := c.DefaultQuery("noSave", "0") == "0"
item, err := h.service.Upload(c.Request.Context(), claims.ID, header.Filename, header.Header.Get("Content-Type"), category, opened, save)
if err != nil {
@@ -276,12 +280,16 @@ func (h *Media) CompleteUpload(c *gin.Context) {
}
func (h *Media) CancelUpload(c *gin.Context) {
claims := Claims(c)
id, _ := strconv.ParseUint(c.Param("uploadId"), 10, 64)
if claims == nil {
Fail(c, "未登录")
return
}
if err := h.service.CancelUpload(c.Request.Context(), claims.ID, uint(id)); err != nil {
id, err := positiveUintParam(c, "uploadId")
if err != nil {
Fail(c, "上传会话 ID 非法")
return
}
if err := h.service.CancelUpload(c.Request.Context(), claims.ID, id); err != nil {
Fail(c, err.Error())
return
}
+29 -5
View File
@@ -1,11 +1,31 @@
package handler
import (
"errors"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
var errInvalidPositiveUint = errors.New("参数必须是正整数")
func positiveUint(raw string) (uint, error) {
parsed, err := strconv.ParseUint(strings.TrimSpace(raw), 10, 64)
if err != nil || parsed == 0 || uint64(uint(parsed)) != parsed {
return 0, errInvalidPositiveUint
}
return uint(parsed), nil
}
func positiveUintQuery(c *gin.Context, key string) (uint, error) {
return positiveUint(c.Query(key))
}
func positiveUintParam(c *gin.Context, key string) (uint, error) {
return positiveUint(c.Param(key))
}
func page(c *gin.Context) (int, int, error) {
var value, size int
if raw, exists := c.GetQuery("page"); exists && raw != "" {
@@ -25,14 +45,18 @@ func page(c *gin.Context) (int, int, error) {
return value, size, nil
}
func IDsFromQuery(c *gin.Context) []uint {
func IDsFromQuery(c *gin.Context) ([]uint, error) {
values := c.QueryArray("IDs[]")
if len(values) == 0 {
return nil, errInvalidPositiveUint
}
ids := make([]uint, 0, len(values))
for _, value := range values {
id, _ := strconv.ParseUint(value, 10, 64)
if id > 0 {
ids = append(ids, uint(id))
id, err := positiveUint(value)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids
return ids, nil
}
+4 -2
View File
@@ -1,6 +1,7 @@
package handler
import (
"kra/internal/server/middleware"
"kra/internal/service"
"github.com/gin-gonic/gin"
@@ -10,9 +11,10 @@ type Session struct{ tokens *service.TokenService }
func NewSession(tokens *service.TokenService) *Session { return &Session{tokens: tokens} }
func (h *Session) Logout(c *gin.Context) {
token := c.GetHeader("x-token")
token := middleware.RequestToken(c, false)
if token == "" {
token, _ = c.Cookie("x-token")
NoAuth(c, "未登录或非法访问")
return
}
if err := h.tokens.BlacklistToken(c.Request.Context(), token); err != nil {
Fail(c, "jwt作废失败")
+28 -8
View File
@@ -27,8 +27,12 @@ func versionStage(err error) (system.VersionStage, bool) {
}
func (h *Version) Delete(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
if err := h.service.DeleteVersions(c.Request.Context(), []uint{uint(id)}); err != nil {
id, err := positiveUintQuery(c, "ID")
if err != nil {
Fail(c, "版本ID非法")
return
}
if err := h.service.DeleteVersions(c.Request.Context(), []uint{id}); err != nil {
Fail(c, "删除失败:"+err.Error())
return
}
@@ -38,8 +42,16 @@ func (h *Version) DeleteMany(c *gin.Context) {
raw := c.QueryArray("IDs[]")
ids := make([]uint, 0, len(raw))
for _, value := range raw {
id, _ := strconv.ParseUint(value, 10, 64)
ids = append(ids, uint(id))
id, err := positiveUint(value)
if err != nil {
Fail(c, "版本ID非法")
return
}
ids = append(ids, id)
}
if len(ids) == 0 {
Fail(c, "版本ID不能为空")
return
}
if err := h.service.DeleteVersions(c.Request.Context(), ids); err != nil {
Fail(c, "批量删除失败:"+err.Error())
@@ -48,8 +60,12 @@ func (h *Version) DeleteMany(c *gin.Context) {
Write(c, CodeSuccess, gin.H{}, "批量删除成功")
}
func (h *Version) Find(c *gin.Context) {
id, _ := strconv.ParseUint(c.Query("ID"), 10, 64)
item, err := h.service.Version(c.Request.Context(), uint(id))
id, err := positiveUintQuery(c, "ID")
if err != nil {
Fail(c, "版本ID非法")
return
}
item, err := h.service.Version(c.Request.Context(), id)
if err != nil {
Fail(c, "查询失败:"+err.Error())
return
@@ -112,8 +128,12 @@ func (h *Version) Download(c *gin.Context) {
Fail(c, "版本ID不能为空")
return
}
id, _ := strconv.ParseUint(rawID, 10, 64)
raw, code, err := h.service.VersionData(c.Request.Context(), uint(id))
id, err := positiveUint(rawID)
if err != nil {
Fail(c, "版本ID非法")
return
}
raw, code, err := h.service.VersionData(c.Request.Context(), id)
if err != nil {
Fail(c, "获取版本记录失败:"+err.Error())
return