package util import ( "github.com/gin-gonic/gin" ) type PosterCookie struct { Cookie map[string]string ctx *gin.Context } // NewPosterCookie 实例化Cookie func NewPosterCookie(ctx *gin.Context) *PosterCookie { return &PosterCookie{ctx: ctx, Cookie: map[string]string{}} } // SetCookie 设置cookie func (cookie *PosterCookie) SetCookie(name, value string, expireAt int) { cookie.Cookie[name] = value for key, val := range cookie.Cookie { cookie.ctx.SetCookie(key, val, expireAt, "/", "", true, false) } } // Get 获取cookie func (cookie *PosterCookie) Get(name string) *string { val, err := cookie.ctx.Cookie(name) if err != nil { return nil } return &val }