From 75eec8eebef7fc48dd831de9edce9d2a93cc0ed4 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Thu, 2 Jul 2020 00:03:57 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Agen=20code=20to=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apis/tools/gen.go | 10 ++++------ router/sys_router.go | 2 ++ template/api.go.template | 2 +- template/router.go.template | 30 ++++++++++++++++++------------ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/apis/tools/gen.go b/apis/tools/gen.go index 15c1e418..a44ac3c4 100644 --- a/apis/tools/gen.go +++ b/apis/tools/gen.go @@ -49,7 +49,6 @@ func Preview(c *gin.Context) { c.JSON(http.StatusOK, res.ReturnOK()) } - func GenCode(c *gin.Context) { table := tools.SysTables{} id, err := tools2.StringToInt(c.Param("tableId")) @@ -63,10 +62,9 @@ func GenCode(c *gin.Context) { t3, err := template.ParseFiles("template/router.go.template") tools2.HasError(err, "", -1) tab, _ := table.Get() - _ = tools2.PathCreate("./apis/") - _ = tools2.PathCreate("./apis/") + _ = tools2.PathCreate("./apis/" + tab.ModuleName + "/") _ = tools2.PathCreate("./models/") - _ = tools2.PathCreate("./router/" + tab.ModuleName + "/") + _ = tools2.PathCreate("./router/") var b1 bytes.Buffer err = t1.Execute(&b1, tab) @@ -75,7 +73,7 @@ func GenCode(c *gin.Context) { var b3 bytes.Buffer err = t3.Execute(&b3, tab) tools2.FileCreate(b1, "./models/"+tab.PackageName+".go") - tools2.FileCreate(b2, "./apis/"+tab.PackageName+".go") + tools2.FileCreate(b2, "./apis/"+tab.ModuleName+"/"+tab.PackageName+".go") tools2.FileCreate(b3, "./router/"+tab.PackageName+".go") - + app.OK(c, "", "代码生成成功!") } diff --git a/router/sys_router.go b/router/sys_router.go index 5780fcde..eede6d2a 100644 --- a/router/sys_router.go +++ b/router/sys_router.go @@ -52,6 +52,8 @@ 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("/menuTreeselect", system.GetMenuTreeelect) v1.GET("/dict/databytype/:dictType", dict.GetDictDataByDictType) diff --git a/template/api.go.template b/template/api.go.template index 77010c9e..80239660 100644 --- a/template/api.go.template +++ b/template/api.go.template @@ -1,4 +1,4 @@ -package apis +package {{.ModuleName}} import ( "github.com/gin-gonic/gin" diff --git a/template/router.go.template b/template/router.go.template index 496c0229..0a4e3b5c 100644 --- a/template/router.go.template +++ b/template/router.go.template @@ -1,3 +1,11 @@ +package router + +import ( +"github.com/gin-gonic/gin" +"go-admin/middleware" +jwt "go-admin/pkg/jwtauth" +"go-admin/apis/{{.ModuleName}}" +) // 需认证的路由代码 func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { @@ -18,16 +26,14 @@ l.GET("/{{.ModuleName}}List",{{.ModuleName}}.Get{{.ClassName}}List) } // 无需认证的路由代码 -func register{{.ClassName}}Router(v1 *gin.RouterGroup) { - -v1.GET("/{{.ModuleName}}List",{{.ModuleName}}.Get{{.ClassName}}List) - -r := v1.Group("/{{.ModuleName}}") -{ -r.GET("/:{{.PkJsonField}}", {{.ModuleName}}.Get{{.ClassName}}) -r.POST("", {{.ModuleName}}.Insert{{.ClassName}}) -r.PUT("", {{.ModuleName}}.Update{{.ClassName}}) -r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}}) -} -} +// func register{{.ClassName}}Router(v1 *gin.RouterGroup) { +// v1.GET("/{{.ModuleName}}List",{{.ModuleName}}.Get{{.ClassName}}List) +// r := v1.Group("/{{.ModuleName}}") +// { +// r.GET("/:{{.PkJsonField}}", {{.ModuleName}}.Get{{.ClassName}}) +// r.POST("", {{.ModuleName}}.Insert{{.ClassName}}) +// r.PUT("", {{.ModuleName}}.Update{{.ClassName}}) +// r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}}) +// } +// }