response.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package response
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "reflect"
  7. "time"
  8. )
  9. var emptyObj = struct{}{}
  10. var emptyArray = []struct{}{}
  11. // 响应码
  12. type ResponseCode struct {
  13. Code int32 `json:"code"` // 响应码
  14. Msg string `json:"message"` // 响应消息
  15. }
  16. var (
  17. ResponseCodeInsufficientAuthority = &ResponseCode{Code: 301, Msg: "权限不足"}
  18. ResponseCodeSignError = &ResponseCode{Code: 302, Msg: "验签失败"}
  19. ResponseCodeMissAuthToken = &ResponseCode{Code: 401, Msg: "Authorization不存在"}
  20. ResponseCodeTokenInvalid = &ResponseCode{Code: 401, Msg: "Token过期失效"}
  21. )
  22. var (
  23. ResponseCodeSuccess = &ResponseCode{Code: 200, Msg: "操作成功"}
  24. ResponseCodeFailure = &ResponseCode{Code: 500, Msg: "操作失败"}
  25. ResponseCodeParamError = &ResponseCode{Code: 502, Msg: "参数错误"}
  26. ResponseCodeFrequentOperation = &ResponseCode{Code: 503, Msg: "请求频繁"} // 操作频繁
  27. )
  28. func (rc *ResponseCode) Error() string {
  29. return rc.Msg
  30. }
  31. func (rc *ResponseCode) ReplaceMsg(msg string) *ResponseCode {
  32. rc.Msg = msg
  33. return rc
  34. }
  35. // 响应结构
  36. type Response struct {
  37. Code int32 `json:"code"` // 响应码
  38. Msg string `json:"msg"` // 响应消息
  39. Time int64 `json:"time"` // 服务器时间
  40. Data any `json:"data,omitempty"` // 响应数据
  41. }
  42. func (r *Response) MarshalJSON() ([]byte, error) {
  43. var resp struct {
  44. Code int32 `json:"code"` // 返回码
  45. Msg string `json:"msg"` // 返回消息
  46. Time int64 `json:"time"` // 服务器时间
  47. Data any `json:"data"` // 返回数据
  48. }
  49. resp.Code = r.Code
  50. resp.Msg = r.Msg
  51. resp.Time = r.Time
  52. if r.Data == nil {
  53. resp.Data = emptyObj
  54. } else {
  55. value := reflect.ValueOf(r.Data)
  56. if value.Kind() == reflect.Slice {
  57. if value.Len() == 0 {
  58. resp.Data = emptyArray
  59. } else {
  60. resp.Data = r.Data
  61. }
  62. } else {
  63. resp.Data = r.Data
  64. }
  65. }
  66. return json.Marshal(resp)
  67. }
  68. func dataToResponse(data any) *Response {
  69. res := &Response{
  70. Code: ResponseCodeSuccess.Code,
  71. Data: data,
  72. Msg: ResponseCodeSuccess.Msg,
  73. Time: currentTimestamp(),
  74. }
  75. return res
  76. }
  77. func currentTimestamp() int64 {
  78. return time.Now().Unix()
  79. }
  80. func success() *Response {
  81. res := &Response{
  82. Code: ResponseCodeSuccess.Code,
  83. Msg: ResponseCodeSuccess.Msg,
  84. Time: currentTimestamp(),
  85. }
  86. return res
  87. }
  88. func failureWithMsg(msg string) *Response {
  89. res := &Response{
  90. Code: ResponseCodeFailure.Code,
  91. Msg: msg,
  92. }
  93. return res
  94. }
  95. func ErrorObjByCode(rc *ResponseCode) *Response {
  96. res := &Response{
  97. Code: rc.Code,
  98. Msg: rc.Msg,
  99. Time: currentTimestamp(),
  100. }
  101. return res
  102. }
  103. func errorWithCode(rc *ResponseCode) *Response {
  104. res := &Response{
  105. Code: rc.Code,
  106. Msg: rc.Msg,
  107. Time: currentTimestamp(),
  108. }
  109. return res
  110. }
  111. // 分页列表
  112. type PageList struct {
  113. PageNum int64 `json:"pageNum,string"` // 页码
  114. PageSize int64 `json:"pageSize,string"` // 分页大小
  115. Total int64 `json:"total,string"` // 总记录数
  116. List any `json:"list"` // 数据列表
  117. Menu any `json:"menu"` // 类型列表
  118. }
  119. // 用于直接返回
  120. func respOkData(c *gin.Context, resp interface{}) {
  121. c.JSON(http.StatusOK, dataToResponse(resp))
  122. }
  123. func respOk(c *gin.Context) {
  124. c.JSON(http.StatusOK, success())
  125. }
  126. // 用于code多语言
  127. func respFailureCode(c *gin.Context, respObj *ResponseCode) {
  128. c.JSON(http.StatusOK, errorWithCode(respObj))
  129. }
  130. func respFailureMsg(c *gin.Context, msg string) {
  131. c.JSON(http.StatusOK, failureWithMsg(msg))
  132. }
  133. func Resp(c *gin.Context, args ...interface{}) {
  134. var data interface{}
  135. if len(args) > 0 {
  136. data = args[0]
  137. } else {
  138. respOk(c)
  139. return
  140. }
  141. switch data.(type) {
  142. case string:
  143. respFailureMsg(c, data.(string))
  144. case *ResponseCode:
  145. respFailureCode(c, data.(*ResponseCode))
  146. case error:
  147. respFailureMsg(c, data.(error).Error())
  148. default:
  149. respOkData(c, data)
  150. }
  151. }