context.go 11 KB

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