respond.go 521 B

12345678910111213141516171819202122
  1. package controller
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. // 返回状态码和错误信息
  7. func respondError(c *gin.Context, code uint, err error) {
  8. c.JSON(http.StatusOK, gin.H{"code": code, "message": err.Error()})
  9. }
  10. // 返回状态码 500 和错误信息
  11. func respondUnknownError(c *gin.Context, err error) {
  12. respondError(c, http.StatusInternalServerError, err)
  13. }
  14. // 返回状态码 200 和数据
  15. func respondOK(c *gin.Context, data any) {
  16. c.JSON(http.StatusOK, gin.H{"code": 200, "data": data})
  17. }