diff --git a/application/dto/request/gin.go b/application/dto/request/gin.go index aa7b4f8..e881642 100644 --- a/application/dto/request/gin.go +++ b/application/dto/request/gin.go @@ -1,6 +1,11 @@ package request import ( + "mime/multipart" + "reflect" + "strings" + "sync" + "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" "github.com/go-playground/locales/zh" @@ -9,59 +14,71 @@ import ( zh_translations "github.com/go-playground/validator/v10/translations/zh" "github.com/lshaofan/go-framework/application/dto/response" "github.com/lshaofan/go-framework/infrastructure/dao" - "mime/multipart" - "reflect" - "strings" ) type GinRequest struct { - c *gin.Context + c *gin.Context +} + +func NewGinRequest(c *gin.Context) *GinRequest { + return &GinRequest{c: c} +} + +type Validate struct { uni *ut.UniversalTranslator validate *validator.Validate trans ut.Translator } -func NewGinRequest(c *gin.Context) *GinRequest { - //注册翻译器 - zh_ := zh.New() - uni := ut.New(zh_, zh_) - trans, _ := uni.GetTranslator("zh") - //获取gin的校验器 - validate := binding.Validator.Engine().(*validator.Validate) - //注册翻译器 - _ = zh_translations.RegisterDefaultTranslations(validate, trans) - return &GinRequest{ - c: c, - uni: uni, - trans: trans, - validate: validate, - } +var ( + validate *Validate + once sync.Once +) + +func NewValidate() *Validate { + once.Do(func() { + validate = &Validate{} + //注册翻译器 + zh_ := zh.New() + uni := ut.New(zh_, zh_) + trans, _ := uni.GetTranslator("zh") + //获取gin的校验器 + val := binding.Validator.Engine().(*validator.Validate) + //注册翻译器 + _ = zh_translations.RegisterDefaultTranslations(val, trans) + validate.validate = val + validate.uni = uni + validate.trans = trans + }) + return validate } // TranslateToString tag 为结构体的tag定义的验证字段来源 如:json form uri query 返回第一条验证错误信息的字符串 func (g *GinRequest) TranslateToString(tag string, err error, obj interface{}) string { + v := NewValidate() getObj := reflect.TypeOf(obj) var result string - errors := err.(validator.ValidationErrors) - for _, err := range errors { - //TODO 如果翻译为空,就返回参数错误 ,按时不需要后续出现为空err情况再说 - //if err.Translate(g.trans) == "" { - // result = "参数错误" - // return result - //} - if f, exist := getObj.Elem().FieldByName(err.Field()); exist && f.Tag.Get(tag) != "" { - result = f.Tag.Get(tag) + strings.Replace(err.Translate(g.trans), err.Field(), "", -1) - } else { - result = err.Translate(g.trans) + // 判断err 是否是 validator.ValidationErrors 类型 + if _, ok := err.(validator.ValidationErrors); !ok { + result = err.Error() + } else { + + errors := err.(validator.ValidationErrors) + for _, err := range errors { + if f, exist := getObj.Elem().FieldByName(err.Field()); exist && f.Tag.Get(tag) != "" { + result = f.Tag.Get(tag) + strings.Replace(err.Translate(v.trans), err.Field(), "", -1) + } else { + result = err.Translate(v.trans) + } + return result } - return result } - return result } // TranslateToMap Translate 翻译错误信息 tag 为结构体的tag定义的验证字段来源 如:json form uri query 返回所有验证错误信息的map func (g *GinRequest) TranslateToMap(tag string, err error, obj interface{}) map[string][]string { + v := NewValidate() getObj := reflect.TypeOf(obj) var result = make(map[string][]string) errors := err.(validator.ValidationErrors) @@ -69,10 +86,10 @@ func (g *GinRequest) TranslateToMap(tag string, err error, obj interface{}) map[ for _, err := range errors { if f, exist := getObj.Elem().FieldByName(err.Field()); exist { - str := f.Tag.Get(tag) + strings.Replace(err.Translate(g.trans), err.Field(), "", -1) + str := f.Tag.Get(tag) + strings.Replace(err.Translate(v.trans), err.Field(), "", -1) result[err.Field()] = append(result[err.Field()], str) } else { - result[err.Field()] = append(result[err.Field()], err.Translate(g.trans)) + result[err.Field()] = append(result[err.Field()], err.Translate(v.trans)) } } return result @@ -80,10 +97,11 @@ func (g *GinRequest) TranslateToMap(tag string, err error, obj interface{}) map[ // Translate TranslateToMap Translate 翻译错误信息 func (g *GinRequest) Translate(err error) map[string][]string { + v := NewValidate() var result = make(map[string][]string) errors := err.(validator.ValidationErrors) for _, err := range errors { - result[err.Field()] = append(result[err.Field()], err.Translate(g.trans)) + result[err.Field()] = append(result[err.Field()], err.Translate(v.trans)) } return result } diff --git a/application/micro/micro.go b/application/micro/micro.go index dfe74e9..b2d2979 100644 --- a/application/micro/micro.go +++ b/application/micro/micro.go @@ -144,7 +144,6 @@ func (s *Server) Init() error { s.etcdConfig.DialTimeout = s.etcdDialTimeout } } - s.ctx = context.Background() s.etcdClient, err = clientv3.New(*s.etcdConfig) if err != nil {