go 参数校验,并且返回翻译成中文提示
package main import ( "errors" "fmt" "github.com/gin-gonic/gin" "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" "github.com/go-playground/validator/v10" zh_translations "github.com/go-playground/validator/v10/translations/zh" "github.com/xuri/excelize/v2" "net/http" "reflect" "time" ) type PublishDistributionController struct { controller.BaseController } // ValidateData 全局model数据验证器 func Validate(dataStruct interface{}) error { //验证 zh_ch := zh.New() validate := validator.New() //注册一个函数,获取struct tag里自定义的label作为字段名 validate.RegisterTagNameFunc(func(fld reflect.StructField) string { name := fld.Tag.Get("label") return name }) uni := ut.New(zh_ch) trans, _ := uni.GetTranslator("zh") //验证器注册翻译器 zh_translations.RegisterDefaultTranslations(validate, trans) err := validate.Struct(dataStruct) if err != nil { for _, err := range err.(validator.ValidationErrors) { return errors.New(err.Translate(trans)) } } return nil } //验证请求参数 type PublishDistributionParam struct{ Sku string `form:"sku" json:"sku" binding:"required" validate:"required" label:"sku"` Spu string `json:"spu" validate:"omitempty" label:"spu"` SkuName string `json:"sku_name" validate:"omitempty" label:"sku名称"` CatIdOne int `json:"cat_id_one" validate:"omitempty" label:"cat_id"` ShopId string `json:"shop_id" validate:"omitempty" label:"店铺id"` IsPublish string `json:"is_publish" validate:"omitempty" label:"是否刊登"` DistriGroupId int `json:"distri_group_id" validate:"omitempty" label:"分组id"` DistriSeller string `json:"distri_seller" validate:"omitempty" label:"分组销售员"` DoneAtStart time.Time `json:"done_at_start" validate:"omitempty" label:"开始时间"` DoneAtEnd time.Time `json:"done_at_end" validate:"omitempty" label:"结束时间"` PageNum string `json:"page_num" binding:"required"` PageSize string `json:"page_size" binding:"required"` } func () main(context *gin.Context){ defer common.Recover(context) //res := new(listResponse) Sku := context.Query("sku") Spu := context.Query("spu") SkuName := context.Query("sku_name") CatIdOne := context.GetInt("cat_id_one") ShopId := context.Query("shop_id") IsPublish := context.Query("is_publish") DistriGroupId :=context.GetInt("distri_group_id") DistriSeller :=context.Query("distri_seller") DoneAtStart :=context.GetTime("done_at_start") DoneAtEnd :=context.GetTime("done_at_end") page_num := context.DefaultQuery("page_num","1") page_size := context.DefaultQuery("page_size","10") PublishDistributionParam :=&PublishDistributionParam{ Sku:Sku, Spu:Spu, SkuName:SkuName, CatIdOne:CatIdOne, ShopId:ShopId, IsPublish:IsPublish, DistriGroupId:DistriGroupId, DistriSeller:DistriSeller, DoneAtStart:DoneAtStart, DoneAtEnd:DoneAtEnd, PageNum:page_num, PageSize:page_size, } if err :=Validate(PublishDistributionParam); err!=nil{ context.JSON(http.StatusInternalServerError, gin.H{"code": 5000,"msg": err.Error()}) return } res:=map[string]int{ "ssss":11, "kkk":22, } context.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": res, }) }