hotime/code.go
2022-07-08 09:55:02 +08:00

389 lines
12 KiB
Go

package hotime
import (
. "code.hoteas.com/golang/hotime/common"
"io"
"os"
"strings"
"time"
)
// Project 管理端项目
var TptProject = Proj{
//"user": UserCtr,
"hotimeCommon": Ctr{
"info": func(that *Context) {
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()})
str, inData := that.MakeCodeRouter[hotimeName].Info(that.RouterString[1], data, that.Db)
where := Map{"id": that.RouterString[2]}
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(that.RouterString[1], str, where)
if re == nil {
that.Display(4, "找不到对应信息")
return
}
for k, v := range re {
column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k]
if column == nil {
continue
}
if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" {
seStr := "id," + column.GetString("value")
if that.MakeCodeRouter[hotimeName].TableColumns[column.GetString("link")]["phone"] != nil {
seStr = seStr + ",phone"
}
link := strings.Replace(column.GetString("name"), "_id", "", -1)
if link == "parent" {
link = that.RouterString[1]
}
re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v})
}
}
//如果有table字段则代为link
if re["table"] != nil && re["table_id"] != nil {
column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][re.GetString("table")]
v := re.GetCeilInt64("table_id")
seStr := "id," + column.GetString("value")
if that.MakeCodeRouter[hotimeName].TableColumns[column.GetString("link")]["phone"] != nil {
seStr = seStr + ",phone"
}
link := strings.Replace(column.GetString("name"), "_id", "", -1)
if link == "parent" {
link = that.RouterString[1]
}
re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v})
}
that.Display(0, re)
},
"add": func(that *Context) {
that.Log = Map{"table": that.RouterString[1], "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)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
re := that.Db.Insert(that.RouterString[1], inData)
if re == 0 {
that.Display(4, "无法插入对应数据")
return
}
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetString("index") != "" {
index := that.Db.Get(that.RouterString[1], "`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})
} else if inData.GetString("index") != "" {
inData["index"] = "," + ObjToStr(re) + ","
that.Db.Update(that.RouterString[1], 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]}
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)
},
"remove": func(that *Context) {
that.Log = Map{"table": that.RouterString[1], "type": 3, "table_id": that.RouterString[2]}
hotimeName := that.RouterString[0]
inData := that.MakeCodeRouter[hotimeName].Delete(that.RouterString[1], that.Req)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
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] + ","})
} else {
re = that.Db.Delete(that.RouterString[1], Map{"id": that.RouterString[2]})
}
if re == 0 {
that.Display(4, "删除数据失败")
return
}
that.Display(0, "删除成功")
},
"search": func(that *Context) {
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()})
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"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
count := that.Db.Count(that.RouterString[1], leftJoin, where)
reData := that.Db.Page(page, pageSize).
PageSelect(that.RouterString[1], leftJoin, columnStr, where)
for _, v := range reData {
v.RangeSort(func(k string, v1 interface{}) (isEnd bool) {
//如果有table字段则代为link
if v["table"] != nil && v["table_id"] != nil {
id := v.GetCeilInt64("table_id")
tableName := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v.GetString("table")).GetString("label")
v["table_table_name"] = strings.Replace(tableName, "管理", "", -1)
parentC := that.Db.Get(v.GetString("table"), "name", Map{"id": id})
v["table_table_id_name"] = ""
if parentC != nil {
v["table_table_id_name"] = parentC.GetString("name")
}
}
column := that.MakeCodeRouter[hotimeName].TableColumns[that.RouterString[1]][k]
if column == nil {
return isEnd
}
if (column["list"] == nil || column["list"] == true) && column["name"] == "parent_id" && column.GetString("link") != "" {
parentC := that.Db.Get(column.GetString("link"), column.GetString("value"), Map{"id": v.GetCeilInt(k)})
v[column.GetString("link")+"_"+column.GetString("name")+"_"+column.GetString("value")] = ""
if parentC != nil {
v[column.GetString("link")+"_"+column.GetString("name")+"_"+column.GetString("value")] = parentC.GetString(column.GetString("value"))
}
}
return isEnd
})
}
that.Display(0, Map{"count": count, "data": reData})
},
},
"hotime": Ctr{
"file": func(that *Context) {
//文件上传接口
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
if that.Session(fileConfig.GetString("table")+"_id").Data == nil {
that.Display(2, "你还没有登录")
return
}
//读取网络文件
fi, fheader, err := that.Req.FormFile("file")
if err != nil {
that.Display(3, err)
return
}
filePath := that.Config.GetString("filePath")
if filePath == "" {
filePath = "/file/2006/01/02/"
}
path := time.Now().Format(filePath)
e := os.MkdirAll(that.Config.GetString("tpt")+path, os.ModeDir)
if e != nil {
that.Display(3, e)
return
}
filePath = path + Md5(ObjToStr(RandX(100000, 9999999))) + fheader.Filename[strings.LastIndex(fheader.Filename, "."):]
newFile, e := os.Create(that.Config.GetString("tpt") + filePath)
if e != nil {
that.Display(3, e)
return
}
_, e = io.Copy(newFile, fi)
if e != nil {
that.Display(3, e)
return
}
that.Display(0, filePath)
},
"login": func(that *Context) {
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
name := that.Req.FormValue("name")
password := that.Req.FormValue("password")
if name == "" || password == "" {
that.Display(3, "参数不足")
return
}
user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"AND": Map{"OR": Map{"name": name, "phone": name}, "password": Md5(password)}})
if user == nil {
that.Display(5, "登录失败")
return
}
that.Session(fileConfig.GetString("table")+"_id", user.GetCeilInt("id"))
that.Session(fileConfig.GetString("table")+"_name", name)
delete(user, "password")
that.Display(0, user)
},
"logout": func(that *Context) {
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
that.Session(fileConfig.GetString("table")+"_id", nil)
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]}
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()})
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, "找不到对应信息")
return
}
for k, v := range re {
column := that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")][k]
if column == nil {
continue
}
if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" {
//是角色表则取下角色值
if column.GetString("link") == "role" {
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})
}
}
}
that.Display(0, re)
},
"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)
},
},
}