base.go 4.3 KB

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