2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-08 15:24:28 +08:00

feat: 前端优化

This commit is contained in:
耗子
2024-10-19 18:01:18 +08:00
parent 2d10110eef
commit 93fc5e08f6
21 changed files with 856 additions and 206 deletions

View File

@@ -1,2 +1 @@
export * from './local'
export * from './session'

View File

@@ -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
}

View File

@@ -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()
}