response.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Package response 响应处理工具
  2. package response
  3. import (
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/runningwater/gohub/pkg/logger"
  7. "gorm.io/gorm"
  8. )
  9. // JSON 响应 200 和 JSON 数据
  10. func JSON(c *gin.Context, data any) {
  11. c.JSON(http.StatusOK, data)
  12. }
  13. // Success 响应 200 和预设『操作成功!』的 JSON 数据
  14. // 执行某个『没有具体返回数据』的『变更』操作成功后调用,例如删除、修改密码、修改手机号
  15. func Success(c *gin.Context) {
  16. JSON(c, gin.H{
  17. "success": true,
  18. "message": "操作成功!",
  19. })
  20. }
  21. // Data 响应 200 和带 data 键的 JSON 数据
  22. // 执行『更新操作』成功后调用,例如更新话题,成功后返回已更新的话题
  23. func Data(c *gin.Context, data any) {
  24. JSON(c, gin.H{
  25. "success": true,
  26. "data": data,
  27. })
  28. }
  29. // Created 响应 201 和带 data 键的 JSON 数据
  30. // 执行『更新操作』成功后调用,例如更新话题,成功后返回已更新的话题
  31. func Created(c *gin.Context, data any) {
  32. c.JSON(http.StatusCreated, gin.H{
  33. "success": true,
  34. "data": data,
  35. })
  36. }
  37. // CreatedJSON 响应 201 和 JSON 数据
  38. func CreatedJSON(c *gin.Context, data any) {
  39. c.JSON(http.StatusCreated, data)
  40. }
  41. // Abort404 响应 404,未传参 msg 时使用默认消息
  42. func Abort404(c *gin.Context, msg ...string) {
  43. c.AbortWithStatusJSON(http.StatusNotFound, gin.H{
  44. "message": defaultMessage("数据不存在,请确定请求正确", msg...),
  45. })
  46. }
  47. // Abort403 响应 403,未传参 msg 时使用默认消息
  48. func Abort403(c *gin.Context, msg ...string) {
  49. c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
  50. "message": defaultMessage("权限不足,请确定您有对应的权限", msg...),
  51. })
  52. }
  53. // Abort500 响应 500,未传参 msg 时使用默认消息
  54. func Abort500(c *gin.Context, msg ...string) {
  55. c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
  56. "message": defaultMessage("服务器内部错误,请稍后再试", msg...),
  57. })
  58. }
  59. // BadRequest 响应 400,传参 err 对象,未传参 msg 时使用默认消息
  60. // 在解析用户请求,请求的格式或者方法不符合预期时调用
  61. func BadRequest(c *gin.Context, err error, msg ...string) {
  62. logger.LogIf(err)
  63. c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
  64. "message": defaultMessage("请求解析错误,请确认请求格式是否正确。上传文件请使用 multipart 标头,参数请使用 JSON 格式。", msg...),
  65. "error": err.Error(),
  66. })
  67. }
  68. // Error 响应 404 或 422,未传参 msg 时使用默认消息
  69. // 处理请求时出现错误 err,会附带返回 error 信息,如登录错误、找不到 ID 对应的 Model
  70. func Error(c *gin.Context, err error, msg ...string) {
  71. logger.LogIf(err)
  72. // error 类型为『数据库未找到内容』
  73. if err == gorm.ErrRecordNotFound {
  74. Abort404(c)
  75. return
  76. }
  77. c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
  78. "message": defaultMessage("请求处理失败,请查看 error 的值", msg...),
  79. "error": err.Error(),
  80. })
  81. }
  82. // ValidationError 处理表单验证不通过的错误,返回的 JSON 示例:
  83. //
  84. // {
  85. // "errors": {
  86. // "phone": [
  87. // "手机号为必填项,参数名称 phone",
  88. // "手机号长度必须为 11 位的数字"
  89. // ]
  90. // },
  91. // "message": "请求验证不通过,具体请查看 errors"
  92. // }
  93. func ValidationError(c *gin.Context, errors map[string][]string) {
  94. c.AbortWithStatusJSON(http.StatusUnprocessableEntity, gin.H{
  95. "message": "请求验证不通过,具体请查看 errors",
  96. "errors": errors,
  97. })
  98. }
  99. // Unauthorized 响应 401,未传参 msg 时使用默认消息
  100. // 登录失败、jwt 解析失败时调用
  101. func Unauthorized(c *gin.Context, msg ...string) {
  102. c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
  103. "message": defaultMessage("请求解析错误,请确认请求格式是否正确。上传文件请使用 multipart 标头,参数请使用 JSON 格式。", msg...),
  104. })
  105. }
  106. // defaultMessage 内用的辅助函数,用以支持默认参数默认值
  107. // Go 不支持参数默认值,只能使用多变参数来实现类似效果
  108. func defaultMessage(defaultMsg string, msg ...string) (message string) {
  109. if len(msg) > 0 {
  110. message = msg[0]
  111. } else {
  112. message = defaultMsg
  113. }
  114. return
  115. }