You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.3 KiB

package home
import (
"github.com/gin-gonic/gin"
"goskeleton/app/global/consts"
"goskeleton/app/http/controller/api"
common_data_type "goskeleton/app/http/validator/common/data_type"
"goskeleton/app/http/validator/core/data_transfer"
"goskeleton/app/utils/response"
)
// 门户类前端接口模拟一个获取新闻的参数验证器
type News struct {
NewsType string `form:"newsType" json:"newsType" binding:"required,min=1"` // 验证规则必填最小长度为1
common_data_type.Page
}
func (n News) CheckParams(context *gin.Context) {
//1.先按照验证器提供的基本语法基本可以校验90%以上的不合格参数
if err := context.ShouldBind(&n); err != nil {
// 将表单参数验证器出现的错误直接交给错误翻译器统一处理即可
response.ValidatorError(context, err)
return
}
// 该函数主要是将绑定的数据以 键=>值 形式直接传递给下一步(控制器)
extraAddBindDataContext := data_transfer.DataAddContext(n, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "HomeNews表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&api.Home{}).News(extraAddBindDataContext)
}
}