return.go 984 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package response
  2. import (
  3. "github.com/google/uuid"
  4. "net/http"
  5. )
  6. // Error 失败数据处理
  7. func Error(code int, err error, msg string) Response {
  8. var res Response
  9. if err != nil {
  10. res.SetMsg(err.Error())
  11. }
  12. if msg != "" {
  13. res.SetMsg(msg)
  14. }
  15. res.SetTraceID(uuid.New().String())
  16. res.SetCode(int32(code))
  17. res.SetSuccess(false)
  18. return res
  19. }
  20. // OK 通常成功数据处理
  21. func OK(data interface{}, msg string) Response {
  22. var res Response
  23. res.SetSuccess(true)
  24. if msg != "" {
  25. res.SetMsg(msg)
  26. }
  27. res.SetTraceID(uuid.New().String())
  28. res.SetCode(http.StatusOK)
  29. res.Data = data
  30. return res
  31. }
  32. // PageOK 分页数据处理
  33. func PageOK(result interface{}, count int, pageIndex int, pageSize int, msg string) Page {
  34. var res Page
  35. res.SetSuccess(true)
  36. if msg != "" {
  37. res.SetMsg(msg)
  38. }
  39. res.SetTraceID(uuid.New().String())
  40. res.SetCode(http.StatusOK)
  41. res.Data.List = result
  42. res.Data.Count = count
  43. res.Data.Page = pageIndex
  44. res.Data.PageSize = pageSize
  45. return res
  46. }