base.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. REPORT_NOT_RELEASE_CODE = 4002 // 报告未发布错误
  17. )
  18. type ResultData struct {
  19. Code int `json:"code" description:"状态码"`
  20. Msg string `json:"msg" description:"提示信息"`
  21. Data interface{} `json:"data" description:"返回数据"`
  22. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  23. }
  24. func result(code int, resultData ResultData, c *gin.Context) {
  25. jsonByte, _ := json.Marshal(resultData)
  26. logSlice := utils.GetContextLogListByClaims(c)
  27. logSlice = append(logSlice, fmt.Sprint("ResultData:", string(jsonByte)))
  28. //记录错误日志
  29. if resultData.ErrMsg != "" {
  30. logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
  31. //global.LOG.Info(strings.Join(logSlice, ";"))
  32. }
  33. global.LOG.Info(strings.Join(logSlice, "\n"))
  34. // 测试环境不加密
  35. if global.CONFIG.Serve.RunMode == "debug" {
  36. c.JSON(code, resultData)
  37. } else {
  38. encryptResult := utils.DesBase64Encrypt(jsonByte)
  39. c.JSON(code, string(encryptResult))
  40. }
  41. c.Abort()
  42. }
  43. // OK 操作成功
  44. func Ok(msg string, c *gin.Context) {
  45. resultData := ResultData{
  46. Code: OK_CODE,
  47. Msg: msg,
  48. }
  49. result(200, resultData, c)
  50. }
  51. // OkData 成功返回数据
  52. func OkData(msg string, data interface{}, c *gin.Context) {
  53. resultData := ResultData{
  54. Code: OK_CODE,
  55. Msg: msg,
  56. Data: data,
  57. }
  58. result(200, resultData, c)
  59. }
  60. // Fail 操作失败
  61. func Fail(msg string, c *gin.Context) {
  62. resultData := ResultData{
  63. Code: FAIL_CODE,
  64. Msg: msg,
  65. }
  66. result(200, resultData, c)
  67. }
  68. // FailData 成功返回数据
  69. func FailData(msg string, data interface{}, c *gin.Context) {
  70. resultData := ResultData{
  71. Code: FAIL_CODE,
  72. Msg: msg,
  73. Data: data,
  74. }
  75. result(200, resultData, c)
  76. }
  77. // Custom 自定义状态码+操作成功
  78. func Custom(code int, msg string, c *gin.Context) {
  79. resultData := ResultData{
  80. Code: code,
  81. Msg: msg,
  82. }
  83. result(200, resultData, c)
  84. }
  85. // CustomData 自定义状态码+返回数据
  86. func CustomData(code int, msg string, data interface{}, c *gin.Context) {
  87. resultData := ResultData{
  88. Code: code,
  89. Msg: msg,
  90. Data: data,
  91. }
  92. result(200, resultData, c)
  93. }
  94. // TokenError token异常
  95. func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
  96. resultData := ResultData{
  97. Code: TOKEN_ERROR_CODE,
  98. Msg: message,
  99. Data: data,
  100. ErrMsg: errMsg,
  101. }
  102. result(200, resultData, c)
  103. }
  104. // AuthError 没有权限
  105. func AuthError(data interface{}, message string, c *gin.Context) {
  106. resultData := ResultData{
  107. Code: NO_AUTH,
  108. Msg: message,
  109. Data: data,
  110. }
  111. result(200, resultData, c)
  112. }
  113. // SpecificFail 业务指定错误
  114. func SpecificFail(data interface{}, message string, c *gin.Context) {
  115. resultData := ResultData{
  116. Code: SPECIFIC_FAIL_CODE,
  117. Msg: message,
  118. Data: data,
  119. }
  120. result(200, resultData, c)
  121. }
  122. // FailMsg 操作失败
  123. func FailMsg(msg, errMsg string, c *gin.Context) {
  124. resultData := ResultData{
  125. Code: FAIL_CODE,
  126. Msg: msg,
  127. ErrMsg: errMsg,
  128. }
  129. result(200, resultData, c)
  130. }
  131. type BaseResponse struct {
  132. Ret int
  133. Msg string
  134. ErrMsg string
  135. ErrCode string
  136. Data interface{}
  137. Success bool `description:"true 执行成功,false 执行失败"`
  138. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  139. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  140. }
  141. // NotReleaseMsg 报告未发布错误
  142. func NotReleaseMsg(msg, errMsg string, c *gin.Context) {
  143. resultData := ResultData{
  144. Code: REPORT_NOT_RELEASE_CODE,
  145. Msg: msg,
  146. ErrMsg: errMsg,
  147. }
  148. result(200, resultData, c)
  149. }