From fa07a57d22b2fd7653b8b091fa853eb9ed51c572 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Thu, 10 Sep 2020 02:14:48 +0800 Subject: [PATCH] feat : update actions gen --- app/admin/apis/tools/gen.go | 110 +++++++++++++----- app/admin/router/initrouter.go | 3 +- app/admin/router/sysrouter.go | 2 +- app/admin/service/dto/sysjob.go | 1 + examples/run.go | 3 +- template/js.go.template | 7 +- template/model.go.template | 9 +- template/{v1.2 => v2}/dto.go.template | 33 +----- template/{v1.2 => v2}/model.go.template | 13 ++- .../{v1.2 => v2}/routercheckrole.go.template | 3 +- .../routernocheckrole.go.template | 7 +- template/vue.go.template | 8 +- test/gen_test.go | 68 +++++------ tools/file.go | 5 +- 14 files changed, 160 insertions(+), 112 deletions(-) rename template/{v1.2 => v2}/dto.go.template (83%) rename template/{v1.2 => v2}/model.go.template (90%) rename template/{v1.2 => v2}/routercheckrole.go.template (88%) rename template/{v1.2 => v2}/routernocheckrole.go.template (76%) diff --git a/app/admin/apis/tools/gen.go b/app/admin/apis/tools/gen.go index 4de3a4a..67ea70f 100644 --- a/app/admin/apis/tools/gen.go +++ b/app/admin/apis/tools/gen.go @@ -30,7 +30,7 @@ func Preview(c *gin.Context) { tools2.HasError(err, "", -1) t5, err := template.ParseFiles("template/router.go.template") tools2.HasError(err, "", -1) - t6, err := template.ParseFiles("template/dto.go.template") + t6, err := template.ParseFiles("template/v2/dto.go.template") tools2.HasError(err, "", -1) tab, _ := table.Get() var b1 bytes.Buffer @@ -68,7 +68,6 @@ func GenCode(c *gin.Context) { routerfile := "template/routercheckrole.go.template" - oldTest := "// {{认证路由自动补充在此处请勿删除}}" newText := "// {{认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1,authMiddleware)" @@ -88,7 +87,7 @@ func GenCode(c *gin.Context) { tools2.HasError(err, "", -1) t5, err := template.ParseFiles("template/vue.go.template") tools2.HasError(err, "", -1) - t6, err := template.ParseFiles("template/dto.go.template") + t6, err := template.ParseFiles("template/v2/dto.go.template") tools2.HasError(err, "", -1) _ = tools2.PathCreate("./apis/" + tab.ModuleName + "/") @@ -136,7 +135,6 @@ func GenCode(c *gin.Context) { app.OK(c, "", "Code generated successfully!") } - func GenCodeV2(c *gin.Context) { table := tools.SysTables{} id, err := tools2.StringToInt(c.Param("tableId")) @@ -144,14 +142,23 @@ func GenCodeV2(c *gin.Context) { table.TableId = id tab, _ := table.Get() - routerfile := "template/v1.2/routercheckrole.go.template" - - - if !tab.IsAuth { - routerfile = "template/v1.2/routernocheckrole.go.template" + if tab.IsActions { + ActionsGen(tab) + } else { + NOActionsGen(tab) } - t1, err := template.ParseFiles("template/v1.2/model.go.template") + app.OK(c, "", "Code generated successfully!") +} + +func NOActionsGen(tab tools.SysTables) { + routerfile := "template/routercheckrole.go.template" + + if !tab.IsAuth { + routerfile = "template/routernocheckrole.go.template" + } + + t1, err := template.ParseFiles("template/model.go.template") tools2.HasError(err, "", -1) t2, err := template.ParseFiles("template/api.go.template") tools2.HasError(err, "", -1) @@ -161,15 +168,15 @@ func GenCodeV2(c *gin.Context) { tools2.HasError(err, "", -1) t5, err := template.ParseFiles("template/vue.go.template") tools2.HasError(err, "", -1) - t6, err := template.ParseFiles("template/v1.2/dto.go.template") + t6, err := template.ParseFiles("template/v2/dto.go.template") tools2.HasError(err, "", -1) - _ = tools2.PathCreate("./app/admin/apis/" + tab.ModuleName + "/") - _ = tools2.PathCreate("./app/admin/models/") - _ = tools2.PathCreate("./app/admin/router/") - _ = tools2.PathCreate("./app/admin/dto/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/apis/" + tab.ModuleName + "/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/models/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/router/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/") _ = tools2.PathCreate(config.GenConfig.FrontPath + "/api/") - _ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.PackageName) + _ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.BusinessName) var b1 bytes.Buffer err = t1.Execute(&b1, tab) @@ -183,15 +190,60 @@ func GenCodeV2(c *gin.Context) { err = t5.Execute(&b5, tab) var b6 bytes.Buffer err = t6.Execute(&b6, tab) - tools2.FileCreate(b1, "./app/admin/models/"+tab.PackageName+".go") - tools2.FileCreate(b2, "./app/admin/apis/"+tab.ModuleName+"/"+tab.PackageName+".go") - tools2.FileCreate(b3, "./app/admin/router/"+tab.PackageName+".go") - tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.PackageName+".js") - tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.PackageName+"/index.vue") - tools2.FileCreate(b6, "./app/admin/service/dto/"+tab.PackageName+".go") + tools2.FileCreate(b1, "./app/"+tab.PackageName+"/models/"+tab.BusinessName+".go") + tools2.FileCreate(b2, "./app/"+tab.PackageName+"/apis/"+tab.ModuleName+"/"+tab.BusinessName+".go") + tools2.FileCreate(b3, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go") + tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.BusinessName+".js") + tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.BusinessName+"/index.vue") + tools2.FileCreate(b6, "./app/"+tab.PackageName+"/service/dto/"+tab.BusinessName+".go") +} - app.OK(c, "", "Code generated successfully!") +func ActionsGen(tab tools.SysTables) { + routerfile := "template/v2/routercheckrole.go.template" + + if !tab.IsAuth { + routerfile = "template/v2/routernocheckrole.go.template" + } + + t1, err := template.ParseFiles("template/v2/model.go.template") + tools2.HasError(err, "", -1) + //t2, err := template.ParseFiles("template/v2/router.go.template") + //tools2.HasError(err, "", -1) + t3, err := template.ParseFiles(routerfile) + tools2.HasError(err, "", -1) + t4, err := template.ParseFiles("template/js.go.template") + tools2.HasError(err, "", -1) + t5, err := template.ParseFiles("template/vue.go.template") + tools2.HasError(err, "", -1) + t6, err := template.ParseFiles("template/v2/dto.go.template") + tools2.HasError(err, "", -1) + + _ = tools2.PathCreate("./app/" + tab.PackageName + "/models/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/router/") + _ = tools2.PathCreate("./app/" + tab.PackageName + "/service/dto/") + _ = tools2.PathCreate(config.GenConfig.FrontPath + "/api/") + _ = tools2.PathCreate(config.GenConfig.FrontPath + "/views/" + tab.BusinessName) + + var b1 bytes.Buffer + err = t1.Execute(&b1, tab) + //var b2 bytes.Buffer + //err = t2.Execute(&b2, tab) + var b3 bytes.Buffer + err = t3.Execute(&b3, tab) + var b4 bytes.Buffer + err = t4.Execute(&b4, tab) + var b5 bytes.Buffer + err = t5.Execute(&b5, tab) + var b6 bytes.Buffer + err = t6.Execute(&b6, tab) + + tools2.FileCreate(b1, "./app/"+tab.PackageName+"/models/"+tab.BusinessName+".go") + //tools2.FileCreate(b2, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go") + tools2.FileCreate(b3, "./app/"+tab.PackageName+"/router/"+tab.BusinessName+".go") + tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.BusinessName+".js") + tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.BusinessName+"/index.vue") + tools2.FileCreate(b6, "./app/"+tab.PackageName+"/service/dto/"+tab.BusinessName+".go") } func GenMenuAndApi(c *gin.Context) { @@ -228,10 +280,10 @@ func GenMenuAndApi(c *gin.Context) { Cmenu.Path = tab.TBName Cmenu.MenuType = "C" Cmenu.Action = "无" - Cmenu.Permission = tab.PackageName + ":" + tab.ModuleName + ":list" + Cmenu.Permission = tab.ModuleName + ":" + tab.BusinessName + ":list" Cmenu.ParentId = Mmenu.MenuId Cmenu.NoCache = false - Cmenu.Component = "/" + tab.ModuleName + "/index" + Cmenu.Component = "/" + tab.BusinessName + "/index" Cmenu.Sort = 0 Cmenu.Visible = "0" Cmenu.IsFrame = "0" @@ -248,7 +300,7 @@ func GenMenuAndApi(c *gin.Context) { MList.Path = tab.TBName MList.MenuType = "F" MList.Action = "无" - MList.Permission = tab.PackageName + ":" + tab.ModuleName + ":query" + MList.Permission = tab.ModuleName + ":" + tab.BusinessName + ":query" MList.ParentId = Cmenu.MenuId MList.NoCache = false MList.Sort = 0 @@ -267,7 +319,7 @@ func GenMenuAndApi(c *gin.Context) { MCreate.Path = tab.TBName MCreate.MenuType = "F" MCreate.Action = "无" - MCreate.Permission = tab.PackageName + ":" + tab.ModuleName + ":add" + MCreate.Permission = tab.ModuleName + ":" + tab.BusinessName + ":add" MCreate.ParentId = Cmenu.MenuId MCreate.NoCache = false MCreate.Sort = 0 @@ -286,7 +338,7 @@ func GenMenuAndApi(c *gin.Context) { MUpdate.Path = tab.TBName MUpdate.MenuType = "F" MUpdate.Action = "无" - MUpdate.Permission = tab.PackageName + ":" + tab.ModuleName + ":edit" + MUpdate.Permission = tab.ModuleName + ":" + tab.BusinessName + ":edit" MUpdate.ParentId = Cmenu.MenuId MUpdate.NoCache = false MUpdate.Sort = 0 @@ -305,7 +357,7 @@ func GenMenuAndApi(c *gin.Context) { MDelete.Path = tab.TBName MDelete.MenuType = "F" MDelete.Action = "无" - MDelete.Permission = tab.PackageName + ":" + tab.ModuleName + ":remove" + MDelete.Permission = tab.ModuleName + ":" + tab.BusinessName + ":remove" MDelete.ParentId = Cmenu.MenuId MDelete.NoCache = false MDelete.Sort = 0 diff --git a/app/admin/router/initrouter.go b/app/admin/router/initrouter.go index 7c6c040..8e3e4e8 100644 --- a/app/admin/router/initrouter.go +++ b/app/admin/router/initrouter.go @@ -5,7 +5,6 @@ import ( "go-admin/app/admin/middleware" "go-admin/app/admin/middleware/handler" "go-admin/common/global" - jwt "go-admin/pkg/jwtauth" _ "go-admin/pkg/jwtauth" "go-admin/tools" config2 "go-admin/tools/config" @@ -19,7 +18,7 @@ func InitRouter(r *gin.Engine) *gin.Engine { middleware.InitMiddleware(r) // the jwt middleware var err error - authMiddleware, err = middleware.AuthInit() + authMiddleware, err := middleware.AuthInit() tools.HasError(err, "JWT Init Error", 500) // 注册系统路由 diff --git a/app/admin/router/sysrouter.go b/app/admin/router/sysrouter.go index 60776a8..a836a4f 100644 --- a/app/admin/router/sysrouter.go +++ b/app/admin/router/sysrouter.go @@ -65,7 +65,7 @@ func sysNoCheckRoleRouter(r *gin.RouterGroup) { v1.GET("/monitor/server", monitor.ServerInfo) v1.GET("/getCaptcha", system.GenerateCaptchaHandler) v1.GET("/gen/preview/:tableId", Preview) - v1.GET("/gen/toproject/:tableId", GenCode) + v1.GET("/gen/toproject/:tableId", GenCodeV2) v1.GET("/gen/todb/:tableId", GenMenuAndApi) v1.GET("/gen/tabletree", GetSysTablesTree) v1.GET("/menuTreeselect", system.GetMenuTreeelect) diff --git a/app/admin/service/dto/sysjob.go b/app/admin/service/dto/sysjob.go index 9ad07b4..2c1064b 100644 --- a/app/admin/service/dto/sysjob.go +++ b/app/admin/service/dto/sysjob.go @@ -2,6 +2,7 @@ package dto import ( "github.com/gin-gonic/gin" + "go-admin/app/admin/models" "go-admin/common/dto" models2 "go-admin/common/models" diff --git a/examples/run.go b/examples/run.go index 9841226..625d327 100644 --- a/examples/run.go +++ b/examples/run.go @@ -7,7 +7,6 @@ import ( "gorm.io/driver/mysql" "gorm.io/gorm" - "go-admin/app/admin/router" "go-admin/common/global" mycasbin "go-admin/pkg/casbin" "go-admin/pkg/logger" @@ -22,6 +21,6 @@ func main() { mycasbin.Setup() logger.Setup() global.GinEngine = gin.Default() - router.InitRouter() + //router.InitRouter() log.Fatal(global.GinEngine.Run(":8000")) } diff --git a/template/js.go.template b/template/js.go.template index 5bb85fb..c4ddf21 100644 --- a/template/js.go.template +++ b/template/js.go.template @@ -30,17 +30,18 @@ data: data // 修改{{.ClassName}} export function update{{.ClassName}}(data) { return request({ -url: '/api/v1/{{.ModuleName}}', +url: '/api/v1/{{.ModuleName}}/'+data.{{.PkJsonField}}, method: 'put', data: data }) } // 删除{{.ClassName}} -export function del{{.ClassName}}({{.PkJsonField}}) { +export function del{{.ClassName}}({{.PkJsonField}},data) { return request({ url: '/api/v1/{{.ModuleName}}/' + {{.PkJsonField}}, -method: 'delete' +method: 'delete', +data: data }) } diff --git a/template/model.go.template b/template/model.go.template index e2e0bab..7763c4f 100644 --- a/template/model.go.template +++ b/template/model.go.template @@ -1,8 +1,13 @@ package models import ( - orm "go-admin/global" - "go-admin/tools" + "strconv" + + "go-admin/common/dto" + orm "go-admin/common/global" + "go-admin/common/models" + "go-admin/tools" + {{ range .Columns }} {{- if eq .GoField "CreatedAt" -}} {{- else if eq .GoField "UpdatedAt" -}} diff --git a/template/v1.2/dto.go.template b/template/v2/dto.go.template similarity index 83% rename from template/v1.2/dto.go.template rename to template/v2/dto.go.template index 7354046..3ea2a9a 100644 --- a/template/v1.2/dto.go.template +++ b/template/v2/dto.go.template @@ -5,18 +5,17 @@ import ( "go-admin/app/admin/models" "go-admin/common/dto" models2 "go-admin/common/models" - "net/http" ) type {{.ClassName}}Search struct { - Pagination `search:"-"` + dto.Pagination `search:"-"` {{ $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}}"` // {{.ColumnComment}} - {{end -}} + {{ end -}} {{- end }} } @@ -75,29 +74,16 @@ func (s *{{.ClassName}}Control) GenerateM() (models2.ActiveRecord, error) { {{- else }} {{.GoField}}: s.{{.GoField}}, {{- end }} + {{- end }} }, nil } func (s *{{.ClassName}}Control) GetId() interface{} { - return s.{{.PkColumn}} + return s.{{.PkGoField}} } type {{.ClassName}}ById struct { - Id int `uri:"id" validate:"required"` - Ids []int `json:"ids"` -} - -func (s *{{.ClassName}}ById) Bind(ctx *gin.Context) error { - if ctx.Request.Method == http.MethodDelete { - err := ctx.Bind(s) - if err != nil { - return err - } - if len(s.Ids) > 0 { - return nil - } - } - return ctx.BindUri(s) + dto.ObjectById } func (s *{{.ClassName}}ById) Generate() dto.Control { @@ -107,11 +93,4 @@ func (s *{{.ClassName}}ById) Generate() dto.Control { func (s *{{.ClassName}}ById) GenerateM() (models2.ActiveRecord, error) { return &models.{{.ClassName}}{}, nil -} - -func (s *{{.ClassName}}ById) GetId() interface{} { - if len(s.Ids) > 0 { - return s.Ids - } - return s.Id -} +} \ No newline at end of file diff --git a/template/v1.2/model.go.template b/template/v2/model.go.template similarity index 90% rename from template/v1.2/model.go.template rename to template/v2/model.go.template index b1359b8..732b92e 100644 --- a/template/v1.2/model.go.template +++ b/template/v2/model.go.template @@ -1,14 +1,17 @@ package models import ( + "strconv" + {{- if eq .IsActions true -}} + {{- else }} "go-admin/common/dto" - orm "go-admin/common/global" - "go-admin/common/models" "go-admin/tools" - "strconv" + orm "go-admin/common/global" + {{ end }} + "go-admin/common/models" ) -type SysJob struct { +type {{.ClassName}} struct { {{ range .Columns -}} {{$x := .Pk}} {{- if ($x) }} @@ -34,7 +37,7 @@ func (e *{{.ClassName}}) Generate() models.ActiveRecord { } func (e *{{.ClassName}}) GetId() interface{} { - return e.{{.PkColumn}} + return e.{{.PkGoField}} } func (e *{{.ClassName}}) SetCreateBy(createBy uint) { diff --git a/template/v1.2/routercheckrole.go.template b/template/v2/routercheckrole.go.template similarity index 88% rename from template/v1.2/routercheckrole.go.template rename to template/v2/routercheckrole.go.template index 7d153f7..cda02ba 100644 --- a/template/v1.2/routercheckrole.go.template +++ b/template/v2/routercheckrole.go.template @@ -3,7 +3,6 @@ package router import ( "github.com/gin-gonic/gin" - "go-admin/apis/{{.ModuleName}}" "go-admin/app/admin/middleware" "go-admin/app/admin/models" "go-admin/app/admin/service/dto" @@ -26,7 +25,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJW })) r.GET("/:id", actions.PermissionAction(), actions.ViewAction(new(dto.{{.ClassName}}ById))) r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control))) - r.PUT("", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control))) + r.PUT("/:id", actions.PermissionAction(), actions.UpdateAction(new(dto.{{.ClassName}}Control))) r.DELETE("/:id", actions.PermissionAction(), actions.DeleteAction(new(dto.{{.ClassName}}ById))) } } diff --git a/template/v1.2/routernocheckrole.go.template b/template/v2/routernocheckrole.go.template similarity index 76% rename from template/v1.2/routernocheckrole.go.template rename to template/v2/routernocheckrole.go.template index c42ac94..741c4df 100644 --- a/template/v1.2/routernocheckrole.go.template +++ b/template/v2/routernocheckrole.go.template @@ -3,7 +3,10 @@ package router import ( "github.com/gin-gonic/gin" - "go-admin/apis/{{.ModuleName}}" + "go-admin/app/admin/middleware" + "go-admin/app/admin/models" + "go-admin/app/admin/service/dto" + "go-admin/common/actions" ) // 无需认证的路由代码 @@ -17,7 +20,7 @@ func register{{.ClassName}}Router(v1 *gin.RouterGroup) { })) r.GET("/:id", actions.ViewAction(new(dto.{{.ClassName}}ById))) r.POST("", actions.CreateAction(new(dto.{{.ClassName}}Control))) - r.PUT("", actions.UpdateAction(new(dto.{{.ClassName}}Control))) + r.PUT("/:id", actions.UpdateAction(new(dto.{{.ClassName}}Control))) r.DELETE("/:id", actions.DeleteAction(new(dto.{{.ClassName}}ById))) } } diff --git a/template/vue.go.template b/template/vue.go.template index 222ebc4..a1493e3 100644 --- a/template/vue.go.template +++ b/template/vue.go.template @@ -215,7 +215,7 @@