context.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. tenantInfoKey = "context-tenant-info"
  18. userInfoKey = "context-user-info"
  19. )
  20. type Context struct {
  21. *gin.Context
  22. }
  23. // GetFileHeaderBytes 获取Multipart中传递的文件名和文件内容
  24. // 参数:
  25. // - fileHeader: Multipart的文件头
  26. // 返回值:
  27. // - 文件名
  28. // - 文件字节
  29. // - 错误
  30. func (c *Context) GetFileHeaderBytes(fileHeader *multipart.FileHeader) (string, []byte, error) {
  31. file, err := fileHeader.Open()
  32. if err != nil {
  33. return "", nil, errors.New(err.Error())
  34. }
  35. defer func(file multipart.File) {
  36. err := file.Close()
  37. if err != nil {
  38. logger.GetInstance().Error(errors.New(err.Error()))
  39. return
  40. }
  41. }(file)
  42. contentBytes, err := io.ReadAll(file)
  43. if err != nil {
  44. return "", nil, errors.New(err.Error())
  45. }
  46. return fileHeader.Filename, contentBytes, nil
  47. }
  48. // GetHeader 获取上下文中的Header
  49. // 参数: 无
  50. // 返回值:
  51. // - 上下文中的Header
  52. func (c *Context) GetHeader() *Header {
  53. savedHeader, exist := c.Get(headerKey)
  54. if exist {
  55. return &Header{
  56. c: c,
  57. header: savedHeader.(map[string]string),
  58. }
  59. }
  60. header := make(map[string]string)
  61. for key, values := range c.Request.Header {
  62. header[key] = strings.Join(values, ",")
  63. }
  64. c.Set(headerKey, header)
  65. return &Header{
  66. c: c,
  67. header: header,
  68. }
  69. }
  70. // GetBytesBody 获取上下文中的字节Body
  71. // 参数: 无
  72. // 返回值:
  73. // - 上下文中的字节Body
  74. // - 错误
  75. func (c *Context) GetBytesBody() (*BytesBody, error) {
  76. body, exist := c.Get(bodyKey)
  77. if !exist {
  78. bytesBody, err := c.ReadOriginBody()
  79. if err != nil {
  80. return nil, err
  81. }
  82. c.Set(bodyKey, bytesBody)
  83. return &BytesBody{
  84. c: c,
  85. bytesBody: bytesBody,
  86. }, nil
  87. }
  88. switch b := body.(type) {
  89. case []byte:
  90. return &BytesBody{
  91. c: c,
  92. bytesBody: b,
  93. }, nil
  94. case map[string]any:
  95. bytesBody, err := json.Marshal(b)
  96. if err != nil {
  97. return nil, errors.New(err.Error())
  98. }
  99. return &BytesBody{
  100. c: c,
  101. bytesBody: bytesBody,
  102. }, nil
  103. default:
  104. return nil, errors.New("不支持的body类型")
  105. }
  106. }
  107. // GetJsonBody 获取上下文中的JsonBody
  108. // 参数: 无
  109. // 返回值:
  110. // - 上下文中的JsonBody
  111. // - 错误
  112. func (c *Context) GetJsonBody() (*JsonBody, error) {
  113. body, exist := c.Get(bodyKey)
  114. if !exist {
  115. bytesBody, err := c.ReadOriginBody()
  116. if err != nil {
  117. return nil, err
  118. }
  119. jsonBodyMap := make(map[string]any)
  120. err = json.Unmarshal(bytesBody, &jsonBodyMap)
  121. if err != nil {
  122. return nil, errors.New(err.Error())
  123. }
  124. c.Set(bodyKey, jsonBodyMap)
  125. return &JsonBody{
  126. c: c,
  127. jsonBodyMap: jsonBodyMap,
  128. }, nil
  129. }
  130. switch b := body.(type) {
  131. case []byte:
  132. jsonBodyMap := make(map[string]any)
  133. err := json.Unmarshal(b, &jsonBodyMap)
  134. if err != nil {
  135. return nil, errors.New(err.Error())
  136. }
  137. return &JsonBody{
  138. c: c,
  139. jsonBodyMap: jsonBodyMap,
  140. }, nil
  141. case map[string]any:
  142. return &JsonBody{
  143. c: c,
  144. jsonBodyMap: body.(map[string]any),
  145. }, nil
  146. default:
  147. return nil, errors.New("不支持的body类型")
  148. }
  149. }
  150. // GetQueryParams 获取上下文中的查询参数
  151. // 参数: 无
  152. // 返回值:
  153. // - 上下文中的查询参数
  154. // - 错误
  155. func (c *Context) GetQueryParams() *QueryPrams {
  156. queryParams, exist := c.Get(queryParamsKey)
  157. if !exist {
  158. return &QueryPrams{
  159. c: c,
  160. queryParams: c.GetOriginQueryParams(),
  161. }
  162. }
  163. return &QueryPrams{
  164. c: c,
  165. queryParams: queryParams.(map[string]string),
  166. }
  167. }
  168. // GetPathParams 获取上下文中的路径参数
  169. // 参数: 无
  170. // 返回值:
  171. // - 上下文中的路径参数
  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.GetOriginPathParams(),
  179. }
  180. }
  181. return &PathPrams{
  182. c: c,
  183. pathParams: pathParams.(map[string]string),
  184. }
  185. }
  186. // ReadOriginBody 获取上下文中的原始字节Body
  187. // 参数: 无
  188. // 返回值:
  189. // - 上下文中的原始字节Body
  190. // - 错误
  191. func (c *Context) ReadOriginBody() ([]byte, error) {
  192. if c.Request.Body == nil {
  193. return make([]byte, 0), nil
  194. }
  195. body, err := io.ReadAll(c.Request.Body)
  196. if err != nil {
  197. return nil, errors.New(err.Error())
  198. }
  199. defer func(Body io.ReadCloser) {
  200. err := Body.Close()
  201. if err != nil {
  202. logger.GetInstance().Error(errors.New(err.Error()))
  203. return
  204. }
  205. }(c.Request.Body)
  206. c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
  207. c.Set(bodyKey, body)
  208. return body, nil
  209. }
  210. // GetOriginQueryParams 获取上下文中的原始查询参数
  211. // 参数: 无
  212. // 返回值:
  213. // - 上下文中的原始查询参数
  214. func (c *Context) GetOriginQueryParams() map[string]string {
  215. queryParams := make(map[string]string)
  216. for key, values := range c.Request.URL.Query() {
  217. queryParams[key] = strings.Join(values, ",")
  218. }
  219. return queryParams
  220. }
  221. // GetOriginPathParams 获取上下文中的原始路径参数
  222. // 参数: 无
  223. // 返回值:
  224. // - 上下文中的原始路径参数
  225. func (c *Context) GetOriginPathParams() map[string]string {
  226. pathParams := make(map[string]string)
  227. for _, params := range c.Params {
  228. pathParams[params.Key] = params.Value
  229. }
  230. return pathParams
  231. }
  232. type Header struct {
  233. c *Context
  234. header map[string]string
  235. }
  236. // Set 设置Header
  237. // 参数:
  238. // - key: Header的键
  239. // - value: Header对应键的值
  240. // 返回值: 无
  241. func (header *Header) Set(key string, value string) {
  242. header.header[key] = value
  243. header.c.Set(headerKey, header.header)
  244. }
  245. // Get 获取Header对应键的值
  246. // 参数:
  247. // - key: Header的键
  248. // 返回值:
  249. // - Header对应键的值
  250. func (header *Header) Get(key string) string {
  251. return header.header[key]
  252. }
  253. // Map 获取Header的map表示
  254. // 参数: 无
  255. // 返回值:
  256. // - Header的map表示
  257. func (header *Header) Map() map[string]string {
  258. return header.header
  259. }
  260. type BytesBody struct {
  261. c *Context
  262. bytesBody []byte
  263. }
  264. // Set 设置字节body
  265. // 参数:
  266. // - body: 字节body的内容
  267. // 返回值: 无
  268. func (bytesBody *BytesBody) Set(body []byte) {
  269. bytesBody.bytesBody = body
  270. bytesBody.c.Set(bodyKey, bytesBody.bytesBody)
  271. }
  272. // Bytes 获取字节body的内容
  273. // 参数: 无
  274. // 返回值:
  275. // - 字节body的内容
  276. func (bytesBody *BytesBody) Bytes() []byte {
  277. return bytesBody.bytesBody
  278. }
  279. // Marshal 将input Marshal到字节body
  280. // 参数:
  281. // - input: 输入,一般为结构或map[string]any
  282. // 返回值:
  283. // - 错误
  284. func (bytesBody *BytesBody) Marshal(input any) error {
  285. jsonBytesBody, err := json.Marshal(input)
  286. if err != nil {
  287. return errors.New(err.Error())
  288. }
  289. bytesBody.Set(jsonBytesBody)
  290. return nil
  291. }
  292. // Unmarshal 将字节body的内容Unmarshal到output
  293. // 参数:
  294. // - output: 输出,一般为结构指针或map[string]any指针
  295. // 返回值:
  296. // - 错误
  297. func (bytesBody *BytesBody) Unmarshal(output any) error {
  298. err := json.Unmarshal(bytesBody.Bytes(), output)
  299. if err != nil {
  300. return errors.New(err.Error())
  301. }
  302. return nil
  303. }
  304. type JsonBody struct {
  305. c *Context
  306. jsonBodyMap map[string]any
  307. }
  308. // Set 设置JsonBody键对应的值
  309. // 参数:
  310. // - key: JsonBody的键
  311. // - value: JsonBody对应键的值
  312. // 返回值: 无
  313. func (jsonBody *JsonBody) Set(key string, value any) {
  314. jsonBody.jsonBodyMap[key] = value
  315. jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
  316. }
  317. // Delete 删除JsonBody键对应的值
  318. // 参数:
  319. // - key: JsonBody的键
  320. // 返回值: 无
  321. func (jsonBody *JsonBody) Delete(key string) {
  322. delete(jsonBody.jsonBodyMap, key)
  323. jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
  324. }
  325. // Get 获取JsonBody对应键的值
  326. // 参数:
  327. // - key: JsonBody的键
  328. // 返回值:
  329. // - JsonBody对应键的值
  330. func (jsonBody *JsonBody) Get(key string) any {
  331. return jsonBody.jsonBodyMap[key]
  332. }
  333. // Map 获取JsonBody的map表示
  334. // 参数: 无
  335. // 返回值:
  336. // - JsonBody的map表示
  337. func (jsonBody *JsonBody) Map() map[string]any {
  338. return jsonBody.jsonBodyMap
  339. }
  340. // Bytes 获取JsonBody的内容
  341. // 参数: 无
  342. // 返回值:
  343. // - JsonBody的内容
  344. func (jsonBody *JsonBody) Bytes() ([]byte, error) {
  345. jsonBytes, err := json.Marshal(jsonBody.jsonBodyMap)
  346. if err != nil {
  347. return nil, errors.New(err.Error())
  348. }
  349. return jsonBytes, nil
  350. }
  351. // Unmarshal 将JsonBody的内容Unmarshal到output
  352. // 参数:
  353. // - output: 输出,一般为结构指针或map[string]any指针
  354. // 返回值:
  355. // - 错误
  356. func (jsonBody *JsonBody) Unmarshal(output any) error {
  357. jsonBytes, err := jsonBody.Bytes()
  358. if err != nil {
  359. return err
  360. }
  361. err = json.Unmarshal(jsonBytes, output)
  362. if err != nil {
  363. return errors.New(err.Error())
  364. }
  365. return nil
  366. }
  367. type QueryPrams struct {
  368. c *Context
  369. queryParams map[string]string
  370. }
  371. // Set 设置查询参数
  372. // 参数:
  373. // - key: 查询参数的键
  374. // - value: 查询参数对应键的值
  375. // 返回值: 无
  376. func (queryParams *QueryPrams) Set(key string, value string) {
  377. queryParams.queryParams[key] = value
  378. queryParams.c.Set(queryParamsKey, queryParams.queryParams)
  379. }
  380. // Delete 删除JsonBody键对应的值
  381. // 参数:
  382. // - key: JsonBody的键
  383. // 返回值: 无
  384. func (queryParams *QueryPrams) Delete(key string) {
  385. delete(queryParams.queryParams, key)
  386. queryParams.c.Set(queryParamsKey, queryParams.queryParams)
  387. }
  388. // Get 获取查询参数对应键的值
  389. // 参数:
  390. // - key: 查询参数的键
  391. // 返回值:
  392. // - 查询参数对应键的值
  393. func (queryParams *QueryPrams) Get(key string) string {
  394. return queryParams.queryParams[key]
  395. }
  396. // Map 获取查询参数的map表示
  397. // 参数: 无
  398. // 返回值:
  399. // - 查询参数的map表示
  400. func (queryParams *QueryPrams) Map() map[string]string {
  401. return queryParams.queryParams
  402. }
  403. type PathPrams struct {
  404. c *Context
  405. pathParams map[string]string
  406. }
  407. // Set 设置路径参数
  408. // 参数:
  409. // - key: 路径参数的键
  410. // - value: 路径参数对应键的值
  411. // 返回值: 无
  412. func (pathParams *PathPrams) Set(key string, value string) {
  413. pathParams.pathParams[key] = value
  414. pathParams.c.Set(pathParamsKey, pathParams.pathParams)
  415. }
  416. // Delete 删除JsonBody键对应的值
  417. // 参数:
  418. // - key: JsonBody的键
  419. // 返回值: 无
  420. func (pathParams *PathPrams) Delete(key string) {
  421. delete(pathParams.pathParams, key)
  422. pathParams.c.Set(pathParamsKey, pathParams.pathParams)
  423. }
  424. // Get 获取路径参数对应键的值
  425. // 参数:
  426. // - key: 路径参数的键
  427. // 返回值:
  428. // - 路径参数对应键的值
  429. func (pathParams *PathPrams) Get(key string) string {
  430. return pathParams.pathParams[key]
  431. }
  432. // Map 获取路径参数的map表示
  433. // 参数: 无
  434. // 返回值:
  435. // - 路径参数的map表示
  436. func (pathParams *PathPrams) Map() map[string]string {
  437. return pathParams.pathParams
  438. }
  439. type TenantInfo interface {
  440. GetID() string
  441. GetName() string
  442. }
  443. type UserInfo interface {
  444. GetID() string
  445. GetUserName() string
  446. GetName() string
  447. }
  448. func (c *Context) SetTenantInfo(tenantInfo TenantInfo) {
  449. c.Set(tenantInfoKey, tenantInfo)
  450. }
  451. func (c *Context) SetUserInfo(userInfo UserInfo) {
  452. c.Set(userInfoKey, userInfo)
  453. }
  454. func (c *Context) GetTenantInfo() TenantInfo {
  455. tenantInfo, exist := c.Get(tenantInfoKey)
  456. if !exist {
  457. return nil
  458. }
  459. return tenantInfo.(TenantInfo)
  460. }
  461. func (c *Context) GetUserInfo() UserInfo {
  462. userInfo, exist := c.Get(userInfoKey)
  463. if !exist {
  464. return nil
  465. }
  466. return userInfo.(UserInfo)
  467. }