RawSql.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package controllers
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Device"
  6. "Cold_Api/models/RawSql"
  7. "fmt"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "math"
  10. "strings"
  11. )
  12. //Handle
  13. type RawSqlController struct {
  14. beego.Controller
  15. }
  16. func (c *RawSqlController) RawSql_html() {
  17. // 验证登录
  18. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  19. if !b_ {
  20. c.Ctx.Redirect(302, "Login")
  21. return
  22. }
  23. if admin_r.Id != 1 {
  24. c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
  25. c.ServeJSON()
  26. return
  27. }
  28. page, _ := c.GetInt("page")
  29. println(page)
  30. if page < 1 {
  31. page = 1
  32. }
  33. page_z, _ := c.GetInt("Page_size")
  34. if page_z == 0 {
  35. page_z = conf.Page_size
  36. }
  37. List, cnt := RawSql.Read_RawSql_List(page, page_z)
  38. page_size := math.Ceil(float64(cnt) / float64(conf.Page_size))
  39. c.Data["List"] = List
  40. c.Data["Page"] = page
  41. c.Data["Page_size"] = page_size
  42. c.Data["Pages"] = lib.Func_page(int64(page), int64(page_size))
  43. c.Data["cnt"] = cnt
  44. c.TplName = "RawSql/RawSql.html"
  45. }
  46. func (c *RawSqlController) RawSql__html() {
  47. // 验证登录
  48. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  49. if !b_ {
  50. c.Ctx.Redirect(302, "Login")
  51. return
  52. }
  53. if admin_r.Id != 1 {
  54. c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
  55. c.ServeJSON()
  56. return
  57. }
  58. c.Data["Device_lite"] = Device.Read_Device_ALL_bind(admin_r)
  59. //c.Data["Admin_Power"], _ = Admin.Read_AdminPower_ById(admin_r.Admin_power)
  60. id, _ := c.GetInt("id")
  61. c.Data["id"] = id
  62. if id > 0 {
  63. rs := RawSql.Read_RawSql_ById(id)
  64. c.Data["Date"] = rs
  65. }
  66. c.TplName = "RawSql/RawSql-.html"
  67. }
  68. func (c *RawSqlController) List_Post() {
  69. // 验证登录
  70. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  71. if !b_ {
  72. c.Ctx.Redirect(302, "Login")
  73. return
  74. }
  75. if admin_r.Id != 1 {
  76. c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
  77. c.ServeJSON()
  78. return
  79. }
  80. var id int64
  81. var err error
  82. id, _ = c.GetInt64("id")
  83. T_name := c.GetString("T_name")
  84. T_text := c.GetString("T_text")
  85. var_ := RawSql.RawSql{
  86. T_name: T_name,
  87. T_text: T_text,
  88. }
  89. T_SQL_ := strings.ToLower(T_text)
  90. if strings.Contains(T_SQL_, "show") ||
  91. strings.Contains(T_SQL_, "create") ||
  92. strings.Contains(T_SQL_, "drop") ||
  93. strings.Contains(T_SQL_, "desc") ||
  94. strings.Contains(T_SQL_, "alter") ||
  95. strings.Contains(T_SQL_, "insert") ||
  96. strings.Contains(T_SQL_, "update") ||
  97. strings.Contains(T_SQL_, "delete") {
  98. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败,有违禁 语法!"}
  99. c.ServeJSON()
  100. return
  101. }
  102. if id > 0 {
  103. var_.Id = int(id)
  104. is := RawSql.Update_TRawSql(var_, "T_name", "T_text")
  105. if !is {
  106. c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. } else {
  111. _, err = RawSql.Add_RawSql(var_)
  112. if err != nil {
  113. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  114. c.ServeJSON()
  115. return
  116. }
  117. }
  118. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  119. c.ServeJSON()
  120. return
  121. }
  122. func (c *RawSqlController) List_Del() {
  123. // 验证登录
  124. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  125. if !b_ {
  126. c.Ctx.Redirect(302, "Login")
  127. return
  128. }
  129. if admin_r.Id != 1 {
  130. c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
  131. c.ServeJSON()
  132. return
  133. }
  134. Id, _ := c.GetInt("Id")
  135. if Id > 0 {
  136. RawSql_r := RawSql.Read_RawSql_ById(Id)
  137. RawSql.Delete_RawSql(RawSql_r)
  138. } else {
  139. c.Data["json"] = lib.JSONS{Code: 201, Msg: "e!"}
  140. c.ServeJSON()
  141. return
  142. }
  143. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  144. c.ServeJSON()
  145. return
  146. }
  147. // 执行 SQL
  148. func (c *RawSqlController) Rawv3() {
  149. // 验证登录
  150. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  151. if !b_ {
  152. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  153. c.ServeJSON()
  154. return
  155. }
  156. T_id := c.GetString("T_id")
  157. if len(T_id) != 5 {
  158. c.Data["json"] = lib.JSONS{Code: 203, Msg: "T_id Err!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. RawSql_r := RawSql.Read_RawSql_ByT_id(T_id)
  163. if RawSql_r.Id == 0 {
  164. c.Data["json"] = lib.JSONS{Code: 204, Msg: "T_id Err!"}
  165. c.ServeJSON()
  166. return
  167. }
  168. T_data := c.GetString("T_data")
  169. fmt.Println(" 执行SQL:", T_id, " => [", T_data, "] ", admin_r.Admin_uuid, admin_r.Admin_name)
  170. var T_dataL []string
  171. for _, v := range strings.Split(T_data, "|") {
  172. if len(v) > 0 {
  173. T_dataL = append(T_dataL, v)
  174. }
  175. }
  176. str_, Params := Device.Read_SqlRawL(RawSql_r.T_text, T_dataL)
  177. if len(str_) > 0 {
  178. c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: str_}
  179. c.ServeJSON()
  180. return
  181. }
  182. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Params}
  183. c.ServeJSON()
  184. return
  185. }