From 0dd5bb9379864ec7b2e1ab0b5ec68f6e9db78187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=BA=E4=BA=AE?= <927919732@qq.com> Date: Thu, 27 Jun 2024 14:25:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=A4=A7=E5=B0=8F=E5=86=99?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/go/controller/controller.go.tmpl | 5 +++-- template/vue/index.vue.tmpl | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/template/go/controller/controller.go.tmpl b/template/go/controller/controller.go.tmpl index 8258c57..c64157b 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/vue/index.vue.tmpl b/template/vue/index.vue.tmpl index deea9bd..a9c7962 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"}} -- Gitee From 7fa2bd6b7703c95d67633456604bee9d913d2b72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=BA=E4=BA=AE?= <927919732@qq.com> Date: Thu, 27 Jun 2024 15:21:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=8B=E8=BD=BDZip=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toolServiceImpl/genTableServiceImpl.go | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go b/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go index 1e3ff1e..b74edbf 100644 --- a/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go +++ b/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go @@ -110,13 +110,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 +124,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 { -- Gitee From 346d0a926c423be55b6f6b63766624c11ce1c5f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=BA=E4=BA=AE?= <927919732@qq.com> Date: Thu, 27 Jun 2024 16:21:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96SQl=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../toolServiceImpl/genTableServiceImpl.go | 6 +- app/business/tool/utils/genTable.go | 14 + template/sql/{sql.template => sql.sql.tmpl} | 20 +- template/sql/tmp | 247 ------------------ 4 files changed, 29 insertions(+), 258 deletions(-) rename template/sql/{sql.template => sql.sql.tmpl} (54%) delete mode 100644 template/sql/tmp diff --git a/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go b/app/business/tool/toolService/toolServiceImpl/genTableServiceImpl.go index b74edbf..7e90554 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" @@ -174,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 638ed9f..85681f3 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/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 b6a3056..e4bd663 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 5397019..0000000 --- a/template/sql/tmp +++ /dev/null @@ -1,247 +0,0 @@ - - -- Gitee