hotime/code.go
2022-08-01 16:45:24 +08:00

1176 lines
35 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package hotime
import (
. "code.hoteas.com/golang/hotime/common"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"io"
"io/ioutil"
"os"
"strings"
"time"
)
// Project 管理端项目
var TptProject = Proj{
//"user": UserCtr,
"hotimeCommon": Ctr{
"info": func(that *Context) {
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
tableName := that.RouterString[1]
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.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(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})
}
//权限设置
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")]
}
if userAuth[v1.GetString("table")] != nil {
v1["auth"] = userAuth[v1.GetString("table")]
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
v2 := v1menus.GetMap(k2)
if userAuth[v2.GetString("name")] != nil {
v2["auth"] = userAuth[v2.GetString("name")]
}
if userAuth[v2.GetString("table")] != nil {
v2["auth"] = userAuth[v2.GetString("table")]
}
}
}
re["auth"] = menus.ToJsonString()
//that.Display(0,menus)
}
}
//如果有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})
}
that.Display(0, re)
},
"add": func(that *Context) {
tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "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(tableName, data, that.Req)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
if v.GetString("link") != "" {
if v.GetString("link") == tableName && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["parent_id"] != nil {
if inData["parent_id"] == nil {
inData["parent_id"] = data.GetCeilInt64(v.GetString("name"))
pre := that.Db.Get(v.GetString("link"), "parent_ids", Map{"id": data.GetCeilInt64(v.GetString("name"))})
inData["parent_ids"] = pre.GetString("parent_ids")
}
}
}
}
if inData["auth"] != nil {
btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
if err != nil {
that.Display(4, "找不到配置文件")
return
}
conf := ObjToMap(string(btes))
menus := conf.GetSlice("menus")
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
if v.GetString("link") != "" && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] != nil {
//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("table")] != nil {
v1["auth"] = linkAuth[v1.GetString("table")]
}
if linkAuth[v1.GetString("name")] != nil {
v1["auth"] = linkAuth[v1.GetString("name")]
}
if v1.GetString("name") != "" && v1["auth"] == nil {
v1["auth"] = Slice{"show"}
}
if v1.GetString("table") != "" && v1["auth"] == nil {
v1["auth"] = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v1.GetString("table"))["auth"]
}
if v1.GetString("table") != "" {
myAuth[v1.GetString("table")] = v1
}
if v1.GetString("name") != "" {
myAuth[v1.GetString("name")] = v1
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
v2 := v1menus.GetMap(k2)
if linkAuth[v2.GetString("table")] != nil {
v2["auth"] = linkAuth[v2.GetString("table")]
}
if linkAuth[v2.GetString("name")] != nil {
v2["auth"] = linkAuth[v2.GetString("name")]
}
if v2.GetString("name") != "" && v2["auth"] == nil {
v2["auth"] = Slice{"show"}
}
if v2.GetString("table") != "" && v2["auth"] == nil {
v2["auth"] = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v2.GetString("table"))["auth"]
}
if v2.GetString("table") != "" {
myAuth[v2.GetString("table")] = v2
}
if 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 && myAuth.GetMap(v1.GetString("table")) == nil {
continue
}
auth := Slice{}
str := v1.GetString("name")
if v1.GetString("table") != "" {
str = v1.GetString("table")
}
if v1.GetString("auth") == "" {
if myAuth.GetMap(str).GetString("table") != "" {
auth = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(str).GetSlice("auth")
} else {
auth = Slice{"show"}
}
} else {
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"show"`) &&
strings.Contains(v1.GetString("auth"), `"show"`) {
auth = append(auth, "show")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) &&
strings.Contains(v1.GetString("auth"), `"download"`) {
auth = append(auth, "download")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) &&
strings.Contains(v1.GetString("auth"), `"add"`) {
auth = append(auth, "add")
//continue
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"edit"`) &&
strings.Contains(v1.GetString("auth"), `"edit"`) {
auth = append(auth, "edit")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"delete"`) &&
strings.Contains(v1.GetString("auth"), `"delete"`) {
auth = append(auth, "delete")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"info"`) &&
strings.Contains(v1.GetString("auth"), `"info"`) {
auth = append(auth, "info")
}
}
toDB[str] = auth
v1Menus := v1.GetSlice("menus")
for k2, _ := range v1Menus {
v2 := v1Menus.GetMap(k2)
if myAuth.GetMap(v2.GetString("name")) == nil && myAuth.GetMap(v2.GetString("table")) == nil {
continue
}
auth := Slice{}
str := v2.GetString("name")
if v2.GetString("table") != "" {
str = v2.GetString("table")
}
if v2.GetString("auth") == "" {
if myAuth.GetMap(str).GetString("table") != "" {
auth = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(str).GetSlice("auth")
} else {
auth = Slice{"show"}
}
} else {
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"show"`) &&
strings.Contains(v2.GetString("auth"), `"show"`) {
auth = append(auth, "show")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) &&
strings.Contains(v1.GetString("auth"), `"download"`) {
auth = append(auth, "download")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) &&
strings.Contains(v2.GetString("auth"), `"add"`) {
auth = append(auth, "add")
//continue
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"edit"`) &&
strings.Contains(v2.GetString("auth"), `"edit"`) {
auth = append(auth, "edit")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"delete"`) &&
strings.Contains(v2.GetString("auth"), `"delete"`) {
auth = append(auth, "delete")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"info"`) &&
strings.Contains(v2.GetString("auth"), `"info"`) {
auth = append(auth, "info")
}
}
toDB[str] = auth
}
}
inData["auth"] = toDB.ToJsonString()
break
}
//}
}
}
re := that.Db.Insert(tableName, inData)
if re == 0 {
that.Display(4, "无法插入对应数据")
return
}
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetString("parent_ids") != "" {
index := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")})
inData["parent_ids"] = index.GetString("parent_ids") + ObjToStr(re) + ","
that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re})
} else if inData.GetString("parent_ids") != "" {
inData["parent_ids"] = "," + ObjToStr(re) + ","
that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re})
}
that.Log["table_id"] = re
that.Display(0, re)
},
"update": func(that *Context) {
tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "type": 2, "table_id": that.RouterString[2]}
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
inData := that.MakeCodeRouter[hotimeName].Edit(tableName, that.Req)
if inData == nil {
that.Display(3, "没有找到要更新的数据")
return
}
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
//树状结构不允许修改自身的属性,修改别人的可以
if fileConfig.GetString("table") == tableName && that.RouterString[2] == that.Session(fileConfig.GetString("table")+"_id").ToStr() {
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
if v.GetString("link") == "" {
continue
}
if that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["parent_id"] != nil {
delete(inData, v.GetString("name"))
}
}
}
if inData["auth"] != nil {
btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
if err != nil {
that.Display(4, "找不到配置文件")
return
}
conf := ObjToMap(string(btes))
menus := conf.GetSlice("menus")
for _, v := range that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")] {
if v.GetString("link") != "" && that.MakeCodeRouter[hotimeName].TableColumns[v.GetString("link")]["auth"] != nil {
//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("table")] != nil {
v1["auth"] = linkAuth[v1.GetString("table")]
}
if linkAuth[v1.GetString("name")] != nil {
v1["auth"] = linkAuth[v1.GetString("name")]
}
if v1.GetString("name") != "" && v1["auth"] == nil {
v1["auth"] = Slice{"show"}
}
if v1.GetString("table") != "" && v1["auth"] == nil {
v1["auth"] = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v1.GetString("table"))["auth"]
}
if v1.GetString("table") != "" {
myAuth[v1.GetString("table")] = v1
}
if v1.GetString("name") != "" {
myAuth[v1.GetString("name")] = v1
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
v2 := v1menus.GetMap(k2)
if linkAuth[v2.GetString("table")] != nil {
v2["auth"] = linkAuth[v2.GetString("table")]
}
if linkAuth[v2.GetString("name")] != nil {
v2["auth"] = linkAuth[v2.GetString("name")]
}
if v2.GetString("name") != "" && v2["auth"] == nil {
v2["auth"] = Slice{"show"}
}
if v2.GetString("table") != "" && v2["auth"] == nil {
v2["auth"] = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v2.GetString("table"))["auth"]
}
if v2.GetString("table") != "" {
myAuth[v2.GetString("table")] = v2
}
if 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 && myAuth.GetMap(v1.GetString("table")) == nil {
continue
}
auth := Slice{}
str := v1.GetString("name")
if v1.GetString("table") != "" {
str = v1.GetString("table")
}
if v1.GetString("auth") == "" {
if myAuth.GetMap(str).GetString("table") != "" {
auth = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(str).GetSlice("auth")
} else {
auth = Slice{"show"}
}
} else {
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"show"`) &&
strings.Contains(v1.GetString("auth"), `"show"`) {
auth = append(auth, "show")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) &&
strings.Contains(v1.GetString("auth"), `"download"`) {
auth = append(auth, "download")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) &&
strings.Contains(v1.GetString("auth"), `"add"`) {
auth = append(auth, "add")
//continue
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"edit"`) &&
strings.Contains(v1.GetString("auth"), `"edit"`) {
auth = append(auth, "edit")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"delete"`) &&
strings.Contains(v1.GetString("auth"), `"delete"`) {
auth = append(auth, "delete")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"info"`) &&
strings.Contains(v1.GetString("auth"), `"info"`) {
auth = append(auth, "info")
}
}
toDB[str] = auth
v1Menus := v1.GetSlice("menus")
for k2, _ := range v1Menus {
v2 := v1Menus.GetMap(k2)
if myAuth.GetMap(v2.GetString("name")) == nil && myAuth.GetMap(v2.GetString("table")) == nil {
continue
}
auth := Slice{}
str := v2.GetString("name")
if v2.GetString("table") != "" {
str = v2.GetString("table")
}
if v2.GetString("auth") == "" {
if myAuth.GetMap(str).GetString("table") != "" {
auth = that.MakeCodeRouter[hotimeName].TableConfig.GetMap(str).GetSlice("auth")
} else {
auth = Slice{"show"}
}
} else {
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"show"`) &&
strings.Contains(v2.GetString("auth"), `"show"`) {
auth = append(auth, "show")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"download"`) &&
strings.Contains(v1.GetString("auth"), `"download"`) {
auth = append(auth, "download")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"add"`) &&
strings.Contains(v2.GetString("auth"), `"add"`) {
auth = append(auth, "add")
//continue
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"edit"`) &&
strings.Contains(v2.GetString("auth"), `"edit"`) {
auth = append(auth, "edit")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"delete"`) &&
strings.Contains(v2.GetString("auth"), `"delete"`) {
auth = append(auth, "delete")
}
if strings.Contains(myAuth.GetMap(str).GetString("auth"), `"info"`) &&
strings.Contains(v2.GetString("auth"), `"info"`) {
auth = append(auth, "info")
}
}
toDB[str] = auth
}
}
inData["auth"] = toDB.ToJsonString()
break
}
//}
}
}
//索引管理,便于检索以及权限
if inData.GetString("parent_ids") != "" {
if inData.Get("parent_id") != nil {
Index := that.Db.Get(tableName, "`index`", Map{"id": that.RouterString[2]})
parentIndex := that.Db.Get(tableName, "`index`", Map{"id": inData.Get("parent_id")})
inData["parent_ids"] = parentIndex.GetString("parent_ids") + that.RouterString[2] + ","
childNodes := that.Db.Select(tableName, "id,`index``", Map{"index[~]": "," + that.RouterString[2] + ","})
for _, v := range childNodes {
v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), inData.GetString("parent_ids"), -1)
that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")})
}
} else {
delete(inData, "parent_ids")
}
}
re := that.Db.Update(tableName, inData, Map{"id": that.RouterString[2]})
if re == 0 {
that.Display(4, "更新数据失败")
return
}
that.Display(0, re)
},
"remove": func(that *Context) {
tableName := that.RouterString[1]
that.Log = Map{"table": tableName, "type": 3, "table_id": that.RouterString[2]}
hotimeName := that.RouterString[0]
inData := that.MakeCodeRouter[hotimeName].Delete(tableName, that.Req)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
re := int64(0)
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetSlice("parent_ids") != nil {
re = that.Db.Delete(tableName, Map{"index[~]": "," + that.RouterString[2] + ","})
} else {
re = that.Db.Delete(tableName, 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
tableName := that.RouterString[1]
data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})
columnStr, leftJoin, where := that.MakeCodeRouter[hotimeName].Search(tableName, data, that.Req, that.Db)
page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize"))
download := ObjToInt(that.Req.FormValue("download"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 20
}
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)
}
for _, v := range reData {
for k, _ := range v {
//v1:=v.GetMap(k)
//v.RangeSort(func(k string, v1 interface{}) (isEnd bool) {
//如果有table字段则代为link
if v["table"] != nil && v["table_id"] != nil {
id := v.GetCeilInt64("table_id")
tableNameLabel := that.MakeCodeRouter[hotimeName].TableConfig.GetMap(v.GetString("table")).GetString("label")
//v["table_table_name"] = strings.Replace(tableNameLabel, "管理", "", -1)
v["table_table_name"] = tableNameLabel
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})
v["table_table_id_name"] = ""
if parentC != nil {
v["table_table_id_name"] = parentC.GetString(sname)
}
}
column := that.MakeCodeRouter[hotimeName].TableColumns[tableName][k]
if column == nil {
break
}
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
}
}
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" {
f.SetCellValue(tableNameLabel, convertToTitle(n)+ObjToStr(k1+2), v1.GetString("table_"+v.GetString("name")+"_name"))
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
}
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)
},
"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})
}
//权限设置
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")]
}
if userAuth[v1.GetString("table")] != nil {
v1["auth"] = userAuth[v1.GetString("table")]
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
v2 := v1menus.GetMap(k2)
if userAuth[v2.GetString("name")] != nil {
v2["auth"] = userAuth[v2.GetString("name")]
}
if userAuth[v2.GetString("table")] != nil {
v2["auth"] = userAuth[v2.GetString("table")]
}
}
}
re["auth"] = menus.ToJsonString()
//that.Display(0,menus)
}
}
//如果有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)
},
"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
}
where := Map{"name": name}
if that.MakeCodeRouter[hotimeName].TableColumns[fileConfig.GetString("table")]["phone"] != nil {
where["phone"] = name
where = Map{"AND": Map{"OR": where, "password": Md5(password)}}
} else {
where["password"] = Md5(password)
}
user := that.Db.Get(fileConfig.GetString("table"), "*", where)
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")
user["table"] = fileConfig.GetString("table")
user["token"] = that.SessionId
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, "退出登录成功")
},
"config": func(that *Context) {
hotimeName := that.RouterString[0]
fileConfig := that.MakeCodeRouter[hotimeName].FileConfig
btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
if err != nil {
that.Display(4, "找不到配置文件")
return
}
if that.Session(fileConfig.GetString("table")+"_id").Data == nil {
conf := ObjToMap(string(btes))
delete(conf, "menus")
delete(conf, "tables")
//没有登录只需要返回这些信息
that.Display(0, conf)
return
}
//可读写配置
conf := ObjToMap(string(btes))
menus := conf.GetSlice("menus")
user := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").Data})
if user == nil {
that.Display(2, "暂未登录")
return
}
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")]
}
if linkAuth[v1.GetString("table")] != nil {
v1["auth"] = linkAuth[v1.GetString("table")]
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
v2 := v1menus.GetMap(k2)
if linkAuth[v2.GetString("name")] != nil {
v2["auth"] = linkAuth[v2.GetString("name")]
}
if linkAuth[v2.GetString("table")] != nil {
v2["auth"] = linkAuth[v2.GetString("table")]
}
}
}
}
}
userAuth := user.GetMap("auth")
for k1, _ := range menus {
v1 := menus.GetMap(k1)
if userAuth[v1.GetString("table")] != nil {
v1["auth"] = userAuth[v1.GetString("table")]
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1menus {
v2 := v1menus.GetMap(k2)
if userAuth[v2.GetString("table")] != nil {
v2["auth"] = userAuth[v2.GetString("table")]
}
}
}
//不可读写数据
config := DeepCopyMap(that.MakeCodeRouter[hotimeName].Config).(Map)
config["menus"] = menus
newTables := Map{}
for k1, _ := range menus {
v1 := menus.GetMap(k1)
if config.GetMap("tables").GetMap(v1.GetString("table")) != nil {
newTables[v1.GetString("name")] = config.GetMap("tables").GetMap(v1.GetString("table"))
}
v1menus := v1.GetSlice("menus")
for k2, _ := range v1.GetSlice("menus") {
v2 := v1menus.GetMap(k2)
if config.GetMap("tables").GetMap(v2.GetString("table")) != nil {
//有自定义配置文件
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
}
}
}
}
newTables[v2.GetString("table")] = config.GetMap("tables").GetMap(v2.GetString("table"))
}
}
}
conf["tables"] = newTables
that.Display(0, conf)
},
},
}
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)
}