export default {
/**
* cookie操作
* 增 删 获取
*/
cookie: {
set: function (key, value,expireSeconds) {
document.cookie = key + '=' + value + ';' + expireSeconds;
},
get: function (key) {
// cookie 里获取 access_token
let strCookie = document.cookie;
// 将多cookie切割为多个名/值对
let arrCookie = strCookie.split(';');
let accessToken;
// 遍历cookie数组,处理每个cookie对
for (let i = 0; i < arrCookie.length; i++) {
let arr = arrCookie[i].split('=');
// 找到名称为access_token的cookie,并返回它的值
if (key === arr[0]) {
accessToken = arr[1];
break;
}
}
return accessToken;
},
delete: function (key) {
let myDate = new Date();
myDate.setTime(-1000); // 设置时间
document.cookie = key + "=''; expires=" + myDate.toGMTString();
}
}
};
清除所有cookie
// 清除所有cookie
clearAllCookie () {
let date=new Date();
date.setTime(date.getTime()-10000);
let keys = document.cookie.match(/[^ =;]+(?=\=)/g);
console.log("需要删除的cookie名字:"+keys);
if (keys) {
for (let i = keys.length; i--;)
document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";
}
}
清除本域名cookie
deleteCookies(){
let keys = document.cookie.match(/[^ =;]+(?==)/g);
if (keys) {
for (var i = keys.length; i--;) {
document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如 .m.ratingdog.cn
document.cookie = keys[i] + '=0;path=/;domain=ratingdog.cn;expires=' + new Date(0).toUTCString() // 清除一级域名下的或指定的,例如 .ratingdog.cn
}
}
}
网友评论