context.go 6.6 KB

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