base.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package resp
  2. import (
  3. "encoding/json"
  4. "eta/eta_docs/global"
  5. "eta/eta_docs/utils"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "strings"
  9. )
  10. var (
  11. OK_CODE = 200 //业务成功
  12. FAIL_CODE = 400 //业务错误
  13. TOKEN_ERROR_CODE = 401 //toke异常
  14. NO_AUTH = 403 //没有权限
  15. SPECIFIC_FAIL_CODE = 4001 // 业务指定错误
  16. )
  17. type ResultData struct {
  18. Code int `json:"code" description:"状态码"`
  19. Msg string `json:"msg" description:"提示信息"`
  20. Data interface{} `json:"data" description:"返回数据"`
  21. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  22. }
  23. func result(code int, resultData ResultData, c *gin.Context) {
  24. jsonByte, _ := json.Marshal(resultData)
  25. token := c.Request.Header.Get("Authorization")
  26. if token == "" {
  27. token = c.DefaultQuery("authorization", "")
  28. if token == "" {
  29. token = c.DefaultQuery("Authorization", "")
  30. }
  31. }
  32. logSlice := make([]string, 0)
  33. logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
  34. logSlice = append(logSlice, fmt.Sprint("Token:", token))
  35. logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte)))
  36. //记录错误日志
  37. if resultData.ErrMsg != "" {
  38. logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
  39. //global.LOG.Info(strings.Join(logSlice, ";"))
  40. }
  41. // 测试环境不加密
  42. if global.CONFIG.Serve.RunMode == "debug" {
  43. c.JSON(code, resultData)
  44. } else {
  45. global.LOG.Info(strings.Join(logSlice, ";"))
  46. encryptResult := utils.DesBase64Encrypt(jsonByte)
  47. c.JSON(code, string(encryptResult))
  48. }
  49. c.Abort()
  50. }
  51. // OK 操作成功
  52. func Ok(msg string, c *gin.Context) {
  53. resultData := ResultData{
  54. Code: OK_CODE,
  55. Msg: msg,
  56. }
  57. result(200, resultData, c)
  58. }
  59. // OkData 成功返回数据
  60. func OkData(msg string, data interface{}, c *gin.Context) {
  61. resultData := ResultData{
  62. Code: OK_CODE,
  63. Msg: msg,
  64. Data: data,
  65. }
  66. result(200, resultData, c)
  67. }
  68. // Fail 操作失败
  69. func Fail(msg string, c *gin.Context) {
  70. resultData := ResultData{
  71. Code: FAIL_CODE,
  72. Msg: msg,
  73. }
  74. result(200, resultData, c)
  75. }
  76. // FailData 成功返回数据
  77. func FailData(msg string, data interface{}, c *gin.Context) {
  78. resultData := ResultData{
  79. Code: FAIL_CODE,
  80. Msg: msg,
  81. Data: data,
  82. }
  83. result(200, resultData, c)
  84. }
  85. // Custom 自定义状态码+操作成功
  86. func Custom(code int, msg string, c *gin.Context) {
  87. resultData := ResultData{
  88. Code: code,
  89. Msg: msg,
  90. }
  91. result(200, resultData, c)
  92. }
  93. // CustomData 自定义状态码+返回数据
  94. func CustomData(code int, msg string, data interface{}, c *gin.Context) {
  95. resultData := ResultData{
  96. Code: code,
  97. Msg: msg,
  98. Data: data,
  99. }
  100. result(200, resultData, c)
  101. }
  102. // TokenError token异常
  103. func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
  104. resultData := ResultData{
  105. Code: TOKEN_ERROR_CODE,
  106. Msg: message,
  107. Data: data,
  108. ErrMsg: errMsg,
  109. }
  110. result(200, resultData, c)
  111. }
  112. // AuthError 没有权限
  113. func AuthError(data interface{}, message string, c *gin.Context) {
  114. resultData := ResultData{
  115. Code: NO_AUTH,
  116. Msg: message,
  117. Data: data,
  118. }
  119. result(200, resultData, c)
  120. }
  121. // SpecificFail 业务指定错误
  122. func SpecificFail(data interface{}, message string, c *gin.Context) {
  123. resultData := ResultData{
  124. Code: SPECIFIC_FAIL_CODE,
  125. Msg: message,
  126. Data: data,
  127. }
  128. result(200, resultData, c)
  129. }
  130. // FailMsg 操作失败
  131. func FailMsg(msg, errMsg string, c *gin.Context) {
  132. resultData := ResultData{
  133. Code: FAIL_CODE,
  134. Msg: msg,
  135. ErrMsg: errMsg,
  136. }
  137. result(200, resultData, c)
  138. }