hotime

package module
v1.2.82 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 20 Imported by: 1

README

hotime

golang web服务框架 支持数据库db:mysql、sqlite3 支持缓存cache:redis,memory,数据库 自带工具类,上下文,以及session等功能

Documentation

Index

Constants

View Source
const HEAD_SESSION_ADD = "session#"

HEAD_SESSION_ADD session储存头

Variables

View Source
var App = map[string]*Application{} //整个项目
View Source
var Config = Map{
	"mode": 2,

	"codeConfig": Slice{
		Map{
			"table":  "admin",
			"name":   "",
			"config": "config/app.json",
			"rule":   "config/rule.json",
			"mode":   0,
		},
	},
	"db": Map{
		"sqlite": Map{
			"path": "config/data.db",
		},
	},
	"cache": Map{
		"memory": Map{
			"timeout": 60 * 60 * 2,
			"db":      true,
			"session": true,
		},
	},
	"error": Map{
		"1": "内部系统异常",
		"2": "访问权限异常",
		"3": "请求参数异常",
		"4": "数据处理异常",
		"5": "数据结果异常",
	},
	"tpt":         "tpt",
	"defFile":     []string{"index.html", "index.htm"},
	"port":        "80",
	"sessionName": "HOTIME",
}
View Source
var ConfigNote = Map{
	"logLevel":          "默认0,必须,0关闭,1打印,日志等级",
	"logFile":           "无默认,非必须,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置",
	"webConnectLogShow": "默认true,非必须,访问日志如果需要web访问链接、访问ip、访问时间打印,false为关闭true开启此功能",
	"webConnectLogFile": "无默认,非必须,webConnectLogShow开启之后才能使用,如果需要存储日志文件时使用,保存格式为:a/b/c/20060102150405.txt,将生成:a/b/c/年月日时分秒.txt,按需设置",
	"mode":              "默认0,非必须,0生产模式,1,测试模式,2开发模式,3内嵌代码模式,在开发模式下会显示更多的数据用于开发测试,并能够辅助研发,自动生成配置文件、代码等功能,web无缓存,数据库不启用缓存",

	"codeConfig": Slice{
		"注释:配置即启用,非必须,默认无",
		Map{

			"table":    "默认admin,必须,根据数据库内当前表名做为用户生成数据",
			"name":     "默认无,非必须,有则生成代码到此目录,无则采用缺省模式使用表名,如设置为:admin,将在admin目录生成包名为admin的代码",
			"config":   "默认config/app.json,必须,接口描述配置文件",
			"configDB": "默认无,非必须,有则每次将数据库数据生成到此目录用于配置读写,无则不生成",
			"rule":     "默认config/rule.json,非必须,有则按改规则生成接口,无则按系统内嵌方式生成",
			"mode":     "默认0,非必须,0为内嵌代码模式,1为生成代码模式",
		},
	},
	"db": Map{
		"注释": "配置即启用,非必须,默认使用sqlite数据库",
		"mysql": Map{
			"注释":       "除prefix及主从数据库slave项,其他全部必须",
			"host":     "默认127.0.0.1,必须,数据库ip地址",
			"name":     "默认test,必须,数据库名称",
			"user":     "默认root,必须,数据库用户名",
			"password": "默认root,必须,数据库密码",
			"port":     "默认3306,必须,数据库端口",
			"prefix":   "默认空,非必须,数据表前缀",
			"slave": Map{
				"注释":       "从数据库配置,mysql里配置slave项即启用主从读写,减少数据库压力",
				"host":     "默认127.0.0.1,必须,数据库ip地址",
				"name":     "默认test,必须,数据库名称",
				"user":     "默认root,必须,数据库用户名",
				"password": "默认root,必须,数据库密码",
				"port":     "默认3306,必须,数据库端口",
			},
		},
		"sqlite": Map{
			"path": "默认config/data.db,必须,数据库位置",
		},
	},
	"cache": Map{
		"注释": "可配置memory,db,redis,默认启用memory,默认优先级为memory>redis>db,memory与数据库缓存设置项一致,缓存数据填充会自动反方向反哺,加入memory缓存过期将自动从redis更新,但memory永远不会更新redis,如果是集群建议不要开启memory,配置即启用",
		"memory": Map{
			"timeout": "默认60 * 60 * 2,非必须,过期时间,超时自动删除",
			"db":      "默认true,非必须,缓存数据库,启用后能减少数据库的读写压力",
			"session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存",
		},
		"db": Map{
			"timeout": "默认60 * 60 * 24 * 30,非必须,过期时间,超时自动删除",
			"db":      "默认false,非必须,缓存数据库,启用后能减少数据库的读写压力",
			"session": "默认true,非必须,缓存web session,同时缓存session保持的用户缓存",
		},
		"redis": Map{
			"host":     "默认服务ip:127.0.0.1,必须,如果需要使用redis服务时配置,",
			"port":     "默认服务端口:6379,必须,如果需要使用redis服务时配置,",
			"password": "默认密码空,必须,如果需要使用redis服务时配置,默认密码空",
			"timeout":  "默认60 * 60 * 24 * 15,非必须,过期时间,超时自动删除",
			"db":       "默认true,非必须,缓存数据库,启用后能减少数据库的读写压力",
			"session":  "默认true,非必须,缓存web session,同时缓存session保持的用户缓存",
		},
	},
	"error": Map{
		"1":  "内部系统异常,在环境配置,文件访问权限等基础运行环境条件不足造成严重错误时使用",
		"2":  "访问权限异常,没有登录或者登录异常等时候使用",
		"3":  "请求参数异常,request参数不满足要求,比如参数不足,参数类型错误,参数不满足要求等时候使用",
		"4":  "数据处理异常,数据库操作或者三方请求返回的结果非正常结果,比如数据库突然中断等时候使用",
		"5":  "数据结果异常,一般用于无法给出response要求的格式要求下使用,比如response需要的是string格式但你只能提供int数据时",
		"注释": "web服务内置错误提示,自定义异常建议10开始",
	},
	"tpt":              "默认tpt,必须,web静态文件目录,默认为程序目录下tpt目录",
	"defFile":          "默认访问index.html或者index.htm文件,必须,默认访问文件类型",
	"crossDomain":      "默认空 非必须,空字符串为不开启,如果需要跨域设置,auto为智能开启所有网站允许跨域,http://www.baidu.com为指定域允许跨域",
	"modeRouterStrict": "默认false,必须,路由严格模式false,为大小写忽略必须匹配,true必须大小写匹配",
	"sessionName":      "默认HOTIME,必须,设置session的cookie名",
	"port":             "默认80,必须,web服务开启Http端口,0为不启用http服务,默认80",
	"tlsPort":          "默认空,非必须,web服务https端口,0为不启用https服务",
	"tlsKey":           "默认空,非必须,https密钥",
	"tlsCert":          "默认空,非必须,https证书",
}
View Source
var IsRun = false //当前状态
View Source
var MimeMaps = map[string]string{}/* 539 elements not displayed */
View Source
var TptProject = Proj{

	"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, _ := that.MakeCodeRouter[hotimeName].Info(tableName, data, that.Db)
			where := Map{"id": that.RouterString[2]}

			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["info"] == nil || column.GetBool("info")) && 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 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 && data[v.GetString("name")] != nil {
							inData["parent_id"] = data.GetCeilInt64(v.GetString("name"))

						}

					}
				}

				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")

					if linkAuth == nil {
						btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
						if err != nil {
							that.Display(4, "找不到配置文件")
							return
						}
						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")
							}
							if v1["auth"] != nil {

								if linkAuth[name] == nil {
									linkAuth[name] = v1["auth"]
								} 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
								}
							}

							menusChild := v1.GetSlice("menus")
							for k2, _ := range menusChild {
								v2 := menusChild.GetMap(k2)
								name := v2.GetString("name")
								if name == "" {
									name = v2.GetString("table")
								}
								if v2["auth"] != nil {

									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)
											}
										}
										linkAuth[name] = newAuth
										continue

									}
								}
							}
						}
					}

					toDB := Map{}
					newAuth := inData.GetMap("auth")
					if newAuth == nil {
						inData["auth"] = linkAuth.ToJsonString()
						continue
					}

					for k1, _ := range newAuth {
						v1 := newAuth.GetSlice(k1)
						if linkAuth.GetString(k1) == "" {
							continue
						}
						toDbli := Slice{}
						for k2, _ := range v1 {
							v2 := v1.GetString(k2)

							if !strings.Contains(linkAuth.GetString(k1), `"`+v2+`"`) {
								continue
							}
							toDbli = append(toDbli, v2)
						}
						toDB[k1] = toDbli
					}

					inData["auth"] = toDB.ToJsonString()

				}
			}

			re := that.Db.Insert(tableName, inData)

			if re == 0 {
				that.Display(4, "无法插入对应数据")
				return
			}

			if inData.Get("parent_id") != nil {
				index := that.Db.Get(tableName, "`parent_ids`", Map{"id": inData.Get("parent_id")})
				parent_ids := index.GetString("parent_ids")
				if parent_ids == "" {
					parent_ids = ","
				}
				inData["parent_ids"] = parent_ids + ObjToStr(re) + ","
				that.Db.Update(tableName, Map{"parent_ids": inData["parent_ids"]}, Map{"id": re})
			} else {
				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()})

			btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
			if err != nil {
				that.Display(4, "找不到权限配置文件")
				return
			}

			conf := ObjToMap(string(btes))

			if tableName == fileConfig.GetString("table") {
				flows := conf.GetMap("flow")
				for k, _ := range flows {
					v := flows.GetMap(k)
					if v.GetBool("stop") {

						if inData[k+"_id"] != nil {

							delete(inData, k+"_id")

						}
					}
				}
			} else {
				flow := conf.GetMap("flow").GetMap(tableName)
				if flow != nil && flow.GetBool("stop") {
					if data.GetCeilInt64(tableName+"_id") == ObjToCeilInt64(that.RouterString[2]) {
						that.Display(4, "你没有权限修改当前数据")
						return
					}
				}
			}

			if inData["auth"] != nil {
				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 {
						linkAuthMap := that.Db.Get(v.GetString("link"), "auth", Map{"id": data.GetCeilInt(v.GetString("name"))})
						linkAuth := linkAuthMap.GetMap("auth")

						if linkAuth == nil {
							linkAuth = Map{}
						}

						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 {
									linkAuth[name] = v1["auth"]
								} 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
								}
							}

							menusChild := v1.GetSlice("menus")
							for k2, _ := range menusChild {
								v2 := menusChild.GetMap(k2)
								name := v2.GetString("name")
								if name == "" {
									name = v2.GetString("table")
								}
								if v2["auth"] != nil {

									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)
											}
										}
										linkAuth[name] = newAuth
										continue

									}
								}
							}
						}

						toDB := Map{}
						newAuth := inData.GetMap("auth")
						if newAuth == nil || len(newAuth) == 0 {
							inData["auth"] = linkAuth.ToJsonString()
							break
						}

						for k1, _ := range newAuth {
							v1 := newAuth.GetSlice(k1)
							if linkAuth.GetString(k1) == "" {
								continue
							}
							toDbli := Slice{}
							for k2, _ := range v1 {
								v2 := v1.GetString(k2)
								if !strings.Contains(linkAuth.GetString(k1), `"`+v2+`"`) {
									continue
								}
								toDbli = append(toDbli, v2)
							}
							toDB[k1] = toDbli
						}

						inData["auth"] = toDB.ToJsonString()
						break
					}
				}

			}

			if _, ok := inData["parent_id"]; ok {

				if inData.GetCeilInt64("parent_id") == 0 {
					that.Db.Update(tableName, Map{"parent_ids": "," + that.RouterString[2] + ","}, Map{"id": that.RouterString[2]})
				} else {

					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] + ","
						childNodes := that.Db.Select(tableName, "id,`parent_ids`", Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
						for _, v := range childNodes {
							v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), parent_ids, -1)
							that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")})
						}
					}
				}

			}

			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]
			fileConfig := that.MakeCodeRouter[hotimeName].FileConfig

			btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
			if err != nil {
				that.Display(4, "找不到权限配置文件")
				return
			}

			conf := ObjToMap(string(btes))
			data := that.Db.Get(fileConfig.GetString("table"), "*", Map{"id": that.Session(fileConfig.GetString("table") + "_id").ToCeilInt()})

			if tableName == fileConfig.GetString("table") {

				if data.GetCeilInt64("id") == ObjToCeilInt64(that.RouterString[2]) {
					that.Display(4, "你不能删除自己")
					return
				}
			} else {
				flow := conf.GetMap("flow").GetMap(tableName)
				if flow != nil && flow.GetBool("stop") {
					if data.GetCeilInt64(tableName+"_id") == ObjToCeilInt64(that.RouterString[2]) {
						that.Display(4, "你没有权限修改当前数据")
						return
					}
				}
			}
			re := int64(0)

			if that.MakeCodeRouter[hotimeName].TableColumns[tableName]["parent_id"] != nil {
				re = that.Db.Delete(tableName, Map{"parent_ids[~]": "," + 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()})
			where := Map{}

			btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
			if err != nil {
				that.Display(4, "找不到权限配置文件")
				return
			}

			conf := ObjToMap(string(btes))

			flow := conf.GetMap("flow").GetMap(tableName)
			if flow != nil && flow.GetMap("sql") != nil {
				sql := ObjToMap(DeepCopyMap(flow.GetMap("sql")))
				for k, _ := range sql {

					if k == "parent_ids[~]" && tableName == fileConfig.GetString("table") {
						where[tableName+"."+k] = strings.Replace(sql.GetString(k), "id", data.GetString("id"), -1)
						continue
					}
					if k == "parent_ids[~]" && data[tableName+"_id"] != nil {
						where[tableName+"."+k] = strings.Replace(sql.GetString(k), tableName+"_id", data.GetString(tableName+"_id"), -1)
						continue
					}
					if data[sql.GetString(k)] != nil {
						where[tableName+"."+k] = data[sql.GetString(k)]
					}
				}
			}

			columnStr, where := that.MakeCodeRouter[hotimeName].Search(tableName, data, where, 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, where)
				reData = that.Db.Page(page, pageSize).
					PageSelect(tableName, columnStr, where)
			}
			if download == 1 {
				reData = that.Db.Select(tableName, columnStr, where)
			}

			for _, v := range reData {
				for k, _ := range v {

					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"] = 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 || column["link"] == nil || !(column["list"] == nil || column["list"] == true) || v.GetCeilInt64(column.GetString("name")) == 0 {
						continue
					}

					if column["name"] == "parent_id" {

						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"))
						}
					}

					if column.GetString("name") != "parent_id" {
						reStr := ""

						if column.GetString("value") == "name" && that.MakeCodeRouter[hotimeName].TableColumns[column.GetString("link")]["nickname"] != nil {
							reStr += "nickname,"
						}

						if that.MakeCodeRouter[hotimeName].TableColumns[column.GetString("link")]["phone"] != nil {
							reStr += "phone,"
						}
						reStr += column.GetString("value")

						joinData := that.Db.Get(column.GetString("link"), reStr, Map{"id": v.GetCeilInt64(column.GetString("name"))})

						if joinData != nil {
							for kj, vj := range joinData {
								v[column.GetString("link")+"_"+column.GetString("name")+"_"+kj] = vj
							}

						}

					}

				}
			}

			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
						}

						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/"
				}

				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.Display(0, filePath)
				return
			}

			that.Display(0, Map{"count": count, "data": reData})
		},
		"analyse": 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()})
			where := Map{}

			btes, err := ioutil.ReadFile(fileConfig.GetString("config"))
			if err != nil {
				that.Display(4, "找不到权限配置文件")
				return
			}

			conf := ObjToMap(string(btes))

			flow := conf.GetMap("flow").GetMap(tableName)
			if flow != nil && flow.GetMap("sql") != nil {
				sql := ObjToMap(DeepCopyMap(flow.GetMap("sql")))
				for k, _ := range sql {

					if k == "parent_ids[~]" && tableName == fileConfig.GetString("table") {
						where[k] = strings.Replace(sql.GetString(k), "id", data.GetString("id"), -1)
						continue
					}
					if k == "parent_ids[~]" && data[tableName+"_id"] != nil {
						where[k] = strings.Replace(sql.GetString(k), tableName+"_id", data.GetString(tableName+"_id"), -1)
						continue
					}
					if data[sql.GetString(k)] != nil {
						where[k] = data[sql.GetString(k)]
					}
				}
			}

			_, where = that.MakeCodeRouter[hotimeName].Search(tableName, data, where, that.Req, that.Db)
			delete(where, "ORDER")

			redData := Map{}

			data["count"] = that.Db.Count(tableName, where)
			testQu := []string{}
			testQuData := that.MakeCodeRouter[hotimeName].TableColumns[tableName]
			for key, _ := range testQuData {

				testQu = append(testQu, key)
			}
			sort.Strings(testQu)
			for _, k := range testQu {
				v := testQuData[k]

				if v.GetBool("notUse") {

					continue
				}

				if strings.Contains(k, "state") {
					continue
				}
				if strings.Contains(k, "scope") {
					continue
				}
				if strings.Contains(k, "_code") {
					continue
				}

				if v.GetString("type") == "select" {

					ops := v.GetSlice("options")
					for k1, _ := range ops {
						v1 := ops.GetMap(k1)
						where1 := DeepCopyMap(where).(Map)
						if where1["AND"] != nil {
							and := where1.GetMap("AND")
							and[k] = v1.GetString("value")
						} else {
							where1[k] = v1.GetString("value")
							where1 = Map{"AND": where1}
						}
						v1["count"] = that.Db.Count(tableName, where1)
						ops[k1] = v1
					}
					redData[k] = ops
				}

				if v.GetString("type") == "number" {

					if strings.Contains(k, "area_id") {
						areas := that.Db.Select("area", "id,name", Map{"parent_id": 533301})
						for ak, av := range areas {
							where1 := DeepCopyMap(where).(Map)
							if where1["AND"] != nil {
								and := where1.GetMap("AND")
								and["area_id"] = av.GetCeilInt64("id")
							} else {
								where1["area_id"] = av.GetCeilInt64("id")
								where1 = Map{"AND": where1}
							}
							av["count"] = that.Db.Count(tableName, where1)
							areas[ak] = av
						}

						redData["area"] = areas
						continue
					}

					if strings.Contains(k, "_id") {
						continue
					}
					if k == "id" {
						continue
					}
					if strings.Contains(k, "percent") {
						continue
					}
					if strings.Contains(k, "exp_") {
						continue
					}
					if strings.Contains(k, "side") {
						continue
					}
					if strings.Contains(k, "lat") {
						continue
					}
					if strings.Contains(k, "lng") {
						continue
					}
					if strings.Contains(k, "distance") {
						continue
					}
					where1 := DeepCopyMap(where).(Map)
					redData[k] = that.Db.Sum(tableName, k, where1)

				}

			}

			that.Display(0, redData)

		},
	},
	"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 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{"OR": where, "password": Md5(password)}
			} else {
				where["password"] = Md5(password)
			}

			if len(where) > 1 {
				where = Map{"AND": where}
			}

			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"))})
					oldLinkAuth := linkAuthMap.GetMap("auth")
					linkAuth := Map{}

					if oldLinkAuth != nil {
						linkAuth = oldLinkAuth
					}

					for k1, _ := range menus {
						v1 := menus.GetMap(k1)
						name := v1.GetString("name")
						if name == "" {
							name = v1.GetString("table")
						}
						if v1["auth"] != nil {

							if oldLinkAuth == nil {
								linkAuth[name] = v1["auth"]
							} 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
							}
						}

						menusChild := v1.GetSlice("menus")
						for k2, _ := range menusChild {
							v2 := menusChild.GetMap(k2)
							name := v2.GetString("name")
							if name == "" {
								name = v2.GetString("table")
							}
							if v2["auth"] != nil {

								if oldLinkAuth == 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)
										}
									}
									linkAuth[name] = newAuth
									continue

								}
							}
						}
					}

					for k1, _ := range menus {
						v1 := menus.GetMap(k1)

						name := v1.GetString("name")
						if name == "" {
							name = v1.GetString("table")
						}

						if len(linkAuth.GetSlice(name)) != 0 {
							v1["auth"] = linkAuth[name]
						} else if fileConfig.GetString("table") == v1.GetString("table") {
							v1["auth"] = Slice{"info", "edit"}
						} else {
							v1["auth"] = linkAuth[name]
						}
						v1menus := v1.GetSlice("menus")
						for k2, _ := range v1menus {
							v2 := v1menus.GetMap(k2)

							name := v2.GetString("name")
							if name == "" {
								name = v2.GetString("table")
							}
							if len(linkAuth.GetSlice(name)) != 0 {
								v2["auth"] = linkAuth[name]
							} else if fileConfig.GetString("table") == v2.GetString("table") {
								v2["auth"] = Slice{"info", "edit"}

							} else {
								v2["auth"] = linkAuth[name]
							}

						}
					}

					break
				}
			}

			config := DeepCopyMap(that.MakeCodeRouter[hotimeName].Config).(Map)

			config["menus"] = menus
			newTables := Map{}
			newTables[fileConfig.GetString("table")] = config.GetMap("tables").GetMap(fileConfig.GetString("table"))
			for k1, _ := range menus {
				v1 := menus.GetMap(k1)
				if config.GetMap("tables").GetMap(v1.GetString("table")) != nil && len(v1.GetSlice("auth")) != 0 {
					config.GetMap("tables").GetMap(v1.GetString("table"))["auth"] = Slice{}
					newTables[v1.GetString("table")] = 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 && len(v2.GetSlice("auth")) != 0 {

						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
									}
								}

							}
						}
						config.GetMap("tables").GetMap(v2.GetString("table"))["auth"] = Slice{}
						newTables[v2.GetString("table")] = config.GetMap("tables").GetMap(v2.GetString("table"))

					}
				}
			}

			conf["tables"] = newTables

			that.Display(0, conf)
		},

		"wallpaper": func(that *Context) {
			random := ObjToCeilInt(that.Req.FormValue("random"))

			tp := ObjToCeilInt(that.Req.FormValue("type"))

			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)

		},
	},
}

Project 管理端项目

Functions

func SetDB

func SetDB(appIns *Application)

SetDB 智能数据库设置

func SetMysqlDB

func SetMysqlDB(appIns *Application, config Map)

func SetSqliteDB

func SetSqliteDB(appIns *Application, config Map)

Types

type Application

type Application struct {
	MakeCodeRouter map[string]*code.MakeCode
	MethodRouter
	Router
	Error
	Log           *logrus.Logger
	WebConnectLog *logrus.Logger
	Port          string //端口号
	TLSPort       string //ssl访问端口号

	Config Map
	Db     HoTimeDB
	*HoTimeCache
	*http.Server
	http.Handler
	// contains filtered or unexported fields
}

func Init

func Init(config string) *Application

Init 初始化application

func (*Application) Run

func (that *Application) Run(router Router)

Run 启动实例

func (*Application) ServeHTTP

func (that *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Application) SetCache

func (that *Application) SetCache()

SetCache 设置配置文件路径全路径或者相对路径

func (*Application) SetConfig

func (that *Application) SetConfig(configPath ...string)

SetConfig 设置配置文件路径全路径或者相对路径

func (*Application) SetConnectDB

func (that *Application) SetConnectDB(connect func(err ...*Error) (master, slave *sql.DB))

SetConnectDB 启动实例

func (*Application) SetConnectListener

func (that *Application) SetConnectListener(lis func(that *Context) (isFinished bool))

SetConnectListener 连接判断,返回false继续传输至控制层,true则停止传输

func (*Application) SetDefault

func (that *Application) SetDefault(connect func(err ...*Error) (*sql.DB, *sql.DB))

SetDefault 默认配置缓存和session实现

type Context

type Context struct {
	*Application
	Resp         http.ResponseWriter
	Req          *http.Request
	Log          Map //日志有则创建
	RouterString []string
	Config       Map
	Db           *HoTimeDB
	RespData     Map
	RespFunc     func()
	//CacheIns
	SessionIns
	DataSize   int
	HandlerStr string //复写请求url
}

func (*Context) Display

func (that *Context) Display(statu int, data interface{})

打印

func (*Context) Mtd

func (that *Context) Mtd(router [3]string) Map

Mtd 唯一标志

func (*Context) View

func (that *Context) View()

type Ctr

type Ctr map[string]Method

Ctr 控制器

type Method

type Method func(that *Context)

type MethodRouter

type MethodRouter map[string]Method //直接字符串关联函数

type Proj

type Proj map[string]Ctr

type Router

type Router map[string]Proj

type SessionIns

type SessionIns struct {
	*HoTimeCache
	SessionId string
	Map
	ContextBase
}

session对象

func (*SessionIns) Init

func (that *SessionIns) Init(cache *HoTimeCache)

func (*SessionIns) Session

func (that *SessionIns) Session(key string, data ...interface{}) *Obj

Directories

Path Synopsis
dri
rsa

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL