You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.3 KiB

  1. package dto
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. )
  5. // 定义响应结构
  6. type Result struct {
  7. Code int `json:"code"`
  8. Message string `json:"message"`
  9. Data interface{} `json:"data"` // 修改为实体类型
  10. }
  11. /*成功,没有数据*/
  12. func Success() *Result {
  13. return &Result{
  14. Code: 200,
  15. Message: "success",
  16. Data: g.Map{},
  17. }
  18. }
  19. /*成功,有数据*/
  20. func SuccessWithData(data interface{}) *Result {
  21. return &Result{
  22. Code: 200,
  23. Message: "success",
  24. Data: data,
  25. }
  26. }
  27. /*错误,只有错误信息,错误码0*/
  28. func Error(msg string) *Result {
  29. return &Result{
  30. Code: 0, // 错误码
  31. Message: msg, // 错误信息
  32. Data: g.Map{},
  33. }
  34. }
  35. /*错误,有错误信息,有错误码*/
  36. func ErrorWithCode(code int, msg string) *Result {
  37. return &Result{
  38. Code: code, // 错误码
  39. Message: msg, // 错误信息
  40. Data: g.Map{},
  41. }
  42. }
  43. /*未授权,有错误信息,错误码为401*/
  44. func Unauthorized(msg string) *Result {
  45. return &Result{
  46. Code: 401, // 错误码
  47. Message: msg, // 错误信息
  48. Data: g.Map{},
  49. }
  50. }
  51. /*未授权,有错误信息,允许自定义错误码*/
  52. func UnauthorizedWithCode(code int, msg string) *Result {
  53. return &Result{
  54. Code: code, // 错误码
  55. Message: msg, // 错误信息
  56. Data: g.Map{},
  57. }
  58. }