Skip to content

Commit

Permalink
feat: 优化树形结构自动化模板
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmaxQm committed Nov 18, 2024
1 parent 5bd79ea commit 61f73cb
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 136 deletions.
11 changes: 10 additions & 1 deletion server/resource/package/web/view/form.vue.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ getDataSourceFunc()
<el-form-item label="父节点:" prop="parentID" >
<el-tree-select
v-model="formData.parentID"
:data="tableData"
:data="[rootNode,...tableData]"
check-strictly
:render-after-expand="false"
:props="defaultProps"
Expand Down Expand Up @@ -253,6 +253,9 @@ import {
{{- if .HasDataSource }}
get{{.StructName}}DataSource,
{{- end }}
{{- if .IsTree }}
get{{.StructName}}List,
{{- end }}
create{{.StructName}},
update{{.StructName}},
find{{.StructName}}
Expand Down Expand Up @@ -300,6 +303,12 @@ const defaultProps = {
value: "{{ .PrimaryField.FieldJson }}"
}
const rootNode = {
{{ .PrimaryField.FieldJson }}: 0,
{{ .TreeJson }}: '根节点',
children: []
}
const getTableData = async() => {
const table = await get{{.StructName}}List()
if (table.code === 0) {
Expand Down
12 changes: 9 additions & 3 deletions server/resource/package/web/view/table.vue.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ getDataSourceFunc()
>
<el-table-column type="selection" width="55" />
{{ if .GvaModel }}
<el-table-column align="left" label="日期" prop="createdAt" {{- if .IsTree }}min-{{- end }}width="180">
<el-table-column align="left" label="日期" prop="createdAt" {{- if .IsTree }} min-{{- end }}width="180">
<template #default="scope">{{ "{{ formatDate(scope.row.CreatedAt) }}" }}</template>
</el-table-column>
{{ end }}
Expand Down Expand Up @@ -668,7 +668,7 @@ getDataSourceFunc()
<el-form-item label="父节点:" prop="parentID" >
<el-tree-select
v-model="formData.parentID"
:data="tableData"
:data="[rootNode,...tableData]"
check-strictly
:render-after-expand="false"
:props="defaultProps"
Expand Down Expand Up @@ -757,7 +757,7 @@ getDataSourceFunc()
<el-descriptions-item label="父节点">
<el-tree-select
v-model="detailFrom.parentID"
:data="tableData"
:data="[rootNode,...tableData]"
check-strictly
disabled
:render-after-expand="false"
Expand Down Expand Up @@ -1086,6 +1086,12 @@ const defaultProps = {
value: "{{ .PrimaryField.FieldJson }}"
}
const rootNode = {
{{ .PrimaryField.FieldJson }}: 0,
{{ .TreeJson }}: '根节点',
children: []
}
// 查询
const getTableData = async() => {
const table = await get{{.StructName}}List()
Expand Down
23 changes: 22 additions & 1 deletion server/resource/plugin/server/api/api.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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}}列表
Expand Down Expand Up @@ -171,6 +192,7 @@ func (a *{{.Abbreviation}}) Get{{.StructName}}List(c *gin.Context) {
PageSize: pageInfo.PageSize,
}, "获取成功", c)
}
{{- end }}

{{- if .HasDataSource }}
// Get{{.StructName}}DataSource 获取{{.StructName}}的数据源
Expand All @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions server/resource/plugin/server/model/model.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
Expand All @@ -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 }}

Expand Down
21 changes: 20 additions & 1 deletion server/resource/plugin/server/service/service.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
)

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
65 changes: 58 additions & 7 deletions server/resource/plugin/web/form/form.vue.template
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ const {{ $element }}Options = ref([])
{{- if .HasDataSource }}
// 请引用
get{{.StructName}}DataSource,

// 获取数据源
const dataSource = ref([])
const getDataSourceFunc = async()=>{
Expand All @@ -159,13 +160,27 @@ const getDataSourceFunc = async()=>{
getDataSourceFunc()
{{- end }}
{{- else }}
{{- if not .OnlyTemplate}}
{{- if not .OnlyTemplate }}
<template>
<div>
<div class="gva-form-box">
<el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
{{- if .IsTree }}
<el-form-item label="父节点:" prop="parentID" >
<el-tree-select
v-model="formData.parentID"
:data="[rootNode,...tableData]"
check-strictly
:render-after-expand="false"
:props="defaultProps"
clearable
style="width: 240px"
placeholder="根节点"
/>
</el-form-item>
{{- end }}
{{- range .Fields}}
{{- if .Form }}
{{- if .Form }}
<el-form-item label="{{.FieldDesc}}:" prop="{{.FieldJson}}">
{{- if .CheckDataSource}}
<el-select {{if eq .DataSource.Association 2}} multiple {{ end }} v-model="formData.{{.FieldJson}}" placeholder="请选择{{.FieldDesc}}" style="width:100%" :clearable="{{.Clearable}}" >
Expand Down Expand Up @@ -219,13 +234,13 @@ getDataSourceFunc()
{{- end }}
{{- if eq .FieldType "array" }}
<ArrayCtrl v-model="formData.{{ .FieldJson }}" editable/>
{{- end }}
{{- end }}
{{- end }}
</el-form-item>
{{- end }}
{{- end }}
<el-form-item>
<el-button type="primary" @click="save">保存</el-button>
<el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
<el-button type="primary" @click="back">返回</el-button>
</el-form-item>
</el-form>
Expand All @@ -238,6 +253,9 @@ import {
{{- if .HasDataSource }}
get{{.StructName}}DataSource,
{{- end }}
{{- if .IsTree }}
get{{.StructName}}List,
{{- end }}
create{{.StructName}},
update{{.StructName}},
find{{.StructName}}
Expand Down Expand Up @@ -272,14 +290,47 @@ import RichEdit from '@/components/richtext/rich-edit.vue'
import ArrayCtrl from '@/components/arrayCtrl/arrayCtrl.vue'
{{- end }}


const route = useRoute()
const router = useRouter()

{{- if .IsTree }}
const tableData = ref([])

const defaultProps = {
children: "children",
label: "{{ .TreeJson }}",
value: "{{ .PrimaryField.FieldJson }}"
}

const rootNode = {
{{ .PrimaryField.FieldJson }}: 0,
{{ .TreeJson }}: '根节点',
children: []
}

const getTableData = async() => {
const table = await get{{.StructName}}List()
if (table.code === 0) {
tableData.value = table.data
}
}

getTableData()

{{- end }}

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
{{- range $index, $element := .DictTypes}}
const {{ $element }}Options = ref([])
{{- end }}
const formData = ref({
{{- if .IsTree }}
parentID: undefined,
{{- end }}
{{- range .Fields}}
{{- if .Form }}
{{- if eq .FieldType "bool" }}
Expand Down Expand Up @@ -324,15 +375,13 @@ const formData = ref({
// 验证规则
const rule = reactive({
{{- range .Fields }}
{{- if .Form }}
{{- if eq .Require true }}
{{.FieldJson }} : [{
required: true,
message: '{{ .ErrorText }}',
trigger: ['input','blur'],
}],
{{- end }}
{{- end }}
{{- end }}
})

Expand Down Expand Up @@ -369,8 +418,9 @@ const init = async () => {
init()
// 保存按钮
const save = async() => {
btnLoading.value = true
elFormRef.value?.validate( async (valid) => {
if (!valid) return
if (!valid) return btnLoading.value = false
let res
switch (type.value) {
case 'create':
Expand All @@ -383,6 +433,7 @@ const save = async() => {
res = await create{{.StructName}}(formData.value)
break
}
btnLoading.value = false
if (res.code === 0) {
ElMessage({
type: 'success',
Expand Down
Loading

0 comments on commit 61f73cb

Please sign in to comment.