"use client"; import { useState, type FormEvent } from "react"; import { Copyable } from "../Copyable"; import styles from "../vip.module.css"; type WelcomeBlockProps = { userName: string | null; }; function getStorage(key: string): string | null { if (typeof window === "undefined") return null; return localStorage.getItem(key); } /** 匿名 user_id 格式:user + 数字 */ function isAnonymousUserId(val: string | null): boolean { return !!val && /^user\d+$/.test(val); } export function WelcomeBlock({ userName }: WelcomeBlockProps) { const displayName = userName || getStorage("userName") || "会员"; const userId = getStorage("userId"); const needLinkEmail = isAnonymousUserId(userId); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [passwordConfirm, setPasswordConfirm] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [linked, setLinked] = useState(false); const handleLinkSubmit = async (e: FormEvent) => { e.preventDefault(); setError(null); setLoading(true); try { if (password !== passwordConfirm) { setError("两次密码不一致"); setLoading(false); return; } if (password.length < 8) { setError("密码至少 8 位"); setLoading(false); return; } const res = await fetch("/api/auth/link-email", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: email.trim(), password, anonymousUserId: userId, }), }); const data = await res.json(); if (!data?.ok) { setError(data?.error || "操作失败"); setLoading(false); return; } const { pbSaveAuth } = await import("@/app/lib/pocketbase"); pbSaveAuth(data.token, { id: data.record.id, email: data.record.email }); await fetch("/api/auth/sync-session", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ token: data.token, record: data.record }), credentials: "include", }); if (typeof window !== "undefined") { localStorage.setItem("userId", data.record.id); localStorage.setItem("userName", data.record.email || data.record.id); } setLinked(true); window.location.reload(); } catch { setError("操作失败"); } finally { setLoading(false); } }; if (needLinkEmail && !linked) { return (

欢迎加入,请补充邮箱

补充邮箱后可在各站点通用登录,Memos 账号密码为 12345678

setEmail(e.target.value)} placeholder="your@email.com" required disabled={loading} style={{ width: "100%", padding: "0.75rem 1rem", fontSize: "1rem", border: "1px solid var(--border)", borderRadius: "8px", background: "var(--background)", color: "var(--foreground)", marginBottom: "0.75rem", }} /> setPassword(e.target.value)} placeholder="至少 8 位" required minLength={8} disabled={loading} style={{ width: "100%", padding: "0.75rem 1rem", fontSize: "1rem", border: "1px solid var(--border)", borderRadius: "8px", background: "var(--background)", color: "var(--foreground)", marginBottom: "0.75rem", }} /> setPasswordConfirm(e.target.value)} placeholder="再次输入密码" required minLength={8} disabled={loading} style={{ width: "100%", padding: "0.75rem 1rem", fontSize: "1rem", border: "1px solid var(--border)", borderRadius: "8px", background: "var(--background)", color: "var(--foreground)", marginBottom: "0.75rem", }} /> {error && (

{error}

)}
); } return (

欢迎回来,{displayName}

以下是您的会员资产与推荐动作

星球登录信息
网址 qun.hackrobot.cn 打开
账号 {displayName}
密码 12345678
); }