From 61f73cbffe4d01fbc7172bfb552d2a1ecc4c3bb8 Mon Sep 17 00:00:00 2001 From: pixelmaxQM Date: Mon, 18 Nov 2024 21:27:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=A0=91=E5=BD=A2?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=87=AA=E5=8A=A8=E5=8C=96=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/resource/package/web/view/form.vue.tpl | 11 +- .../resource/package/web/view/table.vue.tpl | 12 +- .../plugin/server/api/api.go.template | 23 +- .../plugin/server/model/model.go.template | 20 +- .../plugin/server/service/service.go.template | 21 +- .../plugin/web/form/form.vue.template | 65 +++- .../plugin/web/view/view.vue.template | 291 +++++++++++------- 7 files changed, 307 insertions(+), 136 deletions(-) diff --git a/server/resource/package/web/view/form.vue.tpl b/server/resource/package/web/view/form.vue.tpl index a49f27edac..33581c7ed5 100644 --- a/server/resource/package/web/view/form.vue.tpl +++ b/server/resource/package/web/view/form.vue.tpl @@ -169,7 +169,7 @@ getDataSourceFunc() { const table = await get{{.StructName}}List() if (table.code === 0) { diff --git a/server/resource/package/web/view/table.vue.tpl b/server/resource/package/web/view/table.vue.tpl index 2cf95a6db0..35dfd1b2b3 100644 --- a/server/resource/package/web/view/table.vue.tpl +++ b/server/resource/package/web/view/table.vue.tpl @@ -540,7 +540,7 @@ getDataSourceFunc() > {{ if .GvaModel }} - + {{ end }} @@ -668,7 +668,7 @@ getDataSourceFunc() { const table = await get{{.StructName}}List() diff --git a/server/resource/plugin/server/api/api.go.template b/server/resource/plugin/server/api/api.go.template index edf4651801..bd6a5f27a6 100644 --- a/server/resource/plugin/server/api/api.go.template +++ b/server/resource/plugin/server/api/api.go.template @@ -5,7 +5,9 @@ import ( "{{.Module}}/global" "{{.Module}}/model/common/response" "{{.Module}}/plugin/{{.Package}}/model" + {{- if not .IsTree}} "{{.Module}}/plugin/{{.Package}}/model/request" + {{- end }} "github.com/gin-gonic/gin" "go.uber.org/zap" {{- if .AutoCreateResource}} @@ -142,6 +144,25 @@ func (a *{{.Abbreviation}}) Find{{.StructName}}(c *gin.Context) { response.OkWithData(re{{.Abbreviation}}, c) } +{{- if .IsTree }} +// Get{{.StructName}}List 分页获取{{.Description}}列表 +// @Tags {{.StructName}} +// @Summary 分页获取{{.Description}}列表 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功" +// @Router /{{.Abbreviation}}/get{{.StructName}}List [get] +func (a *{{.Abbreviation}}) Get{{.StructName}}List(c *gin.Context) { + list, err := service{{ .StructName }}.Get{{.StructName}}InfoList() + if err != nil { + global.GVA_LOG.Error("获取失败!", zap.Error(err)) + response.FailWithMessage("获取失败:" + err.Error(), c) + return + } + response.OkWithDetailed(list, "获取成功", c) +} +{{- else }} // Get{{.StructName}}List 分页获取{{.Description}}列表 // @Tags {{.StructName}} // @Summary 分页获取{{.Description}}列表 @@ -171,6 +192,7 @@ func (a *{{.Abbreviation}}) Get{{.StructName}}List(c *gin.Context) { PageSize: pageInfo.PageSize, }, "获取成功", c) } +{{- end }} {{- if .HasDataSource }} // Get{{.StructName}}DataSource 获取{{.StructName}}的数据源 @@ -197,7 +219,6 @@ func (a *{{.Abbreviation}}) Get{{.StructName}}DataSource(c *gin.Context) { // @Summary 不需要鉴权的{{.Description}}接口 // @accept application/json // @Produce application/json -// @Param data query request.{{.StructName}}Search true "分页获取{{.Description}}列表" // @Success 200 {object} response.Response{data=object,msg=string} "获取成功" // @Router /{{.Abbreviation}}/get{{.StructName}}Public [get] func (a *{{.Abbreviation}}) Get{{.StructName}}Public(c *gin.Context) { diff --git a/server/resource/plugin/server/model/model.go.template b/server/resource/plugin/server/model/model.go.template index edf8a59cfa..503eb0a36e 100644 --- a/server/resource/plugin/server/model/model.go.template +++ b/server/resource/plugin/server/model/model.go.template @@ -72,8 +72,8 @@ type {{.StructName}} struct { DeletedBy uint `gorm:"column:deleted_by;comment:删除者"` {{- end }} {{- if .IsTree }} - Children []{{.StructName}} `json:"children"` - ParentId int `json:"parentId" gorm:"column:parent_id;comment:父节点ID"` + Children []{{.StructName}} `json:"children" gorm:"-"` //子节点 + ParentID int `json:"parentID" gorm:"column:parent_id;comment:父节点"` {{- end }} {{- end }} } @@ -88,23 +88,23 @@ func ({{.StructName}}) TableName() string { {{ if .IsTree }} // GetChildren 实现TreeNode接口 -func ({{.StructName}}) GetChildren() []{{.StructName}} { - return {{.StructName}}.Children +func (s {{.StructName}}) GetChildren() []{{.StructName}} { + return s.Children } // SetChildren 实现TreeNode接口 -func ({{.StructName}}) SetChildren(children []{{.StructName}}) { - {{.StructName}}.Children = children +func (s {{.StructName}}) SetChildren(in *{{.StructName}},children {{.StructName}}) { + in.Children = append(in.Children, children) } // GetID 实现TreeNode接口 -func ({{.StructName}}) GetID() int { - return {{.StructName}}.ID +func (s {{.StructName}}) GetID() int { + return int({{if not .GvaModel}}*{{- end }}s.{{.PrimaryField.FieldName}}) } // GetParentID 实现TreeNode接口 -func ({{.StructName}}) GetParentID() int { - return {{.StructName}}.ParentId +func (s {{.StructName}}) GetParentID() int { + return s.ParentID } {{ end }} diff --git a/server/resource/plugin/server/service/service.go.template b/server/resource/plugin/server/service/service.go.template index 4b1c814ea2..60f63bd6b4 100644 --- a/server/resource/plugin/server/service/service.go.template +++ b/server/resource/plugin/server/service/service.go.template @@ -60,10 +60,15 @@ import ( {{- if not .OnlyTemplate }} "{{.Module}}/global" "{{.Module}}/plugin/{{.Package}}/model" + {{- if not .IsTree }} "{{.Module}}/plugin/{{.Package}}/model/request" + {{- end }} {{- if .AutoCreateResource }} "gorm.io/gorm" {{- end}} +{{- if .IsTree }} + "{{.Module}}/utils" +{{- end }} {{- end }} ) @@ -137,6 +142,20 @@ func (s *{{.Abbreviation}}) Get{{.StructName}}({{.PrimaryField.FieldJson}} strin return } + +{{- if .IsTree }} +// Get{{.StructName}}InfoList 分页获取{{.Description}}记录,Tree模式下不添加分页和搜索 +// Author [yourname](https://github.com/yourname) +func (s *{{.Abbreviation}}) Get{{.StructName}}InfoList() (list []model.{{.StructName}},err error) { + // 创建db + db := {{$db}}.Model(&model.{{.StructName}}{}) + var {{.Abbreviation}}s []model.{{.StructName}} + + err = db.Find(&{{.Abbreviation}}s).Error + + return utils.BuildTree({{.Abbreviation}}s), err +} +{{- else }} // Get{{.StructName}}InfoList 分页获取{{.Description}}记录 // Author [yourname](https://github.com/yourname) func (s *{{.Abbreviation}}) Get{{.StructName}}InfoList(info request.{{.StructName}}Search) (list []model.{{.StructName}}, total int64, err error) { @@ -199,7 +218,7 @@ func (s *{{.Abbreviation}}) Get{{.StructName}}InfoList(info request.{{.StructNam err = db.Find(&{{.Abbreviation}}s).Error return {{.Abbreviation}}s, total, err } - +{{- end }} {{- if .HasDataSource }} func (s *{{.Abbreviation}})Get{{.StructName}}DataSource() (res map[string][]map[string]any, err error) { res = make(map[string][]map[string]any) diff --git a/server/resource/plugin/web/form/form.vue.template b/server/resource/plugin/web/form/form.vue.template index 2c221d912b..6726ee41a0 100644 --- a/server/resource/plugin/web/form/form.vue.template +++ b/server/resource/plugin/web/form/form.vue.template @@ -148,6 +148,7 @@ const {{ $element }}Options = ref([]) {{- if .HasDataSource }} // 请引用 get{{.StructName}}DataSource, + // 获取数据源 const dataSource = ref([]) const getDataSourceFunc = async()=>{ @@ -159,13 +160,27 @@ const getDataSourceFunc = async()=>{ getDataSourceFunc() {{- end }} {{- else }} -{{- if not .OnlyTemplate}} +{{- if not .OnlyTemplate }}