beego mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.0KB

  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. package beego
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. )
  20. // FlashData is a tools to maintain data when using across request.
  21. type FlashData struct {
  22. Data map[string]string
  23. }
  24. // NewFlash return a new empty FlashData struct.
  25. func NewFlash() *FlashData {
  26. return &FlashData{
  27. Data: make(map[string]string),
  28. }
  29. }
  30. // Set message to flash
  31. func (fd *FlashData) Set(key string, msg string, args ...interface{}) {
  32. if len(args) == 0 {
  33. fd.Data[key] = msg
  34. } else {
  35. fd.Data[key] = fmt.Sprintf(msg, args...)
  36. }
  37. }
  38. // Success writes success message to flash.
  39. func (fd *FlashData) Success(msg string, args ...interface{}) {
  40. if len(args) == 0 {
  41. fd.Data["success"] = msg
  42. } else {
  43. fd.Data["success"] = fmt.Sprintf(msg, args...)
  44. }
  45. }
  46. // Notice writes notice message to flash.
  47. func (fd *FlashData) Notice(msg string, args ...interface{}) {
  48. if len(args) == 0 {
  49. fd.Data["notice"] = msg
  50. } else {
  51. fd.Data["notice"] = fmt.Sprintf(msg, args...)
  52. }
  53. }
  54. // Warning writes warning message to flash.
  55. func (fd *FlashData) Warning(msg string, args ...interface{}) {
  56. if len(args) == 0 {
  57. fd.Data["warning"] = msg
  58. } else {
  59. fd.Data["warning"] = fmt.Sprintf(msg, args...)
  60. }
  61. }
  62. // Error writes error message to flash.
  63. func (fd *FlashData) Error(msg string, args ...interface{}) {
  64. if len(args) == 0 {
  65. fd.Data["error"] = msg
  66. } else {
  67. fd.Data["error"] = fmt.Sprintf(msg, args...)
  68. }
  69. }
  70. // Store does the saving operation of flash data.
  71. // the data are encoded and saved in cookie.
  72. func (fd *FlashData) Store(c *Controller) {
  73. c.Data["flash"] = fd.Data
  74. var flashValue string
  75. for key, value := range fd.Data {
  76. flashValue += "\x00" + key + "\x23" + BConfig.WebConfig.FlashSeparator + "\x23" + value + "\x00"
  77. }
  78. c.Ctx.SetCookie(BConfig.WebConfig.FlashName, url.QueryEscape(flashValue), 0, "/")
  79. }
  80. // ReadFromRequest parsed flash data from encoded values in cookie.
  81. func ReadFromRequest(c *Controller) *FlashData {
  82. flash := NewFlash()
  83. if cookie, err := c.Ctx.Request.Cookie(BConfig.WebConfig.FlashName); err == nil {
  84. v, _ := url.QueryUnescape(cookie.Value)
  85. vals := strings.Split(v, "\x00")
  86. for _, v := range vals {
  87. if len(v) > 0 {
  88. kv := strings.Split(v, "\x23"+BConfig.WebConfig.FlashSeparator+"\x23")
  89. if len(kv) == 2 {
  90. flash.Data[kv[0]] = kv[1]
  91. }
  92. }
  93. }
  94. //read one time then delete it
  95. c.Ctx.SetCookie(BConfig.WebConfig.FlashName, "", -1, "/")
  96. }
  97. c.Data["flash"] = flash.Data
  98. return flash
  99. }