context.go 2.8 KB

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