package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/go-xorm/xorm"
|
|
"github.com/kataras/iris/v12"
|
|
)
|
|
|
|
// 请求上下文
|
|
type RequestContext struct {
|
|
// 请求标识
|
|
requestId string
|
|
// Web上下文
|
|
webContext iris.Context
|
|
// XORM会话
|
|
session *xorm.Session
|
|
}
|
|
|
|
// 创建会话上下文
|
|
// 参数
|
|
// 1.请求标识
|
|
// 2.Web上下文
|
|
// 3.XORM会话
|
|
// 返回值:
|
|
// 1.会话上下文
|
|
// 2.错误
|
|
func NewRequestContext(requestId string, webContext iris.Context) (*RequestContext, error) {
|
|
if requestId == "" {
|
|
return nil, errors.New(fmt.Sprintf("请求标识不能为空!"))
|
|
}
|
|
if webContext == nil {
|
|
return nil, errors.New(fmt.Sprintf("Web上下文不能为空!"))
|
|
}
|
|
return &RequestContext{requestId: requestId, webContext: webContext}, nil
|
|
}
|
|
|
|
// 获取请求标识
|
|
// 返回值:
|
|
// 1.请求标识
|
|
func (context *RequestContext) RequestId() string {
|
|
return context.requestId
|
|
}
|
|
|
|
// 获取Web上下文
|
|
// 返回值:
|
|
// 1.Web上下文
|
|
func (context *RequestContext) WebContext() iris.Context {
|
|
return context.webContext
|
|
}
|
|
|
|
// 获取XORM会话
|
|
// 返回值:
|
|
// 1.XORM会话
|
|
func (context *RequestContext) Session() *xorm.Session {
|
|
return context.session
|
|
}
|