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. "hongze/hongze_yb/utils"
  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. }
  40. // 测试环境不加密
  41. if global.CONFIG.Serve.RunMode == "debug" {
  42. c.JSON(code, resultData)
  43. } else {
  44. global.LOG.Info(strings.Join(logSlice, ";"))
  45. encryptResult := utils.DesBase64Encrypt(jsonByte)
  46. c.JSON(code, string(encryptResult))
  47. }
  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. }