框架优化
This commit is contained in:
@@ -61,8 +61,6 @@ type ColumnShow struct {
|
||||
|
||||
var ColumnNameType = []ColumnShow{
|
||||
//通用
|
||||
{"idcard", false, true, true, false, "", false},
|
||||
{"id", true, false, true, false, "", true},
|
||||
{"parent_id", true, true, true, false, "", true},
|
||||
//"sn"{true,true,true,""},
|
||||
{"status", true, true, true, false, "select", false},
|
||||
|
||||
+92
-44
@@ -9,23 +9,26 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type MakeCode struct {
|
||||
FileConfig Map
|
||||
IndexMenus Map
|
||||
TableConfig Map
|
||||
TableColumns map[string]map[string]Map
|
||||
SearchColumns map[string]map[string]Map
|
||||
Config Map
|
||||
RuleConfig []Map
|
||||
Error
|
||||
}
|
||||
|
||||
func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCode bool) {
|
||||
func (that *MakeCode) Db2JSON(db *db.HoTimeDB, config Map) {
|
||||
isMake := false
|
||||
idSlice := Slice{}
|
||||
|
||||
that.FileConfig = config
|
||||
if that.TableColumns == nil {
|
||||
that.TableColumns = make(map[string]map[string]Map)
|
||||
}
|
||||
@@ -34,20 +37,40 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod
|
||||
}
|
||||
|
||||
//加载配置文件
|
||||
btes, err := ioutil.ReadFile(path)
|
||||
Config["name"] = name
|
||||
btes, err := ioutil.ReadFile(config.GetString("config"))
|
||||
|
||||
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 //系统配置
|
||||
//Config[k] = v //系统配置
|
||||
}
|
||||
} else {
|
||||
that.Error.SetError(errors.New("配置文件不存在,或者配置出错,使用缺省默认配置"))
|
||||
that.Error.SetError(errors.New("config配置文件不存在,或者配置出错,使用缺省默认配置"))
|
||||
}
|
||||
//加载规则文件
|
||||
btesRule, errRule := ioutil.ReadFile(config.GetString("rule"))
|
||||
that.RuleConfig = []Map{}
|
||||
if errRule == nil {
|
||||
//cmap := Map{}
|
||||
//文件是否损坏
|
||||
ruleLis := ObjToSlice(string(btesRule), &that.Error)
|
||||
//cmap.JSON()
|
||||
for k, _ := range ruleLis {
|
||||
that.RuleConfig = append(that.RuleConfig, ruleLis.GetMap(k))
|
||||
}
|
||||
} else {
|
||||
for _, v := range ColumnNameType {
|
||||
that.RuleConfig = append(that.RuleConfig, Map{"name": v.Name, "list": v.List, "edit": v.Edit, "info": v.Info, "must": v.Must, "strict": v.Strict, "type": v.Type})
|
||||
}
|
||||
that.Error.SetError(errors.New("rule配置文件不存在,或者配置出错,使用缺省默认配置"))
|
||||
}
|
||||
|
||||
that.IndexMenus = Map{}
|
||||
menusConfig := that.Config.GetSlice("menus")
|
||||
//将配置写入到内存中仅作为判断用
|
||||
@@ -119,7 +142,7 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod
|
||||
}
|
||||
|
||||
//数据库反哺
|
||||
myInit := strings.Replace(InitTpt, "{{name}}", name, -1)
|
||||
myInit := strings.Replace(InitTpt, "{{name}}", config.GetString("table"), -1)
|
||||
ctrList := ""
|
||||
|
||||
nowTables := make([]Map, 0)
|
||||
@@ -205,38 +228,38 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod
|
||||
coloum["label"] = coloum.GetString("label")[:indexNum]
|
||||
}
|
||||
|
||||
for _, ColumnName := range ColumnNameType {
|
||||
if (ColumnName.Strict && coloum.GetString("name") == ColumnName.Name) ||
|
||||
(!ColumnName.Strict && strings.Contains(coloum.GetString("name"), ColumnName.Name)) {
|
||||
for _, ColumnName := range that.RuleConfig {
|
||||
if (ColumnName.GetBool("strict") && coloum.GetString("name") == ColumnName.GetString("name")) ||
|
||||
(!ColumnName.GetBool("strict") && strings.Contains(coloum.GetString("name"), ColumnName.GetString("name"))) {
|
||||
//全部都不需要则不加入
|
||||
if ColumnName.Edit == false && ColumnName.List == false && ColumnName.Info == false {
|
||||
if ColumnName.GetBool("edit") == false && ColumnName.GetBool("list") == false && ColumnName.GetBool("info") == false {
|
||||
coloum["notUse"] = true
|
||||
//continue
|
||||
}
|
||||
coloum["info"] = ColumnName.Info
|
||||
coloum["edit"] = ColumnName.Edit
|
||||
coloum["add"] = ColumnName.Edit
|
||||
coloum["list"] = ColumnName.List
|
||||
coloum["must"] = ColumnName.Must
|
||||
coloum["info"] = ColumnName.GetBool("info")
|
||||
coloum["edit"] = ColumnName.GetBool("edit")
|
||||
coloum["add"] = ColumnName.GetBool("edit")
|
||||
coloum["list"] = ColumnName.GetBool("list")
|
||||
coloum["must"] = ColumnName.GetBool("must")
|
||||
|
||||
if ColumnName.Info {
|
||||
if ColumnName.GetBool("info") {
|
||||
delete(coloum, "info")
|
||||
}
|
||||
if ColumnName.Edit {
|
||||
if ColumnName.GetBool("edit") {
|
||||
delete(coloum, "edit")
|
||||
delete(coloum, "add")
|
||||
}
|
||||
if ColumnName.List {
|
||||
if ColumnName.GetBool("list") {
|
||||
delete(coloum, "list")
|
||||
}
|
||||
if ColumnName.Must {
|
||||
if ColumnName.GetBool("must") {
|
||||
delete(coloum, "must")
|
||||
}
|
||||
|
||||
if ColumnName.Type != "" {
|
||||
coloum["type"] = ColumnName.Type
|
||||
if ColumnName.GetString("type") != "" {
|
||||
coloum["type"] = ColumnName.GetString("type")
|
||||
}
|
||||
if ColumnName.Strict && coloum.GetString("name") == ColumnName.Name {
|
||||
if ColumnName.GetBool("strict") && coloum.GetString("name") == ColumnName.GetString("name") {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -291,16 +314,16 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod
|
||||
|
||||
}
|
||||
|
||||
if makeCode {
|
||||
if config.GetInt("mode") != 0 {
|
||||
//创建模块文件
|
||||
//判断文件是否存在
|
||||
//_, err := os.OpenFile(name+"/"+v.GetString("name"), os.O_RDONLY, os.ModePerm)
|
||||
_, err := os.Stat(name + "/" + v.GetString("name") + ".go")
|
||||
_, err := os.Stat(config.GetString("name") + "/" + v.GetString("name") + ".go")
|
||||
if err != nil { //文件不存在,则根据模板创建
|
||||
myCtr := strings.Replace(CtrTpt, "{{name}}", name, -1)
|
||||
myCtr := strings.Replace(CtrTpt, "{{name}}", config.GetString("name"), -1)
|
||||
myCtr = strings.Replace(myCtr, "{{table}}", v.GetString("name"), -1)
|
||||
_ = os.MkdirAll(name, os.ModeDir)
|
||||
err = ioutil.WriteFile(name+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModePerm)
|
||||
_ = os.MkdirAll(config.GetString("name"), os.ModeDir)
|
||||
err = ioutil.WriteFile(config.GetString("name")+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
@@ -506,25 +529,31 @@ func (that *MakeCode) Db2JSON(name string, path string, db *db.HoTimeDB, makeCod
|
||||
fmt.Println(id, "---", that.Config.GetString("id"))
|
||||
that.Config["id"] = id
|
||||
|
||||
if makeCode {
|
||||
if config.GetInt("mode") != 0 {
|
||||
//init文件初始化
|
||||
myInit = strings.Replace(myInit, "{{id}}", id, -1)
|
||||
myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1)
|
||||
_ = os.MkdirAll(name, os.ModeDir)
|
||||
err = ioutil.WriteFile(name+"/init.go", []byte(myInit), os.ModePerm)
|
||||
_ = os.MkdirAll(config.GetString("name"), os.ModeDir)
|
||||
err = ioutil.WriteFile(config.GetString("name")+"/init.go", []byte(myInit), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
}
|
||||
//写入配置文件
|
||||
//var configByte bytes.Buffer
|
||||
|
||||
//err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
||||
_ = os.MkdirAll(filepath.Dir(path), os.ModeDir)
|
||||
err = ioutil.WriteFile(path, []byte(that.Config.ToJsonString()), os.ModePerm)
|
||||
_ = os.MkdirAll(filepath.Dir(config.GetString("rule")), os.ModeDir)
|
||||
err = ioutil.WriteFile(config.GetString("rule"), []byte(ObjToStr(that.RuleConfig)), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
|
||||
//写入配置文件
|
||||
//err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t")
|
||||
_ = os.MkdirAll(filepath.Dir(config.GetString("config")), os.ModeDir)
|
||||
err = ioutil.WriteFile(config.GetString("config"), []byte(that.Config.ToJsonString()), os.ModePerm)
|
||||
if err != nil {
|
||||
that.Error.SetError(err)
|
||||
}
|
||||
|
||||
fmt.Println("有新的代码生成,请重新运行")
|
||||
os.Exit(-1)
|
||||
|
||||
@@ -534,7 +563,16 @@ func (that *MakeCode) Info(table string, userData Map, db *db.HoTimeDB) (string,
|
||||
reStr := ""
|
||||
data := Map{}
|
||||
var ruleData Map
|
||||
for _, v := range that.TableColumns[table] {
|
||||
testQu := []string{}
|
||||
testQuData := that.TableColumns[table]
|
||||
for key, _ := range testQuData {
|
||||
//fmt.Println(key, ":", value)
|
||||
testQu = append(testQu, key)
|
||||
}
|
||||
sort.Strings(testQu)
|
||||
|
||||
for _, k := range testQu {
|
||||
v := testQuData[k]
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
@@ -682,12 +720,22 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
|
||||
data := Map{}
|
||||
keyword := Map{}
|
||||
daterange := Map{}
|
||||
sort := Map{}
|
||||
sortMap := Map{}
|
||||
var ruleData Map
|
||||
hasUser := false
|
||||
|
||||
keywordStr := req.FormValue("keyword")
|
||||
for _, v := range that.TableColumns[table] {
|
||||
|
||||
testQu := []string{}
|
||||
testQuData := that.TableColumns[table]
|
||||
for key, _ := range testQuData {
|
||||
//fmt.Println(key, ":", value)
|
||||
testQu = append(testQu, key)
|
||||
}
|
||||
sort.Strings(testQu)
|
||||
|
||||
for _, k := range testQu {
|
||||
v := testQuData[k]
|
||||
//不可使用,未在前端展示,但在内存中保持有
|
||||
if v.GetBool("notUse") {
|
||||
|
||||
@@ -803,7 +851,7 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
|
||||
}
|
||||
|
||||
if searchItem.GetString("name") == "sort" {
|
||||
sort["ORDER"] = table + "." + reqValue
|
||||
sortMap["ORDER"] = table + "." + reqValue
|
||||
}
|
||||
|
||||
continue
|
||||
@@ -823,8 +871,8 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
|
||||
data[table+"."+searchItemName] = reqValue
|
||||
|
||||
}
|
||||
if sort["ORDER"] == nil {
|
||||
sort["ORDER"] = table + ".id DESC"
|
||||
if sortMap["ORDER"] == nil {
|
||||
sortMap["ORDER"] = table + ".id DESC"
|
||||
}
|
||||
|
||||
where := Map{}
|
||||
@@ -869,8 +917,8 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
|
||||
|
||||
}
|
||||
|
||||
if len(sort) != 0 {
|
||||
for k, v := range sort {
|
||||
if len(sortMap) != 0 {
|
||||
for k, v := range sortMap {
|
||||
where[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
+16
-10
@@ -15,6 +15,7 @@ var Project = Proj{
|
||||
{{tablesCtr}}
|
||||
"hotime":Ctr{
|
||||
"login": func(that *Context) {
|
||||
|
||||
name := that.Req.FormValue("name")
|
||||
password := that.Req.FormValue("password")
|
||||
if name == "" || password == "" {
|
||||
@@ -36,8 +37,9 @@ var Project = Proj{
|
||||
that.Display(0, "退出登录成功")
|
||||
},
|
||||
"info": func(that *Context) {
|
||||
hotimeName := that.RouterString[0]
|
||||
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()})
|
||||
str, inData := that.MakeCode.Info("admin", data, that.Db)
|
||||
str, inData := that.MakeCodeRouter[hotimeName].Info("admin", data, that.Db)
|
||||
where := Map{"id": that.Session("admin_id").ToCeilInt()}
|
||||
if len(inData) ==1 {
|
||||
inData["id"] =where["id"]
|
||||
@@ -52,7 +54,7 @@ var Project = Proj{
|
||||
return
|
||||
}
|
||||
for k, v := range re {
|
||||
column := that.MakeCode.TableColumns["admin"][k]
|
||||
column := that.MakeCodeRouter[hotimeName].TableColumns["admin"][k]
|
||||
if column == nil {
|
||||
continue
|
||||
}
|
||||
@@ -76,8 +78,9 @@ import (
|
||||
|
||||
var {{table}}Ctr = Ctr{
|
||||
"info": func(that *Context) {
|
||||
hotimeName := that.RouterString[0]
|
||||
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()})
|
||||
str, inData := that.MakeCode.Info(that.RouterString[1], data, that.Db)
|
||||
str, inData := that.MakeCodeRouter[hotimeName].Info(that.RouterString[1], data, that.Db)
|
||||
where := Map{"id": that.RouterString[2]}
|
||||
|
||||
if len(inData) ==1 {
|
||||
@@ -96,7 +99,7 @@ var {{table}}Ctr = Ctr{
|
||||
}
|
||||
|
||||
for k, v := range re {
|
||||
column := that.MakeCode.TableColumns[that.RouterString[1]][k]
|
||||
column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k]
|
||||
if column == nil {
|
||||
continue
|
||||
}
|
||||
@@ -108,7 +111,8 @@ var {{table}}Ctr = Ctr{
|
||||
that.Display(0, re)
|
||||
},
|
||||
"add": func(that *Context) {
|
||||
inData := that.MakeCode.Add(that.RouterString[1], that.Req)
|
||||
hotimeName := that.RouterString[0]
|
||||
inData := that.MakeCodeRouter[hotimeName].Add(that.RouterString[1], that.Req)
|
||||
if inData == nil {
|
||||
that.Display(3, "请求参数不足")
|
||||
return
|
||||
@@ -134,7 +138,8 @@ var {{table}}Ctr = Ctr{
|
||||
that.Display(0, re)
|
||||
},
|
||||
"update": func(that *Context) {
|
||||
inData := that.MakeCode.Edit(that.RouterString[1], that.Req)
|
||||
hotimeName := that.RouterString[0]
|
||||
inData := that.MakeCodeRouter[hotimeName].Edit(that.RouterString[1], that.Req)
|
||||
if inData == nil {
|
||||
that.Display(3, "没有找到要更新的数据")
|
||||
return
|
||||
@@ -165,7 +170,8 @@ var {{table}}Ctr = Ctr{
|
||||
that.Display(0, re)
|
||||
},
|
||||
"remove": func(that *Context) {
|
||||
inData := that.MakeCode.Delete(that.RouterString[1], that.Req)
|
||||
hotimeName := that.RouterString[0]
|
||||
inData := that.MakeCodeRouter[hotimeName].Delete(that.RouterString[1], that.Req)
|
||||
if inData == nil {
|
||||
that.Display(3, "请求参数不足")
|
||||
return
|
||||
@@ -186,10 +192,10 @@ var {{table}}Ctr = Ctr{
|
||||
},
|
||||
|
||||
"search": func(that *Context) {
|
||||
|
||||
hotimeName := that.RouterString[0]
|
||||
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()})
|
||||
|
||||
columnStr, leftJoin, where := that.MakeCode.Search(that.RouterString[1], data, that.Req, that.Db)
|
||||
columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(that.RouterString[1], data, that.Req, that.Db)
|
||||
|
||||
page := ObjToInt(that.Req.FormValue("page"))
|
||||
pageSize := ObjToInt(that.Req.FormValue("pageSize"))
|
||||
@@ -208,7 +214,7 @@ var {{table}}Ctr = Ctr{
|
||||
|
||||
for _, v := range reData {
|
||||
for k, _ := range v {
|
||||
column := that.MakeCode.TableColumns[that.RouterString[1]][k]
|
||||
column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k]
|
||||
if column == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user