mirror of
https://github.com/sendevia/website.git
synced 2026-03-06 15:42:34 +08:00
feat: add cookie utility functions for client-side cookie management
This commit is contained in:
50
.vitepress/theme/utils/cookie.ts
Normal file
50
.vitepress/theme/utils/cookie.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 设置 Cookie
|
||||
* @param name - Cookie 名称
|
||||
* @param value - Cookie 值
|
||||
* @param days - 过期天数(可选,默认 365 天)
|
||||
*/
|
||||
export function setCookie(name: string, value: string, days: number = 365): void {
|
||||
if (typeof document === "undefined") return;
|
||||
|
||||
let expires = "";
|
||||
if (days) {
|
||||
const date = new Date();
|
||||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||
expires = "; expires=" + date.toUTCString();
|
||||
}
|
||||
|
||||
document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Cookie
|
||||
* @param name - Cookie 名称
|
||||
* @returns Cookie 值,如果不存在则返回 null
|
||||
*/
|
||||
export function getCookie(name: string): string | null {
|
||||
if (typeof document === "undefined") return null;
|
||||
|
||||
const nameEQ = name + "=";
|
||||
const ca = document.cookie.split(";");
|
||||
|
||||
for (let i = 0; i < ca.length; i++) {
|
||||
let c = ca[i];
|
||||
while (c.charAt(0) === " ") {
|
||||
c = c.substring(1, c.length);
|
||||
}
|
||||
if (c.indexOf(nameEQ) === 0) {
|
||||
return decodeURIComponent(c.substring(nameEQ.length, c.length));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 Cookie
|
||||
* @param name - Cookie 名称
|
||||
*/
|
||||
export function deleteCookie(name: string): void {
|
||||
setCookie(name, "", -1);
|
||||
}
|
||||
Reference in New Issue
Block a user