diff --git a/blog.config.js b/blog.config.js index 4e874cac..699e0962 100644 --- a/blog.config.js +++ b/blog.config.js @@ -191,7 +191,9 @@ const BLOG = { WEB_WHIZ_ENABLED: process.env.NEXT_PUBLIC_WEB_WHIZ_ENABLED || false, // 是否显示 WEB_WHIZ_BASE_URL: process.env.NEXT_PUBLIC_WEB_WHIZ_BASE_URL || 'https://api.webwhiz.ai', // 可以自建服务器 WEB_WHIZ_CHAT_BOT_ID: process.env.NEXT_PUBLIC_WEB_WHIZ_CHAT_BOT_ID || null, // 在后台获取ID - + DIFY_CHATBOT_ENABLED: process.env.NEXT_PUBLIC_DIFY_CHATBOT_ENABLED || false, + DIFY_CHATBOT_BASE_URL: process.env.NEXT_PUBLIC_DIFY_CHATBOT_BASE_URL || '', + DIFY_CHATBOT_TOKEN: process.env.NEXT_PUBLIC_DIFY_CHATBOT_TOKEN || '', // 悬浮挂件 WIDGET_PET: process.env.NEXT_PUBLIC_WIDGET_PET || true, // 是否显示宠物挂件 WIDGET_PET_LINK: diff --git a/components/DifyChatbot.js b/components/DifyChatbot.js new file mode 100644 index 00000000..c954a872 --- /dev/null +++ b/components/DifyChatbot.js @@ -0,0 +1,32 @@ +import { useEffect } from 'react'; +import { siteConfig } from '@/lib/config'; + +export default function DifyChatbot() { + useEffect(() => { + // 这里使用 siteConfig() 函数调用来获取配置值 + if (!siteConfig('DIFY_CHATBOT_ENABLED')) { + return; + } + + // 配置 DifyChatbot,同样需要调用 siteConfig() 获取相应的配置值 + window.difyChatbotConfig = { + token: siteConfig('DIFY_CHATBOT_TOKEN'), + baseUrl: siteConfig('DIFY_CHATBOT_BASE_URL') + }; + + // 加载 DifyChatbot 脚本 + const script = document.createElement('script'); + script.src = `${siteConfig('DIFY_CHATBOT_BASE_URL')}/embed.min.js`; // 注意调用 siteConfig() + script.id = siteConfig('DIFY_CHATBOT_TOKEN'); // 注意调用 siteConfig() + script.defer = true; + document.body.appendChild(script); + + return () => { + // 在组件卸载时清理 script 标签 + const existingScript = document.getElementById(siteConfig('DIFY_CHATBOT_TOKEN')); // 注意调用 siteConfig() + if (existingScript) document.body.removeChild(existingScript); + }; + }, []); // 注意依赖数组为空,意味着脚本将仅在加载页面时执行一次 + + return null; +} diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js index 260e66b8..bbfba4f3 100644 --- a/components/ExternalPlugins.js +++ b/components/ExternalPlugins.js @@ -12,6 +12,7 @@ const FlutteringRibbon = dynamic(() => import('@/components/FlutteringRibbon'), const Ribbon = dynamic(() => import('@/components/Ribbon'), { ssr: false }) const Sakura = dynamic(() => import('@/components/Sakura'), { ssr: false }) const StarrySky = dynamic(() => import('@/components/StarrySky'), { ssr: false }) +const DifyChatbot = dynamic(() => import('@/components/DifyChatbot'), { ssr: false }); const Analytics = dynamic(() => import('@vercel/analytics/react').then(async (m) => { return m.Analytics }), { ssr: false }) const MusicPlayer = dynamic(() => import('@/components/Player'), { ssr: false }) const Ackee = dynamic(() => import('@/components/Ackee'), { ssr: false }) @@ -69,6 +70,7 @@ const ExternalPlugin = (props) => { const MATOMO_SITE_ID = siteConfig('MATOMO_SITE_ID') const ANALYTICS_51LA_ID = siteConfig('ANALYTICS_51LA_ID') const ANALYTICS_51LA_CK = siteConfig('ANALYTICS_51LA_CK') + const DIFY_CHATBOT_ENABLED = siteConfig('DIFY_CHATBOT_ENABLED') if (DISABLE_PLUGIN) { return null @@ -91,6 +93,7 @@ const ExternalPlugin = (props) => { {FLUTTERINGRIBBON && } {COMMENT_TWIKOO_COUNT_ENABLE && } {RIBBON && } + {DIFY_CHATBOT_ENABLED && } {CUSTOM_RIGHT_CLICK_CONTEXT_MENU && } {!CAN_COPY && } {WEB_WHIZ_ENABLED && }