2024-10-14 13:49:16 +08:00

38 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package home
import (
"catface/app/global/consts"
"catface/app/http/controller/api"
common_data_type "catface/app/http/validator/common/data_type"
"catface/app/http/validator/core/data_transfer"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
// 门户类前端接口模拟一个获取新闻的参数验证器
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)
}
}