package model import ( "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/gogf/gf/util/gconv" "go_sku_server/pkg/e" "reflect" ) type LadderPriceRequest struct { GoodsId string `json:"goods_id" form:"goods_id" binding:"required" required_errcode:"104004"` Num int64 `json:"num" form:"num" binding:"required" required_errcode:"104005"` Currency int `json:"currency" form:"currency" binding:"required" required_errcode:"104006"`//币种 1人民币 2美元 Costly int `json:"costly" form:"costly" `//暂时不知道是什么意思 } type requiredCode struct { msgMap map[string]int } // 验证并返回 LadderPriceRequest func NewPriceRequest(ctx *gin.Context) ( LadderPriceRequest, *e.ApiError) { ladderPriceRequest:=LadderPriceRequest{} typeOfLadderPrice:=reflect.TypeOf(ladderPriceRequest) if err:= ctx.ShouldBind(&ladderPriceRequest); err != nil { if fieldError,ok:=err.(validator.ValidationErrors);ok{ if(fieldError[0].Tag()=="required"){ fieldName:=fieldError[0].StructField() requiredCode:=104001 if field,bool:=typeOfLadderPrice.FieldByName(fieldName);bool==true{ requiredCode=gconv.Int(field.Tag.Get("required_errcode")) } return ladderPriceRequest,e.NewApiError("缺少参数:"+fieldName,requiredCode) } } } return ladderPriceRequest, nil }