功能:
1. 对登录和注册表单进行验证
2. 对表单返回的错误进行中文翻译
3. 对表单返回的数据进行处理,大小写处理和切割
package main import ( "fmt" "net/http" "reflect" "strings" "github.com/gin-gonic/gin" "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" en_translations "github.com/go-playground/validator/v10/translations/en" zh_translations "github.com/go-playground/validator/v10/translations/zh" ) var trans ut.Translator type LoginForm struct { User string `json:"user" binding:"required,min=3,max=10"` Password string `json:"password" binding:"required"` } type SignUpForm struct { //注册的表单验证 Age uint8 `json:"age" binding:"gte=1,lte=130"` Name string `json:"name" binding:"required,min=3"` Email string `json:"email" binding:"required,email"` Password string `json:"password" binding:"required"` RePassword string `json:"re_password" binding:"required,eqfield=Password"` //跨字段 } func removeTopStruct(fileds map[string]string) map[string]string { rsp := map[string]string{} for field, err := range fileds { rsp[field[strings.Index(field, ".")+1:]] = err } return rsp } func InitTrans(locale string) (err error) { //修改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("json"), ",", 2)[0] if name == "-" { return "" } return name }) zhT := zh.New() //中文翻译器 enT := en.New() //英文翻译器 //第一个参数是备用的语言环境,后面的参数是应该支持的语言环境 uni := ut.New(enT, zhT, enT) trans, ok = uni.GetTranslator(locale) if !ok { return fmt.Errorf("uni.GetTranslator(%s)", locale) } switch locale { case "en": en_translations.RegisterDefaultTranslations(v, trans) case "zh": zh_translations.RegisterDefaultTranslations(v, trans) default: en_translations.RegisterDefaultTranslations(v, trans) } return } return } func main() { //代码侵入性很强 中间件 if err := InitTrans("zh"); err != nil { fmt.Println("初始化翻译器错误") return } router := gin.Default() router.POST("/loginJSON", func(c *gin.Context) { var loginForm LoginForm if err := c.ShouldBind(&loginForm); err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { c.JSON(http.StatusOK, gin.H{ "msg": err.Error(), }) } c.JSON(http.StatusBadRequest, gin.H{ "error": removeTopStruct(errs.Translate(trans)), }) return } c.JSON(http.StatusOK, gin.H{ "msg": "登录成功", }) }) router.POST("/signup", func(c *gin.Context) { var signUpFrom SignUpForm if err := c.ShouldBind(&signUpFrom); err != nil { fmt.Println(err.Error()) c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "msg": "注册成功", }) }) _ = router.Run(":8083") }
标签:return,string,err,验证,binding,表单,json,gin From: https://www.cnblogs.com/wlike/p/16824767.html