-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcookie.js
48 lines (43 loc) · 1005 Bytes
/
cookie.js
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
export function formatCookie(cookies = ''){
let _cookies = {};
let re = new RegExp(/([\w\-.]*)=([^\s=]+);/g)
let res = null
while(res = re.exec(cookies)){
if(res[1] !== 'path'){
_cookies[res[1]] = res[2]
}
}
return _cookies
};
export function setCookie(cookies){
const oldCookies = getCookie() || {}
if(typeof cookies == 'string'){
cookies = formatCookie(cookies)
}
uni.setStorageSync('wx-cookie', Object.assign(oldCookies, cookies));
}
export function getCookieStr(){
const cookies = getCookie() || {}
let str = ''
for(let key in cookies){
str += key+'='+cookies[key]+'; '
}
return str
}
export function getCookie(key = ''){
const cookies = uni.getStorageSync('wx-cookie');
if(cookies && key){
return cookies[key]
}else{
return cookies
}
}
export function clearCookie(key = null){
uni.removeStorageSync('wx-cookie')
}
export function removeCookie(key){
let cookies = getCookie()
delete cookies[key]
uni.setStorageSync('wx-cookie', cookies);
return true
}