From be02b3418d94827b54b2b8331c0cf49a66e161be Mon Sep 17 00:00:00 2001 From: hoteas Date: Mon, 25 Jul 2022 05:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=80=E6=9C=AF=E6=80=A7=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code.go | 26 ++++++------- code/config.go | 1 + code/makecode.go | 77 ++++++++++++++++++++++++++++++++++---- db/hotimedb.go | 51 ++++++++----------------- example/config/config.json | 7 +++- 5 files changed, 104 insertions(+), 58 deletions(-) diff --git a/code.go b/code.go index fea07b6..cfe42ad 100644 --- a/code.go +++ b/code.go @@ -150,13 +150,13 @@ var TptProject = Proj{ return } //索引管理,便于检索以及权限 - if inData.Get("parent_id") != nil && inData.GetString("index") != "" { + if inData.Get("parent_id") != nil && inData.GetString("parent_ids") != "" { index := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")}) - inData["index"] = index.GetString("index") + ObjToStr(re) + "," - that.Db.Update(tableName, Map{"index": inData["index"]}, Map{"id": re}) - } else if inData.GetString("index") != "" { - inData["index"] = "," + ObjToStr(re) + "," - that.Db.Update(tableName, Map{"index": inData["index"]}, Map{"id": re}) + inData["parent_ids"] = index.GetString("parent_ids") + ObjToStr(re) + "," + that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re}) + } else if inData.GetString("parent_ids") != "" { + inData["parent_ids"] = "," + ObjToStr(re) + "," + that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re}) } that.Log["table_id"] = re that.Display(0, re) @@ -263,20 +263,20 @@ var TptProject = Proj{ } //索引管理,便于检索以及权限 - if inData.GetString("index") != "" { + if inData.GetString("parent_ids") != "" { if inData.Get("parent_id") != nil { Index := that.Db.Get(tableName, "`index`", Map{"id": that.RouterString[2]}) parentIndex := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")}) - inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + "," + inData["parent_ids"] = parentIndex.GetString("parent_ids") + that.RouterString[2] + "," childNodes := that.Db.Select(tableName, "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","}) for _, v := range childNodes { - v["index"] = strings.Replace(v.GetString("index"), Index.GetString("index"), inData.GetString("index"), -1) - that.Db.Update(tableName, Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")}) + v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), inData.GetString("parent_ids"), -1) + that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")}) } } else { - delete(inData, "index") + delete(inData, "parent_ids") } } @@ -301,7 +301,7 @@ var TptProject = Proj{ } re := int64(0) //索引管理,便于检索以及权限 - if inData.Get("parent_id") != nil && inData.GetSlice("index") != nil { + if inData.Get("parent_id") != nil && inData.GetSlice("parent_ids") != nil { re = that.Db.Delete(tableName, Map{"index[~]": "," + that.RouterString[2] + ","}) } else { re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]}) @@ -531,7 +531,7 @@ var TptProject = Proj{ where := Map{"name": name} if that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")]["phone"] != nil { where["phone"] = name - where = Map{"AND": Map{"OR": where}, "password": Md5(password)} + where = Map{"AND": Map{"OR": where, "password": Md5(password)}} } else { where["password"] = Md5(password) } diff --git a/code/config.go b/code/config.go index 5da32ba..cc47f80 100644 --- a/code/config.go +++ b/code/config.go @@ -79,6 +79,7 @@ var ColumnNameType = []ColumnShow{ {"longitude", false, true, true, false, "", false}, {"index", false, false, false, false, "index", false}, + {"parent_ids", false, false, false, false, "index", false}, {"password", false, true, false, false, "password", false}, {"pwd", false, true, false, false, "password", false}, diff --git a/code/makecode.go b/code/makecode.go index 434ef5c..6aea9fe 100644 --- a/code/makecode.go +++ b/code/makecode.go @@ -855,7 +855,18 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db * if ruleData[v.GetString("link")] != nil { if v.GetString("name") == "parent_id" { - data[table+".index[~]"] = "," + ruleData.GetString(v.GetString("link")) + "," + + parent_idsStr := "" + parent_ids := that.TableColumns[v.GetString("link")]["parent_ids"] + if parent_ids != nil { + parent_idsStr = "parent_ids[~]" + } + index := that.TableColumns[v.GetString("link")]["index"] + if index != nil { + parent_idsStr = "index[~]" + } + + data[table+"."+parent_idsStr] = "," + ruleData.GetString(v.GetString("link")) + "," } else { idMap := db.Select(v.GetString("link"), "id", Map{"index[~]": "," + ruleData.GetString(v.GetString("link")) + ","}) @@ -900,13 +911,37 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db * search := that.TableConfig.GetMap(table).GetSlice("search") for k, _ := range search { - - reqValue := req.FormValue(search.GetMap(k).GetString("name")) - if reqValue == "" { - continue - } searchItem := search.GetMap(k) searchItemName := searchItem.GetString("name") + parent_idsStr := "" + parent_ids := that.TableColumns[searchItem.GetString("link")]["parent_ids"] + if parent_ids != nil { + parent_idsStr = "parent_ids[~]" + } + index := that.TableColumns[searchItem.GetString("link")]["index"] + if index != nil { + parent_idsStr = "index[~]" + } + + reqValue := req.Form[search.GetMap(k).GetString("name")] + + //reqValue := req.FormValue(search.GetMap(k).GetString("name")) + if len(reqValue) == 0 || reqValue[0] == "" { + + if parent_idsStr != "" && userData[searchItem.GetString("name")] != nil { + where := Map{parent_idsStr: "," + ObjToStr(userData.GetCeilInt64(searchItem.GetString("name"))) + ","} + r := db.Select(searchItem.GetString("link"), "id", where) + reqValue = []string{} + for _, v := range r { + reqValue = append(reqValue, v.GetString("id")) + } + + data[table+"."+searchItemName] = reqValue + + } + + continue + } //columns := that.TableConfig.GetMap(table).GetSlice("columns") if searchItem.GetString("type") == "search" { @@ -944,7 +979,7 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db * } if searchItem.GetString("name") == "sort" { - sortMap["ORDER"] = table + "." + reqValue + sortMap["ORDER"] = table + "." + reqValue[0] } continue @@ -960,10 +995,36 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db * } continue } + //如果是树节点则需要判断是否符合权限 + if searchItem.GetString("type") == "tree" { + + if parent_idsStr != "" { + + where := Map{} + + for _, v := range reqValue { + if len(where) == 0 { + where[parent_idsStr] = "," + v + "," + continue + } + where = Map{"OR": where, parent_idsStr: "," + v + ","} + } + //用户 + if userData[searchItem.GetString("name")] != nil { + where = Map{"AND": Map{parent_idsStr: "," + ObjToStr(userData.GetCeilInt64(searchItem.GetString("name"))) + ",", "OR": where}} + } + r := db.Select(searchItem.GetString("link"), "id", where) + for _, v := range r { + reqValue = append(reqValue, v.GetString("id")) + } + + } + + } data[table+"."+searchItemName] = reqValue - } + if sortMap["ORDER"] == nil { sortMap["ORDER"] = table + ".id DESC" } diff --git a/db/hotimedb.go b/db/hotimedb.go index 758fe26..6001945 100644 --- a/db/hotimedb.go +++ b/db/hotimedb.go @@ -810,14 +810,15 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { if !strings.Contains(k, ".") { k = "`" + k + "`" } - if reflect.ValueOf(v).Type().String() == "common.Slice" { + if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") { + vs := ObjToSlice(v) where += k + " IN (" - res = append(res, v.(Slice)...) + res = append(res, vs...) if len(v.(Slice)) == 0 { where += ") " } else { - for i := 0; i < len(v.(Slice)); i++ { - if i+1 != len(v.(Slice)) { + for i := 0; i < len(vs); i++ { + if i+1 != len(vs) { where += "?," } else { where += "?) " @@ -841,25 +842,13 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { } if v == nil { where += k + " IS NULL" - } else if reflect.ValueOf(v).Type().String() == "common.Slice" { - + } else if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") { + vs := ObjToSlice(v) //fmt.Println(v) where += k + " IN (" - res = append(res, v.(Slice)...) - for i := 0; i < len(v.(Slice)); i++ { - if i+1 != len(v.(Slice)) { - where += "?," - } else { - where += "?) " - } - //res=append(res,(v.(Slice))[i]) - } - } else if reflect.ValueOf(v).Type().String() == "[]interface {}" { - - where += k + " IN (" - res = append(res, v.([]interface{})...) - for i := 0; i < len(v.([]interface{})); i++ { - if i+1 != len(v.([]interface{})) { + res = append(res, vs...) + for i := 0; i < len(vs); i++ { + if i+1 != len(vs) { where += "?," } else { where += "?) " @@ -885,22 +874,12 @@ func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa where += k + " IS NOT NULL " - } else if reflect.ValueOf(v).Type().String() == "common.Slice" { + } else if reflect.ValueOf(v).Type().String() == "common.Slice" || strings.Contains(reflect.ValueOf(v).Type().String(), "[]") { + vs := ObjToSlice(v) where += k + " NOT IN (" - res = append(res, v.(Slice)...) - for i := 0; i < len(v.(Slice)); i++ { - if i+1 != len(v.(Slice)) { - where += "?," - } else { - where += "?) " - } - //res=append(res,(v.(Slice))[i]) - } - } else if reflect.ValueOf(v).Type().String() == "[]interface {}" { - where += k + " NOT IN (" - res = append(res, v.([]interface{})...) - for i := 0; i < len(v.([]interface{})); i++ { - if i+1 != len(v.([]interface{})) { + res = append(res, vs...) + for i := 0; i < len(vs); i++ { + if i+1 != len(vs) { where += "?," } else { where += "?) " diff --git a/example/config/config.json b/example/config/config.json index a1e35aa..ee1cf4c 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -1,5 +1,10 @@ { "cache": { + "db": { + "db": false, + "session": true, + "timeout": 7200 + }, "memory": { "db": true, "session": true, @@ -19,7 +24,7 @@ "db": { "mysql": { "host": "192.168.6.253", - "name": "iedc_dev", + "name": "dgs-cms", "password": "dasda8454456", "port": "3306", "user": "root"