You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB

  1. package container
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-xorm/xorm"
  6. "github.com/kataras/iris/v12"
  7. )
  8. // 请求上下文
  9. type RequestContext struct {
  10. // 请求标识
  11. requestId string
  12. // Web上下文
  13. webContext iris.Context
  14. // XORM会话
  15. session *xorm.Session
  16. }
  17. // 创建会话上下文
  18. // 参数
  19. // 1.请求标识
  20. // 2.Web上下文
  21. // 3.XORM会话
  22. // 返回值:
  23. // 1.会话上下文
  24. // 2.错误
  25. func NewRequestContext(requestId string, webContext iris.Context) (*RequestContext, error) {
  26. if requestId == "" {
  27. return nil, errors.New(fmt.Sprintf("请求标识不能为空!"))
  28. }
  29. if webContext == nil {
  30. return nil, errors.New(fmt.Sprintf("Web上下文不能为空!"))
  31. }
  32. return &RequestContext{requestId: requestId, webContext: webContext}, nil
  33. }
  34. // 获取请求标识
  35. // 返回值:
  36. // 1.请求标识
  37. func (context *RequestContext) RequestId() string {
  38. return context.requestId
  39. }
  40. // 获取Web上下文
  41. // 返回值:
  42. // 1.Web上下文
  43. func (context *RequestContext) WebContext() iris.Context {
  44. return context.webContext
  45. }
  46. // 获取XORM会话
  47. // 返回值:
  48. // 1.XORM会话
  49. func (context *RequestContext) Session() *xorm.Session {
  50. return context.session
  51. }