mirror of
https://github.com/2930134478/AI-CS.git
synced 2026-06-15 00:44:30 +08:00
修复bug
This commit is contained in:
@@ -4,10 +4,11 @@ import { useEffect, useState, useSyncExternalStore } from "react";
|
||||
import { ChatWidget } from "@/components/visitor/ChatWidget";
|
||||
import { FloatingButton } from "@/components/visitor/FloatingButton";
|
||||
import { isChatEmbedMode } from "@/lib/chat-embed";
|
||||
import { getOrCreateVisitorId } from "@/lib/visitor-id";
|
||||
|
||||
/**
|
||||
* 访客聊天页面
|
||||
* - 独立打开:右下角浮动按钮 + 聊天小窗
|
||||
* - 独立打开:默认展开聊天小窗(仍保留右下角浮动按钮)
|
||||
* - iframe / ?embed=1:直接铺满,供宿主站点一次点击打开 iframe 即可使用
|
||||
*/
|
||||
export default function ChatPage() {
|
||||
@@ -19,17 +20,16 @@ export default function ChatPage() {
|
||||
() => false
|
||||
);
|
||||
|
||||
// 初始化访客 ID(使用 localStorage 保持连续性)
|
||||
useEffect(() => {
|
||||
let stored = window.localStorage.getItem("visitor_id");
|
||||
if (!stored) {
|
||||
stored = `${Date.now()}${Math.floor(Math.random() * 100000)}`;
|
||||
window.localStorage.setItem("visitor_id", stored);
|
||||
}
|
||||
const parsed = Number.parseInt(stored, 10);
|
||||
setVisitorId(Number.isNaN(parsed) ? null : parsed);
|
||||
setVisitorId(getOrCreateVisitorId());
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!embedded && visitorId !== null) {
|
||||
setIsOpen(true);
|
||||
}
|
||||
}, [embedded, visitorId]);
|
||||
|
||||
const handleToggle = () => {
|
||||
setIsOpen((prev) => !prev);
|
||||
};
|
||||
|
||||
@@ -5,10 +5,15 @@ import { Github, Mail, MessageSquare, Users } from "lucide-react";
|
||||
import { websiteConfig } from "@/lib/website-config";
|
||||
import { useI18n } from "@/lib/i18n/provider";
|
||||
|
||||
interface FooterProps {
|
||||
/** 首页打开右下角客服小窗;不传则回退为链到首页并带 openChat 参数 */
|
||||
onOpenChat?: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 官网底部页脚
|
||||
*/
|
||||
export function Footer() {
|
||||
export function Footer({ onOpenChat }: FooterProps) {
|
||||
const { t } = useI18n();
|
||||
|
||||
return (
|
||||
@@ -145,10 +150,20 @@ export function Footer() {
|
||||
</a>
|
||||
</li>
|
||||
<li className="flex items-center space-x-2 text-muted-foreground">
|
||||
<MessageSquare className="h-4 w-4" />
|
||||
<Link href="/chat" className="transition-colors hover:text-foreground">
|
||||
<MessageSquare className="h-4 w-4 shrink-0" />
|
||||
{onOpenChat ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onOpenChat}
|
||||
className="text-left transition-colors hover:text-foreground"
|
||||
>
|
||||
{t("footer.onlineChat")}
|
||||
</button>
|
||||
) : (
|
||||
<Link href="/?openChat=1" className="transition-colors hover:text-foreground">
|
||||
{t("footer.onlineChat")}
|
||||
</Link>
|
||||
)}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
@@ -28,6 +28,7 @@ import { Header } from "@/components/layout/Header";
|
||||
import { Footer } from "@/components/layout/Footer";
|
||||
import { FadeIn, FadeInStagger, FadeInItem } from "@/components/ui/fade-in";
|
||||
import { websiteConfig } from "@/lib/website-config";
|
||||
import { getOrCreateVisitorId } from "@/lib/visitor-id";
|
||||
import { stats } from "@/lib/stats-config";
|
||||
import type { I18nKey } from "@/lib/i18n/dict";
|
||||
import { useI18n } from "@/lib/i18n/provider";
|
||||
@@ -149,15 +150,20 @@ export function HomePageClient() {
|
||||
const [activeScreenshot, setActiveScreenshot] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
let stored = window.localStorage.getItem("visitor_id");
|
||||
if (!stored) {
|
||||
stored = `${Date.now()}${Math.floor(Math.random() * 100000)}`;
|
||||
window.localStorage.setItem("visitor_id", stored);
|
||||
}
|
||||
const parsed = Number.parseInt(stored, 10);
|
||||
setVisitorId(Number.isNaN(parsed) ? null : parsed);
|
||||
setVisitorId(getOrCreateVisitorId());
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
if (params.get("openChat") === "1") {
|
||||
handleOpenChat();
|
||||
params.delete("openChat");
|
||||
const next = `${window.location.pathname}${params.toString() ? `?${params}` : ""}`;
|
||||
window.history.replaceState(null, "", next);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- 仅响应 URL 参数
|
||||
}, [visitorId]);
|
||||
|
||||
const handleToggleChat = () => setIsChatOpen((prev) => !prev);
|
||||
|
||||
const handleOpenChat = () => {
|
||||
@@ -469,7 +475,7 @@ export function HomePageClient() {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<Footer />
|
||||
<Footer onOpenChat={handleOpenChat} />
|
||||
|
||||
{visitorId !== null && (
|
||||
<>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* 访客 ID:写入 localStorage,保证为可安全表示的正整数。
|
||||
* 旧版 `${Date.now()}${random}` 拼接过长会导致精度问题,此处会迁移为新格式。
|
||||
*/
|
||||
export function getOrCreateVisitorId(): number {
|
||||
const stored = window.localStorage.getItem("visitor_id");
|
||||
if (stored) {
|
||||
const parsed = Number.parseInt(stored, 10);
|
||||
if (!Number.isNaN(parsed) && parsed > 0) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
const id = Date.now() * 1000 + Math.floor(Math.random() * 100000);
|
||||
window.localStorage.setItem("visitor_id", String(id));
|
||||
return id;
|
||||
}
|
||||
Reference in New Issue
Block a user