R.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package e
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "project_management/unity"
  6. )
  7. type R struct {
  8. Code Rescode `json:"code"`
  9. Message any `json:"message"`
  10. Data interface{} `json:"data"`
  11. }
  12. func ResponseError(c *gin.Context, code Rescode) {
  13. c.JSON(http.StatusOK, &R{
  14. Code: code,
  15. Message: code.GetMsg(),
  16. Data: nil,
  17. })
  18. }
  19. func ResponseSuccess(c *gin.Context, data interface{}) {
  20. c.JSON(http.StatusOK, &R{
  21. Code: SUCCESS,
  22. Message: SUCCESS.GetMsg(),
  23. Data: data,
  24. })
  25. }
  26. func ResponseWithMsg(c *gin.Context, code Rescode, msg any) {
  27. c.JSON(http.StatusOK, &R{
  28. Code: code,
  29. Message: msg,
  30. Data: nil,
  31. })
  32. }
  33. func ResPonsePage(c *gin.Context, data interface{}, total int64, params unity.QueryPageParams) {
  34. c.JSON(http.StatusOK, &R{
  35. Code: SUCCESS,
  36. Message: SUCCESS.GetMsg(),
  37. Data: gin.H{
  38. "result": data,
  39. "total": total,
  40. "current": params.Page,
  41. "page_size": params.Size,
  42. },
  43. })
  44. }
  45. func CapResPonsePage(c *gin.Context, data interface{}, total int64, params unity.CapQueryPageParams) {
  46. c.JSON(http.StatusOK, &R{
  47. Code: SUCCESS,
  48. Message: SUCCESS.GetMsg(),
  49. Data: gin.H{
  50. "result": data,
  51. "total": total,
  52. "current": params.Page,
  53. "page_size": params.Size,
  54. },
  55. })
  56. }