base.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. }
  21. func result(code int, resultData ResultData, c *gin.Context) {
  22. jsonByte, _ := json.Marshal(resultData)
  23. token := c.Request.Header.Get("Authorization")
  24. if token == "" {
  25. token = c.DefaultQuery("authorization", "")
  26. if token == "" {
  27. token = c.DefaultQuery("Authorization", "")
  28. }
  29. }
  30. logSlice := make([]string, 0)
  31. logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
  32. logSlice = append(logSlice, fmt.Sprint("Token:", token))
  33. logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte)))
  34. global.LOG.Info(strings.Join(logSlice, ";"))
  35. //测试环境,数据不进行加密
  36. /*if global.CONFIG.Serve.RunMode == "debug" {
  37. c.JSON(code, resultData)
  38. } else {
  39. responseResult := utils.DesBase64Encrypt(jsonByte)
  40. c.JSON(code, responseResult)
  41. }*/
  42. c.JSON(code, resultData)
  43. c.Abort()
  44. }
  45. // OK 操作成功
  46. func Ok(msg string, c *gin.Context) {
  47. resultData := ResultData{
  48. Code: OK_CODE,
  49. Msg: msg,
  50. }
  51. result(200, resultData, c)
  52. }
  53. // OkData 成功返回数据
  54. func OkData(msg string, data interface{}, c *gin.Context) {
  55. resultData := ResultData{
  56. Code: OK_CODE,
  57. Msg: msg,
  58. Data: data,
  59. }
  60. result(200, resultData, c)
  61. }
  62. // Fail 操作失败
  63. func Fail(msg string, c *gin.Context) {
  64. resultData := ResultData{
  65. Code: FAIL_CODE,
  66. Msg: msg,
  67. }
  68. result(200, resultData, c)
  69. }
  70. // FailData 成功返回数据
  71. func FailData(msg string, data interface{}, c *gin.Context) {
  72. resultData := ResultData{
  73. Code: FAIL_CODE,
  74. Msg: msg,
  75. Data: data,
  76. }
  77. result(200, resultData, c)
  78. }
  79. // Custom 自定义状态码+操作成功
  80. func Custom(code int, msg string, c *gin.Context) {
  81. resultData := ResultData{
  82. Code: code,
  83. Msg: msg,
  84. }
  85. result(200, resultData, c)
  86. }
  87. // CustomData 自定义状态码+返回数据
  88. func CustomData(code int, msg string, data interface{}, c *gin.Context) {
  89. resultData := ResultData{
  90. Code: code,
  91. Msg: msg,
  92. Data: data,
  93. }
  94. result(200, resultData, c)
  95. }
  96. // TokenError token异常
  97. func TokenError(data interface{}, message string, c *gin.Context) {
  98. resultData := ResultData{
  99. Code: TOKEN_ERROR_CODE,
  100. Msg: message,
  101. Data: data,
  102. }
  103. result(200, resultData, c)
  104. }
  105. // AuthError 没有权限
  106. func AuthError(data interface{}, message string, c *gin.Context) {
  107. resultData := ResultData{
  108. Code: NO_AUTH,
  109. Msg: message,
  110. Data: data,
  111. }
  112. result(200, resultData, c)
  113. }
  114. // SpecificFail 业务指定错误
  115. func SpecificFail(data interface{}, message string, c *gin.Context) {
  116. resultData := ResultData{
  117. Code: SPECIFIC_FAIL_CODE,
  118. Msg: message,
  119. Data: data,
  120. }
  121. result(200, resultData, c)
  122. }