response.go 632 B

12345678910111213141516171819202122232425262728293031323334
  1. package micro
  2. const (
  3. SUCCESS = 200
  4. FAIL = 500
  5. ERROR = 400
  6. UNAUTHORIZED = 401
  7. )
  8. type Response struct {
  9. Code int64 `json:"code"`
  10. Message string `json:"message"`
  11. Data any `json:"data"`
  12. }
  13. // Success 成功
  14. func Success(data interface{}) Response {
  15. return Response{SUCCESS, "success", data}
  16. }
  17. // Fail 失败
  18. func Fail(msg string) Response {
  19. return Response{FAIL, msg, ""}
  20. }
  21. // Error 错误
  22. func Error(msg string) Response {
  23. return Response{ERROR, msg, ""}
  24. }
  25. // Unauthorized 认证失败
  26. func Unauthorized(msg string, data interface{}) Response {
  27. return Response{UNAUTHORIZED, msg, data}
  28. }