[优化] 重构 API 处理逻辑,简化参数解析和响应处理

This commit is contained in:
ryan
2026-05-31 13:31:32 +08:00
parent bcd669722e
commit ec70794577
11 changed files with 183 additions and 547 deletions
+19 -1
View File
@@ -5,11 +5,12 @@ import (
"errors"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
const invalidParamsMessage = "鏃犳晥鐨勫弬鏁?"
const invalidParamsMessage = "参数错误"
func respondSuccess(c *gin.Context, data any) {
c.JSON(http.StatusOK, gin.H{
@@ -72,3 +73,20 @@ func decodeOptionalJSONBody(body io.Reader, target any) error {
}
return nil
}
func parseIDParam(c *gin.Context) (uint, bool) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
respondBadRequest(c, "")
return 0, false
}
return uint(id), true
}
func bindJSON(c *gin.Context, target any) bool {
if err := decodeJSONBody(c.Request.Body, target); err != nil {
respondBadRequest(c, "")
return false
}
return true
}