iedc-go/code.go

554 lines
16 KiB
Go
Raw Normal View History

2021-12-27 12:40:16 +00:00
package hotime
import (
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/common"
2022-07-08 01:55:02 +00:00
"io"
2022-07-18 11:44:00 +00:00
"io/ioutil"
2022-07-08 01:55:02 +00:00
"os"
2021-12-27 12:40:16 +00:00
"strings"
2022-07-08 01:55:02 +00:00
"time"
2021-12-27 12:40:16 +00:00
)
// Project 管理端项目
var TptProject = Proj{
//"user": UserCtr,
"hotimeCommon": Ctr{
"info": func(that *Context) {
hotimeName := that.RouterString[0]
2022-03-13 09:02:19 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
2022-07-18 11:44:00 +00:00
tableName := that.RouterString[1]
2022-03-26 08:53:40 +00:00
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
2022-07-18 11:44:00 +00:00
str, inData := that.MakeCodeRouter[hotimeName].Info(tableName, data, that.Db)
2021-12-27 12:40:16 +00:00
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}
}
2022-07-18 11:44:00 +00:00
re := that.Db.Get(tableName, str, where)
2021-12-27 12:40:16 +00:00
if re == nil {
that.Display(4, "找不到对应信息")
return
}
for k, v := range re {
2022-07-18 11:44:00 +00:00
column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][k]
2021-12-27 12:40:16 +00:00
if column == nil {
continue
}
2022-03-26 08:53:40 +00:00
2021-12-27 12:40:16 +00:00
if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" {
2022-05-06 03:35:40 +00:00
seStr := "id," + column.GetString("value")
if that.MakeCodeRouter[hotimeName].TableColumns[column.GetString("link")]["phone"] != nil {
seStr = seStr + ",phone"
2022-03-26 08:53:40 +00:00
}
2022-05-06 03:35:40 +00:00
link := strings.Replace(column.GetString("name"), "_id", "", -1)
2022-05-06 05:08:13 +00:00
if link == "parent" {
2022-07-18 11:44:00 +00:00
link = tableName
2022-05-06 05:08:13 +00:00
}
2022-03-26 08:53:40 +00:00
re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v})
2021-12-27 12:40:16 +00:00
}
2022-07-18 11:44:00 +00:00
//权限设置
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)
}
2021-12-27 12:40:16 +00:00
}
2022-03-26 08:53:40 +00:00
//如果有table字段则代为link
2022-05-06 03:35:40 +00:00
if re["table"] != nil && re["table_id"] != nil {
2022-03-26 08:53:40 +00:00
2022-07-18 11:44:00 +00:00
column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][re.GetString("table")]
2022-05-06 03:35:40 +00:00
v := re.GetCeilInt64("table_id")
2022-03-26 08:53:40 +00:00
2022-05-06 03:35:40 +00:00
seStr := "id," + column.GetString("value")
2022-03-26 08:53:40 +00:00
2022-05-06 03:35:40 +00:00
if that.MakeCodeRouter[hotimeName].TableColumns[column.GetString("link")]["phone"] != nil {
seStr = seStr + ",phone"
2022-03-26 08:53:40 +00:00
}
2022-05-06 03:35:40 +00:00
link := strings.Replace(column.GetString("name"), "_id", "", -1)
if link == "parent" {
2022-07-18 11:44:00 +00:00
link = tableName
2022-05-06 03:35:40 +00:00
}
2022-03-26 08:53:40 +00:00
re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v})
}
2021-12-27 12:40:16 +00:00
that.Display(0, re)
},
"add": func(that *Context) {
2022-07-18 11:44:00 +00:00
tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "type": 1}
2022-03-26 08:53:40 +00:00
2022-03-12 21:06:28 +00:00
hotimeName := that.RouterString[0]
2022-03-26 08:53:40 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
2022-07-18 11:44:00 +00:00
inData := that.MakeCodeRouter[hotimeName].Add(tableName, data, that.Req)
2021-12-27 12:40:16 +00:00
if inData == nil {
that.Display(3, "请求参数不足")
return
}
2022-07-18 11:44:00 +00:00
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)
2021-12-27 12:40:16 +00:00
if re == 0 {
that.Display(4, "无法插入对应数据")
return
}
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetString("index") != "" {
2022-07-18 11:44:00 +00:00
index := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")})
2021-12-27 12:40:16 +00:00
inData["index"] = index.GetString("index") + ObjToStr(re) + ","
2022-07-18 11:44:00 +00:00
that.Db.Update(tableName, Map{"index": inData["index"]}, Map{"id": re})
2021-12-27 12:40:16 +00:00
} else if inData.GetString("index") != "" {
inData["index"] = "," + ObjToStr(re) + ","
2022-07-18 11:44:00 +00:00
that.Db.Update(tableName, Map{"index": inData["index"]}, Map{"id": re})
2021-12-27 12:40:16 +00:00
}
2022-05-06 03:35:40 +00:00
that.Log["table_id"] = re
2021-12-27 12:40:16 +00:00
that.Display(0, re)
},
"update": func(that *Context) {
2022-07-18 11:44:00 +00:00
tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "type": 2, "table_id": that.RouterString[2]}
2022-03-26 08:53:40 +00:00
2022-03-12 21:06:28 +00:00
hotimeName := that.RouterString[0]
2022-07-18 11:44:00 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
inData := that.MakeCodeRouter[hotimeName].Edit(tableName, that.Req)
2022-03-26 08:53:40 +00:00
2021-12-27 12:40:16 +00:00
if inData == nil {
that.Display(3, "没有找到要更新的数据")
return
}
2022-07-18 11:44:00 +00:00
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
}
}
}
}
2021-12-27 12:40:16 +00:00
//索引管理,便于检索以及权限
2022-01-16 20:47:39 +00:00
if inData.GetString("index") != "" {
if inData.Get("parent_id") != nil {
2022-07-18 11:44:00 +00:00
Index := that.Db.Get(tableName, "`index`", Map{"id": that.RouterString[2]})
parentIndex := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")})
2022-01-16 20:47:39 +00:00
inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + ","
2022-07-18 11:44:00 +00:00
childNodes := that.Db.Select(tableName, "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","})
2022-01-16 20:47:39 +00:00
for _, v := range childNodes {
v["index"] = strings.Replace(v.GetString("index"), Index.GetString("index"), inData.GetString("index"), -1)
2022-07-18 11:44:00 +00:00
that.Db.Update(tableName, Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")})
2022-01-16 20:47:39 +00:00
}
} else {
delete(inData, "index")
2021-12-27 12:40:16 +00:00
}
}
2022-07-18 11:44:00 +00:00
re := that.Db.Update(tableName, inData, Map{"id": that.RouterString[2]})
2021-12-27 12:40:16 +00:00
if re == 0 {
that.Display(4, "更新数据失败")
return
}
that.Display(0, re)
},
"remove": func(that *Context) {
2022-07-18 11:44:00 +00:00
tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "type": 3, "table_id": that.RouterString[2]}
2022-03-12 21:06:28 +00:00
hotimeName := that.RouterString[0]
2022-07-18 11:44:00 +00:00
inData := that.MakeCodeRouter[hotimeName].Delete(tableName, that.Req)
2021-12-27 12:40:16 +00:00
if inData == nil {
that.Display(3, "请求参数不足")
return
}
re := int64(0)
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetSlice("index") != nil {
2022-07-18 11:44:00 +00:00
re = that.Db.Delete(tableName, Map{"index[~]": "," + that.RouterString[2] + ","})
2021-12-27 12:40:16 +00:00
} else {
2022-07-18 11:44:00 +00:00
re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]})
2021-12-27 12:40:16 +00:00
}
if re == 0 {
that.Display(4, "删除数据失败")
return
}
that.Display(0, "删除成功")
},
"search": func(that *Context) {
hotimeName := that.RouterString[0]
2022-03-13 09:02:19 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
2022-07-18 11:44:00 +00:00
tableName := that.RouterString[1]
2022-03-26 08:53:40 +00:00
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
2021-12-27 12:40:16 +00:00
2022-07-18 11:44:00 +00:00
columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(tableName, data, that.Req, that.Db)
2021-12-27 12:40:16 +00:00
page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
2022-07-18 11:44:00 +00:00
count := that.Db.Count(tableName, leftJoin, where)
2021-12-27 12:40:16 +00:00
reData := that.Db.Page(page, pageSize).
2022-07-18 11:44:00 +00:00
PageSelect(tableName, leftJoin, columnStr, where)
2021-12-27 12:40:16 +00:00
for _, v := range reData {
2022-03-12 21:06:28 +00:00
v.RangeSort(func(k string, v1 interface{}) (isEnd bool) {
2022-05-06 03:35:40 +00:00
2022-03-26 08:53:40 +00:00
//如果有table字段则代为link
2022-05-06 03:35:40 +00:00
if v["table"] != nil && v["table_id"] != nil {
2022-03-26 08:53:40 +00:00
2022-05-06 03:35:40 +00:00
id := v.GetCeilInt64("table_id")
2022-03-26 08:53:40 +00:00
tableName := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v.GetString("table")).GetString("label")
2022-05-06 03:35:40 +00:00
v["table_table_name"] = strings.Replace(tableName, "管理", "", -1)
2022-03-26 08:53:40 +00:00
parentC := that.Db.Get(v.GetString("table"), "name", Map{"id": id})
v["table_table_id_name"] = ""
if parentC != nil {
2022-05-06 03:35:40 +00:00
v["table_table_id_name"] = parentC.GetString("name")
2022-03-26 08:53:40 +00:00
}
}
2022-07-18 11:44:00 +00:00
column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][k]
2022-05-06 03:35:40 +00:00
2021-12-27 12:40:16 +00:00
if column == nil {
2022-05-06 03:35:40 +00:00
return isEnd
2021-12-27 12:40:16 +00:00
}
2022-05-06 03:35:40 +00:00
if (column["list"] == nil || column["list"] == true) && column["name"] == "parent_id" && column.GetString("link") != "" {
2022-03-26 08:53:40 +00:00
2021-12-27 12:40:16 +00:00
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"))
}
}
2022-03-12 21:06:28 +00:00
return isEnd
})
2021-12-27 12:40:16 +00:00
}
that.Display(0, Map{"count": count, "data": reData})
},
},
"hotime": Ctr{
2022-07-08 01:55:02 +00:00
"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)
},
2022-03-12 17:48:54 +00:00
"login": func(that *Context) {
hotimeName := that.RouterString[0]
2022-03-12 21:06:28 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
2022-03-12 17:48:54 +00:00
name := that.Req.FormValue("name")
password := that.Req.FormValue("password")
2021-12-27 12:40:16 +00:00
if name == "" || password == "" {
2022-03-12 17:48:54 +00:00
that.Display(3, "参数不足")
2021-12-27 12:40:16 +00:00
return
}
2022-03-12 21:06:28 +00:00
user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"AND": Map{"OR": Map{"name": name, "phone": name}, "password": Md5(password)}})
2021-12-27 12:40:16 +00:00
if user == nil {
2022-03-12 17:48:54 +00:00
that.Display(5, "登录失败")
2021-12-27 12:40:16 +00:00
return
}
2022-03-12 21:06:28 +00:00
that.Session(fileConfig.GetString("table")+"_id", user.GetCeilInt("id"))
that.Session(fileConfig.GetString("table")+"_name", name)
2022-03-06 15:55:14 +00:00
delete(user, "password")
2022-07-18 11:44:00 +00:00
user["table"] = fileConfig.GetString("table")
2022-03-12 17:48:54 +00:00
that.Display(0, user)
2021-12-27 12:40:16 +00:00
},
2022-03-12 17:48:54 +00:00
"logout": func(that *Context) {
hotimeName := that.RouterString[0]
2022-03-12 21:06:28 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
that.Session(fileConfig.GetString("table")+"_id", nil)
that.Session(fileConfig.GetString("table")+"_name", nil)
2022-03-12 17:48:54 +00:00
that.Display(0, "退出登录成功")
2021-12-27 12:40:16 +00:00
},
2022-07-18 11:44:00 +00:00
"config": func(that *Context) {
2021-12-27 12:40:16 +00:00
hotimeName := that.RouterString[0]
2022-03-12 21:06:28 +00:00
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
2022-07-18 11:44:00 +00:00
if that.Session(fileConfig.GetString("table")+"_id").Data == nil {
2022-03-12 21:06:28 +00:00
2022-07-18 11:44:00 +00:00
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
2021-12-27 12:40:16 +00:00
}
2022-07-18 11:44:00 +00:00
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, "暂未登录")
2021-12-27 12:40:16 +00:00
return
}
2022-07-18 11:44:00 +00:00
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")]
}
}
}
2021-12-27 12:40:16 +00:00
}
2022-07-18 11:44:00 +00:00
}
2022-01-12 09:33:20 +00:00
2022-07-18 11:44:00 +00:00
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")]
}
}
}
2022-01-12 09:33:20 +00:00
2022-07-18 11:44:00 +00:00
config := DeepCopyMap(that.MakeCodeRouter[hotimeName].Config).(Map)
2022-01-12 09:33:20 +00:00
2022-07-18 11:44:00 +00:00
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"))
}
2022-01-12 09:33:20 +00:00
2022-07-18 11:44:00 +00:00
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"))
2022-01-12 09:33:20 +00:00
}
2021-12-27 12:40:16 +00:00
}
}
2022-07-18 11:44:00 +00:00
config["tables"] = menus
that.Display(0, config)
2022-07-08 01:55:02 +00:00
},
2022-07-18 11:44:00 +00:00
},
2021-12-27 12:40:16 +00:00
}