/** 设置缓存 **/
export function setLocalStorage(key: string, data: string | object | any) {
let value: string = ''
value = ['object'].includes(typeof data) ? JSON.stringify(data) : (data as string)
if (key == 'userInfo') {
const users = JSON.stringify({
companyId: data?.companyId,
token: data?.token,
companyName: data?.companyName,
companyLogo: data?.companyLogo,
serviceType: data?.serviceType,
})
setCookie('token', data?.token, 2)
setCookie('userInfo', encodeURIComponent(users), 2, import.meta.env.VITE_DOMAIN)
}
window.localStorage.setItem(key, value)
}
/** 获取缓存 **/
export function getLocalStorage(key: string) {
try {
return JSON.parse(window.localStorage.getItem(key) as string)
} catch (error) {
return window.localStorage.getItem(key) as any
}
}
/** 删除缓存 **/
export function removeLocalStorage(key: string) {
if (key == 'userInfo') {
clearAllCookie()
}
window.localStorage.removeItem(key)
}
/** 删除全部缓存 **/
export function removeAllLocalStorage() {
clearAllCookie()
window.localStorage.clear()
}
js封装函数-缓存操作
最新推荐文章于 2024-08-25 09:57:22 发布