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.

80 lines
1.5 KiB

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