poster_cookie.go 684 B

1234567891011121314151617181920212223242526272829303132
  1. package util
  2. import (
  3. "github.com/gin-gonic/gin"
  4. )
  5. type PosterCookie struct {
  6. Cookie map[string]string
  7. ctx *gin.Context
  8. }
  9. // NewPosterCookie 实例化Cookie
  10. func NewPosterCookie(ctx *gin.Context) *PosterCookie {
  11. return &PosterCookie{ctx: ctx, Cookie: map[string]string{}}
  12. }
  13. // SetCookie 设置cookie
  14. func (cookie *PosterCookie) SetCookie(name, value string, expireAt int) {
  15. cookie.Cookie[name] = value
  16. for key, val := range cookie.Cookie {
  17. cookie.ctx.SetCookie(key, val, expireAt, "/", "", true, false)
  18. }
  19. }
  20. // Get 获取cookie
  21. func (cookie *PosterCookie) Get(name string) *string {
  22. val, err := cookie.ctx.Cookie(name)
  23. if err != nil {
  24. return nil
  25. }
  26. return &val
  27. }