/** 设置缓存 **/ export function localStorageSetItem(name, data) { window.localStorage.setItem(name, data) } /** 获取缓存 **/ export function localStorageGetItem(data) { return window.localStorage.getItem(data); } /** 删除缓存 **/ export function localStorageRemoveItem(data) { window.localStorage.removeItem(data) } /** 删除全部缓存 **/ export function localStorageRemoveItemAll() { window.localStorage.clear() } /** 设置缓存 **/ export function sessionSetItem(name, data) { window.sessionStorage.setItem(name, data) } /** 获取缓存 **/ export function sessionGetItem(data) { return window.sessionStorage.getItem(data); } /** 删除缓存 **/ export function sessionRemoveItem(data) { window.sessionStorage.removeItem(data) } /** 删除全部缓存 **/ export function sessionRemoveItemAll() { window.sessionStorage.clear() } /** 设置cookie **/ export function setCookie(c_name, value, expiredays) { var exp = new Date(); // exp.setTime(exp.getTime() + 30*60*1000); document.cookie = c_name + "=" + escape(value) + ";path=/;expires=" + ';domain=' + getDomain(); } /** 获取cookie **/ export function getCookie(c_name) { if (document.cookie.length > 0) { let c_start, c_end; let l = document.cookie.split(';') for (let i = l.length - 1; i >= 0; i--) { if (l[i].split('=')[0] == c_name || l[i].split('=')[0] == (' ' + c_name) && l[i].split('=')[1] != '') { c_start = l[i].split('=')[1] return unescape(c_start) } } } return "" } /** 删除cookie **/ export function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + '' + ";path=/;expires=" + exp.toGMTString(); } // 清空所有cookie export function clearCookie() { var keys = document.cookie.match(/[^ =;]+(?=\=)/g); if (keys) { for (var i = keys.length; i--;) { delCookie(keys[i]); delCookie(keys[i], { path: '/' }); delCookie(keys[i], { path: '/', expires: -1 }); delCookie(keys[i], { path: '/', domain: document.domain, expires: -1 }); delCookie(keys[i], { path: '/', domain: "mp.csdn.net", expires: -1 }); } } }
使用说明
import { delCookie, getCookie, setCookie, clearCookie, localStorageSetItem, localStorageGetItem, localStorageRemoveItem, localStorageRemoveItemAll, } from "../index"; setCookie('Token', res.data.token) // 存入 getCookie("Token") // 获取 delCookie('Token') // 删除 clearCookie()//清除所有 localStorageSetItem('Token', JSON.stringify(Token))// 存入 JSON.parse(localStorageGetItem('Token')) // 获取 localStorageRemoveItem('Token') //删除 localStorageRemoveItemAll() //清除所有 sessionSetItem('Token', JSON.stringify(1))// 存入 JSON.parse(sessionGetItem("Token"))// 获取 sessionRemoveItem('Token') //删除 sessionRemoveItemAll() //清除所有
标签:function,储存,封装,name,功能,Token,export,cookie,data From: https://www.cnblogs.com/yugueilou/p/17318258.html