storage.ts
1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { JSONStringify, JSONParse } from './utils'
/**
* * 存储本地会话数据
* @param k 键名
* @param v 键值(无需stringiiy)
* @returns RemovableRef
*/
export const setLocalStorage = <T>(k: string, v: T) => {
try {
window.localStorage.setItem(k, JSONStringify(v))
} catch (error) {
return false
}
}
/**
* * 获取本地会话数据
* @param k 键名
* @returns any
*/
export const getLocalStorage = (k: string) => {
const item = window.localStorage.getItem(k)
try {
return item ? JSONParse(item) : item
} catch (err) {
return item
}
}
/**
* * 清除本地会话数据
* @param name
*/
export const clearLocalStorage = (name: string) => {
window.localStorage.removeItem(name)
}
/**
* * 存储临时会话数据
* @param k 键名
* @param v 键值
* @returns RemovableRef
*/
export const setSessionStorage = <T>(k: string, v: T) => {
try {
window.sessionStorage.setItem(k, JSONStringify(v))
} catch (error) {
return false
}
}
/**
* * 获取临时会话数据
* @returns any
*/
export const getSessionStorage: (k: string) => any = (k: string) => {
const item = window.sessionStorage.getItem(k)
try {
return item ? JSONParse(item) : item
} catch (err) {
return item
}
}
/**
* * 清除本地会话数据
* @param name
*/
export const clearSessioStorage = (name: string) => {
window.sessionStorage.removeItem(name)
}