增加menu自动化添加功能

This commit is contained in:
hoteas 2021-06-11 08:06:44 +08:00
parent 1077f09755
commit 32167d4721
5 changed files with 135 additions and 27 deletions

View File

@ -8,21 +8,23 @@ var Config = Map{
"name": "HoTimeDashBoard",
"id": "2f92h3herh23rh2y8",
"label": "HoTime管理平台",
"menu": []Map{
"menus": []Map{
{"label": "平台首页", "name": "HelloWorld", "icon": "el-icon-s-home"},
{"label": "测试表格", "table": "table", "icon": "el-icon-suitcase"},
{"label": "系统管理", "name": "setting", "icon": "el-icon-setting",
"menu": []Map{
{"label": "用户管理", "table": "user"},
{"label": "组织管理", "table": "organization"},
{"label": "地区管理", "table": "area"},
{"label": "角色管理", "table": "role"},
{"label": "日志管理", "table": "log"},
{"label": "系统设置", "table": "system", "default": "edit"},
},
},
//{"label": "测试表格", "table": "table", "icon": "el-icon-suitcase"},
//{"label": "系统管理", "name": "setting", "icon": "el-icon-setting",
// "menus": []Map{
// {"label": "用户管理", "table": "user"},
// {"label": "组织管理", "table": "organization"},
// {"label": "地区管理", "table": "area"},
// {"label": "角色管理", "table": "role"},
// {"label": "日志管理", "table": "log"},
// {"label": "系统设置", "table": "system", "default": "edit"},
// },
//},
},
}
var DefaultMenuParentName = "system"
var ColumnDataType = map[string]string{
//sqlite专有类型
"real": "number",
@ -55,9 +57,11 @@ var ColumnNameType = []ColumnShow{
{"idcard", false, true, true, false, "", false},
{"id", true, false, true, false, "", true},
//"sn"{true,true,true,""},
{"statu", true, true, true, false, "select", false},
{"status", true, true, true, false, "select", false},
{"state", false, true, true, false, "select", false},
{"sex", true, true, true, false, "select", false},
{"delete", false, false, false, false, "", false},
{"index", false, false, false, false, "", false},
{"password", false, true, false, false, "password", false},
{"info", false, true, true, false, "", false},
@ -66,6 +70,8 @@ var ColumnNameType = []ColumnShow{
{"sort", false, true, true, false, "", false},
{"note", false, true, true, false, "", false},
{"description", false, true, true, false, "", false},
{"abstract", false, true, true, false, "", false},
{"content", false, true, true, false, "", false},
{"address", false, true, true, false, "", false},
{"full_name", false, true, true, false, "", false},
{"create_time", false, false, true, false, "", false},
@ -75,6 +81,6 @@ var ColumnNameType = []ColumnShow{
{"file", false, true, true, false, "file", false},
{"age", false, true, true, false, "", false},
{"email", false, true, true, false, "", false},
{"sex", true, true, true, false, "select", false},
{"level", false, false, true, false, "", false},
}

View File

@ -13,6 +13,7 @@ import (
)
type MakeCode struct {
IndexMenus Map
TableConfig Map
TableColumns map[string]map[string]Map
Config Map
@ -42,6 +43,39 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
} else {
that.Error.SetError(errors.New("配置文件不存在,或者配置出错,使用缺省默认配置"))
}
that.IndexMenus = Map{}
menusConfig := that.Config.GetSlice("menus")
//将配置写入到内存中仅作为判断用
if menusConfig != nil {
for kmenu, _ := range menusConfig {
menu := menusConfig.GetMap(kmenu)
if menu != nil {
mname := menu.GetString("table")
if mname == "" { //如果为空则不是表格
mname = menu.GetString("name")
}
that.IndexMenus[mname] = menu
childMenus := menu.GetSlice("menus")
if childMenus != nil {
for ckmenu, _ := range childMenus {
cmenu := childMenus.GetMap(ckmenu)
if cmenu != nil {
cname := cmenu.GetString("table")
if cmenu.GetString("table") == "" {
continue
}
that.IndexMenus[mname+"/"+cname] = cmenu
}
}
}
}
}
}
that.TableConfig = that.Config.GetMap("tables")
if that.TableConfig == nil {
that.TableConfig = Map{}
@ -76,7 +110,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
for _, v := range nowTables {
if that.TableConfig.GetMap(v.GetString("name")) == nil {
if v.GetString("label") == "" {
v["label"] = "备注"
v["label"] = v.GetString("name")
}
that.TableConfig[v.GetString("name")] = Map{
"label": v.GetString("label"),
@ -108,6 +142,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
if that.TableColumns[v.GetString("name")] == nil {
that.TableColumns[v.GetString("name")] = make(map[string]Map)
}
tableInfo := make([]Map, 0)
if db.Type == "mysql" {
tableInfo = db.Select("INFORMATION_SCHEMA.COLUMNS", "COLUMN_NAME AS name,COLUMN_TYPE AS type,COLUMN_COMMENT AS label", Map{"AND": Map{"TABLE_SCHEMA": db.DBName, "TABLE_NAME": v.GetString("name")}})
@ -119,7 +154,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
idSlice = append(idSlice, tableInfo)
for _, info := range tableInfo {
if info.GetString("label") == "" {
info["label"] = "备注"
info["label"] = info.GetString("name")
}
coloum := that.TableColumns[v.GetString("name")][info.GetString("name")]
@ -220,16 +255,68 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
}
return
}
//数据生成完后开始关联id
//数据生成完后进一步解析
for fk, fv := range that.TableColumns {
//判断是否将表写入menu中
isMenusGet := false //判断是否被目录收录
for indexKey, _ := range that.IndexMenus {
indexCode := strings.Index(indexKey, fk)
//如果相等或者表名在目录中已经设置(主要前一位是/并且他是最后一个字符串)
if indexKey == fk || (indexCode != -1 && indexKey[indexCode-1] == '/' && indexKey[indexCode:] == fk) {
isMenusGet = true
break
}
}
//目录没有收录
if !isMenusGet {
tablePrefixCode := strings.Index(fk, "_")
isNewPrefix := false //假定表名没有前缀
prefixName := ""
//并且代表有前缀,根据数据表分库设定使用
if tablePrefixCode != -1 {
prefixName = fk[:tablePrefixCode]
}
if tablePrefixCode != -1 {
for ck, _ := range that.TableColumns {
//判断不止一个前缀相同
if strings.Index(ck, prefixName) == 0 && ck != prefixName && ck != fk {
isNewPrefix = true
break
}
}
}
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 {
that.IndexMenus.GetMap(prefixName)["menus"] = append(that.IndexMenus.GetMap(prefixName).GetSlice("menus"), menuIns)
that.IndexMenus[prefixName+"/"+fk] = menuIns
} 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 {
//虚招后缀是_id结尾的表字段
if len(k) <= 3 || strings.LastIndex(k, "_id") != len(k)-3 {
continue
}
//普通表匹配
oldTableName := k[:len(k)-3]
//上级ID匹配
if oldTableName == "parent" {
oldTableName = fk
@ -258,9 +345,9 @@ func (that *MakeCode) Db2JSON(name string, path string, db db.HoTimeDB) {
tableName = oldTableName
}
}
}
//如果数据匹配则写入到配置中
if that.TableConfig[tableName] != nil {
v["link"] = tableName
//一般查询name字段如果没有name字段则默认第二个地段

View File

@ -84,9 +84,23 @@ var {{table}}Ctr = Ctr{
},
"search": func(that *Context) {
columnStr, where := that.MakeCode.Search(that.RouterString[1], that.Req)
reData := that.Db.Page(ObjToInt(that.Req.FormValue("page")), ObjToInt(that.Req.FormValue("pageRow"))).
Select(that.RouterString[1], columnStr, where)
page:=ObjToInt(that.Req.FormValue("page"))
pageSize:=ObjToInt(that.Req.FormValue("pageSize"))
if page<1{
page=1
}
if pageSize<=0{
pageSize=20
}
count:=that.Db.Count(that.RouterString[1], where)
reData := that.Db.Page(page, pageSize).
PageSelect(that.RouterString[1], columnStr, where)
for _, v := range reData {
for k, _ := range v {
column:=that.MakeCode.TableColumns[that.RouterString[1]][k]
@ -101,7 +115,8 @@ var {{table}}Ctr = Ctr{
}
}
that.Display(0, reData)
that.Display(0, Map{"count":count,"data":reData})
},
}
`

View File

@ -32,7 +32,7 @@ func (that *Context) Mtd(router [3]string) Map {
//打印
func (that *Context) Display(statu int, data interface{}) {
resp := Map{"statu": statu}
resp := Map{"status": statu}
if statu != 0 {
temp := Map{}

View File

@ -610,7 +610,7 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
where += " " + k
}
if reflect.ValueOf(v).Type().String() == "hotime.Slice" {
if reflect.ValueOf(v).Type().String() == "common.Slice" {
for i := 0; i < len(v.(Slice)); i++ {
where += " " + ObjToStr(v.(Slice)[i])