From bb9092c3e54988b6bf665d59670757aaabb217ed Mon Sep 17 00:00:00 2001 From: hoteas <等> Date: Mon, 18 Jul 2022 19:44:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=83=E9=99=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code.go | 429 +++++++++++++++++++++++++-------- code/config.go | 1 + code/makecode.go | 156 ++++++------ example/config/config.json | 3 +- example/config/configNote.json | 5 +- var.go | 13 +- 6 files changed, 434 insertions(+), 173 deletions(-) diff --git a/code.go b/code.go index 2006e0e..b9246f9 100644 --- a/code.go +++ b/code.go @@ -3,6 +3,7 @@ package hotime import ( . "code.hoteas.com/golang/hotime/common" "io" + "io/ioutil" "os" "strings" "time" @@ -15,8 +16,10 @@ var TptProject = Proj{ "info": func(that *Context) { hotimeName := that.RouterString[0] fileConfig := that.MakeCodeRouter[hotimeName].FileConfig + tableName := that.RouterString[1] + data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()}) - str, inData := that.MakeCodeRouter[hotimeName].Info(that.RouterString[1], data, that.Db) + str, inData := that.MakeCodeRouter[hotimeName].Info(tableName, data, that.Db) where := Map{"id": that.RouterString[2]} if len(inData) == 1 { @@ -27,7 +30,7 @@ var TptProject = Proj{ where = Map{"AND": where} } - re := that.Db.Get(that.RouterString[1], str, where) + re := that.Db.Get(tableName, str, where) if re == nil { that.Display(4, "找不到对应信息") @@ -35,7 +38,7 @@ var TptProject = Proj{ } for k, v := range re { - column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k] + column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][k] if column == nil { continue } @@ -48,16 +51,48 @@ var TptProject = Proj{ link := strings.Replace(column.GetString("name"), "_id", "", -1) if link == "parent" { - link = that.RouterString[1] + link = tableName } re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v}) } + + //权限设置 + if column["type"] == "auth" { + + btes, err := ioutil.ReadFile(fileConfig.GetString("config")) + + if err != nil { + that.Display(4, "找不到权限配置文件") + return + } + + conf := ObjToMap(string(btes)) + menus := conf.GetSlice("menus") + + userAuth := re.GetMap("auth") + + for k1, _ := range menus { + v1 := menus.GetMap(k1) + if userAuth[v1.GetString("name")] != nil { + v1["auth"] = userAuth[v1.GetString("name")] + } + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if userAuth[v2.GetString("name")] != nil { + v2["auth"] = userAuth[v2.GetString("name")] + } + } + } + re["auth"] = menus + //that.Display(0,menus) + } + } //如果有table字段则代为link if re["table"] != nil && re["table_id"] != nil { - column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][re.GetString("table")] + column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][re.GetString("table")] v := re.GetCeilInt64("table_id") seStr := "id," + column.GetString("value") @@ -68,7 +103,7 @@ var TptProject = Proj{ link := strings.Replace(column.GetString("name"), "_id", "", -1) if link == "parent" { - link = that.RouterString[1] + link = tableName } re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v}) @@ -77,18 +112,38 @@ var TptProject = Proj{ that.Display(0, re) }, "add": func(that *Context) { - that.Log = Map{"table": that.RouterString[1], "type": 1} + tableName := that.RouterString[1] + that.Log = Map{"table": tableName, "type": 1} hotimeName := that.RouterString[0] fileConfig := that.MakeCodeRouter[hotimeName].FileConfig data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()}) - inData := that.MakeCodeRouter[hotimeName].Add(that.RouterString[1], data, that.Req) + inData := that.MakeCodeRouter[hotimeName].Add(tableName, data, that.Req) if inData == nil { that.Display(3, "请求参数不足") return } - re := that.Db.Insert(that.RouterString[1], inData) + if that.MakeCodeRouter[hotimeName].TableColumns[tableName]["auth"] != nil { + for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] { + if v.GetString("link") != "" { + linkHasAuth := that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] + if linkHasAuth != nil { + + linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": data.GetCeilInt(v.GetString("name"))}) + linkAuth := linkAuthMap.GetMap("auth") + + if linkAuth != nil { + inData["auth"] = ObjToStr(linkAuth) + } + + break + } + } + } + } + + re := that.Db.Insert(tableName, inData) if re == 0 { that.Display(4, "无法插入对应数据") @@ -96,40 +151,129 @@ var TptProject = Proj{ } //索引管理,便于检索以及权限 if inData.Get("parent_id") != nil && inData.GetString("index") != "" { - index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")}) + index := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")}) inData["index"] = index.GetString("index") + ObjToStr(re) + "," - that.Db.Update(that.RouterString[1], Map{"index": inData["index"]}, Map{"id": re}) + that.Db.Update(tableName, Map{"index": inData["index"]}, Map{"id": re}) } else if inData.GetString("index") != "" { inData["index"] = "," + ObjToStr(re) + "," - that.Db.Update(that.RouterString[1], Map{"index": inData["index"]}, Map{"id": re}) + that.Db.Update(tableName, Map{"index": inData["index"]}, Map{"id": re}) } that.Log["table_id"] = re that.Display(0, re) }, "update": func(that *Context) { - - that.Log = Map{"table": that.RouterString[1], "type": 2, "table_id": that.RouterString[2]} + tableName := that.RouterString[1] + that.Log = Map{"table": tableName, "type": 2, "table_id": that.RouterString[2]} hotimeName := that.RouterString[0] - inData := that.MakeCodeRouter[hotimeName].Edit(that.RouterString[1], that.Req) + fileConfig := that.MakeCodeRouter[hotimeName].FileConfig + inData := that.MakeCodeRouter[hotimeName].Edit(tableName, that.Req) if inData == nil { that.Display(3, "没有找到要更新的数据") return } + if inData["auth"] != nil { + + btes, err := ioutil.ReadFile(fileConfig.GetString("config")) + + if err != nil { + that.Display(4, "找不到配置文件") + } + + conf := ObjToMap(string(btes)) + menus := conf.GetSlice("menus") + + data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()}) + for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] { + if v.GetString("link") != "" { + linkHasAuth := that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] + if linkHasAuth != nil { + + linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": data.GetCeilInt(v.GetString("name"))}) + linkAuth := linkAuthMap.GetMap("auth") + myAuth := Map{} + if linkAuth != nil { + + for k1, _ := range menus { + + v1 := menus.GetMap(k1) + if linkAuth[v1.GetString("name")] != nil { + v1["auth"] = linkAuth[v1.GetString("name")] + } + + myAuth[v1.GetString("name")] = v1 + + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if linkAuth[v2.GetString("name")] != nil { + v2["auth"] = linkAuth[v2.GetString("name")] + } + myAuth[v2.GetString("name")] = v2 + } + } + } + + newAuth := inData.GetSlice("auth") + toDB := Map{} + for k1, _ := range newAuth { + v1 := newAuth.GetMap(k1) + + if myAuth.GetMap(v1.GetString("name")) == nil { + continue + } + + auth := Slice{} + toDB[v1.GetString("name")] = auth + + if strings.Contains(myAuth.GetMap(v1.GetString("name")).GetString("auth"), `"show"`) && + strings.Contains(v1.GetString("auth"), `"show"`) { + auth = append(auth, "show") + + } + + if strings.Contains(myAuth.GetMap(v1.GetString("name")).GetString("auth"), `"add"`) && + strings.Contains(v1.GetString("auth"), `"add"`) { + auth = append(auth, "add") + //continue + } + if strings.Contains(myAuth.GetMap(v1.GetString("name")).GetString("auth"), `"edit"`) && + strings.Contains(v1.GetString("auth"), `"edit"`) { + auth = append(auth, "edit") + + } + if strings.Contains(myAuth.GetMap(v1.GetString("name")).GetString("auth"), `"delete"`) && + strings.Contains(v1.GetString("auth"), `"delete"`) { + auth = append(auth, "delete") + + } + if strings.Contains(myAuth.GetMap(v1.GetString("name")).GetString("auth"), `"info"`) && + strings.Contains(v1.GetString("auth"), `"info"`) { + auth = append(auth, "info") + + } + + } + inData["auth"] = toDB.ToJsonString() + break + } + } + } + } + //索引管理,便于检索以及权限 if inData.GetString("index") != "" { if inData.Get("parent_id") != nil { - Index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": that.RouterString[2]}) - parentIndex := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")}) + 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] + "," - childNodes := that.Db.Select(that.RouterString[1], "id,`index``", Map{"index[~]": "," + 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(that.RouterString[1], Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")}) + that.Db.Update(tableName, Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")}) } } else { delete(inData, "index") @@ -137,7 +281,7 @@ var TptProject = Proj{ } - re := that.Db.Update(that.RouterString[1], inData, Map{"id": that.RouterString[2]}) + re := that.Db.Update(tableName, inData, Map{"id": that.RouterString[2]}) if re == 0 { that.Display(4, "更新数据失败") @@ -147,9 +291,10 @@ var TptProject = Proj{ that.Display(0, re) }, "remove": func(that *Context) { - that.Log = Map{"table": that.RouterString[1], "type": 3, "table_id": that.RouterString[2]} + tableName := that.RouterString[1] + that.Log = Map{"table": tableName, "type": 3, "table_id": that.RouterString[2]} hotimeName := that.RouterString[0] - inData := that.MakeCodeRouter[hotimeName].Delete(that.RouterString[1], that.Req) + inData := that.MakeCodeRouter[hotimeName].Delete(tableName, that.Req) if inData == nil { that.Display(3, "请求参数不足") return @@ -157,9 +302,9 @@ var TptProject = Proj{ re := int64(0) //索引管理,便于检索以及权限 if inData.Get("parent_id") != nil && inData.GetSlice("index") != nil { - re = that.Db.Delete(that.RouterString[1], Map{"index[~]": "," + that.RouterString[2] + ","}) + re = that.Db.Delete(tableName, Map{"index[~]": "," + that.RouterString[2] + ","}) } else { - re = that.Db.Delete(that.RouterString[1], Map{"id": that.RouterString[2]}) + re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]}) } if re == 0 { @@ -172,10 +317,10 @@ var TptProject = Proj{ "search": func(that *Context) { hotimeName := that.RouterString[0] fileConfig := that.MakeCodeRouter[hotimeName].FileConfig - + tableName := that.RouterString[1] data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()}) - columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(that.RouterString[1], data, that.Req, that.Db) + columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(tableName, data, that.Req, that.Db) page := ObjToInt(that.Req.FormValue("page")) pageSize := ObjToInt(that.Req.FormValue("pageSize")) @@ -188,9 +333,9 @@ var TptProject = Proj{ pageSize = 20 } - count := that.Db.Count(that.RouterString[1], leftJoin, where) + count := that.Db.Count(tableName, leftJoin, where) reData := that.Db.Page(page, pageSize). - PageSelect(that.RouterString[1], leftJoin, columnStr, where) + PageSelect(tableName, leftJoin, columnStr, where) for _, v := range reData { v.RangeSort(func(k string, v1 interface{}) (isEnd bool) { @@ -209,7 +354,7 @@ var TptProject = Proj{ } - column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k] + column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][k] if column == nil { return isEnd @@ -294,6 +439,7 @@ var TptProject = Proj{ that.Session(fileConfig.GetString("table")+"_id", user.GetCeilInt("id")) that.Session(fileConfig.GetString("table")+"_name", name) delete(user, "password") + user["table"] = fileConfig.GetString("table") that.Display(0, user) }, "logout": func(that *Context) { @@ -303,86 +449,181 @@ var TptProject = Proj{ that.Session(fileConfig.GetString("table")+"_name", nil) that.Display(0, "退出登录成功") }, - "info": func(that *Context) { - that.Log = Map{"table": that.RouterString[1], "type": 2, "table_id": that.RouterString[2]} + "config": func(that *Context) { + hotimeName := that.RouterString[0] fileConfig := that.MakeCodeRouter[hotimeName].FileConfig + if that.Session(fileConfig.GetString("table")+"_id").Data == nil { - data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()}) - str, inData := that.MakeCodeRouter[hotimeName].Info(fileConfig.GetString("table"), data, that.Db) - where := Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()} - if len(inData) == 1 { - inData["id"] = where["id"] - where = Map{"AND": inData} - } else if len(inData) > 1 { - where["OR"] = inData - where = Map{"AND": where} - } - re := that.Db.Get(fileConfig.GetString("table"), str, where) - if re == nil { - that.Display(4, "找不到对应信息") + btes, err := ioutil.ReadFile(fileConfig.GetString("config")) + + if err != nil { + that.Display(4, "找不到配置文件") + } + + conf := ObjToMap(string(btes)) + delete(conf, "menus") + delete(conf, "tables") + //没有登录只需要返回这些信息 + that.Display(0, conf) return } - for k, v := range re { - column := that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")][k] - if column == nil { - continue + + btes, err := ioutil.ReadFile(fileConfig.GetString("config")) + + if err != nil { + that.Display(4, "找不到配置文件") + } + + conf := ObjToMap(string(btes)) + menus := conf.GetSlice("menus") + + user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": fileConfig.GetString("table") + "_id"}) + + if user == nil { + that.Display(2, "暂未登录") + return + } + + for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] { + if v.GetString("link") != "" { + linkHasAuth := that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] + if linkHasAuth == nil { + continue + } + + linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": user.GetCeilInt(v.GetString("name"))}) + linkAuth := linkAuthMap.GetMap("auth") + + for k1, _ := range menus { + v1 := menus.GetMap(k1) + if linkAuth[v1.GetString("name")] != nil { + v1["auth"] = linkAuth[v1.GetString("name")] + } + + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if linkAuth[v2.GetString("name")] != nil { + v2["auth"] = linkAuth[v2.GetString("name")] + } + } + } + } - if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" { + } - //是角色表则取下角色值 - if column.GetString("link") == "role" { + userAuth := user.GetMap("auth") + for k1, _ := range menus { + v1 := menus.GetMap(k1) + if userAuth[v1.GetString("name")] != nil { + v1["auth"] = userAuth[v1.GetString("name")] + } - re[column.GetString("link")] = that.Db.Get(column.GetString("link"), "id,auth,"+column.GetString("value"), Map{"id": v}) - - } else { - - re[column.GetString("link")] = that.Db.Get(column.GetString("link"), "id,"+column.GetString("value"), Map{"id": v}) + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if userAuth[v2.GetString("name")] != nil { + v2["auth"] = userAuth[v2.GetString("name")] } } } - that.Display(0, re) + config := DeepCopyMap(that.MakeCodeRouter[hotimeName].Config).(Map) + + config["menus"] = menus + newTables := Map{} + for k1, _ := range menus { + v1 := menus.GetMap(k1) + if config.GetMap("tables").GetMap(v1.GetString("name")) != nil { + newTables[v1.GetString("name")] = config.GetMap("tables").GetMap(v1.GetString("name")) + } + + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if config.GetMap("tables").GetMap(v2.GetString("name")) != nil { + newTables[v2.GetString("name")] = config.GetMap("tables").GetMap(v2.GetString("name")) + } + } + } + + config["tables"] = menus + + that.Display(0, config) }, + }, + "auth": { + //权限操作 + "info": func(that *Context) { + //不存在配置文件则生成,存在则不管 + hotimeName := that.RouterString[0] + fileConfig := that.MakeCodeRouter[hotimeName].FileConfig + btes, err := ioutil.ReadFile(fileConfig.GetString("config")) + + if err != nil { + that.Display(4, "找不到配置文件") + } + + conf := ObjToMap(string(btes)) + menus := conf.GetSlice("menus") + + user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": fileConfig.GetString("table") + "_id"}) + + if user == nil { + that.Display(2, "暂未登录") + return + } + + for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] { + if v.GetString("link") != "" { + linkHasAuth := that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] + if linkHasAuth == nil { + continue + } + + linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": user.GetCeilInt(v.GetString("name"))}) + linkAuth := linkAuthMap.GetMap("auth") + + for k1, _ := range menus { + v1 := menus.GetMap(k1) + if linkAuth[v1.GetString("name")] != nil { + v1["auth"] = linkAuth[v1.GetString("name")] + } + + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if linkAuth[v2.GetString("name")] != nil { + v2["auth"] = linkAuth[v2.GetString("name")] + } + } + } + + } + } + + userAuth := user.GetMap("auth") + for k1, _ := range menus { + v1 := menus.GetMap(k1) + if userAuth[v1.GetString("name")] != nil { + v1["auth"] = userAuth[v1.GetString("name")] + } + + for k2, _ := range v1.GetSlice("menus") { + v2 := menus.GetMap(k2) + if userAuth[v2.GetString("name")] != nil { + v2["auth"] = userAuth[v2.GetString("name")] + } + } + } + + that.Display(0, menus) + + }, + //权限操作 + "reset": func(that *Context) { + + }, + //权限操作 "update": func(that *Context) { - that.Log = Map{"table": that.RouterString[1], "type": 2, "table_id": that.RouterString[2]} - - hotimeName := that.RouterString[0] - inData := that.MakeCodeRouter[hotimeName].Edit(that.RouterString[1], that.Req) - - if inData == nil { - that.Display(3, "没有找到要更新的数据") - return - } - - //索引管理,便于检索以及权限 - if inData.GetString("index") != "" { - if inData.Get("parent_id") != nil { - Index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": that.RouterString[2]}) - parentIndex := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")}) - inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + "," - - childNodes := that.Db.Select(that.RouterString[1], "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(that.RouterString[1], Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")}) - } - } else { - delete(inData, "index") - } - - } - - re := that.Db.Update(that.RouterString[1], inData, Map{"id": that.RouterString[2]}) - - if re == 0 { - that.Display(4, "更新数据失败") - return - } - - that.Display(0, re) }, }, } diff --git a/code/config.go b/code/config.go index 5060194..5da32ba 100644 --- a/code/config.go +++ b/code/config.go @@ -103,6 +103,7 @@ var ColumnNameType = []ColumnShow{ {"time", true, true, true, false, "time", false}, {"level", false, false, true, false, "", false}, {"rule", true, true, true, false, "form", false}, + {"auth", false, true, true, false, "auth", true}, {"table", true, false, true, false, "table", false}, {"table_id", true, false, true, false, "table_id", false}, } diff --git a/code/makecode.go b/code/makecode.go index 0d2c8de..434ef5c 100644 --- a/code/makecode.go +++ b/code/makecode.go @@ -27,7 +27,7 @@ type MakeCode struct { func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { isMake := false - hasConfigFile := false + //hasConfigFile := false idSlice := Slice{} that.FileConfig = config if that.TableColumns == nil { @@ -38,25 +38,25 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { } //加载配置文件 - btes, err := ioutil.ReadFile(config.GetString("config")) - + //btes, err := ioutil.ReadFile(config.GetString("config")) + var err error that.Config = DeepCopyMap(Config).(Map) that.Config["name"] = config.GetString("table") - if err == nil { - cmap := Map{} - //文件是否损坏 - cmap.JsonToMap(string(btes), &that.Error) - for k, v := range cmap { - that.Config[k] = v //程序配置 - //Config[k] = v //系统配置 - } - hasConfigFile = true - } else { - - that.Error.SetError(errors.New("config配置文件不存在,或者配置出错,使用缺省默认配置")) - - } + //if err == nil { + // cmap := Map{} + // //文件是否损坏 + // cmap.JsonToMap(string(btes), &that.Error) + // for k, v := range cmap { + // that.Config[k] = v //程序配置 + // //Config[k] = v //系统配置 + // } + // hasConfigFile = true + //} else { + // + // that.Error.SetError(errors.New("config配置文件不存在,或者配置出错,使用缺省默认配置")) + // + //} //加载规则文件 btesRule, errRule := ioutil.ReadFile(config.GetString("rule")) that.RuleConfig = []Map{} @@ -83,7 +83,7 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { that.Error.SetError(errors.New("rule配置文件不存在,或者配置出错,使用缺省默认配置")) } - //that.IndexMenus = Map{} + that.IndexMenus = Map{} //menusConfig := that.Config.GetSlice("menus") ////将配置写入到内存中仅作为判断用 //if menusConfig != nil { @@ -384,56 +384,56 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { //目录没有收录 //if !isMenusGet { - if !hasConfigFile { - tablePrefixCode := strings.Index(fk, "_") - isNewPrefix := false //假定表名没有前缀 - prefixName := "" - //并且代表有前缀,根据数据表分库设定使用 - if tablePrefixCode != -1 { - prefixName = fk[:tablePrefixCode] - } else { - prefixName = fk - } - //if tablePrefixCode != -1 { - for ck, _ := range that.TableColumns { - //判断不止一个前缀相同 - if (strings.Index(ck, prefixName) == 0 || strings.Index(prefixName, ck) == 4) && ck != fk { - isNewPrefix = true - break - } - } - //} - - prefixName = DefaultMenuParentName + ":" + prefixName - - menuIns := Map{"label": that.TableConfig.GetMap(fk).GetString("label"), "table": fk} - //多耗费一点内存 - mMenu := Map{"menus": Slice{menuIns}, "label": that.TableConfig.GetMap(fk).GetString("label"), "name": prefixName, "icon": "el-icon-setting"} - //表名有前缀 - if !isNewPrefix { - //是否已有对应前缀,已经有对应的menu只需要push进去即可 - prefixName = DefaultMenuParentName - mMenu = Map{"menus": Slice{menuIns}, "label": "系统管理", "name": prefixName, "icon": "el-icon-setting"} - } - //没有新前缀 - if that.IndexMenus[prefixName] != nil { - if that.IndexMenus[prefixName+"/"+fk] == nil { - that.IndexMenus.GetMap(prefixName)["menus"] = append(that.IndexMenus.GetMap(prefixName).GetSlice("menus"), menuIns) - that.IndexMenus[prefixName+"/"+fk] = menuIns - } else { - for k, v := range menuIns { - that.IndexMenus.GetMap(prefixName + "/" + fk)[k] = v - } - } - - } else { - - that.Config["menus"] = append(that.Config.GetSlice("menus"), mMenu) //注入配置 - //写入Index - that.IndexMenus[prefixName] = mMenu - that.IndexMenus[prefixName+"/"+fk] = menuIns + //if !hasConfigFile { + tablePrefixCode := strings.Index(fk, "_") + isNewPrefix := false //假定表名没有前缀 + prefixName := "" + //并且代表有前缀,根据数据表分库设定使用 + if tablePrefixCode != -1 { + prefixName = fk[:tablePrefixCode] + } else { + prefixName = fk + } + //if tablePrefixCode != -1 { + for ck, _ := range that.TableColumns { + //判断不止一个前缀相同 + if (strings.Index(ck, prefixName) == 0 || strings.Index(prefixName, ck) == 4) && ck != fk { + isNewPrefix = true + break } } + //} + + prefixName = DefaultMenuParentName + ":" + prefixName + + menuIns := Map{"label": that.TableConfig.GetMap(fk).GetString("label"), "table": fk} + //多耗费一点内存 + mMenu := Map{"menus": Slice{menuIns}, "label": that.TableConfig.GetMap(fk).GetString("label"), "name": prefixName, "icon": "el-icon-setting"} + //表名有前缀 + if !isNewPrefix { + //是否已有对应前缀,已经有对应的menu只需要push进去即可 + prefixName = DefaultMenuParentName + mMenu = Map{"menus": Slice{menuIns}, "label": "系统管理", "name": prefixName, "icon": "el-icon-setting"} + } + //没有新前缀 + if that.IndexMenus[prefixName] != nil { + if that.IndexMenus[prefixName+"/"+fk] == nil { + that.IndexMenus.GetMap(prefixName)["menus"] = append(that.IndexMenus.GetMap(prefixName).GetSlice("menus"), menuIns) + that.IndexMenus[prefixName+"/"+fk] = menuIns + } else { + for k, v := range menuIns { + that.IndexMenus.GetMap(prefixName + "/" + fk)[k] = v + } + } + + } else { + + that.Config["menus"] = append(that.Config.GetSlice("menus"), mMenu) //注入配置 + //写入Index + that.IndexMenus[prefixName] = mMenu + that.IndexMenus[prefixName+"/"+fk] = menuIns + } + //} for k, v := range fv { @@ -566,12 +566,28 @@ func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) { } } - //写入配置文件 - //err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t") - if !hasConfigFile { + //配置文件 + if config.GetString("configDB") != "" { + + that.Config["id"] = id + _ = os.MkdirAll(filepath.Dir(config.GetString("configDB")), os.ModeDir) + err = ioutil.WriteFile(config.GetString("configDB"), []byte(that.Config.ToJsonString()), os.ModePerm) + if err != nil { + that.Error.SetError(err) + } + that.Logger.Warn("新建") + + } + + //不存在配置文件则生成,存在则不管 + _, err = ioutil.ReadFile(config.GetString("config")) + if err != nil { + that.Config["id"] = id _ = os.MkdirAll(filepath.Dir(config.GetString("config")), os.ModeDir) - err = ioutil.WriteFile(config.GetString("config"), []byte(that.Config.ToJsonString()), os.ModePerm) + newConfig := DeepCopyMap(that.Config).(Map) + delete(newConfig, "tables") + err = ioutil.WriteFile(config.GetString("config"), []byte(newConfig.ToJsonString()), os.ModePerm) if err != nil { that.Error.SetError(err) } diff --git a/example/config/config.json b/example/config/config.json index 08806d5..b6727d9 100644 --- a/example/config/config.json +++ b/example/config/config.json @@ -8,7 +8,8 @@ }, "codeConfig": [ { - "config": "config/app.json", + "config": "config/admin.json", + "configDB": "config/adminDB.json", "mode": 0, "name": "", "rule": "config/rule.json", diff --git a/example/config/configNote.json b/example/config/configNote.json index aa87daf..bd53084 100644 --- a/example/config/configNote.json +++ b/example/config/configNote.json @@ -24,7 +24,8 @@ "注释:配置即启用,非必须,默认无", { "config": "默认config/app.json,必须,接口描述配置文件", - "mode": "默认0,非必须,0为内嵌代码模式,1为生成代码模式,在开发模式下会显示更多的数据用于开发测试,并能够辅助研发,自动生成配置文件、代码等功能,", + "configDB": "默认无,非必须,有则每次将数据库数据生成到此目录用于配置读写,无则不生成", + "mode": "默认0,非必须,0为内嵌代码模式,1为生成代码模式", "name": "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码", "rule": "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成", "table": "默认admin,必须,根据数据库内当前表名做为用户生成数据" @@ -65,7 +66,7 @@ }, "logFile": "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置", "logLevel": "默认0,必须,0关闭,1打印,日志等级", - "mode": "默认0,非必须,0生产模式,1,测试模式,2开发模式,web无缓存,数据库不启用缓存", + "mode": "默认0,非必须,0生产模式,1,测试模式,2开发模式,3内嵌代码模式,在开发模式下会显示更多的数据用于开发测试,并能够辅助研发,自动生成配置文件、代码等功能,web无缓存,数据库不启用缓存", "modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配", "port": "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80", "sessionName": "默认HOTIME,必须,设置session的cookie名", diff --git a/var.go b/var.go index 567f6bc..0ba2954 100644 --- a/var.go +++ b/var.go @@ -18,7 +18,7 @@ var Config = Map{ Map{ "table": "admin", //默认admin,必须,有则根据数据库内当前表名做为用户生成数据 "name": "", //默认admin,非必须,有则生成代码到此目录,无则采用缺省模式使用表名 - "config": "config/app.json", //默认config/app.json,必须,接口描述配置文件 + "config": "config/app.json", //默认config/app.json,必须,接口描述配置文件,无则自动生成 "rule": "config/rule.json", //默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成 "mode": 0, //默认0,非必须,0为内嵌代码模式,1为生成代码模式 }, @@ -65,11 +65,12 @@ var ConfigNote = Map{ "注释:配置即启用,非必须,默认无", Map{ //默认无,必须,接口类别名称 //"注释": "", // - "table": "默认admin,必须,根据数据库内当前表名做为用户生成数据", - "name": "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码", - "config": "默认config/app.json,必须,接口描述配置文件", // - "rule": "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成", - "mode": "默认0,非必须,0为内嵌代码模式,1为生成代码模式", + "table": "默认admin,必须,根据数据库内当前表名做为用户生成数据", + "name": "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码", + "config": "默认config/app.json,必须,接口描述配置文件", // + "configDB": "默认无,非必须,有则每次将数据库数据生成到此目录用于配置读写,无则不生成", // + "rule": "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成", + "mode": "默认0,非必须,0为内嵌代码模式,1为生成代码模式", }, }, "db": Map{