cookie

·  阅读 23
	设置cookie
        $cookie(name, value) 
        $cookie(name, value, options);

    获取cookie
        $cookie(name);

    删除cookie
        $cookie(name, null);
复制代码

封装好的cookie函数

function $cookie(name){
    switch(arguments.length){
        case 1:
            return getCookie(name);
            break;
        case 3:
            setCookie(...arguments);
            break;
        case 2:
            arguments[1] ? setCookie(...arguments) : removeCookie(name);
            break;
        default:
            console.log("cookie parameter is error");
            break;
    }
}

function setCookie(){
    var c1 = new Cookie(...arguments);
    c1.init();
    c1.setCookie();
}

function getCookie(name){
    var c1 = new Cookie();
    return c1.getCookie(name);
}

function removeCookie(name){
    var c1 = new Cookie();
    c1.removeCookie(name);
}

class Cookie{
    //写个默认值
    constructor(name, value, options = {}){
        this.name = name;
        this.value = value;

        /*
            options
                expires  过期时间
                path     路径
                domain   域名
                secure   访问限制  true false
                raw      默认 true 编码(只管value)
                             false不编码
        */
        this.options = options;
    }
    init(){
        this.expires = this.options.expires ? this.options.expires : 7;
        this.path = this.options.path ? this.options.path : null;
        this.domain = this.options.domain ? this.options.domain : null;
        this.secure = this.options.secure ? "secure" : null;
        this.raw = this.options.raw == false ? false : true;
    }
    setCookie(){
        //name和value最好通过编码去存储
        var cookieStr = encodeURIComponent(this.name) + "=" + (this.raw ? encodeURIComponent(this.value) : this.value);
        
        if(this.expires){
            cookieStr += ";expires=" + afterOfDate(this.expires);
        }
        if(this.path){
            cookieStr += ";path=" + this.path;
        }
        if(this.domain){
            cookieStr += ";domain=" + this.domain;
        }
        if(this.secure){
            cookieStr += ";" + this.secure;
        }
        //设置cookie
        document.cookie = cookieStr;

    }
    //超级英雄=钢铁侠; 赛亚人=孙悟空; 变种人=万磁王 
    getCookie(name){
        //1、将存储的cookie字符串取出,并解码
        var cookieStr = decodeURIComponent(document.cookie);
        //2、查找开始的位置
        var start = cookieStr.indexOf(name);
        if(start == -1){
            return null;
        }else{
            var end = cookieStr.indexOf("; ", start);
            if(end == -1){
                end = cookieStr.length;
            }

            //3、提去下标对应的字符串[start, end)
            var subStr = cookieStr.substring(start, end);
            // console.log(subStr, start, end);
            var arr = subStr.split("=");
            return arr[0] == name && (cookieStr[start - 1] == " " || !start) ? arr[1] : null;
        }

    }
    removeCookie(name){
        //删除cookie的原理是,直接设置过期的时间
        document.cookie = encodeURIComponent(name) + "=;expires=" + new Date(0);
    }
}


function afterOfDate(n){
    var d = new Date();
    var day = d.getDate();
    d.setDate(day + n);
    return d;
}


export {
    setCookie,
    getCookie,
    removeCookie,
    $cookie
}
复制代码
分类:
前端
标签: