123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package controllers
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "Cold_Api/models/Device"
- "Cold_Api/models/RawSql"
- "fmt"
- beego "github.com/beego/beego/v2/server/web"
- "math"
- "strings"
- )
- //Handle
- type RawSqlController struct {
- beego.Controller
- }
- func (c *RawSqlController) RawSql_html() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- if admin_r.Id != 1 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
- c.ServeJSON()
- return
- }
- page, _ := c.GetInt("page")
- println(page)
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("Page_size")
- if page_z == 0 {
- page_z = conf.Page_size
- }
- List, cnt := RawSql.Read_RawSql_List(page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
- c.Data["List"] = List
- c.Data["Page"] = page
- c.Data["Page_size"] = page_size
- c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
- c.Data["cnt"] = cnt
- c.TplName = "RawSql/RawSql.html"
- }
- func (c *RawSqlController) RawSql__html() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- if admin_r.Id != 1 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
- c.ServeJSON()
- return
- }
- c.Data["Device_lite"] = Device.Read_Device_ALL_bind(admin_r)
- //c.Data["Admin_Power"], _ = Admin.Read_AdminPower_ById(admin_r.Admin_power)
- id, _ := c.GetInt("id")
- c.Data["id"] = id
- if id > 0 {
- rs := RawSql.Read_RawSql_ById(id)
- c.Data["Date"] = rs
- }
- c.TplName = "RawSql/RawSql-.html"
- }
- func (c *RawSqlController) List_Post() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- if admin_r.Id != 1 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
- c.ServeJSON()
- return
- }
- var id int64
- var err error
- id, _ = c.GetInt64("id")
- T_name := c.GetString("T_name")
- T_text := c.GetString("T_text")
- var_ := RawSql.RawSql{
- T_name: T_name,
- T_text: T_text,
- }
- T_SQL_ := strings.ToLower(T_text)
- if strings.Contains(T_SQL_, "show") ||
- strings.Contains(T_SQL_, "create") ||
- strings.Contains(T_SQL_, "drop") ||
- strings.Contains(T_SQL_, "desc") ||
- strings.Contains(T_SQL_, "alter") ||
- strings.Contains(T_SQL_, "insert") ||
- strings.Contains(T_SQL_, "update") ||
- strings.Contains(T_SQL_, "delete") {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败,有违禁 语法!"}
- c.ServeJSON()
- return
- }
- if id > 0 {
- var_.Id = int(id)
- is := RawSql.Update_TRawSql(var_, "T_name", "T_text")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- } else {
- _, err = RawSql.Add_RawSql(var_)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func (c *RawSqlController) List_Del() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Ctx.Redirect(302, "Login")
- return
- }
- if admin_r.Id != 1 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
- c.ServeJSON()
- return
- }
- Id, _ := c.GetInt("Id")
- if Id > 0 {
- RawSql_r := RawSql.Read_RawSql_ById(Id)
- RawSql.Delete_RawSql(RawSql_r)
- } else {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 执行 SQL
- func (c *RawSqlController) Rawv3() {
- // 验证登录
- b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
- if !b_ {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
- c.ServeJSON()
- return
- }
- T_id := c.GetString("T_id")
- if len(T_id) != 5 {
- c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- RawSql_r := RawSql.Read_RawSql_ByT_id(T_id)
- if RawSql_r.Id == 0 {
- c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
- c.ServeJSON()
- return
- }
- T_data := c.GetString("T_data")
- fmt.Println(" 执行SQL:", T_id, " => [", T_data, "] ", admin_r.Admin_uuid, admin_r.Admin_name)
- var T_dataL []string
- for _, v := range strings.Split(T_data, "|") {
- if len(v) > 0 {
- T_dataL = append(T_dataL, v)
- }
- }
- str_, Params := Device.Read_SqlRawL(RawSql_r.T_text, T_dataL)
- if len(str_) > 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: str_}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Params}
- c.ServeJSON()
- return
- }
|