context.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. "github.com/pkg/errors"
  8. "io"
  9. "mime/multipart"
  10. "strings"
  11. )
  12. const (
  13. bodyKey = "body-context"
  14. queryParamsKey = "query-params-context"
  15. pathParamsKey = "path-params-context"
  16. )
  17. type Context struct {
  18. *gin.Context
  19. }
  20. // GetFileHeaderBytes 获取传递的文件名和文件内容
  21. func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
  22. file, err := fileHeader.Open()
  23. if err != nil {
  24. return "", nil, errors.New(err.Error())
  25. }
  26. defer func(file multipart.File) {
  27. err := file.Close()
  28. if err != nil {
  29. logger.GetInstance().Error(errors.New(err.Error()))
  30. return
  31. }
  32. }(file)
  33. contentBytes, err := io.ReadAll(file)
  34. if err != nil {
  35. return "", nil, errors.New(err.Error())
  36. }
  37. return fileHeader.Filename, contentBytes, nil
  38. }
  39. func (c *Context) GetHeaders() map[string]string {
  40. headers := make(map[string]string, 0)
  41. for key, values := range c.Request.Header {
  42. headers[key] = strings.Join(values, ",")
  43. }
  44. return headers
  45. }
  46. type CacheBody struct {
  47. c *Context
  48. bytesBody []byte
  49. }
  50. func (cacheBody *CacheBody) Set(bytesBody []byte) {
  51. cacheBody.bytesBody = bytesBody
  52. cacheBody.c.Set(bodyKey, cacheBody.bytesBody)
  53. }
  54. func (cacheBody *CacheBody) Bytes() []byte {
  55. return cacheBody.bytesBody
  56. }
  57. func (c *Context) GetBytesBody() (*CacheBody, error) {
  58. body, exist := c.Get(bodyKey)
  59. if !exist {
  60. bytesBody, err := c.readOriginBody()
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &CacheBody{
  65. c: c,
  66. bytesBody: bytesBody,
  67. }, nil
  68. }
  69. return &CacheBody{
  70. c: c,
  71. bytesBody: body.([]byte),
  72. }, nil
  73. }
  74. type JsonBody struct {
  75. c *Context
  76. jsonBodyMap map[string]any
  77. }
  78. func (jsonBody *JsonBody) Set(key string, value any) {
  79. jsonBody.jsonBodyMap[key] = value
  80. jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
  81. }
  82. func (jsonBody *JsonBody) Delete(key string) {
  83. delete(jsonBody.jsonBodyMap, key)
  84. jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
  85. }
  86. func (jsonBody *JsonBody) Get(key string) any {
  87. return jsonBody.jsonBodyMap[key]
  88. }
  89. func (jsonBody *JsonBody) Map() map[string]any {
  90. return jsonBody.jsonBodyMap
  91. }
  92. func (jsonBody *JsonBody) Bytes() ([]byte, error) {
  93. return json.Marshal(jsonBody.jsonBodyMap)
  94. }
  95. func (jsonBody *JsonBody) Unmarshal(output any) error {
  96. jsonBytes, err := jsonBody.Bytes()
  97. if err != nil {
  98. return err
  99. }
  100. return json.Unmarshal(jsonBytes, output)
  101. }
  102. func (c *Context) GetJsonBody() (*JsonBody, error) {
  103. body, exist := c.Get(bodyKey)
  104. if !exist {
  105. bytesBody, err := c.readOriginBody()
  106. if err != nil {
  107. return nil, err
  108. }
  109. jsonBodyMap := make(map[string]any)
  110. err = json.Unmarshal(bytesBody, &jsonBodyMap)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &JsonBody{
  115. c: c,
  116. jsonBodyMap: jsonBodyMap,
  117. }, nil
  118. }
  119. return &JsonBody{
  120. c: c,
  121. jsonBodyMap: body.(map[string]any),
  122. }, nil
  123. }
  124. type QueryPrams struct {
  125. c *Context
  126. queryParams map[string]string
  127. }
  128. func (queryParams *QueryPrams) Set(key string, value string) {
  129. queryParams.queryParams[key] = value
  130. queryParams.c.Set(queryParamsKey, queryParams.queryParams)
  131. }
  132. func (queryParams *QueryPrams) Delete(key string) {
  133. delete(queryParams.queryParams, key)
  134. queryParams.c.Set(queryParamsKey, queryParams.queryParams)
  135. }
  136. func (queryParams *QueryPrams) Get(key string) string {
  137. return queryParams.queryParams[key]
  138. }
  139. func (queryParams *QueryPrams) Map() map[string]string {
  140. return queryParams.queryParams
  141. }
  142. func (c *Context) GetQueryParams() *QueryPrams {
  143. queryParams, exist := c.Get(queryParamsKey)
  144. if !exist {
  145. return &QueryPrams{
  146. c: c,
  147. queryParams: c.getAllQueryParams(),
  148. }
  149. }
  150. return &QueryPrams{
  151. c: c,
  152. queryParams: queryParams.(map[string]string),
  153. }
  154. }
  155. type PathPrams struct {
  156. c *Context
  157. pathParams map[string]string
  158. }
  159. func (pathParams *PathPrams) Set(key string, value string) {
  160. pathParams.pathParams[key] = value
  161. pathParams.c.Set(pathParamsKey, pathParams.pathParams)
  162. }
  163. func (pathParams *PathPrams) Delete(key string) {
  164. delete(pathParams.pathParams, key)
  165. pathParams.c.Set(pathParamsKey, pathParams.pathParams)
  166. }
  167. func (pathParams *PathPrams) Get(key string) string {
  168. return pathParams.pathParams[key]
  169. }
  170. func (pathParams *PathPrams) Map() map[string]string {
  171. return pathParams.pathParams
  172. }
  173. func (c *Context) GetPathParams() *PathPrams {
  174. pathParams, exist := c.Get(pathParamsKey)
  175. if !exist {
  176. return &PathPrams{
  177. c: c,
  178. pathParams: c.getAllPathParams(),
  179. }
  180. }
  181. return &PathPrams{
  182. c: c,
  183. pathParams: pathParams.(map[string]string),
  184. }
  185. }
  186. func (c *Context) getAllQueryParams() map[string]string {
  187. queryParams := make(map[string]string, 0)
  188. for key, values := range c.Request.URL.Query() {
  189. queryParams[key] = strings.Join(values, ",")
  190. }
  191. return queryParams
  192. }
  193. func (c *Context) getAllPathParams() map[string]string {
  194. pathParams := make(map[string]string, 0)
  195. for _, params := range c.Params {
  196. pathParams[params.Key] = params.Value
  197. }
  198. return pathParams
  199. }
  200. func (c *Context) readOriginBody() ([]byte, error) {
  201. if c.Request.Body == nil {
  202. return make([]byte, 0), nil
  203. }
  204. body, err := io.ReadAll(c.Request.Body)
  205. if err != nil {
  206. return nil, errors.New(err.Error())
  207. }
  208. defer func(Body io.ReadCloser) {
  209. err := Body.Close()
  210. if err != nil {
  211. logger.GetInstance().Error(errors.New(err.Error()))
  212. return
  213. }
  214. }(c.Request.Body)
  215. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  216. c.Set(bodyKey, body)
  217. return body, nil
  218. }
  219. const (
  220. tenantInfoKey = "context-tenant-info"
  221. userInfoKey = "context-user-info"
  222. )
  223. type TenantInfo interface {
  224. GetID() string
  225. GetName() string
  226. }
  227. type UserInfo interface {
  228. GetID() string
  229. GetUserName() string
  230. GetName() string
  231. }
  232. func (c *Context) GetTenantInfo() TenantInfo {
  233. tenantInfo, exist := c.Get(tenantInfoKey)
  234. if !exist {
  235. return nil
  236. }
  237. return tenantInfo.(TenantInfo)
  238. }
  239. func (c *Context) GetUserInfo() UserInfo {
  240. userInfo, exist := c.Get(userInfoKey)
  241. if !exist {
  242. return nil
  243. }
  244. return userInfo.(UserInfo)
  245. }