flash support
Showing
1 changed file
with
66 additions
and
0 deletions
flash.go
0 → 100644
| 1 | package beego | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "net/url" | ||
| 6 | "strings" | ||
| 7 | ) | ||
| 8 | |||
| 9 | type FlashData struct { | ||
| 10 | Data map[string]string | ||
| 11 | } | ||
| 12 | |||
| 13 | func (fd *FlashData) Notice(msg string, args ...interface{}) { | ||
| 14 | if len(args) == 0 { | ||
| 15 | fd.Data["notice"] = msg | ||
| 16 | } else { | ||
| 17 | fd.Data["notice"] = fmt.Sprintf(msg, args...) | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | func (fd *FlashData) Warning(msg string, args ...interface{}) { | ||
| 22 | if len(args) == 0 { | ||
| 23 | fd.Data["warning"] = msg | ||
| 24 | } else { | ||
| 25 | fd.Data["warning"] = fmt.Sprintf(msg, args...) | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | func (fd *FlashData) Error(msg string, args ...interface{}) { | ||
| 30 | if len(args) == 0 { | ||
| 31 | fd.Data["error"] = msg | ||
| 32 | } else { | ||
| 33 | fd.Data["error"] = fmt.Sprintf(msg, args...) | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | func (fd *FlashData) Store(c *Controller) { | ||
| 38 | c.Data["flash"] = fd.Data | ||
| 39 | var flashValue string | ||
| 40 | for key, value := range fd.Data { | ||
| 41 | flashValue += "\x00" + key + ":" + value + "\x00" | ||
| 42 | } | ||
| 43 | c.Ctx.SetCookie("BEEGO_FLASH", url.QueryEscape(flashValue), 0, "/") | ||
| 44 | } | ||
| 45 | |||
| 46 | func ReadFromRequest(c *Controller) *FlashData { | ||
| 47 | flash := &FlashData{ | ||
| 48 | Data: make(map[string]string), | ||
| 49 | } | ||
| 50 | if cookie, err := c.Ctx.Request.Cookie("BEEGO_FLASH"); err == nil { | ||
| 51 | vals := strings.Split(cookie.Value, "\x00") | ||
| 52 | for _, v := range vals { | ||
| 53 | if len(v) > 0 { | ||
| 54 | kv := strings.Split(v, ":") | ||
| 55 | if len(kv) == 2 { | ||
| 56 | flash.Data[kv[0]] = kv[1] | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | //read one time then delete it | ||
| 61 | cookie.MaxAge = -1 | ||
| 62 | c.Ctx.Request.AddCookie(cookie) | ||
| 63 | } | ||
| 64 | c.Data["flash"] = flash.Data | ||
| 65 | return flash | ||
| 66 | } |
-
Please register or sign in to post a comment