github.com/astaxie/beego@v1.12.3/flash.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package beego 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 ) 22 23 // FlashData is a tools to maintain data when using across request. 24 type FlashData struct { 25 Data map[string]string 26 } 27 28 // NewFlash return a new empty FlashData struct. 29 func NewFlash() *FlashData { 30 return &FlashData{ 31 Data: make(map[string]string), 32 } 33 } 34 35 // Set message to flash 36 func (fd *FlashData) Set(key string, msg string, args ...interface{}) { 37 if len(args) == 0 { 38 fd.Data[key] = msg 39 } else { 40 fd.Data[key] = fmt.Sprintf(msg, args...) 41 } 42 } 43 44 // Success writes success message to flash. 45 func (fd *FlashData) Success(msg string, args ...interface{}) { 46 if len(args) == 0 { 47 fd.Data["success"] = msg 48 } else { 49 fd.Data["success"] = fmt.Sprintf(msg, args...) 50 } 51 } 52 53 // Notice writes notice message to flash. 54 func (fd *FlashData) Notice(msg string, args ...interface{}) { 55 if len(args) == 0 { 56 fd.Data["notice"] = msg 57 } else { 58 fd.Data["notice"] = fmt.Sprintf(msg, args...) 59 } 60 } 61 62 // Warning writes warning message to flash. 63 func (fd *FlashData) Warning(msg string, args ...interface{}) { 64 if len(args) == 0 { 65 fd.Data["warning"] = msg 66 } else { 67 fd.Data["warning"] = fmt.Sprintf(msg, args...) 68 } 69 } 70 71 // Error writes error message to flash. 72 func (fd *FlashData) Error(msg string, args ...interface{}) { 73 if len(args) == 0 { 74 fd.Data["error"] = msg 75 } else { 76 fd.Data["error"] = fmt.Sprintf(msg, args...) 77 } 78 } 79 80 // Store does the saving operation of flash data. 81 // the data are encoded and saved in cookie. 82 func (fd *FlashData) Store(c *Controller) { 83 c.Data["flash"] = fd.Data 84 var flashValue string 85 for key, value := range fd.Data { 86 flashValue += "\x00" + key + "\x23" + BConfig.WebConfig.FlashSeparator + "\x23" + value + "\x00" 87 } 88 c.Ctx.SetCookie(BConfig.WebConfig.FlashName, url.QueryEscape(flashValue), 0, "/") 89 } 90 91 // ReadFromRequest parsed flash data from encoded values in cookie. 92 func ReadFromRequest(c *Controller) *FlashData { 93 flash := NewFlash() 94 if cookie, err := c.Ctx.Request.Cookie(BConfig.WebConfig.FlashName); err == nil { 95 v, _ := url.QueryUnescape(cookie.Value) 96 vals := strings.Split(v, "\x00") 97 for _, v := range vals { 98 if len(v) > 0 { 99 kv := strings.Split(v, "\x23"+BConfig.WebConfig.FlashSeparator+"\x23") 100 if len(kv) == 2 { 101 flash.Data[kv[0]] = kv[1] 102 } 103 } 104 } 105 //read one time then delete it 106 c.Ctx.SetCookie(BConfig.WebConfig.FlashName, "", -1, "/") 107 } 108 c.Data["flash"] = flash.Data 109 return flash 110 }