-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcookie.ts
35 lines (33 loc) · 1.12 KB
/
cookie.ts
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
/**
* 写cookies
* @param {string} name 写cookie的key
* @param {string|number} value 写cookie的值
* @param {number} day 存储的时间,默认30天
*/
export const setCookie = (name: string, value: string | number, day: number = 30): void => {
const exp = new Date();
exp.setTime(exp.getTime() + day * 24 * 60 * 60 * 1000);
document.cookie = name + '=' + escape(value + '') + ';path=/;expires=' + exp.toUTCString();
};
/**
* 读取cookies
* @param {string} name 要获取的cookie名称
* @param {number|boolean} type 是否直接获取对应的值,若存入真值,则直接返回,否则进行解码
*/
export const getCookie = (name: string, type: number | boolean = 0): string | null => {
const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
const arr = document.cookie.match(reg);
if (arr) {
return type ? arr[2] : unescape(arr[2]);
}
return null;
};
/**
* 删除cookie
* @param name 删除的cookie名称
*/
export const delCookie = (name: string) => {
const ex: Date = new Date();
ex.setTime(ex.getTime() - 1);
document.cookie = name + '=; expires=' + ex.toUTCString() + ';path=/';
};