You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
63 lines
1.5 KiB
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/HFO4/cloudreve/models"
|
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
|
"github.com/gin-gonic/gin"
|
|
"gopkg.in/go-playground/validator.v8"
|
|
)
|
|
|
|
// ParamErrorMsg 根据Validator返回的错误信息给出错误提示
|
|
func ParamErrorMsg(filed string, tag string) string {
|
|
// 未通过验证的表单域与中文对应
|
|
fieldMap := map[string]string{
|
|
"UserName": "邮箱",
|
|
"Password": "密码",
|
|
"Path": "路径",
|
|
}
|
|
// 未通过的规则与中文对应
|
|
tagMap := map[string]string{
|
|
"required": "不能为空",
|
|
"min": "太短",
|
|
"max": "太长",
|
|
"email": "格式不正确",
|
|
}
|
|
fieldVal, findField := fieldMap[filed]
|
|
tagVal, findTag := tagMap[tag]
|
|
if findField && findTag {
|
|
// 返回拼接出来的错误信息
|
|
return fieldVal + tagVal
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ErrorResponse 返回错误消息
|
|
func ErrorResponse(err error) serializer.Response {
|
|
// 处理 Validator 产生的错误
|
|
if ve, ok := err.(validator.ValidationErrors); ok {
|
|
for _, e := range ve {
|
|
return serializer.ParamErr(
|
|
ParamErrorMsg(e.Field, e.Tag),
|
|
err,
|
|
)
|
|
}
|
|
}
|
|
|
|
if _, ok := err.(*json.UnmarshalTypeError); ok {
|
|
return serializer.ParamErr("JSON类型不匹配", err)
|
|
}
|
|
|
|
return serializer.ParamErr("参数错误", err)
|
|
}
|
|
|
|
// CurrentUser 获取当前用户
|
|
func CurrentUser(c *gin.Context) *model.User {
|
|
if user, _ := c.Get("user"); user != nil {
|
|
if u, ok := user.(*model.User); ok {
|
|
return u
|
|
}
|
|
}
|
|
return nil
|
|
}
|