Przeglądaj źródła

fix:session逻辑修改

duxin 1 rok temu
rodzic
commit
35a30679a0
1 zmienionych plików z 27 dodań i 19 usunięć
  1. 27 19
      go-xweb/xweb/action.go

+ 27 - 19
go-xweb/xweb/action.go

@@ -56,6 +56,7 @@ type Action struct {
 	RootTemplate *template.Template
 	RootTemplate *template.Template
 	RequestBody  []byte
 	RequestBody  []byte
 	StatusCode   int
 	StatusCode   int
+	SessionMap   map[string]interface{}
 }
 }
 
 
 type Mapper struct {
 type Mapper struct {
@@ -67,7 +68,7 @@ func XsrfName() string {
 	return XSRF_TAG
 	return XSRF_TAG
 }
 }
 
 
-//[SWH|+]:
+// [SWH|+]:
 // Protocol returns request protocol name, such as HTTP/1.1 .
 // Protocol returns request protocol name, such as HTTP/1.1 .
 func (c *Action) Protocol() string {
 func (c *Action) Protocol() string {
 	return c.Request.Proto
 	return c.Request.Proto
@@ -378,21 +379,21 @@ func (c *Action) NotFound(message string) error {
 
 
 // ParseStruct mapping forms' name and values to struct's field
 // ParseStruct mapping forms' name and values to struct's field
 // For example:
 // For example:
-//		<form>
-//			<input name="user.id"/>
-//			<input name="user.name"/>
-//			<input name="user.age"/>
-//		</form>
 //
 //
-//		type User struct {
-//			Id int64
-//			Name string
-//			Age string
-//		}
+//	<form>
+//		<input name="user.id"/>
+//		<input name="user.name"/>
+//		<input name="user.age"/>
+//	</form>
 //
 //
-//		var user User
-//		err := action.MapForm(&user)
+//	type User struct {
+//		Id int64
+//		Name string
+//		Age string
+//	}
 //
 //
+//	var user User
+//	err := action.MapForm(&user)
 func (c *Action) MapForm(st interface{}, names ...string) error {
 func (c *Action) MapForm(st interface{}, names ...string) error {
 	v := reflect.ValueOf(st)
 	v := reflect.ValueOf(st)
 	var name string
 	var name string
@@ -708,7 +709,7 @@ func (c *Action) Render(tmpl string, params ...*T) error {
 	return err
 	return err
 }
 }
 
 
-//仅生成网页内容
+// 仅生成网页内容
 var regInclude = regexp.MustCompile(`\{\{\s*include\s*"(.*?\.html)".*\}\}`)
 var regInclude = regexp.MustCompile(`\{\{\s*include\s*"(.*?\.html)".*\}\}`)
 
 
 func (c *Action) NamedRender4Cache(name, content string, params ...*T) ([]byte, error) {
 func (c *Action) NamedRender4Cache(name, content string, params ...*T) ([]byte, error) {
@@ -754,7 +755,7 @@ func (c *Action) NamedRender4Cache(name, content string, params ...*T) ([]byte,
 	return nil, err
 	return nil, err
 }
 }
 
 
-//生成可缓存的数据但并未写到流中
+// 生成可缓存的数据但并未写到流中
 func (c *Action) Render4Cache(tmpl string, params ...*T) ([]byte, error) {
 func (c *Action) Render4Cache(tmpl string, params ...*T) ([]byte, error) {
 	content, err := c.getTemplate(tmpl)
 	content, err := c.getTemplate(tmpl)
 
 
@@ -858,7 +859,7 @@ func (c *Action) GetForm() url.Values {
 	return c.Request.Form
 	return c.Request.Form
 }
 }
 
 
-//增加_过滤脚本等
+// 增加_过滤脚本等
 func FilterXSS(str string) string {
 func FilterXSS(str string) string {
 	str = strings.Replace(str, "<", "&#60;", -1)
 	str = strings.Replace(str, "<", "&#60;", -1)
 	str = strings.Replace(str, ">", "&#62;", -1)
 	str = strings.Replace(str, ">", "&#62;", -1)
@@ -869,7 +870,7 @@ func FilterXSS(str string) string {
 	return str
 	return str
 }
 }
 
 
-//增加_原GetString方法
+// 增加_原GetString方法
 func (c *Action) GetStringComm(key string) string {
 func (c *Action) GetStringComm(key string) string {
 	s := c.GetSlice(key)
 	s := c.GetSlice(key)
 	if len(s) > 0 {
 	if len(s) > 0 {
@@ -878,7 +879,7 @@ func (c *Action) GetStringComm(key string) string {
 	return ""
 	return ""
 }
 }
 
 
-//修改_防Xss注入
+// 修改_防Xss注入
 func (c *Action) GetString(key string) string {
 func (c *Action) GetString(key string) string {
 	return FilterXSS(c.GetStringComm(key))
 	return FilterXSS(c.GetStringComm(key))
 }
 }
@@ -950,14 +951,21 @@ func (c *Action) Session() *httpsession.Session {
 }
 }
 
 
 func (c *Action) GetSession(key string) interface{} {
 func (c *Action) GetSession(key string) interface{} {
-	return c.Session().Get(key)
+	if c.SessionMap == nil {
+		c.SessionMap = c.session.GetMultiple()
+	}
+	return c.SessionMap[key]
 }
 }
 
 
 func (c *Action) SetSession(key string, value interface{}) {
 func (c *Action) SetSession(key string, value interface{}) {
+	if c.SessionMap != nil {
+		c.SessionMap[key] = value
+	}
 	c.Session().Set(key, value)
 	c.Session().Set(key, value)
 }
 }
 
 
 func (c *Action) DelSession(key string) {
 func (c *Action) DelSession(key string) {
+	c.SessionMap = nil
 	c.Session().Del(key)
 	c.Session().Del(key)
 }
 }