From 98f12a4ab64e7c7ee023cd061834c250b080243e Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Wed, 25 Mar 2020 18:07:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QMPlusServer/model/autoCodeModel/autoCode.go | 77 ++++++++++++++++++- .../model/autoCodeModel/autoCode_test.go | 7 ++ QMPlusServer/tpl/te/struct.go.tpl | 36 +++++++++ .../autoCode/component/componentDialog.vue | 3 + .../src/view/superAdmin/autoCode/index.vue | 6 +- 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 QMPlusServer/model/autoCodeModel/autoCode_test.go create mode 100644 QMPlusServer/tpl/te/struct.go.tpl diff --git a/QMPlusServer/model/autoCodeModel/autoCode.go b/QMPlusServer/model/autoCodeModel/autoCode.go index 7d4f0ff5..dd2cf8d7 100644 --- a/QMPlusServer/model/autoCodeModel/autoCode.go +++ b/QMPlusServer/model/autoCodeModel/autoCode.go @@ -1,14 +1,24 @@ package autoCodeModel +import ( + "fmt" + log "gin-vue-admin/init/initlog" + "html/template" + "os" +) + //开发中功能,若您发现这块代码可以研究,可以无视 type AutoCodeStruct struct { - StructName string `json:"structName"` - Components []Component `json:"components"` + StructName string `json:"structName"` + PackageName string `json:"packageName"` + Abbreviation string `json:"abbreviation"` + Components []Component `json:"components"` } type Component struct { ComponentName string `json:"componentName"` ComponentType string `json:"componentType"` + ComponentJson string `json:"componentJson"` Ismultiple bool `json:"isMultiple"` ComponentShowType string `json:"componentShowType"` NideDictionary bool `json:"nideDictionary"` @@ -20,3 +30,66 @@ type Dictionary struct { Label string `json:"label"` Value string `json:"value"` } + +func Temp() { + tmpl, err := template.ParseFiles("../../tpl/te/struct.go.tpl") + fmt.Println(tmpl, err) + a1 := Component{ + ComponentName: "TestComponent", + ComponentType: "string", + ComponentJson: "testComponent", + Ismultiple: false, + ComponentShowType: "", + NideDictionary: false, + DictionaryName: "", + ComponentDictionary: nil, + } + a2 := Component{ + ComponentName: "TestBigComponent", + ComponentType: "int", + ComponentJson: "testBigComponent", + Ismultiple: false, + ComponentShowType: "", + NideDictionary: false, + DictionaryName: "", + ComponentDictionary: nil, + } + a := AutoCodeStruct{ + StructName: "Test", + PackageName: "autocode", + Components: []Component{a1, a2}, + } + + _dir := "../" + a.PackageName + exist, err := pathExists(_dir) + if err != nil { + log.L.Info("get dir error![%v]\n", err) + return + } + if exist { + log.L.Info("has dir![%v]\n", _dir) + } else { + log.L.Info("no dir![%v]\n", _dir) + // 创建文件夹 + err := os.Mkdir(_dir, os.ModePerm) + if err != nil { + log.L.Error("mkdir failed![%v]\n", err) + } else { + log.L.Info("mkdir success!\n") + } + } + file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755) + err = tmpl.Execute(file, a) +} + +// 判断文件夹是否存在 +func pathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} diff --git a/QMPlusServer/model/autoCodeModel/autoCode_test.go b/QMPlusServer/model/autoCodeModel/autoCode_test.go new file mode 100644 index 00000000..8423e68f --- /dev/null +++ b/QMPlusServer/model/autoCodeModel/autoCode_test.go @@ -0,0 +1,7 @@ +package autoCodeModel + +import "testing" + +func TestTemp(t *testing.T) { + Temp() +} diff --git a/QMPlusServer/tpl/te/struct.go.tpl b/QMPlusServer/tpl/te/struct.go.tpl new file mode 100644 index 00000000..29014100 --- /dev/null +++ b/QMPlusServer/tpl/te/struct.go.tpl @@ -0,0 +1,36 @@ +// 自动生成模板{{.StructName}} +package {{.PackageName}} + +import ( + "gin-vue-admin/controller/servers" + "gin-vue-admin/init/qmsql" + "gin-vue-admin/model/modelInterface" + "github.com/jinzhu/gorm" + "github.com/pkg/errors" +) + +type {{.StructName}} struct { + gorm.Model {{range .Components}} + {{.ComponentName}} {{.ComponentType}} `json:"{{.ComponentJson}}"` {{ end }} +} + +// 创建{{.StructName}} +func ({{.Abbreviation}} *{{StructName}})Create{{.StructName}}()(err error){ + err = qmsql.DEFAULTDB.Create({{.Abbreviation}}).Error + return err +} + +// 删除{{.StructName}} +func ({{.Abbreviation}} *{{StructName}})Delete{{.StructName}}()(err error){ + err = qmsql.DEFAULTDB.Delete({{.Abbreviation}}).Error + return err +} + +// 更新{{.StructName}} +func ({{.Abbreviation}} *{{StructName}})Update{{.StructName}}()(err error, re{{.Abbreviation}} {{.StructName}}){ + +} + +func ({{.Abbreviation}} *{{StructName}})Create(){ + +} \ No newline at end of file diff --git a/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue b/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue index be83695c..efd4502f 100644 --- a/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue +++ b/QMPlusVuePage/src/view/superAdmin/autoCode/component/componentDialog.vue @@ -4,6 +4,9 @@ + + + + + +