axios中對cookie的封裝
阿新 • • 發佈:2019-02-20
function isStandardBrowserEnv() {
if(typeof navigator !== 'undefined' && navigator.product === 'ReactNative'){
return false;
}
return (
typeof window !== 'undefined' &&
typeof document !== 'undefined'
);
}
function isNumber(val) {
return typeof val === 'number';
}
function isString(val) {
return typeof val === 'string';
}
var cookies = (isStandardBrowserEnv() ?
(function standardBrowserEnv(){
return {
write: function write(name, value, expires, path, domain, secure) {
var cookie = [];
cookie.push(name + '=' + encodeURIComponent(value));
if(isNumber(expires)) {
cookie.push('expires=' + new Date(expires).toGMTString());
}
if(isString(path)) {
cookie.push('path=' + path);
}
if(isString(domain)) {
cookie.push('domain=' + domain);
}
if(secure === true) {
cookie.push('secure');
}
document.cookie = cookie.join('; ');
},
read: function read(name) {
var match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
return (match ? decodeURIComponent(match[3]) : null);
},
remove: function remove(name) {
this.write(name, '', Date.now() - 86400000);
}
};
})() :
(function nonStandardBrowserEnv(){
return {
write: function write() {},
read: function read() { return null; },
remove: function remove() {}
};
})()
);