response.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package response
  2. import (
  3. "github.com/360EntSecGroup-Skylar/excelize"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "strconv"
  7. )
  8. type Response struct {
  9. Code int `json:"code"`
  10. Data interface{} `json:"data"`
  11. Msg string `json:"msg"`
  12. }
  13. const (
  14. ERROR = 7
  15. SUCCESS = 0
  16. )
  17. func Result(code int, data interface{}, msg string, c *gin.Context) {
  18. // 开始时间
  19. c.JSON(http.StatusOK, Response{
  20. code,
  21. data,
  22. msg,
  23. })
  24. }
  25. func Ok(c *gin.Context) {
  26. Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
  27. }
  28. func OkWithMessage(message string, c *gin.Context) {
  29. Result(SUCCESS, map[string]interface{}{}, message, c)
  30. }
  31. func OkWithData(data interface{}, c *gin.Context) {
  32. Result(SUCCESS, data, "操作成功", c)
  33. }
  34. func OkDetailed(data interface{}, message string, c *gin.Context) {
  35. Result(SUCCESS, data, message, c)
  36. }
  37. func Fail(c *gin.Context) {
  38. Result(ERROR, map[string]interface{}{}, "操作失败", c)
  39. }
  40. func FailWithMessage(message string, c *gin.Context) {
  41. Result(ERROR, map[string]interface{}{}, message, c)
  42. }
  43. func FailWithDetailed(code int, data interface{}, message string, c *gin.Context) {
  44. Result(code, data, message, c)
  45. }