feat:Code generation upgrade(#173)

This commit is contained in:
zhangwenjian
2020-07-23 14:07:57 +08:00
parent 8c554d7ee7
commit ca62b4ef1d
8 changed files with 86 additions and 16 deletions
+30 -12
View File
@@ -55,6 +55,28 @@ func Preview(c *gin.Context) {
func GenCode(c *gin.Context) { func GenCode(c *gin.Context) {
table := tools.SysTables{} table := tools.SysTables{}
id, err := tools2.StringToInt(c.Param("tableId")) id, err := tools2.StringToInt(c.Param("tableId"))
ischeckrole := true
rouyerfile := "template/routercheckrole.go.template"
if c.Param("ischeckrole") != "" {
ischeckrole, err = tools2.StringToBool(c.Query("ischeckrole"))
if err != nil {
ischeckrole = true
}
}
tab, _ := table.Get()
oldTest := "// {{认证路由自动补充在此处请勿删除}}"
newText := "// {{认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1,authMiddleware)"
if !ischeckrole {
oldTest = "// {{无需认证路由自动补充在此处请勿删除}}"
newText = "// {{无需认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1)"
rouyerfile = "template/routernocheckrole.go.template"
}
tools2.HasError(err, "", -1) tools2.HasError(err, "", -1)
table.TableId = id table.TableId = id
@@ -62,14 +84,13 @@ func GenCode(c *gin.Context) {
tools2.HasError(err, "", -1) tools2.HasError(err, "", -1)
t2, err := template.ParseFiles("template/api.go.template") t2, err := template.ParseFiles("template/api.go.template")
tools2.HasError(err, "", -1) tools2.HasError(err, "", -1)
t3, err := template.ParseFiles("template/router.go.template") t3, err := template.ParseFiles(rouyerfile)
tools2.HasError(err, "", -1) tools2.HasError(err, "", -1)
t4, err := template.ParseFiles("template/js.go.template") t4, err := template.ParseFiles("template/js.go.template")
tools2.HasError(err, "", -1) tools2.HasError(err, "", -1)
t5, err := template.ParseFiles("template/vue.go.template") t5, err := template.ParseFiles("template/vue.go.template")
tools2.HasError(err, "", -1) tools2.HasError(err, "", -1)
tab, _ := table.Get()
_ = tools2.PathCreate("./apis/" + tab.ModuleName + "/") _ = tools2.PathCreate("./apis/" + tab.ModuleName + "/")
_ = tools2.PathCreate("./models/") _ = tools2.PathCreate("./models/")
_ = tools2.PathCreate("./router/") _ = tools2.PathCreate("./router/")
@@ -92,9 +113,6 @@ func GenCode(c *gin.Context) {
tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.PackageName+".js") tools2.FileCreate(b4, config.GenConfig.FrontPath+"/api/"+tab.PackageName+".js")
tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.PackageName+"/index.vue") tools2.FileCreate(b5, config.GenConfig.FrontPath+"/views/"+tab.PackageName+"/index.vue")
oldTest := "// {{认证路由自动补充在此处请勿删除}}"
newText := "// {{认证路由自动补充在此处请勿删除}} \r\n register" + tab.ClassName + "Router(v1,authMiddleware)"
helper := tools2.ReplaceHelper{ helper := tools2.ReplaceHelper{
Root: "./router/router.go", Root: "./router/router.go",
OldText: oldTest, OldText: oldTest,
@@ -126,11 +144,11 @@ func GenMenuAndApi(c *gin.Context) {
Mmenu.MenuName = tab.TBName + "管理" Mmenu.MenuName = tab.TBName + "管理"
Mmenu.Title = tab.TableComment Mmenu.Title = tab.TableComment
Mmenu.Icon = "pass" Mmenu.Icon = "pass"
Mmenu.Path = "/" + tab.TBName Mmenu.Path = tab.TBName
Mmenu.MenuType = "M" Mmenu.MenuType = "C"
Mmenu.Action = "无" Mmenu.Action = "无"
Mmenu.Permission = tab.PackageName + ":" + tab.ModuleName + ":list" Mmenu.Permission = tab.PackageName + ":" + tab.ModuleName + ":list"
Mmenu.ParentId = 0 Mmenu.ParentId = 4
Mmenu.NoCache = false Mmenu.NoCache = false
Mmenu.Component = "/" + tab.ModuleName + "/index" Mmenu.Component = "/" + tab.ModuleName + "/index"
Mmenu.Sort = 0 Mmenu.Sort = 0
@@ -143,8 +161,8 @@ func GenMenuAndApi(c *gin.Context) {
Mmenu.MenuId, err = Mmenu.Create() Mmenu.MenuId, err = Mmenu.Create()
MList := models.Menu{} MList := models.Menu{}
MList.MenuName = "分页获取" + tab.TBName MList.MenuName = tab.TBName
MList.Title = tab.TableComment MList.Title = "分页获取" + tab.TableComment
MList.Icon = "pass" MList.Icon = "pass"
MList.Path = tab.TBName MList.Path = tab.TBName
MList.MenuType = "F" MList.MenuType = "F"
@@ -182,9 +200,9 @@ func GenMenuAndApi(c *gin.Context) {
MUpdate := models.Menu{} MUpdate := models.Menu{}
MUpdate.MenuName = tab.TBName MUpdate.MenuName = tab.TBName
MUpdate.Title = tab.TableComment MUpdate.Title = "修改" + tab.TableComment
MUpdate.Icon = "pass" MUpdate.Icon = "pass"
MUpdate.Path = "修改" + tab.TBName MUpdate.Path = tab.TBName
MUpdate.MenuType = "F" MUpdate.MenuType = "F"
MUpdate.Action = "无" MUpdate.Action = "无"
MUpdate.Permission = tab.PackageName + ":" + tab.ModuleName + ":edit" MUpdate.Permission = tab.PackageName + ":" + tab.ModuleName + ":edit"
+2
View File
@@ -267,6 +267,7 @@ INSERT INTO sys_dict_type VALUES (11, '1', '1', 1, '1', '1', '1', '2020-04-11 15
INSERT INTO `sys_menu` VALUES (2, 'Upms', '系统管理', 'example', '/upms', '/0/2', 'M', '', '', 0, 1, '', 'Layout', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (2, 'Upms', '系统管理', 'example', '/upms', '/0/2', 'M', '', '', 0, 1, '', 'Layout', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL);
INSERT INTO `sys_menu` VALUES (3, 'Sysuser', '用户管理', 'user', 'sysuser', '/0/2/3', 'C', '', 'system:sysuser:list', 2, NULL, NULL, '/sysuser/index', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', '2020-04-12 11:10:42', NULL); INSERT INTO `sys_menu` VALUES (3, 'Sysuser', '用户管理', 'user', 'sysuser', '/0/2/3', 'C', '', 'system:sysuser:list', 2, NULL, NULL, '/sysuser/index', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', '2020-04-12 11:10:42', NULL);
INSERT INTO `sys_menu` VALUES (4, 'GenCode', '新功能', 'example', '/gencode', '/0/4', 'M', '', '', 0, 1, '', 'Layout', 1, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL);
INSERT INTO `sys_menu` VALUES (43, NULL, '新增用户', NULL, '/api/v1/sysuser', '/0/2/3/43', 'F', 'POST', 'system:sysuser:add', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (43, NULL, '新增用户', NULL, '/api/v1/sysuser', '/0/2/3/43', 'F', 'POST', 'system:sysuser:add', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL);
INSERT INTO `sys_menu` VALUES (44, NULL, '查询用户', NULL, '/api/v1/sysuser', '/0/2/3/44', 'F', 'GET', 'system:sysuser:query', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (44, NULL, '查询用户', NULL, '/api/v1/sysuser', '/0/2/3/44', 'F', 'GET', 'system:sysuser:query', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL);
INSERT INTO `sys_menu` VALUES (45, NULL, '修改用户', NULL, '/api/v1/sysuser/', '/0/2/3/45', 'F', 'PUT', 'system:sysuser:edit', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL); INSERT INTO `sys_menu` VALUES (45, NULL, '修改用户', NULL, '/api/v1/sysuser/', '/0/2/3/45', 'F', 'PUT', 'system:sysuser:edit', 3, NULL, NULL, NULL, 0, '0', '1', '1', '0', '2020-04-11 15:52:48', NULL, NULL);
@@ -495,6 +496,7 @@ INSERT INTO sys_role_menu VALUES (2, 263, 'common', NULL, NULL);
INSERT INTO sys_role_menu VALUES (2, 267, 'common', NULL, NULL); INSERT INTO sys_role_menu VALUES (2, 267, 'common', NULL, NULL);
INSERT INTO sys_role_menu VALUES (1, 2, 'admin', NULL, NULL); INSERT INTO sys_role_menu VALUES (1, 2, 'admin', NULL, NULL);
INSERT INTO sys_role_menu VALUES (1, 3, 'admin', NULL, NULL); INSERT INTO sys_role_menu VALUES (1, 3, 'admin', NULL, NULL);
INSERT INTO sys_role_menu VALUES (1, 4, 'admin', NULL, NULL);
INSERT INTO sys_role_menu VALUES (1, 43, 'admin', NULL, NULL); INSERT INTO sys_role_menu VALUES (1, 43, 'admin', NULL, NULL);
INSERT INTO sys_role_menu VALUES (1, 44, 'admin', NULL, NULL); INSERT INTO sys_role_menu VALUES (1, 44, 'admin', NULL, NULL);
INSERT INTO sys_role_menu VALUES (1, 45, 'admin', NULL, NULL); INSERT INTO sys_role_menu VALUES (1, 45, 'admin', NULL, NULL);
+1 -1
View File
@@ -25,4 +25,4 @@ settings:
# source: host=myhost port=myport user=gorm dbname=gorm password=mypassword # source: host=myhost port=myport user=gorm dbname=gorm password=mypassword
gen: gen:
dbname: dbname dbname: dbname
frontpath: /go-admin-ui/src frontpath: /go-admin-ui/src # 代码生成是使用前端代码存放位置,需要指定到src文件夹,相对路径
+1 -1
View File
@@ -99,7 +99,7 @@ func (role *SysRole) GetList() (SysRole []SysRole, err error) {
func (role *SysRole) GetRoleMeunId() ([]int, error) { func (role *SysRole) GetRoleMeunId() ([]int, error) {
menuIds := make([]int, 0) menuIds := make([]int, 0)
menuList := make([]MenuIdList, 0) menuList := make([]MenuIdList, 0)
if err := orm.Eloquent.Table("sys_role_menu").Select("sys_role_menu.menu_id").Joins("LEFT JOIN sys_menu on sys_menu.menu_id=sys_role_menu.menu_id").Where("role_id = ? ", role.RoleId).Where(" sys_role_menu.menu_id not in(select sys_menu.parent_id from sys_role_menu LEFT JOIN sys_menu on sys_menu.menu_id=sys_role_menu.menu_id where role_id =? )", role.RoleId).Find(&menuList).Error; err != nil { if err := orm.Eloquent.Table("sys_role_menu").Select("sys_role_menu.menu_id").Where("role_id = ? ", role.RoleId).Where(" sys_role_menu.menu_id not in(select sys_menu.parent_id from sys_role_menu LEFT JOIN sys_menu on sys_menu.menu_id=sys_role_menu.menu_id where role_id =? and parent_id is not null)", role.RoleId).Find(&menuList).Error; err != nil {
return nil, err return nil, err
} }
+26
View File
@@ -0,0 +1,26 @@
package router
import (
"github.com/gin-gonic/gin"
"go-admin/middleware"
"go-admin/apis/{{.ModuleName}}"
jwt "go-admin/pkg/jwtauth"
)
// 需认证的路由代码
func register{{.ClassName}}Router(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
r := v1.Group("/{{.ModuleName}}").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
r.GET("/:{{.PkJsonField}}", {{.ModuleName}}.Get{{.ClassName}})
r.POST("", {{.ModuleName}}.Insert{{.ClassName}})
r.PUT("", {{.ModuleName}}.Update{{.ClassName}})
r.DELETE("/:{{.PkJsonField}}", {{.ModuleName}}.Delete{{.ClassName}})
}
l := v1.Group("").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
{
l.GET("/{{.ModuleName}}List",{{.ModuleName}}.Get{{.ClassName}}List)
}
}
+20
View File
@@ -0,0 +1,20 @@
package router
import (
"github.com/gin-gonic/gin"
"go-admin/apis/{{.ModuleName}}"
)
// 无需认证的路由代码
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}})
}
}
+2 -2
View File
@@ -160,7 +160,7 @@
</template> </template>
<script> <script>
import {add, del, get, list, update} from '@/api/' import {add{{.ClassName}}, del{{.ClassName}}, get{{.ClassName}}, list{{.ClassName}}, update{{.ClassName}}} from '@/api/{{ .PackageName}}'
export default { export default {
name: 'Config', name: 'Config',
@@ -183,7 +183,7 @@
isEdit: false, isEdit: false,
// 类型数据字典 // 类型数据字典
typeOptions: [], typeOptions: [],
{{.ModuleName}}List: [], list{{.ClassName}}: [],
{{range .Columns}} {{range .Columns}}
{{- if ne .DictType "" -}} {{- if ne .DictType "" -}}
{{.JsonField}}Options: [], {{.JsonField}}Options: [],
+4
View File
@@ -17,6 +17,10 @@ func StringToInt(e string) (int, error) {
return strconv.Atoi(e) return strconv.Atoi(e)
} }
func StringToBool(e string) (bool, error) {
return strconv.ParseBool(e)
}
func GetCurrentTimeStr() string { func GetCurrentTimeStr() string {
return time.Now().Format("2006/01/02 15:04:05") return time.Now().Format("2006/01/02 15:04:05")
} }