diff --git a/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go b/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go index 1e3ff1e23cc0630fd60b846daf6b9502136f58de..7e90554fbcc800f2cf27f1a1d0f143d8f7929f31 100644 --- a/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go +++ b/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go @@ -5,6 +5,7 @@ import ( "baize/app/business/tool/toolDao" "baize/app/business/tool/toolDao/toolDaoImpl" "baize/app/business/tool/toolModels" + genUtils "baize/app/business/tool/utils" "baize/app/utils/baizeContext" "baize/app/utils/snowflake" "baize/app/utils/zipUtils" @@ -110,13 +111,11 @@ func (genTabletService *GenTabletService) PreviewCode(c *gin.Context, tableId in func (genTabletService *GenTabletService) GenCode(c *gin.Context, tableId int64) []byte { // 创建一个内存缓冲区 buffer := new(bytes.Buffer) - // 创建一个新的 zip Writer zipWriter := zip.NewWriter(buffer) data := make(map[string]any) data["Table"] = genTabletService.genTabletDao.SelectGenTableById(c, genTabletService.data, tableId) data["Columns"] = genTabletService.genTabletColumnDao.SelectGenTableColumnListByTableId(c, genTabletService.data, tableId) - root := "./template/go/" var files []string err := filepath.Walk(root, visit(&files)) @@ -126,13 +125,30 @@ func (genTabletService *GenTabletService) GenCode(c *gin.Context, tableId int64) for _, file := range files { formattedCode, err := format.Source(genTabletService.loadTemplate("./"+file, data)) if err != nil { - panic(err) + fmt.Println(err) } if err := zipUtils.AddFileToZip(zipWriter, strings.TrimSuffix(strings.TrimPrefix(file, "template\\"), ".tmpl"), string(formattedCode)); err != nil { panic(err) } } + root = "./template/vue" + files = files[:0] + err = filepath.Walk(root, visit(&files)) + if err != nil { + panic(err) + } + for _, file := range files { + loadTemplate := genTabletService.loadTemplate("./"+file, data) + if err := zipUtils.AddFileToZip(zipWriter, strings.TrimSuffix(strings.TrimPrefix(file, "template\\"), ".tmpl"), string(loadTemplate)); err != nil { + panic(err) + } + } + // 关闭压缩包 + if err := zipWriter.Close(); err != nil { + panic(err) + } + // 将缓冲区的内容写入到返回的字节切片中 return buffer.Bytes() } func visit(files *[]string) filepath.WalkFunc { @@ -159,7 +175,10 @@ func (genTabletService *GenTabletService) loadTemplate(templateName string, data panic(err) } templateStr := string(b) - tmpl, err := template.New(templateName).Parse(templateStr) //建立一个模板,内容是"hello, {{.}}" + tmpl := template.New(templateName) + tmpl.Funcs(template.FuncMap{"Contains": genUtils.Contains, "CaseCamelLower": genUtils.CaseCamelLower, "HasSuffix": strings.HasSuffix}) + // 解析模板字符串 + tmpl, err = tmpl.Parse(templateStr) if err != nil { panic(err) } diff --git a/app/business/tool/utils/genTable.go b/app/business/tool/utils/genTable.go index 638ed9f040134c2fb2e41f77acc6da1cf55e702c..85681f31e2c37a2cec647b6adaef66d2f8c99117 100644 --- a/app/business/tool/utils/genTable.go +++ b/app/business/tool/utils/genTable.go @@ -1,6 +1,7 @@ package genUtils import ( + "regexp" "strconv" "strings" ) @@ -113,3 +114,16 @@ func CheckTypeColumn(columnName string) bool { } return false } + +// Contains 不区分大小写的Contains函数 +func Contains(s, substr string) bool { + return strings.Contains(strings.ToLower(s), strings.ToLower(substr)) +} + +// CaseCamelLower 将字符串转换为驼峰命名法的首字母小写形式 +func CaseCamelLower(s string) string { + re := regexp.MustCompile("([a-z0-9])([A-Z])") + return re.ReplaceAllStringFunc(s, func(in string) string { + return strings.ToLower(in[:1]) + in[1:] + }) +} diff --git a/template/go/controller/controller.go.tmpl b/template/go/controller/controller.go.tmpl index 8258c5791f68ab61ef364ff40c834ea078c4edc9..c64157b7c6243a616f49b9dca024609984d4b7c0 100644 --- a/template/go/controller/controller.go.tmpl +++ b/template/go/controller/controller.go.tmpl @@ -62,7 +62,8 @@ func ({{.Table.BusinessName}} *{{.Table.StructName}}) {{.Table.StructName}}Expor // @Param {{.IdField}} path string true "{{.IdField}}" // @Security BearerAuth // @Produce application/json -// @Success 200 {object} response.ResponseData{data={{.Table.ModuleName}}.{{.Table.BusinessName}}Vo} "成功" + +// @Success 200 {object} response.ResponseData{data={{.Table.ModuleName}}Models.{{.Table.StructName}}Vo}} "成功" // @Router /{{.Table.ModuleName}}/{{.Table.BusinessName}}/{{ "{" }}{{.IdField}}} [get] func ({{.Table.BusinessName}} *{{.Table.StructName}}) {{.Table.StructName}}GetInfo(c *gin.Context) { {{.IdField}} := baizeContext.ParamInt64(c, "{{.IdField}}") @@ -117,7 +118,7 @@ func ({{.Table.BusinessName}} *{{.Table.StructName}}) {{.Table.StructName}}Edit( // @Summary 删除{{.Table.FunctionName}} // @Description 删除{{.Table.FunctionName}} // @Tags {{.Table.FunctionName}}相关 -// @Param {{.IdField}} path []string true "{{.IdField}}" +// @Param {{.IdField}} path []string true "{{.IdField}}s" // @Security BearerAuth // @Produce application/json // @Success 200 {object} response.ResponseData "成功" diff --git a/template/sql/sql.template b/template/sql/sql.sql.tmpl similarity index 54% rename from template/sql/sql.template rename to template/sql/sql.sql.tmpl index b6a305609a647a18d766b881692c3a663c0bb605..e4bd6631686d637948ec0057574f7cb85131f0a8 100644 --- a/template/sql/sql.template +++ b/template/sql/sql.sql.tmpl @@ -1,12 +1,12 @@ /* ========================================================================== -生成日期:{{.table.CreateTime}} -生成人:{{.table.FunctionAuthor}} +生成日期:{{.GenerateTime.Format "2006-01-02 15:04:05" }} +生成人:{{.Table.FunctionAuthor}} ========================================================================== */ {{$plugin:=""}} -{{if ContainsI $.table.PackageName "plugins"}} +{{if Contains $.Table.PackageName "plugins"}} {{$plugin = "plugins/"}} {{end}} @@ -15,34 +15,34 @@ select @now := now(); -- 目录 SQL INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(0,'{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}','{{.table.FunctionName}}管理','form','','{{.table.FunctionName}}管理',0,0,1,1,'{{$plugin}}{{.table.BusinessName | CaseCamelLower}}','','',0,'sys_admin',0,@now,@now,NULL ); +VALUES(0,'{{$plugin}}{{.table.ModuleName}}/{{ CaseCamelLower .Table.BusinessName}}','{{.table.FunctionName}}管理','form','','{{.table.FunctionName}}管理',0,0,1,1,'{{$plugin}}{{CaseCamelLower .Table.BusinessName}}','','',0,'sys_admin',0,@now,@now,NULL ); -- 菜单父目录ID SELECT @parentId := LAST_INSERT_ID(); -- 菜单 SQL INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}/list','{{.table.FunctionName}}列表','list','','{{.table.FunctionName}}列表',1,0,1,1,'{{.table.BusinessName | CaseCamelLower}}List','','{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}/list',0,'sys_admin',0,@now,@now,NULL ); +VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/list','{{.table.FunctionName}}列表','list','','{{.table.FunctionName}}列表',1,0,1,1,'{{CaseCamelLower .Table.BusinessName}}List','','{{$plugin}}{{.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/list',0,'sys_admin',0,@now,@now,NULL ); -- 按钮父目录ID SELECT @parentId := LAST_INSERT_ID(); -- 按钮 SQL INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}/get','{{.table.FunctionName}}查询','','','{{.table.FunctionName}}查询',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); +VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/get','{{.table.FunctionName}}查询','','','{{.table.FunctionName}}查询',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}/add','{{.table.FunctionName}}添加','','','{{.table.FunctionName}}添加',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); +VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/add','{{.table.FunctionName}}添加','','','{{.table.FunctionName}}添加',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}/edit','{{.table.FunctionName}}修改','','','{{.table.FunctionName}}修改',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); +VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/edit','{{.table.FunctionName}}修改','','','{{.table.FunctionName}}修改',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{.table.BusinessName | CaseCamelLower}}/delete','{{.table.FunctionName}}删除','','','{{.table.FunctionName}}删除',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); +VALUES(@parentId,'{{$plugin}}{{.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/delete','{{.table.FunctionName}}删除','','','{{.table.FunctionName}}删除',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); {{range $index,$column:= .table.Columns}} {{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }} INSERT INTO `sys_auth_rule` (`pid`,`name`,`title`,`icon`,`condition`,`remark`,`menu_type`,`weigh`,`status`,`always_show`,`path`,`jump_path`,`component`,`is_frame`,`module_type`,`model_id`,`created_at`,`updated_at`,`deleted_at` ) -VALUES(@parentId,'{{$plugin}}{{$.table.ModuleName}}/{{$.table.BusinessName | CaseCamelLower}}/change{{$column.GoField}}','{{$.table.FunctionName}}{{$column.ColumnComment}}修改','','','{{$.table.FunctionName}}{{$column.ColumnComment}}修改',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); +VALUES(@parentId,'{{$plugin}}{{$.table.ModuleName}}/{{CaseCamelLower .Table.BusinessName}}/change{{$column.GoField}}','{{$.table.FunctionName}}{{$column.ColumnComment}}修改','','','{{$.table.FunctionName}}{{$column.ColumnComment}}修改',2,0,1,1,'','','',0,'sys_admin',0,@now,@now,NULL ); {{end}} {{end}} diff --git a/template/sql/tmp b/template/sql/tmp deleted file mode 100644 index 5397019d6855fbbba56c0431aad94a27b05c7494..0000000000000000000000000000000000000000 --- a/template/sql/tmp +++ /dev/null @@ -1,247 +0,0 @@ - - diff --git a/template/vue/index.vue.tmpl b/template/vue/index.vue.tmpl index deea9bdde8681ffca24f1e9c96fcd880d7b31f2c..a9c79629461aa1d369adac81df5596c48060338e 100644 --- a/template/vue/index.vue.tmpl +++ b/template/vue/index.vue.tmpl @@ -16,7 +16,7 @@ {{else}} @@ -132,11 +132,11 @@ pk {{range $index, $column := .Columns}} {{if ne $column.IsPk "1"}} -{{if ne $column.htmlType "input"}} +{{if ne $column.HtmlType "input"}} -{{else if ne $column.htmlType "select"}} +{{else if ne $column.HtmlType "select"}} -{{else if ne $column.htmlType "datetime"}} +{{else if ne $column.HtmlType "datetime"}} -{{else if ne $column.htmlType "textarea"}} +{{else if ne $column.HtmlType "textarea"}}