context.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. headerKey = "header-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. type Header struct {
  41. c *Context
  42. header map[string]string
  43. }
  44. func (header *Header) Set(key string, value string) {
  45. header.header[key] = value
  46. header.c.Set(headerKey, header.header)
  47. }
  48. func (header *Header) Get(key string) string {
  49. return header.header[key]
  50. }
  51. func (header *Header) Map() map[string]string {
  52. return header.header
  53. }
  54. func (c *Context) GetHeader() *Header {
  55. savedHeader, exist := c.Get(headerKey)
  56. if exist {
  57. return &Header{
  58. c: c,
  59. header: savedHeader.(map[string]string),
  60. }
  61. }
  62. header := make(map[string]string)
  63. for key, values := range c.Request.Header {
  64. header[key] = strings.Join(values, ",")
  65. }
  66. c.Set(headerKey, header)
  67. return &Header{
  68. c: c,
  69. header: header,
  70. }
  71. }
  72. type CacheBody struct {
  73. c *Context
  74. bytesBody []byte
  75. }
  76. func (cacheBody *CacheBody) Set(bytesBody []byte) {
  77. cacheBody.bytesBody = bytesBody
  78. cacheBody.c.Set(bodyKey, cacheBody.bytesBody)
  79. }
  80. func (cacheBody *CacheBody) Bytes() []byte {
  81. return cacheBody.bytesBody
  82. }
  83. func (cacheBody *CacheBody) Marshal(output any) error {
  84. bytesBody, err := json.Marshal(output)
  85. if err != nil {
  86. return errors.New(err.Error())
  87. }
  88. cacheBody.Set(bytesBody)
  89. return nil
  90. }
  91. func (cacheBody *CacheBody) Unmarshal(output any) error {
  92. err := json.Unmarshal(cacheBody.Bytes(), output)
  93. if err != nil {
  94. return errors.New(err.Error())
  95. }
  96. return nil
  97. }
  98. func (c *Context) GetBytesBody() (*CacheBody, error) {
  99. body, exist := c.Get(bodyKey)
  100. if !exist {
  101. bytesBody, err := c.readOriginBody()
  102. if err != nil {
  103. return nil, err
  104. }
  105. return &CacheBody{
  106. c: c,
  107. bytesBody: bytesBody,
  108. }, nil
  109. }
  110. switch b := body.(type) {
  111. case []byte:
  112. return &CacheBody{
  113. c: c,
  114. bytesBody: b,
  115. }, nil
  116. case map[string]any:
  117. bytesBody, err := json.Marshal(b)
  118. if err != nil {
  119. return nil, errors.New(err.Error())
  120. }
  121. return &CacheBody{
  122. c: c,
  123. bytesBody: bytesBody,
  124. }, nil
  125. default:
  126. return nil, errors.New("不支持的body类型")
  127. }
  128. }
  129. type JsonBody struct {
  130. c *Context
  131. jsonBodyMap map[string]any
  132. }
  133. func (jsonBody *JsonBody) Set(key string, value any) {
  134. jsonBody.jsonBodyMap[key] = value
  135. jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
  136. }
  137. func (jsonBody *JsonBody) Delete(key string) {
  138. delete(jsonBody.jsonBodyMap, key)
  139. jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
  140. }
  141. func (jsonBody *JsonBody) Get(key string) any {
  142. return jsonBody.jsonBodyMap[key]
  143. }
  144. func (jsonBody *JsonBody) Map() map[string]any {
  145. return jsonBody.jsonBodyMap
  146. }
  147. func (jsonBody *JsonBody) Bytes() ([]byte, error) {
  148. jsonBytes, err := json.Marshal(jsonBody.jsonBodyMap)
  149. if err != nil {
  150. return nil, errors.New(err.Error())
  151. }
  152. return jsonBytes, nil
  153. }
  154. func (jsonBody *JsonBody) Unmarshal(output any) error {
  155. jsonBytes, err := jsonBody.Bytes()
  156. if err != nil {
  157. return err
  158. }
  159. err = json.Unmarshal(jsonBytes, output)
  160. if err != nil {
  161. return errors.New(err.Error())
  162. }
  163. return nil
  164. }
  165. func (c *Context) GetJsonBody() (*JsonBody, error) {
  166. body, exist := c.Get(bodyKey)
  167. if !exist {
  168. bytesBody, err := c.readOriginBody()
  169. if err != nil {
  170. return nil, err
  171. }
  172. jsonBodyMap := make(map[string]any)
  173. err = json.Unmarshal(bytesBody, &jsonBodyMap)
  174. if err != nil {
  175. return nil, errors.New(err.Error())
  176. }
  177. return &JsonBody{
  178. c: c,
  179. jsonBodyMap: jsonBodyMap,
  180. }, nil
  181. }
  182. switch b := body.(type) {
  183. case []byte:
  184. jsonBodyMap := make(map[string]any)
  185. err := json.Unmarshal(b, &jsonBodyMap)
  186. if err != nil {
  187. return nil, errors.New(err.Error())
  188. }
  189. return &JsonBody{
  190. c: c,
  191. jsonBodyMap: jsonBodyMap,
  192. }, nil
  193. case map[string]any:
  194. return &JsonBody{
  195. c: c,
  196. jsonBodyMap: body.(map[string]any),
  197. }, nil
  198. default:
  199. return nil, errors.New("不支持的body类型")
  200. }
  201. }
  202. type QueryPrams struct {
  203. c *Context
  204. queryParams map[string]string
  205. }
  206. func (queryParams *QueryPrams) Set(key string, value string) {
  207. queryParams.queryParams[key] = value
  208. queryParams.c.Set(queryParamsKey, queryParams.queryParams)
  209. }
  210. func (queryParams *QueryPrams) Delete(key string) {
  211. delete(queryParams.queryParams, key)
  212. queryParams.c.Set(queryParamsKey, queryParams.queryParams)
  213. }
  214. func (queryParams *QueryPrams) Get(key string) string {
  215. return queryParams.queryParams[key]
  216. }
  217. func (queryParams *QueryPrams) Map() map[string]string {
  218. return queryParams.queryParams
  219. }
  220. func (c *Context) GetQueryParams() *QueryPrams {
  221. queryParams, exist := c.Get(queryParamsKey)
  222. if !exist {
  223. return &QueryPrams{
  224. c: c,
  225. queryParams: c.getAllQueryParams(),
  226. }
  227. }
  228. return &QueryPrams{
  229. c: c,
  230. queryParams: queryParams.(map[string]string),
  231. }
  232. }
  233. type PathPrams struct {
  234. c *Context
  235. pathParams map[string]string
  236. }
  237. func (pathParams *PathPrams) Set(key string, value string) {
  238. pathParams.pathParams[key] = value
  239. pathParams.c.Set(pathParamsKey, pathParams.pathParams)
  240. }
  241. func (pathParams *PathPrams) Delete(key string) {
  242. delete(pathParams.pathParams, key)
  243. pathParams.c.Set(pathParamsKey, pathParams.pathParams)
  244. }
  245. func (pathParams *PathPrams) Get(key string) string {
  246. return pathParams.pathParams[key]
  247. }
  248. func (pathParams *PathPrams) Map() map[string]string {
  249. return pathParams.pathParams
  250. }
  251. func (c *Context) GetPathParams() *PathPrams {
  252. pathParams, exist := c.Get(pathParamsKey)
  253. if !exist {
  254. return &PathPrams{
  255. c: c,
  256. pathParams: c.getAllPathParams(),
  257. }
  258. }
  259. return &PathPrams{
  260. c: c,
  261. pathParams: pathParams.(map[string]string),
  262. }
  263. }
  264. func (c *Context) getAllQueryParams() map[string]string {
  265. queryParams := make(map[string]string)
  266. for key, values := range c.Request.URL.Query() {
  267. queryParams[key] = strings.Join(values, ",")
  268. }
  269. return queryParams
  270. }
  271. func (c *Context) getAllPathParams() map[string]string {
  272. pathParams := make(map[string]string)
  273. for _, params := range c.Params {
  274. pathParams[params.Key] = params.Value
  275. }
  276. return pathParams
  277. }
  278. func (c *Context) readOriginBody() ([]byte, error) {
  279. if c.Request.Body == nil {
  280. return make([]byte, 0), nil
  281. }
  282. body, err := io.ReadAll(c.Request.Body)
  283. if err != nil {
  284. return nil, errors.New(err.Error())
  285. }
  286. defer func(Body io.ReadCloser) {
  287. err := Body.Close()
  288. if err != nil {
  289. logger.GetInstance().Error(errors.New(err.Error()))
  290. return
  291. }
  292. }(c.Request.Body)
  293. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  294. c.Set(bodyKey, body)
  295. return body, nil
  296. }
  297. const (
  298. tenantInfoKey = "context-tenant-info"
  299. userInfoKey = "context-user-info"
  300. )
  301. type TenantInfo interface {
  302. GetID() string
  303. GetName() string
  304. }
  305. type UserInfo interface {
  306. GetID() string
  307. GetUserName() string
  308. GetName() string
  309. }
  310. func (c *Context) SetTenantInfo(tenantInfo TenantInfo) {
  311. c.Set(tenantInfoKey, tenantInfo)
  312. }
  313. func (c *Context) SetUserInfo(userInfo UserInfo) {
  314. c.Set(userInfoKey, userInfo)
  315. }
  316. func (c *Context) GetTenantInfo() TenantInfo {
  317. tenantInfo, exist := c.Get(tenantInfoKey)
  318. if !exist {
  319. return nil
  320. }
  321. return tenantInfo.(TenantInfo)
  322. }
  323. func (c *Context) GetUserInfo() UserInfo {
  324. userInfo, exist := c.Get(userInfoKey)
  325. if !exist {
  326. return nil
  327. }
  328. return userInfo.(UserInfo)
  329. }