mirror of
https://github.com/acepanel/panel.git
synced 2026-02-08 15:24:28 +08:00
feat: 前端优化
This commit is contained in:
@@ -1,2 +1 @@
|
||||
export * from './local'
|
||||
export * from './session'
|
||||
|
||||
@@ -1,31 +1,28 @@
|
||||
import { decrypto, encrypto } from '@/utils'
|
||||
|
||||
interface StorageData {
|
||||
value: unknown
|
||||
value: any
|
||||
expire: number | null
|
||||
}
|
||||
|
||||
/** 默认缓存期限为7天 */
|
||||
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7
|
||||
/** 默认保存期限为永久 */
|
||||
const DEFAULT_CACHE_TIME = 0
|
||||
|
||||
export function setLocal(key: string, value: unknown, expire: number | null = DEFAULT_CACHE_TIME) {
|
||||
export function setLocal(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
|
||||
const storageData: StorageData = {
|
||||
value,
|
||||
expire: expire !== null ? new Date().getTime() + expire * 1000 : null
|
||||
expire: expire !== 0 && expire !== null ? new Date().getTime() + expire * 1000 : 0
|
||||
}
|
||||
const json = encrypto(storageData)
|
||||
const json = JSON.stringify(storageData)
|
||||
window.localStorage.setItem(key, json)
|
||||
}
|
||||
|
||||
export function getLocal<T>(key: string) {
|
||||
const json = window.localStorage.getItem(key)
|
||||
if (json) {
|
||||
let storageData: StorageData | null = null
|
||||
storageData = decrypto(json)
|
||||
const storageData = JSON.parse(json)
|
||||
if (storageData) {
|
||||
const { value, expire } = storageData
|
||||
// 没有过期时间或者在有效期内则直接返回
|
||||
if (expire === null || expire >= Date.now()) return value as T
|
||||
if (expire === 0 || expire >= Date.now()) return value as T
|
||||
}
|
||||
removeLocal(key)
|
||||
return null
|
||||
@@ -36,8 +33,7 @@ export function getLocal<T>(key: string) {
|
||||
export function getLocalExpire(key: string): number | null {
|
||||
const json = window.localStorage.getItem(key)
|
||||
if (json) {
|
||||
let storageData: StorageData | null = null
|
||||
storageData = decrypto(json)
|
||||
const storageData = JSON.parse(json)
|
||||
if (storageData) {
|
||||
return storageData.expire
|
||||
}
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import { decrypto, encrypto } from '@/utils'
|
||||
|
||||
export function setSession(key: string, value: unknown) {
|
||||
const json = encrypto(value)
|
||||
sessionStorage.setItem(key, json)
|
||||
}
|
||||
|
||||
export function getSession<T>(key: string) {
|
||||
const json = sessionStorage.getItem(key)
|
||||
let data: T | null = null
|
||||
if (json) {
|
||||
data = decrypto(json)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
export function removeSession(key: string) {
|
||||
window.sessionStorage.removeItem(key)
|
||||
}
|
||||
|
||||
export function clearSession() {
|
||||
window.sessionStorage.clear()
|
||||
}
|
||||
Reference in New Issue
Block a user