context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  6. "git.sxidc.com/service-supports/fserr"
  7. "github.com/gin-gonic/gin"
  8. "io"
  9. "mime/multipart"
  10. "strings"
  11. )
  12. type Context struct {
  13. *gin.Context
  14. }
  15. // GetFileHeaderBytes 获取传递的文件名和文件内容
  16. func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
  17. file, err := fileHeader.Open()
  18. if err != nil {
  19. return "", nil, fserr.New(err.Error())
  20. }
  21. defer func(file multipart.File) {
  22. err := file.Close()
  23. if err != nil {
  24. logger.GetInstance().Error(fserr.New(err.Error()))
  25. return
  26. }
  27. }(file)
  28. contentBytes, err := io.ReadAll(file)
  29. if err != nil {
  30. return "", nil, fserr.New(err.Error())
  31. }
  32. return fileHeader.Filename, contentBytes, nil
  33. }
  34. func (c *Context) GetHeaders() map[string]string {
  35. headers := make(map[string]string, 0)
  36. for key, values := range c.Request.Header {
  37. headers[key] = strings.Join(values, ",")
  38. }
  39. return headers
  40. }
  41. func (c *Context) ReadBody() ([]byte, error) {
  42. if c.Request.Body == nil {
  43. return make([]byte, 0), nil
  44. }
  45. body, err := io.ReadAll(c.Request.Body)
  46. if err != nil {
  47. return nil, fserr.New(err.Error())
  48. }
  49. defer func(Body io.ReadCloser) {
  50. err := Body.Close()
  51. if err != nil {
  52. logger.GetInstance().Error(fserr.New(err.Error()))
  53. return
  54. }
  55. }(c.Request.Body)
  56. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  57. return body, nil
  58. }
  59. func (c *Context) ReplaceBody(body []byte) error {
  60. if c.Request.Body != nil {
  61. err := c.Request.Body.Close()
  62. if err != nil {
  63. return fserr.New(err.Error())
  64. }
  65. }
  66. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  67. return nil
  68. }
  69. func (c *Context) ReadJsonBody(output any) error {
  70. body, err := c.ReadBody()
  71. if err != nil {
  72. return err
  73. }
  74. return json.Unmarshal(body, output)
  75. }
  76. func (c *Context) ReplaceJsonBody(body any) error {
  77. bodyBytes, err := json.Marshal(body)
  78. if err != nil {
  79. return fserr.New(err.Error())
  80. }
  81. return c.ReplaceBody(bodyBytes)
  82. }
  83. func (c *Context) GetAllQueryParams() map[string]string {
  84. queryParams := make(map[string]string, 0)
  85. for key, values := range c.Request.URL.Query() {
  86. queryParams[key] = strings.Join(values, ",")
  87. }
  88. return queryParams
  89. }
  90. func (c *Context) GetAllPathParams() map[string]string {
  91. pathParams := make(map[string]string, 0)
  92. for _, params := range c.Params {
  93. pathParams[params.Key] = params.Value
  94. }
  95. return pathParams
  96. }
  97. const (
  98. tenantInfoKey = "context-tenant-info"
  99. userInfoKey = "context-user-info"
  100. )
  101. type TenantInfo interface {
  102. GetID() string
  103. GetName() string
  104. }
  105. type UserInfo interface {
  106. GetID() string
  107. GetUserName() string
  108. GetName() string
  109. }
  110. func (c *Context) GetTenantInfo() TenantInfo {
  111. tenantInfo, exist := c.Get(tenantInfoKey)
  112. if !exist {
  113. return nil
  114. }
  115. return tenantInfo.(TenantInfo)
  116. }
  117. func (c *Context) GetUserInfo() UserInfo {
  118. userInfo, exist := c.Get(userInfoKey)
  119. if !exist {
  120. return nil
  121. }
  122. return userInfo.(UserInfo)
  123. }