base.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package response
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/utils"
  7. )
  8. var (
  9. OK_CODE = 200 //业务成功
  10. FAIL_CODE = 400 //业务错误
  11. TOKEN_ERROR_CODE = 401 //toke异常
  12. NO_AUTH = 403 //没有权限
  13. SPECIFIC_FAIL_CODE = 4001 // 业务指定错误
  14. )
  15. type ResultData struct {
  16. Code int `json:"code" description:"状态码"`
  17. Msg string `json:"msg" description:"提示信息"`
  18. Data interface{} `json:"data" description:"返回数据"`
  19. }
  20. func result(code int, resultData ResultData, c *gin.Context) {
  21. jsonByte, _ := json.Marshal(resultData)
  22. global.LOG.Debug("resultData:", string(jsonByte))
  23. responseResult := utils.DesBase64Encrypt(jsonByte)
  24. c.JSON(code, responseResult)
  25. c.Abort()
  26. }
  27. // OK 操作成功
  28. func Ok(msg string, c *gin.Context) {
  29. resultData := ResultData{
  30. Code: OK_CODE,
  31. Msg: msg,
  32. }
  33. result(200, resultData, c)
  34. }
  35. // OkData 成功返回数据
  36. func OkData(msg string, data interface{}, c *gin.Context) {
  37. resultData := ResultData{
  38. Code: OK_CODE,
  39. Msg: msg,
  40. Data: data,
  41. }
  42. result(200, resultData, c)
  43. }
  44. // Fail 操作失败
  45. func Fail(msg string, c *gin.Context) {
  46. resultData := ResultData{
  47. Code: FAIL_CODE,
  48. Msg: msg,
  49. }
  50. result(200, resultData, c)
  51. }
  52. // FailData 成功返回数据
  53. func FailData(msg string, data interface{}, c *gin.Context) {
  54. resultData := ResultData{
  55. Code: FAIL_CODE,
  56. Msg: msg,
  57. Data: data,
  58. }
  59. result(200, resultData, c)
  60. }
  61. // Custom 自定义状态码+操作成功
  62. func Custom(code int, msg string, c *gin.Context) {
  63. resultData := ResultData{
  64. Code: code,
  65. Msg: msg,
  66. }
  67. result(200, resultData, c)
  68. }
  69. // CustomData 自定义状态码+返回数据
  70. func CustomData(code int, msg string, data interface{}, c *gin.Context) {
  71. resultData := ResultData{
  72. Code: code,
  73. Msg: msg,
  74. Data: data,
  75. }
  76. result(200, resultData, c)
  77. }
  78. // TokenError token异常
  79. func TokenError(data interface{}, message string, c *gin.Context) {
  80. resultData := ResultData{
  81. Code: TOKEN_ERROR_CODE,
  82. Msg: message,
  83. Data: data,
  84. }
  85. result(200, resultData, c)
  86. }
  87. // AuthError 没有权限
  88. func AuthError(data interface{}, message string, c *gin.Context) {
  89. resultData := ResultData{
  90. Code: NO_AUTH,
  91. Msg: message,
  92. Data: data,
  93. }
  94. result(200, resultData, c)
  95. }
  96. // SpecificFail 业务指定错误
  97. func SpecificFail(data interface{}, message string, c *gin.Context) {
  98. resultData := ResultData{
  99. Code: SPECIFIC_FAIL_CODE,
  100. Msg: message,
  101. Data: data,
  102. }
  103. result(200, resultData, c)
  104. }