import Cookies from 'js-cookie' import router from '@/router' const TokenKey = 'token' export function getToken() { return Cookies.get(TokenKey) ?? '' } export function setToken(token: string) { return Cookies.set(TokenKey, token) } export function removeToken() { return Cookies.remove(TokenKey) } export function setCookie(key:any,val: string) { return Cookies.set(key, val) } export function getCookie(key:any) { return Cookies.get(key) ?? '' } export function removeCookie(key) { return Cookies.remove(key) } export function setSession(key: any, value: any) { // return Cookies.set(key, value) window.sessionStorage.setItem(key, JSON.stringify({ data: value, time: new Date().getTime() })) } export function getSession(key, exp = 24*60*60*1000) { // 获取数据 let data = window.sessionStorage.getItem(key) if (!data) return null let dataObj = JSON.parse(data) console.log('exp', new Date().getTime() - dataObj.time > exp) // 与过期时间比较 if (new Date().getTime() - dataObj.time > exp) { // 过期删除返回null removeSession(key) return null } else { return dataObj.data } } export function removeSession(key: any) { window.sessionStorage.removeItem(key) } export function setStorage (key, value) { window.localStorage.setItem(key, JSON.stringify({ data: value, time: new Date().getTime() })) } export function getStorage (key, exp = 86400000) { // 获取数据 let data = window.localStorage.getItem(key) if (!data) return null let dataObj = JSON.parse(data) // 与过期时间比较 if (new Date().getTime() - dataObj.time > exp) { // 过期删除返回null removeStorage(key) router.push('/login') return null } else { return dataObj.data } } export function removeStorage (key) { window.localStorage.removeItem(key) }
标签:function,Cookies,return,Storage,Session,export,key,data From: https://www.cnblogs.com/gqx-html/p/18687416