controller.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package base
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego"
  6. "github.com/astaxie/beego/logs"
  7. "nginx-ui/server/middleware"
  8. "nginx-ui/server/models"
  9. "strconv"
  10. )
  11. type Controller struct {
  12. beego.Controller
  13. jsonData *models.RespData
  14. // Json real data
  15. respData map[string]any
  16. }
  17. func (c *Controller) Json() {
  18. c.checkJsonData()
  19. if c.respData != nil {
  20. c.jsonData.Data = c.respData
  21. }
  22. c.Data["json"] = c.jsonData
  23. c.ServeJSON()
  24. }
  25. func (c *Controller) PostJson(json interface{}) {
  26. c.Data["json"] = json
  27. c.ServeJSON()
  28. }
  29. func (c *Controller) ErrorJson(error error) {
  30. c.SetCode(-1).SetMsg(error.Error()).Json()
  31. }
  32. func (c *Controller) checkJsonData() *Controller {
  33. data := c.jsonData
  34. if data == nil {
  35. data = &models.RespData{
  36. Code: 0,
  37. Msg: "success",
  38. Data: nil,
  39. }
  40. c.jsonData = data
  41. }
  42. return c
  43. }
  44. func (c *Controller) SetData(v interface{}) *Controller {
  45. c.checkJsonData()
  46. c.jsonData.Data = v
  47. return c
  48. }
  49. func (c *Controller) AddRespData(k string, v interface{}) *Controller {
  50. c.checkJsonData()
  51. if c.respData == nil {
  52. c.respData = map[string]any{}
  53. }
  54. c.respData[k] = v
  55. return c
  56. }
  57. func (c *Controller) SetCode(code int) *Controller {
  58. c.checkJsonData()
  59. c.jsonData.Code = code
  60. return c
  61. }
  62. func (c *Controller) SetMsg(msg string) *Controller {
  63. c.checkJsonData()
  64. c.jsonData.Msg = msg
  65. return c
  66. }
  67. func (c *Controller) GetParam(k string) string {
  68. return c.Ctx.Input.Param(k)
  69. }
  70. func (c *Controller) GetIntParam(k string) (int, error) {
  71. idStr := c.Ctx.Input.Param(k)
  72. id, err := strconv.Atoi(idStr)
  73. logs.Info("id", id)
  74. if err != nil {
  75. return 0, err
  76. }
  77. return id, nil
  78. }
  79. func (c *Controller) GetQuery(k string) string {
  80. return c.Ctx.Input.Query(k)
  81. }
  82. func (c *Controller) GetIntQuery(k string) (int, error) {
  83. idStr := c.Ctx.Input.Query(k)
  84. id, err := strconv.Atoi(idStr)
  85. logs.Info("id", id)
  86. if err != nil {
  87. return 0, err
  88. }
  89. return id, nil
  90. }
  91. func (c *Controller) GetUser(required bool) *models.User {
  92. data := c.GetSession("user")
  93. if data == nil && required {
  94. middleware.WriteForbidden(c.Ctx.ResponseWriter)
  95. return nil
  96. }
  97. if data == nil {
  98. return nil
  99. }
  100. user := data.(models.User)
  101. return &user
  102. }
  103. func (c *Controller) RequiredUser() *models.User {
  104. return c.GetUser(true)
  105. }
  106. // ReadBody data 非指针
  107. func (c *Controller) ReadBody(data any) bool {
  108. err := json.Unmarshal(c.Ctx.Input.RequestBody, data)
  109. if err != nil {
  110. logs.Error(err, string(c.Ctx.Input.RequestBody))
  111. c.ErrorJson(errors.New("数据解析异常!"))
  112. return false
  113. }
  114. return true
  115. }
  116. func (c *Controller) Forbidden() {
  117. c.SetCode(403).SetMsg("您没有权限操作该实例!").Json()
  118. }