From 60f92fd9afc5eb34998f248c51403129d3313d46 Mon Sep 17 00:00:00 2001
From: 18586361686 <1207669650@qq.com>
Date: 星期二, 27 五月 2025 21:42:40 +0800
Subject: [PATCH] feat: iframe 匿名用户访问功能 docs: 匿名用户访问 iframe 文档

---
 aiflowy-ui-react/src/pages/ExternalBot.tsx |   66 +++++++++++++++++++++++++--------
 1 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/aiflowy-ui-react/src/pages/ExternalBot.tsx b/aiflowy-ui-react/src/pages/ExternalBot.tsx
index 4876863..2903204 100644
--- a/aiflowy-ui-react/src/pages/ExternalBot.tsx
+++ b/aiflowy-ui-react/src/pages/ExternalBot.tsx
@@ -2,7 +2,7 @@
     Conversations, ConversationsProps,
 } from '@ant-design/x';
 import { createStyles } from 'antd-style';
-import React, { useEffect, useState } from 'react';
+import React, {useEffect, useRef, useState} from 'react';
 import {
     DeleteOutlined,
     EditOutlined, ExclamationCircleFilled,
@@ -93,10 +93,37 @@
 
 export const ExternalBot: React.FC = () => {
     const urlParams = new URLSearchParams(location.search);
-    const token = urlParams.get('authKey');
-    if (token) {
-        localStorage.setItem('authKey', token);
-    }
+    const [isExternalIFrame, setIsExternalIFrame] = useState<boolean>(false);
+    const isExternalIFrameRef = useRef(isExternalIFrame);
+   const {doGet: doGetCreateToken} =  useGetManual('/api/temp-token/create')
+    useEffect(() => {
+        isExternalIFrameRef.current = isExternalIFrame;
+    }, [isExternalIFrame]);
+    useEffect(() => {
+        const isFrame = urlParams.get('isIframe');
+        const token = urlParams.get('authKey');
+        if (isFrame) {
+            const newValue = true;
+            setIsExternalIFrame(newValue);
+            isExternalIFrameRef.current = newValue; // 鎵嬪姩鍚屾 ref
+            doGetCreateToken().then((res: any) => {
+                if (res.data.errorCode === 0){
+                    localStorage.setItem('authKey', res.data.data);
+                    const link = document.querySelector("link[rel*='icon']") as HTMLLinkElement;
+                    doGetBotInfo({params: {id: params?.id}}).then((r: any) => {
+                        if (link) {
+                            link.href = r?.data?.data?.icon || '/favicon.png';
+                        }
+                        document.title = r?.data?.data?.title;
+                    });
+                }
+            });
+        } else if (token) {
+                localStorage.setItem('authKey', token);
+        }
+
+
+    }, []); // 绌轰緷璧栨暟缁勮〃绀哄彧鍦ㄧ粍浠舵寕杞芥椂鎵ц涓�娆�
     const [newTitle, setNewTitle] = useState<string>('');
 
     // ==================== Style ====================
@@ -171,17 +198,24 @@
         }
         return [];
     };
-    useEffect(() => {
-        if (chats.length === 2 && chats[1].content.length < 1){
-            getConversationManualGet({
-                params:  { "botId": params?.id }
-            }).then((r: any) => {
-                setConversationsItems(getConversations(r?.data?.data?.cons));
-            });
-        }
-    }, [chats]);
+     useEffect(() => {
+         if (isExternalIFrameRef.current) {
+             return;
+         }
+            if (chats.length === 2 && chats[1].content.length < 1){
+                getConversationManualGet({
+                    params:  { "botId": params?.id }
+                }).then((r: any) => {
+                    setConversationsItems(getConversations(r?.data?.data?.cons));
+                });
+            }
+        }, [chats])
+
 
     useEffect(() => {
+        if (isExternalIFrameRef.current) {
+            return;
+        }
         const link = document.querySelector("link[rel*='icon']") as HTMLLinkElement;
         doGetBotInfo({params: {id: params?.id}}).then((r: any) => {
             if (link) {
@@ -199,7 +233,7 @@
             setActiveKey(getExternalSessionId());
             setConversationsItems(getConversations(r?.data?.data?.cons));
         });
-    }, []);
+    }, [])
 
     const onAddConversation = () => {
         setNewExternalSessionId();
@@ -271,7 +305,7 @@
     };
     // ==================== Render ====================
     return (
-        <div className={styles.layout}>
+       <div className={styles.layout}>
             <Modal
                 title="淇敼浼氳瘽鍚嶇О"
                 open={open}

--
Gitblit v1.8.0