From 60f183b8e47a26b439fbda705d34e89416d1352a Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Wed, 10 Jun 2026 15:24:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(frontend):=20=E6=AD=A5=E9=AA=A4=206.5=20?= =?UTF-8?q?=E2=80=94=20Cookie=20=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/utils/cookie.ts | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 frontend/src/utils/cookie.ts diff --git a/frontend/src/utils/cookie.ts b/frontend/src/utils/cookie.ts new file mode 100644 index 0000000..92906b4 --- /dev/null +++ b/frontend/src/utils/cookie.ts @@ -0,0 +1,40 @@ +const COOKIE_NAME = "ai_agent_login"; +const COOKIE_DAYS = 7; + +export interface UserInfo { + user: string; + ts: number; +} + +function setCookie(name: string, value: string, days: number) { + const expires = new Date(Date.now() + days * 864e5).toUTCString(); + document.cookie = `${name}=${encodeURIComponent(value)};expires=${expires};path=/`; +} + +function getCookie(name: string): string | null { + const match = document.cookie.match(new RegExp("(^| )" + name + "=([^;]+)")); + return match ? decodeURIComponent(match[2]) : null; +} + +function deleteCookie(name: string) { + document.cookie = `${name}=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/`; +} + +export function setUserInfo(user: string) { + const info: UserInfo = { user, ts: Date.now() }; + setCookie(COOKIE_NAME, JSON.stringify(info), COOKIE_DAYS); +} + +export function getUserInfo(): UserInfo | null { + const raw = getCookie(COOKIE_NAME); + if (!raw) return null; + try { + return JSON.parse(raw) as UserInfo; + } catch { + return null; + } +} + +export function clearUserInfo() { + deleteCookie(COOKIE_NAME); +}