response.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package response
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. const (
  7. ERROR int = -1
  8. SUCCESS int = 0
  9. //服务级错误码
  10. EmptyResult int = 201 //查询无结果
  11. ParamError int = 202 //参数错误
  12. ParamEmpty = 203 //参数为空
  13. ParamLenInValid int = 204 //参数长度小于4
  14. Waiting int = 205 //等待处理中
  15. MoreThanQueryDataNumberLimit int = 206 //请求数据的条数超过上限
  16. LeftNumEmpty = 207 //余额不足
  17. QueryError int = 299 //系统查询异常,请联系客服
  18. //系统级错误码
  19. InValidKey = 101 //当前KEY无效
  20. RemainingLack = 102 //当前KEY余额|余量不足
  21. DeleteKey = 103 //当前Key被暂停使用,请联系管理员
  22. TokenInvalid = 104 //身份验证错误
  23. TokenExpired = 107 //身份验证已过期
  24. //105非法请求过多,请联系管理员
  25. IpInvalid = 108 //被禁止的IP
  26. MoreThanEveryDayQueryTimesLimit = 109 //请求超过每日系统限制
  27. //108当前相同查询连续出错,请等2小时后重试
  28. InterfaceRightInvalid = 110 //接口权限未开通
  29. InterfaceExpired = 111 //您的账号剩余使用量已过期
  30. InterfaceDeleted = 112 //接口已停用,请联系管理员
  31. MoreThanEveryDayDataNumberLimit = 113 //请求超过每日调用总量限制
  32. OtherError = 199 //系统未知错误,请联系技术客服
  33. //
  34. )
  35. type Response struct {
  36. Code int `json:"code"`
  37. Data interface{} `json:"data"`
  38. Msg string `json:"msg"`
  39. }
  40. type ResponseArr struct {
  41. Code int `json:"code"`
  42. Data []map[string]interface{} `json:"data"`
  43. Msg string `json:"msg"`
  44. }
  45. func Result(code int, data interface{}, msg string, c *gin.Context) {
  46. c.JSON(http.StatusOK, Response{
  47. code,
  48. data,
  49. msg,
  50. })
  51. }
  52. func ResultArr(code int, data []map[string]interface{}, msg string, c *gin.Context) {
  53. c.JSON(http.StatusOK, ResponseArr{
  54. code,
  55. data,
  56. msg,
  57. })
  58. }
  59. func Ok(c *gin.Context) {
  60. Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
  61. }
  62. func OkWithMessage(message string, c *gin.Context) {
  63. Result(SUCCESS, map[string]interface{}{}, message, c)
  64. }
  65. func OkWithData(data interface{}, c *gin.Context) {
  66. Result(SUCCESS, data, "操作成功", c)
  67. }
  68. func OkWithDatas(data []map[string]interface{}, c *gin.Context) {
  69. Result(SUCCESS, data, "操作成功", c)
  70. }
  71. func OkWithDetailed(data interface{}, message string, c *gin.Context) {
  72. Result(SUCCESS, data, message, c)
  73. }
  74. func Fail(c *gin.Context) {
  75. Result(ERROR, map[string]interface{}{}, "操作失败", c)
  76. }
  77. func FailWithMessage(message string, c *gin.Context) {
  78. Result(ERROR, map[string]interface{}{}, message, c)
  79. }
  80. func FailWithDetailed(code int, data []map[string]interface{}, message string, c *gin.Context) {
  81. Result(code, data, message, c)
  82. }