hotime/code.go

977 lines
28 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-31 19:40:09 +00:00
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
2022-07-08 01:55:02 +00:00
"io"
2022-07-18 11:44:00 +00:00
"io/ioutil"
2022-10-08 08:52:42 +00:00
"net/http"
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-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-26 09:37:17 +00:00
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
if v.GetString("link") != "" {
2022-08-24 21:01:50 +00:00
//与用户信息有关联的parent_id自填充
2022-07-26 09:37:17 +00:00
if v.GetString("link") == tableName && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["parent_id"] != nil {
2022-08-03 18:41:41 +00:00
if inData["parent_id"] == nil && data[v.GetString("name")] != nil {
2022-07-26 09:37:17 +00:00
inData["parent_id"] = data.GetCeilInt64(v.GetString("name"))
2022-08-24 21:01:50 +00:00
//pre := that.Db.Get(v.GetString("link"), "parent_ids", Map{"id": data.GetCeilInt64(v.GetString("name"))})
//inData["parent_ids"] = pre.GetString("parent_ids")+ObjToStr(data.GetCeilInt64(v.GetString("name")))+","
2022-07-26 09:37:17 +00:00
}
}
2022-07-27 09:09:56 +00:00
}
2022-08-03 18:41:41 +00:00
if v.GetString("link") == tableName && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] != nil {
linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": data.GetCeilInt(v.GetString("name"))})
linkAuth := linkAuthMap.GetMap("auth")
2022-07-27 09:09:56 +00:00
2022-08-03 18:41:41 +00:00
if linkAuth == nil {
btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
if err != nil {
that.Display(4, "找不到配置文件")
return
2022-07-27 09:09:56 +00:00
}
2022-08-03 18:41:41 +00:00
linkAuth = Map{}
conf := ObjToMap(string(btes))
menus := conf.GetSlice("menus")
for k1, _ := range menus {
v1 := menus.GetMap(k1)
name := v1.GetString("name")
if name == "" {
name = v1.GetString("table")
2022-07-27 09:09:56 +00:00
}
2022-08-03 18:41:41 +00:00
if v1["auth"] != nil {
2022-07-27 09:09:56 +00:00
2022-08-24 19:44:58 +00:00
if linkAuth[name] == nil {
linkAuth[name] = v1["auth"]
2022-07-27 09:09:56 +00:00
} else {
2022-08-24 19:44:58 +00:00
newAuth := Slice{}
for k2, _ := range linkAuth.GetSlice(name) {
v2 := linkAuth.GetSlice(name).GetString(k2)
if strings.Contains(v1.GetString("auth"), v2) {
newAuth = append(newAuth, v2)
}
}
linkAuth[name] = newAuth
2022-07-27 09:09:56 +00:00
}
}
2022-08-03 18:41:41 +00:00
menusChild := v1.GetSlice("menus")
for k2, _ := range menusChild {
v2 := menusChild.GetMap(k2)
name := v2.GetString("name")
if name == "" {
name = v2.GetString("table")
2022-07-27 09:09:56 +00:00
}
2022-08-03 18:41:41 +00:00
if v2["auth"] != nil {
2022-08-24 19:44:58 +00:00
if linkAuth[name] == nil {
linkAuth[name] = v2["auth"]
2022-07-27 09:09:56 +00:00
} else {
2022-08-24 19:44:58 +00:00
newAuth := Slice{}
for k3, _ := range linkAuth.GetSlice(name) {
v3 := linkAuth.GetSlice(name).GetString(k3)
if strings.Contains(v2.GetString("auth"), v3) {
newAuth = append(newAuth, v3)
}
}
linkAuth[name] = newAuth
continue
2022-07-27 09:09:56 +00:00
}
2022-08-03 18:41:41 +00:00
}
}
}
}
2022-07-27 09:09:56 +00:00
2022-08-03 18:41:41 +00:00
toDB := Map{}
newAuth := inData.GetMap("auth")
if newAuth == nil {
inData["auth"] = linkAuth.ToJsonString()
continue
}
2022-07-27 09:09:56 +00:00
2022-08-03 18:41:41 +00:00
for k1, _ := range newAuth {
v1 := newAuth.GetSlice(k1)
if linkAuth.GetString(k1) == "" {
continue
}
toDbli := Slice{}
for k2, _ := range v1 {
v2 := v1.GetString(k2)
2022-07-18 11:44:00 +00:00
2022-08-03 18:41:41 +00:00
if !strings.Contains(linkAuth.GetString(k1), `"`+v2+`"`) {
continue
2022-07-18 11:44:00 +00:00
}
2022-08-03 18:41:41 +00:00
toDbli = append(toDbli, v2)
2022-07-18 11:44:00 +00:00
}
2022-08-03 18:41:41 +00:00
toDB[k1] = toDbli
2022-07-18 11:44:00 +00:00
}
2022-08-03 18:41:41 +00:00
inData["auth"] = toDB.ToJsonString()
//break
2022-07-18 11:44:00 +00:00
}
}
re := that.Db.Insert(tableName, inData)
2021-12-27 12:40:16 +00:00
if re == 0 {
that.Display(4, "无法插入对应数据")
return
}
//索引管理,便于检索以及权限
2022-08-24 21:01:50 +00:00
if inData.Get("parent_id") != nil {
2022-07-26 09:37:17 +00:00
index := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")})
2022-08-24 21:01:50 +00:00
parent_ids := index.GetString("parent_ids")
if parent_ids == "" {
parent_ids = ","
}
inData["parent_ids"] = parent_ids + ObjToStr(re) + ","
2022-07-24 21:37:19 +00:00
that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re})
2021-12-27 12:40:16 +00:00
}
2022-08-24 21:01:50 +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
2022-07-27 09:09:56 +00:00
2022-07-18 11:44:00 +00:00
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-08-03 18:41:41 +00:00
2022-07-27 09:09:56 +00:00
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
//树状结构不允许修改自身的属性,修改别人的可以
2022-08-03 18:41:41 +00:00
btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
if err != nil {
that.Display(4, "找不到权限配置文件")
return
}
2022-07-27 09:09:56 +00:00
2022-08-03 18:41:41 +00:00
conf := ObjToMap(string(btes))
2022-07-27 09:09:56 +00:00
2022-08-03 18:41:41 +00:00
stop := conf.GetSlice("stop")
for k, _ := range stop {
v := stop.GetString(k)
//不能改对应行数据
if tableName == v && data.GetCeilInt64(v+"_id") == ObjToCeilInt64(that.RouterString[2]) {
that.Display(4, "你没有权限修改当前数据")
return
}
//不能改自身对应的数据
if tableName == fileConfig.GetString("table") && inData[v+"_id"] != nil {
delete(inData, v+"_id")
2022-07-27 09:09:56 +00:00
}
}
2021-12-27 12:40:16 +00:00
2022-07-18 11:44:00 +00:00
if inData["auth"] != nil {
menus := conf.GetSlice("menus")
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
2022-08-03 18:41:41 +00:00
if v.GetString("link") != "" && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] != nil {
2022-07-27 09:09:56 +00:00
linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": data.GetCeilInt(v.GetString("name"))})
linkAuth := linkAuthMap.GetMap("auth")
2022-08-24 19:44:58 +00:00
//conf := ObjToMap(string(btes))
//menus := conf.GetSlice("menus")
2022-08-03 18:41:41 +00:00
if linkAuth == nil {
linkAuth = Map{}
2022-08-24 19:44:58 +00:00
}
for k1, _ := range menus {
v1 := menus.GetMap(k1)
name := v1.GetString("name")
if name == "" {
name = v1.GetString("table")
}
if v1["auth"] != nil {
if linkAuth[name] == nil {
2022-08-03 18:41:41 +00:00
linkAuth[name] = v1["auth"]
2022-08-24 19:44:58 +00:00
} else {
newAuth := Slice{}
for k2, _ := range linkAuth.GetSlice(name) {
v2 := linkAuth.GetSlice(name).GetString(k2)
if strings.Contains(v1.GetString("auth"), v2) {
newAuth = append(newAuth, v2)
}
}
linkAuth[name] = newAuth
2022-07-27 09:09:56 +00:00
}
2022-08-24 19:44:58 +00:00
}
2022-07-27 09:09:56 +00:00
2022-08-24 19:44:58 +00:00
menusChild := v1.GetSlice("menus")
for k2, _ := range menusChild {
v2 := menusChild.GetMap(k2)
name := v2.GetString("name")
if name == "" {
name = v2.GetString("table")
2022-08-03 18:41:41 +00:00
}
2022-08-24 19:44:58 +00:00
if v2["auth"] != nil {
if linkAuth[name] == nil {
2022-08-03 18:41:41 +00:00
linkAuth[name] = v2["auth"]
2022-08-24 19:44:58 +00:00
} else {
newAuth := Slice{}
for k3, _ := range linkAuth.GetSlice(name) {
v3 := linkAuth.GetSlice(name).GetString(k3)
if strings.Contains(v2.GetString("auth"), v3) {
newAuth = append(newAuth, v3)
}
2022-08-03 18:41:41 +00:00
}
2022-08-24 19:44:58 +00:00
linkAuth[name] = newAuth
continue
2022-07-18 11:44:00 +00:00
}
}
}
2022-07-27 09:09:56 +00:00
}
2022-07-18 11:44:00 +00:00
2022-07-27 09:09:56 +00:00
toDB := Map{}
2022-08-03 18:41:41 +00:00
newAuth := inData.GetMap("auth")
if newAuth == nil || len(newAuth) == 0 {
inData["auth"] = linkAuth.ToJsonString()
break
}
//不可超出用户权限
2022-07-27 09:09:56 +00:00
for k1, _ := range newAuth {
2022-08-03 18:41:41 +00:00
v1 := newAuth.GetSlice(k1)
if linkAuth.GetString(k1) == "" {
2022-07-27 09:09:56 +00:00
continue
}
2022-08-03 18:41:41 +00:00
toDbli := Slice{}
for k2, _ := range v1 {
v2 := v1.GetString(k2)
if !strings.Contains(linkAuth.GetString(k1), `"`+v2+`"`) {
2022-07-27 09:09:56 +00:00
continue
}
2022-08-03 18:41:41 +00:00
toDbli = append(toDbli, v2)
2022-07-27 09:09:56 +00:00
}
2022-08-03 18:41:41 +00:00
toDB[k1] = toDbli
2022-07-18 11:44:00 +00:00
}
2022-08-03 18:41:41 +00:00
2022-07-27 09:09:56 +00:00
inData["auth"] = toDB.ToJsonString()
break
2022-07-18 11:44:00 +00:00
}
}
2022-08-03 18:41:41 +00:00
2022-07-18 11:44:00 +00:00
}
2022-08-24 21:01:50 +00:00
if inData.Get("parent_id") != nil {
//1
//12 62
//123 623
Index := that.Db.Get(tableName, "parent_id,`parent_ids`", Map{"id": that.RouterString[2]})
if inData.GetCeilInt64("parent_id") != Index.GetCeilInt64("parent_id") {
parentIndex := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")})
if strings.Contains(parentIndex.GetString("parent_ids"), ","+that.RouterString[2]+",") {
that.Display(4, "不能将子级设置为父级")
return
}
parent_ids := parentIndex.GetString("parent_ids") + that.RouterString[2] + ","
2022-08-25 05:02:44 +00:00
childNodes := that.Db.Select(tableName, "id,`parent_ids`", Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
2022-01-16 20:47:39 +00:00
for _, v := range childNodes {
2022-08-24 21:01:50 +00:00
v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), parent_ids, -1)
2022-07-24 21:37:19 +00:00
that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")})
2022-01-16 20:47:39 +00:00
}
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
}
2022-08-24 21:01:50 +00:00
//索引管理,便于检索以及权限
2021-12-27 12:40:16 +00:00
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-08-24 21:01:50 +00:00
2021-12-27 12:40:16 +00:00
re := int64(0)
2022-08-24 21:01:50 +00:00
//有索引则删除对应的参数
if that.MakeCodeRouter[hotimeName].TableColumns[tableName]["parent_id"] != nil {
2022-08-25 05:02:44 +00:00
re = that.Db.Delete(tableName, Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
} else {
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"))
2022-07-31 19:40:09 +00:00
download := ObjToInt(that.Req.FormValue("download"))
2021-12-27 12:40:16 +00:00
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
2022-07-31 19:40:09 +00:00
var count int
var reData []Map
//是否下载
if download == 0 {
count = that.Db.Count(tableName, leftJoin, where)
reData = that.Db.Page(page, pageSize).
PageSelect(tableName, leftJoin, columnStr, where)
}
if download == 1 {
reData = that.Db.Select(tableName, leftJoin, columnStr, where)
}
2021-12-27 12:40:16 +00:00
for _, v := range reData {
2022-08-01 08:45:24 +00:00
for k, _ := range v {
//v1:=v.GetMap(k)
//v.RangeSort(func(k string, v1 interface{}) (isEnd bool) {
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-08-01 08:45:24 +00:00
tableNameLabel := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v.GetString("table")).GetString("label")
//v["table_table_name"] = strings.Replace(tableNameLabel, "管理", "", -1)
v["table_table_name"] = tableNameLabel
2022-07-31 19:40:09 +00:00
sname := "name"
if that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("table")][sname] == nil {
sname = "title"
if that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("table")][sname] == nil {
sname = "id"
}
}
parentC := that.Db.Get(v.GetString("table"), sname, Map{"id": id})
2022-03-26 08:53:40 +00:00
v["table_table_id_name"] = ""
if parentC != nil {
2022-07-31 19:40:09 +00:00
v["table_table_id_name"] = parentC.GetString(sname)
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-08-01 08:45:24 +00:00
break
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-08-01 08:45:24 +00:00
//return isEnd
}
2021-12-27 12:40:16 +00:00
}
2022-07-31 19:40:09 +00:00
if download == 1 {
tableNameLabel := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(tableName).GetString("label")
f := excelize.NewFile()
// 创建一个工作表
f.NewSheet(tableNameLabel)
f.DeleteSheet("Sheet1")
columns := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(tableName).GetSlice("columns")
//单列
n := 0
for k, _ := range columns {
v := columns.GetMap(k)
if v["list"] != nil && v.GetBool("list") == false {
continue
}
n++
//第一个要加状态行
//单行
for k1, v1 := range reData {
if k1 == 0 {
f.SetCellValue(tableNameLabel, convertToTitle(n)+"1", v.GetString("label"))
}
if v.GetString("link") != "" {
f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString(v.GetString("link")+"_"+v.GetString("name")+"_"+v.GetString("value")))
continue
}
if v.GetString("name") == "table" {
2022-08-01 08:45:24 +00:00
f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString("table_"+v.GetString("name")+"_name"))
2022-07-31 19:40:09 +00:00
continue
}
if v.GetString("name") == "table_id" {
f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString("table_"+v.GetString("name")+"_name"))
continue
}
//select类型
options := v.GetSlice("options")
if len(options) != 0 {
isEnd := false
for ok, _ := range options {
ov := options.GetMap(ok)
if ov.GetString("value") == v1.GetString(v.GetString("name")) {
f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), ov.GetString("name"))
isEnd = true
break
}
}
if isEnd {
continue
}
}
f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString(v.GetString("name")))
}
}
filePath := that.Config.GetString("filePath")
if filePath == "" {
filePath = "/file/temp/"
}
//path := time.Now().Format(filePath)
e := os.MkdirAll(that.Config.GetString("tpt")+filePath, os.ModeDir)
if e != nil {
that.Display(3, e)
return
}
filePath = filePath + tableName + time.Now().Format("2006-01-02-15-04-05") + ".xlsx"
// 根据指定路径保存文件
if err := f.SaveAs(that.Config.GetString("tpt") + filePath); err != nil {
fmt.Println(err)
that.Display(4, "输出异常")
return
}
f.Save()
//that.Resp.Header().Set("Location", filePath)
//that.Resp.WriteHeader(307) //关键在这里!
that.Display(0, filePath)
return
}
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-07-23 16:00:36 +00:00
"info": func(that *Context) {
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
tableName := fileConfig.GetString("table")
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
str, inData := that.MakeCodeRouter[hotimeName].Info(tableName, 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(tableName, str, where)
if re == nil {
that.Display(4, "找不到对应信息")
return
}
for k, v := range re {
column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][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 = tableName
}
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[tableName][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 = tableName
}
re[link] = that.Db.Get(column.GetString("link"), seStr, Map{"id": v})
}
re["table"] = fileConfig.GetString("table")
that.Display(0, re)
},
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-07-23 16:00:36 +00:00
where := Map{"name": name}
if that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")]["phone"] != nil {
where["phone"] = name
2022-08-11 02:41:35 +00:00
where = Map{"OR": where, "password": Md5(password)}
2022-07-23 16:00:36 +00:00
} else {
where["password"] = Md5(password)
}
2022-08-11 02:41:35 +00:00
if len(where) > 1 {
where = Map{"AND": where}
}
2022-07-23 16:00:36 +00:00
user := that.Db.Get(fileConfig.GetString("table"), "*", where)
2022-03-12 21:06:28 +00:00
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-07-23 16:00:36 +00:00
user["token"] = that.SessionId
2022-03-12 17:48:54 +00:00
that.Display(0, user)
2021-12-27 12:40:16 +00:00
},
2022-07-23 16:00:36 +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-20 16:24:39 +00:00
btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
2022-07-18 11:44:00 +00:00
2022-07-20 16:24:39 +00:00
if err != nil {
that.Display(4, "找不到配置文件")
return
}
if that.Session(fileConfig.GetString("table")+"_id").Data == nil {
2022-07-18 11:44:00 +00:00
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-20 16:24:39 +00:00
//可读写配置
2022-07-18 11:44:00 +00:00
conf := ObjToMap(string(btes))
menus := conf.GetSlice("menus")
2022-07-20 16:24:39 +00:00
user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").Data})
2022-07-18 11:44:00 +00:00
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")
2022-08-04 04:14:18 +00:00
//conf := ObjToMap(string(btes))
//menus := conf.GetSlice("menus")
2022-08-03 18:41:41 +00:00
if linkAuth == nil {
linkAuth = Map{}
2022-08-04 04:14:18 +00:00
}
2022-08-03 18:41:41 +00:00
2022-08-04 04:14:18 +00:00
for k1, _ := range menus {
v1 := menus.GetMap(k1)
name := v1.GetString("name")
if name == "" {
name = v1.GetString("table")
}
if v1["auth"] != nil {
if linkAuth[name] == nil {
2022-08-03 18:41:41 +00:00
linkAuth[name] = v1["auth"]
2022-08-04 04:14:18 +00:00
} else {
newAuth := Slice{}
for k2, _ := range linkAuth.GetSlice(name) {
v2 := linkAuth.GetSlice(name).GetString(k2)
if strings.Contains(v1.GetString("auth"), v2) {
newAuth = append(newAuth, v2)
}
}
linkAuth[name] = newAuth
2022-08-03 18:41:41 +00:00
}
2022-08-04 04:14:18 +00:00
}
2022-08-03 18:41:41 +00:00
2022-08-04 04:14:18 +00:00
menusChild := v1.GetSlice("menus")
for k2, _ := range menusChild {
v2 := menusChild.GetMap(k2)
name := v2.GetString("name")
if name == "" {
name = v2.GetString("table")
2022-08-03 18:41:41 +00:00
}
2022-08-04 04:14:18 +00:00
if v2["auth"] != nil {
2022-08-11 02:18:48 +00:00
if linkAuth[name] == nil {
linkAuth[name] = v2["auth"]
} else {
newAuth := Slice{}
for k3, _ := range linkAuth.GetSlice(name) {
v3 := linkAuth.GetSlice(name).GetString(k3)
if strings.Contains(v2.GetString("auth"), v3) {
newAuth = append(newAuth, v3)
}
2022-08-03 18:41:41 +00:00
}
2022-08-11 02:18:48 +00:00
linkAuth[name] = newAuth
continue
2022-08-03 18:41:41 +00:00
}
}
}
}
2022-07-18 11:44:00 +00:00
for k1, _ := range menus {
v1 := menus.GetMap(k1)
2022-08-03 18:41:41 +00:00
//保证个人权限可用
if fileConfig.GetString("table") == v1.GetString("table") {
v1["auth"] = Slice{"info", "edit"}
}
2022-08-04 04:14:18 +00:00
name := v1.GetString("name")
if name == "" {
name = v1.GetString("table")
2022-07-27 09:09:56 +00:00
}
2022-08-04 04:14:18 +00:00
if linkAuth[name] != nil {
v1["auth"] = linkAuth[name]
2022-07-18 11:44:00 +00:00
}
2022-08-04 04:14:18 +00:00
2022-07-20 16:24:39 +00:00
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
2022-07-23 16:00:36 +00:00
v2 := v1menus.GetMap(k2)
2022-08-03 18:41:41 +00:00
//保证个人权限可用
if fileConfig.GetString("table") == v2.GetString("table") {
v2["auth"] = Slice{"info", "edit"}
}
2022-08-04 04:14:18 +00:00
name := v2.GetString("name")
if name == "" {
name = v2.GetString("table")
2022-07-27 09:09:56 +00:00
}
2022-08-04 04:14:18 +00:00
if linkAuth[name] != nil {
v2["auth"] = linkAuth[name]
2022-07-18 11:44:00 +00:00
}
2022-08-04 04:14:18 +00:00
2022-07-18 11:44:00 +00:00
}
}
2022-08-03 18:41:41 +00:00
break
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-20 16:24:39 +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{}
2022-08-03 18:41:41 +00:00
newTables[fileConfig.GetString("table")] = config.GetMap("tables").GetMap(fileConfig.GetString("table"))
2022-07-18 11:44:00 +00:00
for k1, _ := range menus {
v1 := menus.GetMap(k1)
2022-08-04 04:14:18 +00:00
if config.GetMap("tables").GetMap(v1.GetString("table")) != nil && len(v1.GetSlice("auth")) != 0 {
2022-08-03 18:41:41 +00:00
config.GetMap("tables").GetMap(v1.GetString("table"))["auth"] = Slice{}
2022-07-20 16:24:39 +00:00
newTables[v1.GetString("name")] = config.GetMap("tables").GetMap(v1.GetString("table"))
2022-07-18 11:44:00 +00:00
}
2022-07-20 16:24:39 +00:00
v1menus := v1.GetSlice("menus")
2022-07-18 11:44:00 +00:00
for k2, _ := range v1.GetSlice("menus") {
2022-07-20 16:24:39 +00:00
v2 := v1menus.GetMap(k2)
2022-08-04 04:14:18 +00:00
if config.GetMap("tables").GetMap(v2.GetString("table")) != nil && len(v2.GetSlice("auth")) != 0 {
2022-08-03 18:41:41 +00:00
2022-07-20 16:24:39 +00:00
//有自定义配置文件
if conf.GetMap("tables") != nil && conf.GetMap("tables").GetMap(v2.GetString("table")) != nil {
columns := config.GetMap("tables").GetMap(v2.GetString("table")).GetSlice("columns")
columnsConf := conf.GetMap("tables").GetMap(v2.GetString("table")).GetSlice("columns")
for k3, _ := range columns {
v3 := columns.GetMap(k3)
for k4, _ := range columnsConf {
v4 := columnsConf.GetMap(k4)
if v3.GetString("name") == v4.GetString("name") {
columns[k3] = columnsConf[k4]
break
}
}
}
}
2022-08-03 18:41:41 +00:00
config.GetMap("tables").GetMap(v2.GetString("table"))["auth"] = Slice{}
2022-07-20 16:24:39 +00:00
newTables[v2.GetString("table")] = config.GetMap("tables").GetMap(v2.GetString("table"))
2022-01-12 09:33:20 +00:00
}
2021-12-27 12:40:16 +00:00
}
}
2022-07-23 16:00:36 +00:00
conf["tables"] = newTables
2022-07-18 11:44:00 +00:00
2022-07-23 16:00:36 +00:00
that.Display(0, conf)
2022-07-08 01:55:02 +00:00
},
2022-10-08 08:52:42 +00:00
//壁纸
"wallpaper": func(that *Context) {
random := ObjToCeilInt(that.Req.FormValue("random")) //壁纸随机0为默认1为随机
tp := ObjToCeilInt(that.Req.FormValue("type")) //返回类型0json1302跳转到图片
images := that.Application.Cache("wallpaper").ToSlice()
if images == nil || len(images) == 0 {
url := "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=7&mkt=zh-CN"
res, e := http.Get(url)
if e != nil {
that.Display(4, "无法取得数据0")
return
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
that.Display(4, "无法取得数据1")
return
}
w := ObjToMap(string(b))
if len(w) == 0 {
that.Display(4, "无法取得数据2")
return
}
images = w.GetSlice("images")
that.Application.Cache("wallpaper", images)
}
if random == 1 {
random = RandX(0, len(images)-1)
}
img := images.GetMap(random)
if !strings.Contains(img.GetString("url"), "http") {
img["url"] = "https://cn.bing.com" + img.GetString("url")
}
if tp == 0 {
that.Display(0, img)
return
}
that.Resp.Header().Set("Location", img.GetString("url"))
that.Resp.WriteHeader(302)
},
2022-07-18 11:44:00 +00:00
},
2021-12-27 12:40:16 +00:00
}
2022-07-31 19:40:09 +00:00
func convertToTitle(columnNumber int) string {
var res []byte
for columnNumber > 0 {
a := columnNumber % 26
if a == 0 {
a = 26
}
res = append(res, 'A'+byte(a-1))
columnNumber = (columnNumber - a) / 26
}
// 上面输出的res是反着的前后交换
for i, n := 0, len(res); i < n/2; i++ {
res[i], res[n-1-i] = res[n-1-i], res[i]
}
return string(res)
}