diff --git a/app/admin/service/dto/sys_category.go b/app/admin/service/dto/sys_category.go index 7925f680..88ca2585 100644 --- a/app/admin/service/dto/sys_category.go +++ b/app/admin/service/dto/sys_category.go @@ -1,6 +1,8 @@ package dto import ( + "errors" + vd "github.com/bytedance/go-tagexpr/v2/validator" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/sdk/api" @@ -11,9 +13,9 @@ import ( type SysCategorySearch struct { dto.Pagination `search:"-"` - Name string `form:"name" search:"type:exact;column:name;table:sys_category" comment:"名称"` - Status string `form:"status" search:"type:exact;column:status;table:sys_category" comment:"状态"` - CateId int `form:"cateId" search:"type:exact;column:cate_id;table:sys_category" comment:"分类id"` + Name string `form:"name" search:"type:exact;column:name;table:sys_category" comment:"名称" vd:"?"` + Status string `form:"status" search:"type:exact;column:status;table:sys_category" comment:"状态" vd:"?"` + CateId int `form:"cateId" search:"type:exact;column:cate_id;table:sys_category" comment:"分类id" vd:"?"` } func (m *SysCategorySearch) GetNeedSearch() interface{} { @@ -22,10 +24,16 @@ func (m *SysCategorySearch) GetNeedSearch() interface{} { func (m *SysCategorySearch) Bind(ctx *gin.Context) error { log := api.GetRequestLogger(ctx) + err := ctx.ShouldBind(m) if err != nil { log.Debugf("ShouldBind error: %s", err.Error()) } + + if err = vd.Validate(m); err != nil { + log.Errorf("Validate error: %s", err.Error()) + return err + } return err } @@ -36,23 +44,28 @@ func (m *SysCategorySearch) Generate() dto.Index { type SysCategoryControl struct { ID int `uri:"Id" comment:"标识"` - Name string `json:"name" comment:"名称"` - Img string `json:"img" comment:"图标"` - Sort int `json:"sort" comment:"排序"` - Status int `json:"status" comment:"状态"` - Remark string `json:"remark" comment:"备注"` + Name string `json:"name" comment:"名称" vd:"len($)>0 && $!=' '; msg:'invalid name: 不能是空字符串'"` + Img string `json:"img" comment:"图标" vd:"?"` + Sort int `json:"sort" comment:"排序" vd:"?"` + Status int `json:"status" comment:"状态" vd:"$>0; msg:'invalid status: 状态无效'"` + Remark string `json:"remark" comment:"备注" vd:"?"` } func (s *SysCategoryControl) Bind(ctx *gin.Context) error { log := api.GetRequestLogger(ctx) err := ctx.ShouldBindUri(s) if err != nil { - log.Debugf("ShouldBindUri error: %s", err.Error()) - return err + log.Errorf("ShouldBindUri error: %s", err.Error()) + return errors.New("数据绑定出错") } err = ctx.ShouldBind(s) if err != nil { - log.Debugf("ShouldBind error: %s", err.Error()) + log.Errorf("ShouldBind error: %s", err.Error()) + err = errors.New("数据绑定出错") + } + if err1 := vd.Validate(s); err != nil { + log.Errorf("Validate error: %s", err1.Error()) + return err1 } return err } @@ -78,7 +91,7 @@ func (s *SysCategoryControl) GetId() interface{} { } type SysCategoryById struct { - dto.ObjectById + dto.ObjectById `vd:"?"` } func (s *SysCategoryById) Generate() dto.Control { diff --git a/common/actions/create.go b/common/actions/create.go index e15c4098..4de722d9 100644 --- a/common/actions/create.go +++ b/common/actions/create.go @@ -27,7 +27,7 @@ func CreateAction(control dto.Control) gin.HandlerFunc { req := control.Generate() err = req.Bind(c) if err != nil { - response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败") + response.Error(c, http.StatusUnprocessableEntity, err, err.Error()) return } var object models.ActiveRecord @@ -46,4 +46,4 @@ func CreateAction(control dto.Control) gin.HandlerFunc { response.OK(c, object.GetId(), "创建成功") c.Next() } -} +} \ No newline at end of file diff --git a/common/dto/generate.go b/common/dto/generate.go index f37877cf..799f4e5b 100644 --- a/common/dto/generate.go +++ b/common/dto/generate.go @@ -1,6 +1,7 @@ package dto import ( + vd "github.com/bytedance/go-tagexpr/v2/validator" "net/http" "github.com/gin-gonic/gin" @@ -8,8 +9,8 @@ import ( ) type ObjectById struct { - Id int `uri:"id"` - Ids []int `json:"ids"` + Id int `uri:"id" vd:"($>0||len(Ids)$==0)"` + Ids []int `json:"ids" vd:"($==0||len(Ids)$>0)'"` } func (s *ObjectById) Bind(ctx *gin.Context) error { @@ -36,6 +37,10 @@ func (s *ObjectById) Bind(ctx *gin.Context) error { s.Ids = append(s.Ids, s.Id) } } + if err = vd.Validate(s); err != nil { + log.Errorf("Validate error: %s", err.Error()) + return err + } return err } @@ -45,4 +50,4 @@ func (s *ObjectById) GetId() interface{} { return s.Ids } return s.Id -} +} \ No newline at end of file diff --git a/config/settings.yml b/config/settings.yml index 8bca95b6..1f41a29a 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -31,10 +31,14 @@ settings: driver: mysql # 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms - databases: - registers: - - sources: - - user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms +# databases: +# 'locaohost:8000': +# driver: mysql +# # 数据库连接字符串 mysql 缺省信息 charset=utf8&parseTime=True&loc=Local&timeout=1000ms +# source: user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms +# registers: +# - sources: +# - user:password@tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local&timeout=1000ms gen: # 代码生成读取的数据库名称 dbname: dbname diff --git a/go.mod b/go.mod index 0e1c3a9f..0fd499e1 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/alibaba/sentinel-golang v0.6.1 github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190307165228-86c17b95fcd5 + github.com/bytedance/go-tagexpr/v2 v2.7.10 github.com/casbin/casbin/v2 v2.25.1 github.com/gin-gonic/gin v1.6.3 github.com/go-admin-team/go-admin-core v1.3.3 @@ -27,5 +28,5 @@ require ( gorm.io/driver/mysql v1.0.4-0.20201206014609-ae5fd10184f6 gorm.io/driver/postgres v1.0.6-0.20201208020313-1ed927cfab53 gorm.io/driver/sqlite v1.1.5-0.20201206014648-c84401fbe3ba - gorm.io/gorm v1.21.3 + gorm.io/gorm v1.21.6 ) \ No newline at end of file diff --git a/template/v4/dto.go.template b/template/v4/dto.go.template index fbb6a16e..5b9ce208 100644 --- a/template/v4/dto.go.template +++ b/template/v4/dto.go.template @@ -1,6 +1,8 @@ package dto import ( + "errors" + vd "github.com/bytedance/go-tagexpr/v2/validator" "github.com/gin-gonic/gin" "github.com/go-admin-team/go-admin-core/sdk/api" @@ -10,12 +12,12 @@ import ( ) type {{.ClassName}}Search struct { - dto.Pagination `search:"-"` + dto.Pagination `search:"-" vd:"-"` {{ $tablename := .TBName -}} {{ range .Columns -}} {{$z := .IsQuery}} {{- if ($z) -}} - {{.GoField}} {{.GoType}} `form:"{{.JsonField}}" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"` + {{.GoField}} {{.GoType}} `form:"{{.JsonField}}" vd:"?" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"` {{ end -}} {{- end }} } @@ -33,7 +35,11 @@ func (m *{{.ClassName}}Search) Bind(ctx *gin.Context) error { log := api.GetRequestLogger(ctx) err := ctx.ShouldBind(m) if err != nil { - log.Warnf("ShouldBind error: %s", err.Error()) + log.Errorf("ShouldBind error: %s", err.Error()) + } + if err1 := vd.Validate(s); err != nil { + log.Errorf("Validate error: %s", err1.Error()) + return err1 } return err } @@ -42,14 +48,14 @@ type {{.ClassName}}Control struct { {{ range .Columns -}} {{$x := .Pk}} {{- if ($x) }} - {{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}} + {{.GoField}} {{.GoType}} `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}" vd:"?"` // {{.ColumnComment}} {{- else if eq .GoField "CreatedAt" -}} {{- else if eq .GoField "UpdatedAt" -}} {{- else if eq .GoField "DeletedAt" -}} {{- else if eq .GoField "CreateBy" -}} {{- else if eq .GoField "UpdateBy" -}} {{- else }} - {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` + {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}" vd:"?"` {{end -}} {{- end }} } @@ -63,12 +69,17 @@ func (s *{{.ClassName}}Control) Bind(ctx *gin.Context) error { log := api.GetRequestLogger(ctx) err := ctx.ShouldBindUri(s) if err != nil { - log.Warnf("ShouldBindUri error: %s", err.Error()) - return err + log.Errorf("ShouldBindUri error: %s", err.Error()) + return errors.New("数据绑定出错") } err = ctx.ShouldBind(s) if err != nil { - log.Warnf("ShouldBind error: %s", err.Error()) + log.Errorf("ShouldBind error: %s", err.Error()) + err = errors.New("数据绑定出错") + } + if err1 := vd.Validate(s); err != nil { + log.Errorf("Validate error: %s", err1.Error()) + return err1 } return err } @@ -111,13 +122,18 @@ func (s *{{.ClassName}}ById) Bind(ctx *gin.Context) error { log := api.GetRequestLogger(ctx) err := ctx.ShouldBindUri(s) if err != nil { - log.Warnf("ShouldBindUri error: %s", err.Error()) - return err + log.Errorf("ShouldBindUri error: %s", err.Error()) + return errors.New("数据绑定出错") } err = ctx.ShouldBind(s) if err != nil { - log.Warnf("ShouldBind error: %s", err.Error()) + log.Errorf("ShouldBind error: %s", err.Error()) + err = errors.New("数据绑定出错") } + if err1 := vd.Validate(s); err != nil { + log.Errorf("Validate error: %s", err1.Error()) + return err1 + } return err } @@ -132,4 +148,4 @@ func (s *{{.ClassName}}ById) Generate() dto.Control { func (s *{{.ClassName}}ById) GenerateM() (common.ActiveRecord, error) { return &models.{{.ClassName}}{}, nil -} +} \ No newline at end of file diff --git a/template/v4/no_actions/router_check_role.go.template b/template/v4/no_actions/router_check_role.go.template index d8066c17..0aeb43e8 100644 --- a/template/v4/no_actions/router_check_role.go.template +++ b/template/v4/no_actions/router_check_role.go.template @@ -12,7 +12,7 @@ func init() { routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router) } -// 需认证的路由代码 +// register{{.ClassName}}Router func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { api := &{{.ModuleName}}.{{.ClassName}}{} r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole()) @@ -23,4 +23,4 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.PUT("/:id", api.Update{{.ClassName}}) r.DELETE("", api.Delete{{.ClassName}}) } -} +} \ No newline at end of file diff --git a/template/v4/no_actions/router_no_check_role.go.template b/template/v4/no_actions/router_no_check_role.go.template index 46f09aa8..d079678a 100644 --- a/template/v4/no_actions/router_no_check_role.go.template +++ b/template/v4/no_actions/router_no_check_role.go.template @@ -11,7 +11,7 @@ func init() { routerCheckRole = append(routerCheckRole, register{{.ClassName}}Router) } -// 需认证的路由代码 +// register{{.ClassName}}Router func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { api := &{{.ModuleName}}.{{.ClassName}}{} r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()) @@ -22,4 +22,4 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW r.PUT("/:id", api.Update{{.ClassName}}) r.DELETE("", api.Delete{{.ClassName}}) } -} +} \ No newline at end of file