response.go 470 B

1234567891011121314151617181920212223
  1. package utils
  2. import (
  3. "github.com/beego/beego/v2/server/web"
  4. )
  5. // Response 统一 JSON 结构体
  6. type Response struct {
  7. Code int `json:"code"`
  8. Msg string `json:"msg"`
  9. Data interface{} `json:"data"`
  10. }
  11. // JSON 统一返回处理
  12. func JSON(c *web.Controller, code int, msg string, data interface{}) {
  13. c.Data["json"] = Response{
  14. Code: code,
  15. Msg: msg,
  16. Data: data,
  17. }
  18. c.ServeJSON()
  19. c.StopRun() // 类似 PHP 的 exit()
  20. }