1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 03:40:55 +00:00
Beego/flash.go

111 lines
3.0 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-03 09:55:44 +00:00
package beego
import (
"fmt"
"net/url"
"strings"
)
// FlashData is a tools to maintain data when using across request.
2013-08-03 09:55:44 +00:00
type FlashData struct {
Data map[string]string
}
// NewFlash return a new empty FlashData struct.
2013-08-03 10:18:09 +00:00
func NewFlash() *FlashData {
2013-08-03 10:17:00 +00:00
return &FlashData{
2013-08-03 10:18:09 +00:00
Data: make(map[string]string),
2013-08-03 10:17:00 +00:00
}
}
2014-09-23 15:54:38 +00:00
// Set message to flash
func (fd *FlashData) Set(key string, msg string, args ...interface{}) {
if len(args) == 0 {
fd.Data[key] = msg
} else {
fd.Data[key] = fmt.Sprintf(msg, args...)
}
}
// Success writes success message to flash.
func (fd *FlashData) Success(msg string, args ...interface{}) {
if len(args) == 0 {
fd.Data["success"] = msg
} else {
fd.Data["success"] = fmt.Sprintf(msg, args...)
}
}
// Notice writes notice message to flash.
2013-08-03 09:55:44 +00:00
func (fd *FlashData) Notice(msg string, args ...interface{}) {
if len(args) == 0 {
fd.Data["notice"] = msg
} else {
fd.Data["notice"] = fmt.Sprintf(msg, args...)
}
}
// Warning writes warning message to flash.
2013-08-03 09:55:44 +00:00
func (fd *FlashData) Warning(msg string, args ...interface{}) {
if len(args) == 0 {
fd.Data["warning"] = msg
} else {
fd.Data["warning"] = fmt.Sprintf(msg, args...)
}
}
// Error writes error message to flash.
2013-08-03 09:55:44 +00:00
func (fd *FlashData) Error(msg string, args ...interface{}) {
if len(args) == 0 {
fd.Data["error"] = msg
} else {
fd.Data["error"] = fmt.Sprintf(msg, args...)
}
}
// Store does the saving operation of flash data.
// the data are encoded and saved in cookie.
2013-08-03 09:55:44 +00:00
func (fd *FlashData) Store(c *Controller) {
c.Data["flash"] = fd.Data
var flashValue string
for key, value := range fd.Data {
2015-12-09 15:35:04 +00:00
flashValue += "\x00" + key + "\x23" + BConfig.WebConfig.FlashSeperator + "\x23" + value + "\x00"
2013-08-03 09:55:44 +00:00
}
2015-12-09 15:35:04 +00:00
c.Ctx.SetCookie(BConfig.WebConfig.FlashName, url.QueryEscape(flashValue), 0, "/")
2013-08-03 09:55:44 +00:00
}
// ReadFromRequest parsed flash data from encoded values in cookie.
2013-08-03 09:55:44 +00:00
func ReadFromRequest(c *Controller) *FlashData {
flash := NewFlash()
2015-12-09 15:35:04 +00:00
if cookie, err := c.Ctx.Request.Cookie(BConfig.WebConfig.FlashName); err == nil {
2013-08-20 04:04:50 +00:00
v, _ := url.QueryUnescape(cookie.Value)
vals := strings.Split(v, "\x00")
2013-08-03 09:55:44 +00:00
for _, v := range vals {
if len(v) > 0 {
2015-12-09 15:35:04 +00:00
kv := strings.Split(v, "\x23"+BConfig.WebConfig.FlashSeperator+"\x23")
2013-08-03 09:55:44 +00:00
if len(kv) == 2 {
flash.Data[kv[0]] = kv[1]
}
}
}
//read one time then delete it
2015-12-09 15:35:04 +00:00
c.Ctx.SetCookie(BConfig.WebConfig.FlashName, "", -1, "/")
2013-08-03 09:55:44 +00:00
}
c.Data["flash"] = flash.Data
return flash
}