From d6c11a5d93d12f7eac8f394f4399e8067f3de9ea Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Fri, 4 Jun 2021 03:03:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E7=BB=8F=E6=8E=A5=E5=85=A5=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=8C=E5=BC=80=E5=A7=8B=E5=87=86?= =?UTF-8?q?=E5=A4=87=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/makecode.go | 208 +++++++++++++++++++++++++++++++++++++ example/admin/user.go | 2 + example/config/app.json | 151 +++++++++++++++++++-------- example/config/config.json | 5 +- 4 files changed, 317 insertions(+), 49 deletions(-) create mode 100644 code/makecode.go diff --git a/code/makecode.go b/code/makecode.go new file mode 100644 index 0000000..00e2bf4 --- /dev/null +++ b/code/makecode.go @@ -0,0 +1,208 @@ +package code + +import ( + . "../common" + "../db" + "bytes" + "encoding/json" + "errors" + "io/ioutil" + "net/http" + "os" + "path/filepath" +) + +type MakeCode struct { + TableConfig Map + TableColumns map[string]map[string]Map + Config Map + Error +} + +func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) { + if that.TableColumns == nil { + that.TableColumns = make(map[string]map[string]Map) + } + + //加载配置文件 + btes, err := ioutil.ReadFile(path) + that.Config = DeepCopyMap(Config).(Map) + if err == nil { + cmap := Map{} + //文件是否损坏 + cmap.JsonToMap(string(btes), &that.Error) + for k, v := range cmap { + that.Config[k] = v //程序配置 + Config[k] = v //系统配置 + } + } else { + that.Error.SetError(errors.New("配置文件不存在,或者配置出错,使用缺省默认配置")) + } + tables := that.Config.GetSlice("tables") + that.TableConfig = Map{} + for k, _ := range tables { + tableName := tables.GetMap(k).GetString("table") + that.TableConfig[tableName] = tables.GetMap(k) + columns := tables.GetMap(k).GetSlice("columns") + //初始化 + if that.TableColumns[tableName] == nil { + that.TableColumns[tableName] = map[string]Map{} + } + //注入源数据 + for k, _ := range columns { + columnsName := columns.GetMap(k).GetString("name") + that.TableColumns[tableName][columnsName] = columns.GetMap(k) + } + + } + + if db.Type == "sqlite" { + tables := db.Select("sqlite_sequence", "name") + + for _, v := range tables { + if that.TableConfig.GetMap(v.GetString("name")) == nil { + that.TableConfig[v.GetString("name")] = Map{ + "label": "备注", + "table": v.GetString("name"), + "auth": []string{"add", "delete", "edit", "info"}, + "columns": []Map{}, + "search": []Map{ + //{"type": "tree", "name": "oid", "label": "组织", "table": "organization", "showName": "label", "children": "children"}, + {"type": "text", "name": "keyword", "label": "请输入关键词", "value": nil}, + {"type": "date", "name": "date", "label": "时间段", "value": nil}, + {"type": "select", "name": "state", "label": "状态", "value": nil, + "option": []Map{ + {"name": "正常", "value": 0}, + {"name": "异常", "value": 1}, + {"name": "全部", "value": nil}, + }, + }, + }, + } + } + //初始化 + if that.TableColumns[v.GetString("name")] == nil { + that.TableColumns[v.GetString("name")] = make(map[string]Map) + } + tableInfo := db.Query("pragma table_info([" + v.GetString("name") + "]);") + for _, info := range tableInfo { + coloums := that.TableColumns[v.GetString("name")][info.GetString("name")] + if coloums == nil { + coloums = Map{ + "name": info.GetString("name"), + "type": info.GetString("type"), + "label": "备注", + //"add": false, "info": false, "edit": false, "list": true, + "must": false, + } + } else { + //coloums["name"]=info.GetString("name") + coloums["type"] = info.GetString("type") + } + + that.TableColumns[v.GetString("name")][info.GetString("name")] = coloums + + } + + } + newTables := []Map{} + for k, _ := range that.TableConfig { + newTable := that.TableConfig.GetMap(k) + newTables = append(newTables, newTable) + columns := []Map{} + for _, v := range that.TableColumns[newTable.GetString("table")] { + columns = append(columns, v) + } + newTable["columns"] = columns + } + that.Config["tables"] = newTables + } + //写入配置文件 + var configByte bytes.Buffer + + err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t") + //判断配置文件是否序列有变化,有则修改配置,无则不变 + //fmt.Println(len(btes)) + if len(btes) != 0 && configByte.String() == string(btes) { + return + } + _ = os.MkdirAll(filepath.Dir(path), os.ModeDir) + err = ioutil.WriteFile(path, configByte.Bytes(), os.ModeAppend) + if err != nil { + that.Error.SetError(err) + } +} +func (that *MakeCode) Info(table string) string { + reStr := "" + for _, v := range that.TableColumns[table] { + if v.Get("info") == nil || v.GetBool("info") { + reStr += v.GetString("name") + "," + } + } + if len(reStr) != 0 { + reStr = reStr[:len(reStr)-1] + } + return reStr +} +func (that *MakeCode) Add(table string, req *http.Request) Map { + data := Map{} + for _, v := range that.TableColumns[table] { + if v.Get("add") == nil || v.GetBool("add") { + reqValue := req.FormValue(v.GetString("name")) + if reqValue == "" { + return nil + } + data[v.GetString("name")] = reqValue + } + } + if len(data) == 0 { + return nil + } + + return data +} +func (that *MakeCode) Edit(table string, req *http.Request) Map { + data := Map{} + for _, v := range that.TableColumns[table] { + if v.Get("edit") == nil || v.GetBool("edit") { + reqValue := req.FormValue(v.GetString("name")) + if reqValue == "" { + continue + } + data[v.GetString("name")] = reqValue + } + } + + if len(data) == 0 { + return nil + } + + return data +} + +func (that *MakeCode) Search(table string, req *http.Request) (string, Map) { + reStr := "" + for _, v := range that.TableColumns[table] { + if v.Get("list") == nil || v.GetBool("list") { + reStr += v.GetString("name") + "," + } + } + if len(reStr) != 0 { + reStr = reStr[:len(reStr)-1] + } + + data := Map{} + search := that.TableConfig.GetSlice("search") + for k, _ := range search { + + reqValue := req.FormValue(search.GetMap(k).GetString("name")) + if reqValue == "" { + continue + } + + data[search.GetMap(k).GetString("name")] = reqValue + + } + + return reStr, data +} diff --git a/example/admin/user.go b/example/admin/user.go index c2418be..d774457 100644 --- a/example/admin/user.go +++ b/example/admin/user.go @@ -3,11 +3,13 @@ package admin import ( . "../../../hotime" . "../../../hotime/common" + "fmt" ) var UserCtr = Ctr{ "info": func(that *Context) { re := that.Db.Get(that.RouterString[1], that.MakeCode.Info(that.RouterString[1]), Map{"id": that.RouterString[2]}) + fmt.Println(that.Db.LastQuery) that.Display(0, re) }, "add": func(that *Context) { diff --git a/example/config/app.json b/example/config/app.json index f7eda70..562a997 100644 --- a/example/config/app.json +++ b/example/config/app.json @@ -1,46 +1,107 @@ { - "name":"HoTimeDashBoard", - "ID": "2f92h3herh23rh2y8", - "label":"HoTime管理平台", - "menu":[ - {"label":"平台首页", "name":"HelloWorld", "icon": "el-icon-s-home"}, - {"label":"测试表格", "table":"table", "icon": "el-icon-suitcase"}, - {"label":"系统管理", "name":"setting","icon": "el-icon-setting", - "menu":[ - {"label":"用户管理", "table":"user"}, - {"label":"组织管理", "table":"organization"}, - {"label":"角色管理", "table":"role"}, - {"label":"系统设置", "table":"system", "default": "edit"} - ] - } - ], - "tables": [ - {"label":"用户管理", - "table":"user", - "auth":["add", "delete", "edit", "info"], - "columns": [ - {"name": "id", "type": "int", "label": "ID","add":false, "info": false, "edit": false, "list": true,"must": false}, - {"name": "password", "type": "password", "label": "密码","add":true, "info": true, "edit": true, "list": false, "must": true}, - {"name": "date", "type": "date", "label": "注册日期", "add":true, "info": true, "edit": true, "list": true, "must": true,"sortable": true}, - {"name": "role_id", "type": "text", "label": "角色","value": "role.name", "link": "role","add":true, "info": true, "edit": true, "list": true, "must": true}, - {"name": "organization_id","link": "organization", "type": "checkbox","value": "organization.name", - "label": "组织","add":true, "info": true, "edit": true, "list": true,"must": true}, - {"name": "state", "type": "state", "label": "状态","add":true, "info": true, "edit": true, "list": true, "must": true, - "option":[ - {"name":"正常", "value":0}, - {"name":"异常", "value":1}, - {"name":"全部", "value":null}]} - ], - "search":[ - {"type": "tree", "name": "oid","label": "组织","table": "organization","showName": "label","children": "children"}, - {"type": "text", "name":"keyword", "label":"请输入关键词", "value": null}, - {"type": "date", "name":"date", "label":"时间段", "value": null}, - {"type": "select", "name":"state", "label":"状态", "value": null, - "option":[ - {"name":"正常", "value":0}, - {"name":"异常", "value":1}, - {"name":"全部", "value":null}]} - ] - } - ] -} + "ID": "2f92h3herh23rh2y8", + "label": "HoTime管理平台", + "menu": [ + { + "icon": "el-icon-s-home", + "label": "平台首页", + "name": "HelloWorld" + }, + { + "icon": "el-icon-suitcase", + "label": "测试表格", + "table": "table" + }, + { + "icon": "el-icon-setting", + "label": "系统管理", + "menu": [ + { + "label": "用户管理", + "table": "user" + }, + { + "label": "组织管理", + "table": "organization" + }, + { + "label": "角色管理", + "table": "role" + }, + { + "default": "edit", + "label": "系统设置", + "table": "system" + } + ], + "name": "setting" + } + ], + "name": "HoTimeDashBoard", + "tables": [ + { + "auth": [ + "add", + "delete", + "edit", + "info" + ], + "columns": [ + { + "label": "备注", + "must": false, + "name": "id", + "type": "integer" + }, + { + "label": "备注", + "must": false, + "name": "name", + "type": "text" + }, + { + "label": "备注", + "must": false, + "name": "age", + "type": "int" + } + ], + "label": "备注", + "search": [ + { + "label": "请输入关键词", + "name": "keyword", + "type": "text", + "value": null + }, + { + "label": "时间段", + "name": "date", + "type": "date", + "value": null + }, + { + "label": "状态", + "name": "state", + "option": [ + { + "name": "正常", + "value": 0 + }, + { + "name": "异常", + "value": 1 + }, + { + "name": "全部", + "value": null + } + ], + "type": "select", + "value": null + } + ], + "table": "user" + } + ] +} \ No newline at end of file diff --git a/example/config/config.json b/example/config/config.json index 53eb4a8..4e1bd16 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -7,7 +7,7 @@ } }, "codeConfig": { - "admin": "config/app.json" + "admin": "example/config/app.json" }, "db": { "sqlite": { @@ -18,9 +18,6 @@ "index.html", "index.htm" ], - "devConfig": { - "admin": "example/config/app.json" - }, "error": { "1": "内部系统异常", "2": "访问权限异常",