添加setparams传递

This commit is contained in:
zhucheer 2019-11-14 16:08:38 +08:00
parent 42c1356a54
commit 082a0f69f7
1 changed files with 23 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"gitee.com/zhucheer/orange/request"
"gitee.com/zhucheer/orange/session"
"net/http"
"sync"
)
type Context struct {
@ -18,6 +19,8 @@ type Context struct {
responseBody bytes.Buffer
OrangeInput *request.OrangeInput
CsrfToken string
userParams map[string]interface{}
mutx sync.Mutex
}
func NewCtx(ctx context.Context, w http.ResponseWriter, r *http.Request) *Context {
@ -54,6 +57,26 @@ func (c *Context) Session() session.Store {
return c.session
}
func (c *Context) SetCtxParam(key string, value interface{}) {
c.mutx.Lock()
defer c.mutx.Unlock()
if c.userParams == nil {
c.userParams = make(map[string]interface{})
}
c.userParams[key] = value
}
func (c *Context) GetCtxParam(key string) (val interface{}) {
if c.userParams == nil {
return
}
if val, ok := c.userParams[key]; ok {
return val
}
return
}
func (c *Context) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.response, cookie)
}