response.go 2.9 KB

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