Files
kra-new/internal/server/handler/business_helpers.go
T

36 lines
908 B
Go

package handler
import (
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
func businessID(c *gin.Context) (uint, bool) {
v, e := strconv.ParseUint(c.Param("id"), 10, 64)
if e != nil || v == 0 {
c.JSON(http.StatusBadRequest, Response{Code: CodeError, Msg: "参数无效"})
return 0, false
}
return uint(v), true
}
func bindQuery[T any](c *gin.Context) (*T, bool) {
var v T
if e := c.ShouldBindQuery(&v); e != nil {
c.JSON(http.StatusBadRequest, Response{Code: CodeError, Msg: "查询条件无效"})
return nil, false
}
return &v, true
}
func bindJSON[T any](c *gin.Context) (*T, bool) {
var v T
if e := c.ShouldBindJSON(&v); e != nil {
c.JSON(http.StatusBadRequest, Response{Code: CodeError, Msg: "提交内容无效"})
return nil, false
}
return &v, true
}
func businessFail(c *gin.Context, e error) {
c.JSON(http.StatusBadRequest, Response{Code: CodeError, Msg: e.Error()})
}