2024-07-05 14:41:35 +08:00
|
|
|
|
package component
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"gitee.ltd/lxh/logger/log"
|
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
|
"github.com/go-playground/locales/en"
|
|
|
|
|
"github.com/go-playground/locales/zh"
|
|
|
|
|
ut "github.com/go-playground/universal-translator"
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
|
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var validatorTrans ut.Translator
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
initValidatorTranslator()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Error(err error) string {
|
|
|
|
|
var errs validator.ValidationErrors
|
|
|
|
|
ok := errors.As(err, &errs)
|
|
|
|
|
if !ok {
|
|
|
|
|
return err.Error()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errMap := errs.Translate(validatorTrans)
|
|
|
|
|
|
|
|
|
|
var errMsg string
|
|
|
|
|
for _, v := range errMap {
|
|
|
|
|
errMsg = v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errMsg
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-23 15:58:46 +08:00
|
|
|
|
// Validate
|
|
|
|
|
// @description: 校验
|
|
|
|
|
// @param data
|
|
|
|
|
// @return string
|
|
|
|
|
func Validate(data any) error {
|
|
|
|
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
|
|
|
|
return v.Struct(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 14:41:35 +08:00
|
|
|
|
// initValidatorTranslator
|
|
|
|
|
// @description: 初始化翻译机
|
|
|
|
|
// @receiver vli
|
|
|
|
|
func initValidatorTranslator() {
|
|
|
|
|
//修改gin框架中的Validator属性,实现自定制
|
|
|
|
|
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
|
|
|
|
|
// 注册一个获取json tag的自定义方法
|
|
|
|
|
v.RegisterTagNameFunc(func(fld reflect.StructField) string {
|
|
|
|
|
name := strings.SplitN(fld.Tag.Get("label"), ",", 2)[0]
|
|
|
|
|
if name == "-" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return name
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
zhT := zh.New() //中文翻译器
|
|
|
|
|
enT := en.New() //英文翻译器
|
|
|
|
|
|
|
|
|
|
// 第一个参数是备用(fallback)的语言环境
|
|
|
|
|
// 后面的参数是应该支持的语言环境(支持多个)
|
|
|
|
|
// uni := ut.New(zhT, zhT) 也是可以的
|
|
|
|
|
uni := ut.New(enT, zhT, enT)
|
|
|
|
|
|
|
|
|
|
// locale 通常取决于 http 请求头的 'Accept-Language'
|
|
|
|
|
var ok bool
|
|
|
|
|
// 也可以使用 uni.FindTranslator(...) 传入多个locale进行查找
|
|
|
|
|
validatorTrans, ok = uni.GetTranslator("zh")
|
|
|
|
|
if !ok {
|
|
|
|
|
log.Errorf("获取翻译机失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := overrideTranslator(v, validatorTrans)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("覆盖原有翻译失败: %v", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// overrideTranslator
|
|
|
|
|
// @description: 覆盖原有翻译
|
|
|
|
|
// @param v
|
|
|
|
|
// @param translator
|
|
|
|
|
// @return error
|
|
|
|
|
func overrideTranslator(v *validator.Validate, translator ut.Translator) error {
|
|
|
|
|
err := zhTranslations.RegisterDefaultTranslations(v, translator)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|