banip.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // 实时数据
  2. package controllers
  3. import (
  4. "box-gm/models"
  5. "fmt"
  6. "github.com/astaxie/beego"
  7. )
  8. type BanIPController struct {
  9. beego.Controller
  10. }
  11. func (c *BanIPController) Get() {
  12. userinfo := c.GetSession("user")
  13. if userinfo == nil {
  14. c.Ctx.Redirect(302, "/accountlogin")
  15. return
  16. }
  17. c.Data["username"] = userinfo.(*LoginInfo).Username
  18. c.Data["token"] = models.GetToken()
  19. userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
  20. if (userPermission & (1 << uint(models.ModelSystem))) == 0 {
  21. c.Ctx.Redirect(302, "/accountlogin")
  22. return
  23. }
  24. c.Data["ipList"] = models.GetBanIPInfo()
  25. c.TplName = "banip.tpl"
  26. }
  27. func (c *BanIPController) Post() {
  28. userinfo := c.GetSession("user")
  29. if userinfo == nil {
  30. c.Ctx.Redirect(302, "/accountlogin")
  31. return
  32. }
  33. ip := c.GetString("ip")
  34. if ip == "" {
  35. c.Data["json"] = &map[string]interface{}{"status": false, "info": "请输入正确IP地址"}
  36. c.ServeJSON()
  37. return
  38. }
  39. u := new(models.BanIPInfo)
  40. u.IP = ip
  41. err := models.AddBanIPInfo(u)
  42. if err != nil {
  43. c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
  44. c.ServeJSON()
  45. return
  46. }
  47. c.Data["json"] = &map[string]interface{}{"status": true, "info": "新增IP封禁"}
  48. c.ServeJSON()
  49. }
  50. func (c *BanIPController) Delete() {
  51. userinfo := c.GetSession("user")
  52. if userinfo == nil {
  53. c.Ctx.Redirect(302, "/accountlogin")
  54. return
  55. }
  56. ip := c.GetString("ip")
  57. err := models.DelBanIPInfoByIP(ip)
  58. if err != nil {
  59. c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
  60. c.ServeJSON()
  61. return
  62. }
  63. c.Data["json"] = &map[string]interface{}{"status": true, "info": fmt.Sprintf("IP[%s]解封", ip)}
  64. c.ServeJSON()
  65. }