RawSql.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package controllers
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "Cold_Api/models/Account"
  5. "Cold_Api/models/Device"
  6. "Cold_Api/models/RawSql"
  7. "fmt"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "strings"
  10. )
  11. // Handle
  12. type RawSqlController struct {
  13. beego.Controller
  14. Admin_r Account.Admin // 登陆的用户
  15. }
  16. func (c *RawSqlController) Prepare() {
  17. GetCookie := c.Ctx.GetCookie("User_tokey")
  18. GetString := c.GetString("User_tokey")
  19. User_tokey := GetCookie
  20. if len(User_tokey) == 0 {
  21. User_tokey = GetString
  22. }
  23. c.Admin_r = *Account.Admin_r
  24. }
  25. func (c *RawSqlController) List_Post() {
  26. var id int64
  27. var err error
  28. id, _ = c.GetInt64("id")
  29. T_name := c.GetString("T_name")
  30. T_text := c.GetString("T_text")
  31. var_ := RawSql.RawSql{
  32. T_name: T_name,
  33. T_text: T_text,
  34. }
  35. T_SQL_ := strings.ToLower(T_text)
  36. if strings.Contains(T_SQL_, "show") ||
  37. strings.Contains(T_SQL_, "create") ||
  38. strings.Contains(T_SQL_, "drop") ||
  39. strings.Contains(T_SQL_, "desc") ||
  40. strings.Contains(T_SQL_, "alter") ||
  41. strings.Contains(T_SQL_, "insert") ||
  42. strings.Contains(T_SQL_, "update") ||
  43. strings.Contains(T_SQL_, "delete") {
  44. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败,有违禁 语法!"}
  45. c.ServeJSON()
  46. return
  47. }
  48. if id > 0 {
  49. var_.Id = int(id)
  50. is := RawSql.Update_TRawSql(var_, "T_name", "T_text")
  51. if !is {
  52. c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
  53. c.ServeJSON()
  54. return
  55. }
  56. } else {
  57. _, err = RawSql.Add_RawSql(var_)
  58. if err != nil {
  59. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  60. c.ServeJSON()
  61. return
  62. }
  63. }
  64. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  65. c.ServeJSON()
  66. return
  67. }
  68. func (c *RawSqlController) List_Del() {
  69. Id, _ := c.GetInt("Id")
  70. if Id > 0 {
  71. RawSql_r := RawSql.Read_RawSql_ById(Id)
  72. RawSql.Delete_RawSql(RawSql_r)
  73. } else {
  74. c.Data["json"] = lib.JSONS{Code: 202, Msg: "e!"}
  75. c.ServeJSON()
  76. return
  77. }
  78. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  79. c.ServeJSON()
  80. return
  81. }
  82. // 执行 SQL
  83. func (c *RawSqlController) Raw() {
  84. T_id := c.GetString("T_id")
  85. if len(T_id) != 5 {
  86. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  87. c.ServeJSON()
  88. return
  89. }
  90. RawSql_r := RawSql.Read_RawSql_ByT_id(T_id)
  91. if RawSql_r.Id == 0 {
  92. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  93. c.ServeJSON()
  94. return
  95. }
  96. T_data := c.GetString("T_data")
  97. fmt.Println(" 执行SQL:", T_id, " => [", T_data, "] ", c.Admin_r.T_uuid, c.Admin_r.T_name)
  98. var T_dataL []string
  99. for _, v := range strings.Split(T_data, "|") {
  100. if len(v) > 0 {
  101. T_dataL = append(T_dataL, v)
  102. }
  103. }
  104. str_, Params := Device.Read_SqlRawL(RawSql_r.T_text, T_dataL)
  105. if len(str_) > 0 {
  106. c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: str_}
  107. c.ServeJSON()
  108. return
  109. }
  110. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Params}
  111. c.ServeJSON()
  112. return
  113. }