Files
cloudreve/routers/controllers/main.go

106 lines
2.7 KiB
Go
Raw Normal View History

2019-11-05 19:49:56 +08:00
package controllers
import (
"context"
2019-11-05 19:49:56 +08:00
"encoding/json"
"github.com/cloudreve/Cloudreve/v4/pkg/serializer"
2019-11-12 15:34:54 +08:00
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
2019-11-05 19:49:56 +08:00
)
2019-11-06 16:42:13 +08:00
// ParamErrorMsg 根据Validator返回的错误信息给出错误提示
func ParamErrorMsg(filed string, tag string) string {
// 未通过验证的表单域与中文对应
fieldMap := map[string]string{
"UserName": "Email",
"Password": "Password",
"Path": "Path",
"SourceID": "Source resource",
"URL": "URL",
"Nick": "Nickname",
2019-11-06 16:42:13 +08:00
}
// 未通过的规则与中文对应
tagMap := map[string]string{
"required": "cannot be empty",
"min": "too short",
"max": "too long",
"email": "format error",
2019-11-06 16:42:13 +08:00
}
fieldVal, findField := fieldMap[filed]
if !findField {
fieldVal = filed
}
2019-11-06 16:42:13 +08:00
tagVal, findTag := tagMap[tag]
if findTag {
2019-11-06 16:42:13 +08:00
// 返回拼接出来的错误信息
2022-04-30 16:51:24 +08:00
return fieldVal + " " + tagVal
2019-11-06 16:42:13 +08:00
}
return ""
}
2019-11-05 19:49:56 +08:00
// ErrorResponse 返回错误消息
func ErrorResponse(err error) serializer.Response {
2019-11-06 16:42:13 +08:00
// 处理 Validator 产生的错误
2019-11-05 19:49:56 +08:00
if ve, ok := err.(validator.ValidationErrors); ok {
for _, e := range ve {
return serializer.ParamErrDeprecated(
2020-03-09 14:07:36 +08:00
ParamErrorMsg(e.Field(), e.Tag()),
2019-11-05 19:49:56 +08:00
err,
)
}
}
2019-11-06 16:42:13 +08:00
2019-11-05 19:49:56 +08:00
if _, ok := err.(*json.UnmarshalTypeError); ok {
return serializer.ParamErrDeprecated("JSON marshall error", err)
2019-11-05 19:49:56 +08:00
}
return serializer.ParamErrDeprecated("Parameter error", err)
}
// FromJSON Parse and validate JSON from request body
func FromJSON[T any](ctxKey any) gin.HandlerFunc {
return func(c *gin.Context) {
var service T
if err := c.ShouldBindJSON(&service); err == nil {
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), ctxKey, &service))
c.Next()
} else {
c.JSON(200, ErrorResponse(err))
c.Abort()
}
}
2019-11-05 19:49:56 +08:00
}
2019-11-12 15:34:54 +08:00
// FromQuery Parse and validate form from request query
func FromQuery[T any](ctxKey any) gin.HandlerFunc {
return func(c *gin.Context) {
var service T
if err := c.ShouldBindQuery(&service); err == nil {
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), ctxKey, &service))
c.Next()
} else {
c.JSON(200, ErrorResponse(err))
c.Abort()
2019-11-12 15:34:54 +08:00
}
}
}
// FromUri Parse and validate form from request uri
func FromUri[T any](ctxKey any) gin.HandlerFunc {
return func(c *gin.Context) {
var service T
if err := c.ShouldBindUri(&service); err == nil {
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), ctxKey, &service))
c.Next()
} else {
c.JSON(200, ErrorResponse(err))
c.Abort()
}
}
}
// ParametersFromContext retrieves request parameters from context
func ParametersFromContext[T any](c *gin.Context, ctxKey any) T {
return c.Request.Context().Value(ctxKey).(T)
2019-11-12 15:34:54 +08:00
}