base.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package resp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "hongze/hongze_yb_en_api/global"
  7. "hongze/hongze_yb_en_api/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. HASFORBIDDEN_CODE = 4010 // 管理员账号被禁用
  17. PASSWORDCHANGE_CODE = 4011 // 管理员账号被禁用
  18. EXPIRED_CODE = 4012 // 已到期
  19. UNREGISTERED_CODE = 4013 // 未注册
  20. UNBOUND_CODE = 4014 // 未绑定
  21. REGISTERED_CODE = 4015 // 已注册
  22. BOUND_CODE = 4016 // 已绑定
  23. )
  24. type ResultData struct {
  25. Code int `json:"code" description:"状态码"`
  26. Msg string `json:"msg" description:"提示信息"`
  27. Data interface{} `json:"data" description:"返回数据"`
  28. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  29. }
  30. func result(code int, resultData ResultData, c *gin.Context) {
  31. jsonByte, _ := json.Marshal(resultData)
  32. token := c.Request.Header.Get("Authorization")
  33. if token == "" {
  34. token = c.DefaultQuery("authorization", "")
  35. if token == "" {
  36. token = c.DefaultQuery("Authorization", "")
  37. }
  38. }
  39. logSlice := make([]string, 0)
  40. logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
  41. logSlice = append(logSlice, fmt.Sprint("Token:", token))
  42. logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte)))
  43. //记录错误日志
  44. if resultData.ErrMsg != "" {
  45. logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
  46. global.LOG.Info(strings.Join(logSlice, ";"))
  47. }
  48. // 测试环境不加密
  49. if global.CONFIG.Serve.RunMode == "debug" {
  50. c.JSON(code, resultData)
  51. } else {
  52. global.LOG.Info(strings.Join(logSlice, ";"))
  53. encryptResult := utils.DesBase64Encrypt(jsonByte)
  54. c.JSON(code, string(encryptResult))
  55. }
  56. c.Abort()
  57. }
  58. // OK 操作成功
  59. func Ok(msg string, c *gin.Context) {
  60. resultData := ResultData{
  61. Code: OK_CODE,
  62. Msg: msg,
  63. }
  64. result(200, resultData, c)
  65. }
  66. // OkData 成功返回数据
  67. func OkData(msg string, data interface{}, c *gin.Context) {
  68. resultData := ResultData{
  69. Code: OK_CODE,
  70. Msg: msg,
  71. Data: data,
  72. }
  73. result(200, resultData, c)
  74. }
  75. // Fail 操作失败
  76. func Fail(msg string, c *gin.Context) {
  77. resultData := ResultData{
  78. Code: FAIL_CODE,
  79. Msg: msg,
  80. }
  81. result(200, resultData, c)
  82. }
  83. // FailData 成功返回数据
  84. func FailData(msg string, data interface{}, c *gin.Context) {
  85. resultData := ResultData{
  86. Code: FAIL_CODE,
  87. Msg: msg,
  88. Data: data,
  89. }
  90. result(200, resultData, c)
  91. }
  92. // Custom 自定义状态码+操作成功
  93. func Custom(code int, msg string, c *gin.Context) {
  94. resultData := ResultData{
  95. Code: code,
  96. Msg: msg,
  97. }
  98. result(200, resultData, c)
  99. }
  100. // CustomData 自定义状态码+返回数据
  101. func CustomData(code int, msg string, data interface{}, c *gin.Context) {
  102. resultData := ResultData{
  103. Code: code,
  104. Msg: msg,
  105. Data: data,
  106. }
  107. result(200, resultData, c)
  108. }
  109. // TokenError token异常
  110. func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
  111. resultData := ResultData{
  112. Code: TOKEN_ERROR_CODE,
  113. Msg: message,
  114. Data: data,
  115. ErrMsg: errMsg,
  116. }
  117. result(200, resultData, c)
  118. }
  119. // AuthError 没有权限
  120. func AuthError(data interface{}, message string, c *gin.Context) {
  121. resultData := ResultData{
  122. Code: NO_AUTH,
  123. Msg: message,
  124. Data: data,
  125. }
  126. result(200, resultData, c)
  127. }
  128. // SpecificFail 业务指定错误
  129. func SpecificFail(code int, data interface{}, message string, c *gin.Context) {
  130. resultData := ResultData{
  131. Code: code,
  132. Msg: message,
  133. Data: data,
  134. }
  135. result(200, resultData, c)
  136. }
  137. // FailMsg 操作失败
  138. func FailMsg(msg, errMsg string, c *gin.Context) {
  139. resultData := ResultData{
  140. Code: FAIL_CODE,
  141. Msg: msg,
  142. ErrMsg: errMsg,
  143. }
  144. result(200, resultData, c)
  145. }
  146. // Expired 过期
  147. func Expired(msg string, c *gin.Context) {
  148. resultData := ResultData{
  149. Code: EXPIRED_CODE,
  150. Msg: msg,
  151. }
  152. result(200, resultData, c)
  153. }
  154. // Unregistered 未注册
  155. func Unregistered(msg string, c *gin.Context) {
  156. resultData := ResultData{
  157. Code: UNREGISTERED_CODE,
  158. Msg: msg,
  159. }
  160. result(200, resultData, c)
  161. }
  162. // Unbound 未绑定
  163. func Unbound(msg string, c *gin.Context) {
  164. resultData := ResultData{
  165. Code: UNBOUND_CODE,
  166. Msg: msg,
  167. }
  168. result(200, resultData, c)
  169. }
  170. // Registered 已注册
  171. func Registered(msg string, c *gin.Context) {
  172. resultData := ResultData{
  173. Code: REGISTERED_CODE,
  174. Msg: msg,
  175. }
  176. result(200, resultData, c)
  177. }
  178. // Bound 已绑定
  179. func Bound(msg string, c *gin.Context) {
  180. resultData := ResultData{
  181. Code: BOUND_CODE,
  182. Msg: msg,
  183. }
  184. result(200, resultData, c)
  185. }