CookieUtil

const CookieUtil = {
    get(name){
        let cookieName = `${encodeURIComponent(name)}=`
        let cookieStart = document.cookie.indexOf(cookieName)
        let cookieValue = ''
        if(cookieStart > -1){
            let cookieEnd = document.cookie.indexOf(';', cookieStart)
            if(cookieEnd === -1){
                cookieEnd = document.cookie.length
            }
            cookieValue = `${decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))}`
        }
        return cookieValue
    },
    set(name, value, expires, path, domain, secure){
        let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
        if(expires instanceof Date){
            cookieText += `;expires=${expires.toGMTString()}`
        } else {
            cookieText += `;expires=${new Date(expires).toGMTString()}`

        }
        if(path){
            cookieText += `;path=${path}`
        }
        if(domain){
            cookieText += `;domain=${domain}`
        }
        if(secure){
            cookieText += `;${secure}`
        }
        document.cookie += cookieText
    },
    unset(name, path, domain, secure){
        this.set(name, '', new Date('1970/01/01'), path, domain, secure)
    }
}

  

posted @ 2023-01-29 22:11  671_MrSix  阅读(17)  评论(0编辑  收藏  举报