框架优化

This commit is contained in:
hoteas 2022-03-13 05:06:28 +08:00
parent ebea14c74f
commit b7c243823a
11 changed files with 255 additions and 114 deletions

View File

@ -20,8 +20,7 @@ import (
) )
type Application struct { type Application struct {
*code.MakeCode MakeCodeRouter map[string]*code.MakeCode
MakeCodeRouter Router
MethodRouter MethodRouter
Router Router
ContextBase ContextBase
@ -533,32 +532,41 @@ func Init(config string) Application {
SetDB(&appIns) SetDB(&appIns)
appIns.SetCache() appIns.SetCache()
appIns.MakeCode = &code.MakeCode{}
codeConfig := appIns.Config.GetMap("codeConfig") codeConfig := appIns.Config.GetMap("codeConfig")
appIns.MakeCodeRouter = Router{}
if codeConfig != nil { if codeConfig != nil {
for k, _ := range codeConfig { for k, _ := range codeConfig {
if appIns.Config.GetInt("mode") == 2 { codeMake := codeConfig.GetMap(k)
appIns.MakeCode.Db2JSON(k, codeConfig.GetString(k), &appIns.Db, true) if codeMake == nil {
appIns.MakeCodeRouter[k] = Proj{} continue
} else if appIns.Config.GetInt("mode") == 3 { }
appIns.MakeCode.Db2JSON(k, codeConfig.GetString(k), &appIns.Db, false) codeMake["table"] = k
appIns.MakeCodeRouter[k] = Proj{} if appIns.MakeCodeRouter == nil {
appIns.MakeCodeRouter = map[string]*code.MakeCode{}
}
if appIns.Config.GetInt("mode") > 0 {
appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{}
appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(&appIns.Db, codeMake)
} else { } else {
appIns.MakeCode.Db2JSON(k, codeConfig.GetString(k), nil, false) appIns.MakeCodeRouter[codeMake.GetString("name")] = &code.MakeCode{}
appIns.MakeCodeRouter[k] = Proj{} appIns.MakeCodeRouter[codeMake.GetString("name")].Db2JSON(nil, codeMake)
} }
//接入动态代码层 //接入动态代码层
if appIns.Router == nil { if appIns.Router == nil {
appIns.Router = Router{} appIns.Router = Router{}
} }
appIns.Router[k] = TptProject
for k1, _ := range appIns.MakeCode.TableColumns { if codeMake.GetString("name") == "" {
appIns.Router[k][k1] = appIns.Router[k]["hotimeCommon"] codeMake["name"] = k
} }
setMakeCodeLintener(k, &appIns) appIns.Router[codeMake.GetString("name")] = TptProject
for k1, _ := range appIns.MakeCodeRouter[codeMake.GetString("name")].TableColumns {
appIns.Router[codeMake.GetString("name")][k1] = appIns.Router[codeMake.GetString("name")]["hotimeCommon"]
}
setMakeCodeLintener(codeMake.GetString("name"), &appIns)
} }

52
code.go
View File

@ -12,7 +12,7 @@ var TptProject = Proj{
"info": func(that *Context) { "info": func(that *Context) {
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()}) data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_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]} where := Map{"id": that.RouterString[2]}
if len(inData) == 1 { if len(inData) == 1 {
@ -31,7 +31,7 @@ var TptProject = Proj{
} }
for k, v := range re { 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 { if column == nil {
continue continue
} }
@ -43,7 +43,8 @@ var TptProject = Proj{
that.Display(0, re) that.Display(0, re)
}, },
"add": func(that *Context) { "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 { if inData == nil {
that.Display(3, "请求参数不足") that.Display(3, "请求参数不足")
return return
@ -68,7 +69,8 @@ var TptProject = Proj{
that.Display(0, re) that.Display(0, re)
}, },
"update": func(that *Context) { "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 { if inData == nil {
that.Display(3, "没有找到要更新的数据") that.Display(3, "没有找到要更新的数据")
return return
@ -103,7 +105,8 @@ var TptProject = Proj{
that.Display(0, re) that.Display(0, re)
}, },
"remove": func(that *Context) { "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 { if inData == nil {
that.Display(3, "请求参数不足") that.Display(3, "请求参数不足")
return return
@ -127,7 +130,7 @@ var TptProject = Proj{
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()}) data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_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")) page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize")) pageSize := ObjToInt(that.Req.FormValue("pageSize"))
@ -145,10 +148,11 @@ var TptProject = Proj{
PageSelect(that.RouterString[1], leftJoin, columnStr, where) PageSelect(that.RouterString[1], leftJoin, columnStr, where)
for _, v := range reData { for _, v := range reData {
for k, _ := range v { v.RangeSort(func(k string, v1 interface{}) (isEnd bool) {
column := that.MakeCode.TableColumns[that.RouterString[1]][k] //for k, _ := range v {
column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k]
if column == nil { if column == nil {
continue return true
} }
if column["list"] != false && column["name"] == "parent_id" && column.GetString("link") != "" { if column["list"] != false && column["name"] == "parent_id" && column.GetString("link") != "" {
@ -159,7 +163,9 @@ var TptProject = Proj{
} }
} }
} //}
return isEnd
})
} }
that.Display(0, Map{"count": count, "data": reData}) that.Display(0, Map{"count": count, "data": reData})
@ -168,33 +174,39 @@ var TptProject = Proj{
"hotime": Ctr{ "hotime": Ctr{
"login": func(that *Context) { "login": func(that *Context) {
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
name := that.Req.FormValue("name") name := that.Req.FormValue("name")
password := that.Req.FormValue("password") password := that.Req.FormValue("password")
if name == "" || password == "" { if name == "" || password == "" {
that.Display(3, "参数不足") that.Display(3, "参数不足")
return return
} }
user := that.Db.Get(hotimeName, "*", Map{"AND": Map{"OR": Map{"name": name, "phone": name}, "password": Md5(password)}}) user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"AND": Map{"OR": Map{"name": name, "phone": name}, "password": Md5(password)}})
if user == nil { if user == nil {
that.Display(5, "登录失败") that.Display(5, "登录失败")
return return
} }
that.Session(hotimeName+"_id", user.GetCeilInt("id")) that.Session(fileConfig.GetString("table")+"_id", user.GetCeilInt("id"))
that.Session(hotimeName+"_name", name) that.Session(fileConfig.GetString("table")+"_name", name)
delete(user, "password") delete(user, "password")
that.Display(0, user) that.Display(0, user)
}, },
"logout": func(that *Context) { "logout": func(that *Context) {
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
that.Session(hotimeName+"_id", nil) fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
that.Session(hotimeName+"_name", nil) that.Session(fileConfig.GetString("table")+"_id", nil)
that.Session(fileConfig.GetString("table")+"_name", nil)
that.Display(0, "退出登录成功") that.Display(0, "退出登录成功")
}, },
"info": func(that *Context) { "info": func(that *Context) {
hotimeName := that.RouterString[0] hotimeName := that.RouterString[0]
data := that.Db.Get(hotimeName, "*", Map{"id": that.Session(hotimeName + "_id").ToCeilInt()}) fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
str, inData := that.MakeCode.Info(hotimeName, data, that.Db)
where := Map{"id": that.Session(hotimeName + "_id").ToCeilInt()} data := that.Db.Get(hotimeName, "*", 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 { if len(inData) == 1 {
inData["id"] = where["id"] inData["id"] = where["id"]
where = Map{"AND": inData} where = Map{"AND": inData}
@ -202,13 +214,13 @@ var TptProject = Proj{
where["OR"] = inData where["OR"] = inData
where = Map{"AND": where} where = Map{"AND": where}
} }
re := that.Db.Get(hotimeName, str, where) re := that.Db.Get(fileConfig.GetString("table"), str, where)
if re == nil { if re == nil {
that.Display(4, "找不到对应信息") that.Display(4, "找不到对应信息")
return return
} }
for k, v := range re { for k, v := range re {
column := that.MakeCode.TableColumns[hotimeName][k] column := that.MakeCodeRouter[hotimeName].TableColumns[hotimeName][k]
if column == nil { if column == nil {
continue continue
} }

View File

@ -61,8 +61,6 @@ type ColumnShow struct {
var ColumnNameType = []ColumnShow{ var ColumnNameType = []ColumnShow{
//通用 //通用
{"idcard", false, true, true, false, "", false},
{"id", true, false, true, false, "", true},
{"parent_id", true, true, true, false, "", true}, {"parent_id", true, true, true, false, "", true},
//"sn"{true,true,true,""}, //"sn"{true,true,true,""},
{"status", true, true, true, false, "select", false}, {"status", true, true, true, false, "select", false},

View File

@ -9,23 +9,26 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"time" "time"
) )
type MakeCode struct { type MakeCode struct {
FileConfig Map
IndexMenus Map IndexMenus Map
TableConfig Map TableConfig Map
TableColumns map[string]map[string]Map TableColumns map[string]map[string]Map
SearchColumns map[string]map[string]Map SearchColumns map[string]map[string]Map
Config Map Config Map
RuleConfig []Map
Error 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 isMake := false
idSlice := Slice{} idSlice := Slice{}
that.FileConfig = config
if that.TableColumns == nil { if that.TableColumns == nil {
that.TableColumns = make(map[string]map[string]Map) 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) btes, err := ioutil.ReadFile(config.GetString("config"))
Config["name"] = name
that.Config = DeepCopyMap(Config).(Map) that.Config = DeepCopyMap(Config).(Map)
that.Config["name"] = config.GetString("table")
if err == nil { if err == nil {
cmap := Map{} cmap := Map{}
//文件是否损坏 //文件是否损坏
cmap.JsonToMap(string(btes), &that.Error) cmap.JsonToMap(string(btes), &that.Error)
for k, v := range cmap { for k, v := range cmap {
that.Config[k] = v //程序配置 that.Config[k] = v //程序配置
Config[k] = v //系统配置 //Config[k] = v //系统配置
} }
} else { } 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{} that.IndexMenus = Map{}
menusConfig := that.Config.GetSlice("menus") 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 := "" ctrList := ""
nowTables := make([]Map, 0) 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] coloum["label"] = coloum.GetString("label")[:indexNum]
} }
for _, ColumnName := range ColumnNameType { for _, ColumnName := range that.RuleConfig {
if (ColumnName.Strict && coloum.GetString("name") == ColumnName.Name) || if (ColumnName.GetBool("strict") && coloum.GetString("name") == ColumnName.GetString("name")) ||
(!ColumnName.Strict && strings.Contains(coloum.GetString("name"), ColumnName.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 coloum["notUse"] = true
//continue //continue
} }
coloum["info"] = ColumnName.Info coloum["info"] = ColumnName.GetBool("info")
coloum["edit"] = ColumnName.Edit coloum["edit"] = ColumnName.GetBool("edit")
coloum["add"] = ColumnName.Edit coloum["add"] = ColumnName.GetBool("edit")
coloum["list"] = ColumnName.List coloum["list"] = ColumnName.GetBool("list")
coloum["must"] = ColumnName.Must coloum["must"] = ColumnName.GetBool("must")
if ColumnName.Info { if ColumnName.GetBool("info") {
delete(coloum, "info") delete(coloum, "info")
} }
if ColumnName.Edit { if ColumnName.GetBool("edit") {
delete(coloum, "edit") delete(coloum, "edit")
delete(coloum, "add") delete(coloum, "add")
} }
if ColumnName.List { if ColumnName.GetBool("list") {
delete(coloum, "list") delete(coloum, "list")
} }
if ColumnName.Must { if ColumnName.GetBool("must") {
delete(coloum, "must") delete(coloum, "must")
} }
if ColumnName.Type != "" { if ColumnName.GetString("type") != "" {
coloum["type"] = ColumnName.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 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.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 { //文件不存在,则根据模板创建 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) myCtr = strings.Replace(myCtr, "{{table}}", v.GetString("name"), -1)
_ = os.MkdirAll(name, os.ModeDir) _ = os.MkdirAll(config.GetString("name"), os.ModeDir)
err = ioutil.WriteFile(name+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModePerm) err = ioutil.WriteFile(config.GetString("name")+"/"+v.GetString("name")+".go", []byte(myCtr), os.ModePerm)
if err != nil { if err != nil {
that.Error.SetError(err) 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")) fmt.Println(id, "---", that.Config.GetString("id"))
that.Config["id"] = id that.Config["id"] = id
if makeCode { if config.GetInt("mode") != 0 {
//init文件初始化 //init文件初始化
myInit = strings.Replace(myInit, "{{id}}", id, -1) myInit = strings.Replace(myInit, "{{id}}", id, -1)
myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1) myInit = strings.Replace(myInit, "{{tablesCtr}}", ctrList, -1)
_ = os.MkdirAll(name, os.ModeDir) _ = os.MkdirAll(config.GetString("name"), os.ModeDir)
err = ioutil.WriteFile(name+"/init.go", []byte(myInit), os.ModePerm) err = ioutil.WriteFile(config.GetString("name")+"/init.go", []byte(myInit), os.ModePerm)
if err != nil { if err != nil {
that.Error.SetError(err) that.Error.SetError(err)
} }
} }
//写入配置文件
//var configByte bytes.Buffer
//err = json.Indent(&configByte, []byte(that.Config.ToJsonString()), "", "\t") _ = os.MkdirAll(filepath.Dir(config.GetString("rule")), os.ModeDir)
_ = os.MkdirAll(filepath.Dir(path), os.ModeDir) err = ioutil.WriteFile(config.GetString("rule"), []byte(ObjToStr(that.RuleConfig)), os.ModePerm)
err = ioutil.WriteFile(path, []byte(that.Config.ToJsonString()), os.ModePerm)
if err != nil { if err != nil {
that.Error.SetError(err) 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("有新的代码生成,请重新运行") fmt.Println("有新的代码生成,请重新运行")
os.Exit(-1) os.Exit(-1)
@ -534,7 +563,16 @@ func (that *MakeCode) Info(table string, userData Map, db *db.HoTimeDB) (string,
reStr := "" reStr := ""
data := Map{} data := Map{}
var ruleData 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 { if v == nil {
continue continue
} }
@ -682,12 +720,22 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
data := Map{} data := Map{}
keyword := Map{} keyword := Map{}
daterange := Map{} daterange := Map{}
sort := Map{} sortMap := Map{}
var ruleData Map var ruleData Map
hasUser := false hasUser := false
keywordStr := req.FormValue("keyword") 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") { 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" { if searchItem.GetString("name") == "sort" {
sort["ORDER"] = table + "." + reqValue sortMap["ORDER"] = table + "." + reqValue
} }
continue continue
@ -823,8 +871,8 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
data[table+"."+searchItemName] = reqValue data[table+"."+searchItemName] = reqValue
} }
if sort["ORDER"] == nil { if sortMap["ORDER"] == nil {
sort["ORDER"] = table + ".id DESC" sortMap["ORDER"] = table + ".id DESC"
} }
where := Map{} where := Map{}
@ -869,8 +917,8 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
} }
if len(sort) != 0 { if len(sortMap) != 0 {
for k, v := range sort { for k, v := range sortMap {
where[k] = v where[k] = v
} }
} }

View File

@ -15,6 +15,7 @@ var Project = Proj{
{{tablesCtr}} {{tablesCtr}}
"hotime":Ctr{ "hotime":Ctr{
"login": func(that *Context) { "login": func(that *Context) {
name := that.Req.FormValue("name") name := that.Req.FormValue("name")
password := that.Req.FormValue("password") password := that.Req.FormValue("password")
if name == "" || password == "" { if name == "" || password == "" {
@ -36,8 +37,9 @@ var Project = Proj{
that.Display(0, "退出登录成功") that.Display(0, "退出登录成功")
}, },
"info": func(that *Context) { "info": func(that *Context) {
hotimeName := that.RouterString[0]
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()}) 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()} where := Map{"id": that.Session("admin_id").ToCeilInt()}
if len(inData) ==1 { if len(inData) ==1 {
inData["id"] =where["id"] inData["id"] =where["id"]
@ -52,7 +54,7 @@ var Project = Proj{
return return
} }
for k, v := range re { for k, v := range re {
column := that.MakeCode.TableColumns["admin"][k] column := that.MakeCodeRouter[hotimeName].TableColumns["admin"][k]
if column == nil { if column == nil {
continue continue
} }
@ -76,8 +78,9 @@ import (
var {{table}}Ctr = Ctr{ var {{table}}Ctr = Ctr{
"info": func(that *Context) { "info": func(that *Context) {
hotimeName := that.RouterString[0]
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()}) 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]} where := Map{"id": that.RouterString[2]}
if len(inData) ==1 { if len(inData) ==1 {
@ -96,7 +99,7 @@ var {{table}}Ctr = Ctr{
} }
for k, v := range re { 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 { if column == nil {
continue continue
} }
@ -108,7 +111,8 @@ var {{table}}Ctr = Ctr{
that.Display(0, re) that.Display(0, re)
}, },
"add": func(that *Context) { "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 { if inData == nil {
that.Display(3, "请求参数不足") that.Display(3, "请求参数不足")
return return
@ -134,7 +138,8 @@ var {{table}}Ctr = Ctr{
that.Display(0, re) that.Display(0, re)
}, },
"update": func(that *Context) { "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 { if inData == nil {
that.Display(3, "没有找到要更新的数据") that.Display(3, "没有找到要更新的数据")
return return
@ -165,7 +170,8 @@ var {{table}}Ctr = Ctr{
that.Display(0, re) that.Display(0, re)
}, },
"remove": func(that *Context) { "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 { if inData == nil {
that.Display(3, "请求参数不足") that.Display(3, "请求参数不足")
return return
@ -186,10 +192,10 @@ var {{table}}Ctr = Ctr{
}, },
"search": func(that *Context) { "search": func(that *Context) {
hotimeName := that.RouterString[0]
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()}) 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")) page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize")) pageSize := ObjToInt(that.Req.FormValue("pageSize"))
@ -208,7 +214,7 @@ var {{table}}Ctr = Ctr{
for _, v := range reData { for _, v := range reData {
for k, _ := range v { for k, _ := range v {
column := that.MakeCode.TableColumns[that.RouterString[1]][k] column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k]
if column == nil { if column == nil {
continue continue
} }

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"reflect" "reflect"
"sort"
) )
//hotime的常用map //hotime的常用map
@ -98,6 +99,22 @@ func (that Map) GetBool(key string, err ...*Error) bool {
return v return v
}
func (that Map) RangeSort(callback func(k string, v interface{}) (isEnd bool)) {
testQu := []string{}
//testQuData:= qu[0].(Map)
for key, _ := range that {
//fmt.Println(key, ":", value)
testQu = append(testQu, key)
}
sort.Strings(testQu)
for _, k := range testQu {
re := callback(k, that[k])
if re {
return
}
}
} }
func (that Map) GetMap(key string, err ...*Error) Map { func (that Map) GetMap(key string, err ...*Error) Map {

View File

@ -10,6 +10,7 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os" "os"
"reflect" "reflect"
"sort"
"strings" "strings"
) )
@ -447,7 +448,16 @@ func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map {
} }
if join { if join {
for k, v := range qu[0].(Map) { var testQu = []string{}
testQuData := qu[0].(Map)
for key, _ := range testQuData {
//fmt.Println(key, ":", value)
testQu = append(testQu, key)
}
sort.Strings(testQu)
for _, k := range testQu {
v := testQuData[k]
switch Substr(k, 0, 3) { switch Substr(k, 0, 3) {
case "[>]": case "[>]":
query += " LEFT JOIN " + Substr(k, 3, len(k)-3) + " ON " + v.(string) query += " LEFT JOIN " + Substr(k, 3, len(k)-3) + " ON " + v.(string)
@ -560,7 +570,15 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
res := make([]interface{}, 0) res := make([]interface{}, 0)
//AND OR判断 //AND OR判断
for k, v := range data { testQu := []string{}
//testQuData:= qu[0].(Map)
for key, _ := range data {
//fmt.Println(key, ":", value)
testQu = append(testQu, key)
}
sort.Strings(testQu)
for _, k := range testQu {
v := data[k]
x := 0 x := 0
for i := 0; i < len(condition); i++ { for i := 0; i < len(condition); i++ {
@ -607,7 +625,15 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
//特殊字符 //特殊字符
for j := 0; j < len(vcond); j++ { for j := 0; j < len(vcond); j++ {
for k, v := range data { testQu := []string{}
//testQuData:= qu[0].(Map)
for key, _ := range data {
//fmt.Println(key, ":", value)
testQu = append(testQu, key)
}
sort.Strings(testQu)
for _, k := range testQu {
v := data[k]
if vcond[j] == k { if vcond[j] == k {
if k == "ORDER" { if k == "ORDER" {
where += " " + k + " BY " where += " " + k + " BY "
@ -881,7 +907,15 @@ func (that *HoTimeDB) cond(tag string, data Map) (string, []interface{}) {
res := make([]interface{}, 0) res := make([]interface{}, 0)
lens := len(data) lens := len(data)
//fmt.Println(lens) //fmt.Println(lens)
for k, v := range data { testQu := []string{}
//testQuData:= qu[0].(Map)
for key, _ := range data {
//fmt.Println(key, ":", value)
testQu = append(testQu, key)
}
sort.Strings(testQu)
for _, k := range testQu {
v := data[k]
x := 0 x := 0
for i := 0; i < len(condition); i++ { for i := 0; i < len(condition); i++ {

View File

@ -12,12 +12,9 @@
} }
}, },
"codeConfig": { "codeConfig": {
"admin": "config/app.json"
},
"codeConfig1": {
"admin": { "admin": {
"config": "config/app.json", "config": "config/app.json",
"package": "admin", "name": "admin",
"rule": "config/rule.json" "rule": "config/rule.json"
} }
}, },
@ -44,7 +41,7 @@
"5": "数据结果异常" "5": "数据结果异常"
}, },
"imgPath": "img/2006/01/02/", "imgPath": "img/2006/01/02/",
"mode": 3, "mode": 0,
"port": "8081", "port": "8081",
"sessionName": "HOTIME", "sessionName": "HOTIME",
"smsKey": "b0eb4bf0198b9983cffcb85b69fdf4fa", "smsKey": "b0eb4bf0198b9983cffcb85b69fdf4fa",

View File

@ -21,7 +21,13 @@
"注释": "可配置memorydbredis默认启用memory默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用" "注释": "可配置memorydbredis默认启用memory默认优先级为memory\u003eredis\u003edb,memory与数据库缓存设置项一致缓存数据填充会自动反方向反哺加入memory缓存过期将自动从redis更新但memory永远不会更新redis如果是集群建议不要开启memory配置即启用"
}, },
"codeConfig": { "codeConfig": {
"packageName": "默认无必须包名称以及应用名生成代码的配置文件地址比如config/app.json数据库有更新时自动更新配置文件以及对应的生成文件", "admin": {
"config": "默认config/app.json必须接口描述配置文件",
"mode": "默认0非必须0为内嵌代码模式1为生成代码模式",
"name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码",
"rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成",
"注释": "默认admin非必须有则根据数据库内当前表名做为用户生成数据"
},
"注释": "配置即启用,非必须,默认无" "注释": "配置即启用,非必须,默认无"
}, },
"crossDomain": "默认空 非必须空字符串为不开启如果需要跨域设置auto为智能开启所有网站允许跨域http://www.baidu.com为指定域允许跨域", "crossDomain": "默认空 非必须空字符串为不开启如果需要跨域设置auto为智能开启所有网站允许跨域http://www.baidu.com为指定域允许跨域",

View File

@ -2,6 +2,7 @@ package main
import ( import (
"code.hoteas.com/golang/hotime" "code.hoteas.com/golang/hotime"
//. "code.hoteas.com/golang/hotime/common"
"fmt" "fmt"
"time" "time"
) )
@ -11,5 +12,7 @@ func main() {
fmt.Println(date, date.Unix()) fmt.Println(date, date.Unix())
//fmt.Println("0123456"[1:7]) //fmt.Println("0123456"[1:7])
appIns := hotime.Init("config/config.json") appIns := hotime.Init("config/config.json")
//a:=Map{}
//a.GetBool()
appIns.Run(hotime.Router{}) appIns.Run(hotime.Router{})
} }

32
var.go
View File

@ -10,15 +10,17 @@ var App = map[string]*Application{} //整个项目
//var Db = HoTimeDB{} //数据库实例 //var Db = HoTimeDB{} //数据库实例
var Config = Map{ var Config = Map{
"mode": 3, //模式 0生产模式1测试模式2开发模式,3内嵌代码模式 "mode": 2, //模式 0生产模式1测试模式2开发模式
//"codeConfig": Map{
// "admin": "config/app.json",
//},
"codeConfig": Map{ "codeConfig": Map{
"admin": "config/app.json", "admin": Map{
}, //"table":"admin",//默认admin必须有则根据数据库内当前表名做为用户生成数据
"codeConfig1": Map{ "name": "", //默认admin非必须有则生成代码到此目录无则采用缺省模式使用表名
"admin": Map{ //默认无,必须,接口类别名称
"package": "admin", //默认admin非必须有则生成代码到此目录无则采用缺省模式不生成代码只生成接口
"config": "config/app.json", //默认config/app.json必须接口描述配置文件 "config": "config/app.json", //默认config/app.json必须接口描述配置文件
"rule": "config/rule.json", //默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成 "rule": "config/rule.json", //默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成
"mode": 0, //默认0非必须0为内嵌代码模式1为生成代码模式
}, },
}, },
"db": Map{ "db": Map{
@ -52,12 +54,22 @@ var ConfigNote = Map{
"webConnectLogShow": "默认true非必须访问日志如果需要web访问链接、访问ip、访问时间打印false为关闭true开启此功能", "webConnectLogShow": "默认true非必须访问日志如果需要web访问链接、访问ip、访问时间打印false为关闭true开启此功能",
"webConnectLogFile": "无默认非必须webConnectLogShow开启之后才能使用如果需要存储日志文件时使用保存格式为:a/b/c/20060102150405.txt,将生成a/b/c/年月日时分秒.txt按需设置", "webConnectLogFile": "无默认非必须webConnectLogShow开启之后才能使用如果需要存储日志文件时使用保存格式为:a/b/c/20060102150405.txt,将生成a/b/c/年月日时分秒.txt按需设置",
"mode": "默认0,非必须0生产模式1测试模式2开发模式3内嵌代码模式在开发模式下会显示更多的数据用于开发测试并能够辅助研发自动生成配置文件、代码等功能,web无缓存数据库不启用缓存", //debug 0关闭1开启 "mode": "默认0,非必须0生产模式1测试模式2开发模式3内嵌代码模式在开发模式下会显示更多的数据用于开发测试并能够辅助研发自动生成配置文件、代码等功能,web无缓存数据库不启用缓存", //debug 0关闭1开启
//"codeConfig": Map{
// "注释": "配置即启用,非必须,默认无",
// //"package":"默认admin必须mode模式为2时会自动生成包文件夹和代码文件",
// //"path":""
//
// "packageName": "默认无必须包名称以及应用名生成代码的配置文件地址比如config/app.json数据库有更新时自动更新配置文件以及对应的生成文件",
//},
"codeConfig": Map{ "codeConfig": Map{
"注释": "配置即启用,非必须,默认无", "注释": "配置即启用,非必须,默认无",
//"package":"默认admin必须mode模式为2时会自动生成包文件夹和代码文件", "admin": Map{ //默认无,必须,接口类别名称
//"path":"" "注释": "默认admin非必须有则根据数据库内当前表名做为用户生成数据", //
"name": "默认无非必须有则生成代码到此目录无则采用缺省模式使用表名如设置为admin将在admin目录生成包名为admin的代码",
"packageName": "默认无必须包名称以及应用名生成代码的配置文件地址比如config/app.json数据库有更新时自动更新配置文件以及对应的生成文件", "config": "默认config/app.json必须接口描述配置文件", //
"rule": "默认config/rule.json非必须有则按改规则生成接口无则按系统内嵌方式生成",
"mode": "默认0非必须0为内嵌代码模式1为生成代码模式",
},
}, },
"db": Map{ "db": Map{
"注释": "配置即启用非必须默认使用sqlite数据库", "注释": "配置即启用非必须默认使用sqlite数据库",