base.go 3.3 KB

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