2025-07-10 19:05:38 +08:00
|
|
|
|
<template>
|
2025-07-15 15:16:03 +08:00
|
|
|
|
<div class="chat-wrap">
|
2025-07-28 16:08:02 +08:00
|
|
|
|
<span @click="goChatIndex"> <icon-left /> 返回空间 </span>
|
2025-07-15 15:16:03 +08:00
|
|
|
|
<div class="chat-contain">
|
2025-07-28 16:08:02 +08:00
|
|
|
|
<!-- 使用 arco-design 栅格布局 -->
|
|
|
|
|
|
<a-row :gutter="24" class="chat-grid">
|
|
|
|
|
|
<!-- 左侧 HistoryChat -->
|
|
|
|
|
|
<a-col :xs="isCollapsed ? 0 : 24"
|
|
|
|
|
|
:sm="isCollapsed ? 0 : 12"
|
|
|
|
|
|
:md="isCollapsed ? 0 : 8"
|
|
|
|
|
|
:lg="isCollapsed ? 0 : 6"
|
|
|
|
|
|
:xl="isCollapsed ? 0 : 7"
|
|
|
|
|
|
:xxl="isCollapsed ? 0 : 4"
|
|
|
|
|
|
class="history-chat-col">
|
|
|
|
|
|
<HistoryChat v-if="cozeInfo?.bot_id && !isCollapsed" :cozeInfo="cozeInfo" />
|
|
|
|
|
|
</a-col>
|
2025-07-28 19:59:54 +08:00
|
|
|
|
|
2025-07-28 16:08:02 +08:00
|
|
|
|
<!-- 右侧聊天内容 -->
|
|
|
|
|
|
<a-col :xs="24"
|
|
|
|
|
|
:sm="isCollapsed ? 24 : 12"
|
|
|
|
|
|
:md="isCollapsed ? 24 : 16"
|
|
|
|
|
|
:lg="isCollapsed ? 24 : 18"
|
2025-07-29 09:52:54 +08:00
|
|
|
|
:xl="isCollapsed ? 24 : 16"
|
|
|
|
|
|
:xxl="isCollapsed ? 24 : 16"
|
|
|
|
|
|
class="chat-content-col">
|
2025-07-28 16:08:02 +08:00
|
|
|
|
|
2025-07-28 19:59:54 +08:00
|
|
|
|
<a-card :bordered="false">
|
|
|
|
|
|
<div class="chat-content-col" style="min-height: fit-content;">
|
|
|
|
|
|
<div class="toggle-btn" @click="toggleCollapse">
|
|
|
|
|
|
<a-tooltip :content="isCollapsed ? '展开' : '折叠'">
|
|
|
|
|
|
<img class="status-icon" :src="isCollapsed ? menuUnfold : menuFold" />
|
|
|
|
|
|
</a-tooltip>
|
|
|
|
|
|
</div>
|
2025-07-29 09:52:54 +08:00
|
|
|
|
<div id="coze-chat-container" ></div>
|
2025-07-28 19:59:54 +08:00
|
|
|
|
</div>
|
2025-07-28 16:08:02 +08:00
|
|
|
|
</a-card>
|
|
|
|
|
|
</a-col>
|
|
|
|
|
|
</a-row>
|
2025-07-15 15:16:03 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-07-10 19:05:38 +08:00
|
|
|
|
</template>
|
2025-07-15 15:16:03 +08:00
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2025-07-18 10:11:13 +08:00
|
|
|
|
import { ref, onMounted } from 'vue';
|
2025-07-15 15:16:03 +08:00
|
|
|
|
import { getChatAgent } from '@/api/all/agent';
|
|
|
|
|
|
import HistoryChat from './components/HistoryChat.vue';
|
2025-07-16 18:49:28 +08:00
|
|
|
|
import { useRouter } from 'vue-router';
|
2025-07-28 16:08:02 +08:00
|
|
|
|
import menuFold from '@/assets/svg/menu-fold.svg';
|
|
|
|
|
|
import menuUnfold from '@/assets/svg/menu-unfold.svg';
|
2025-07-17 19:32:47 +08:00
|
|
|
|
|
2025-07-16 18:49:28 +08:00
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
2025-07-15 15:16:03 +08:00
|
|
|
|
// 存储认证令牌
|
2025-07-24 19:07:46 +08:00
|
|
|
|
const authToken = ref('');
|
2025-07-28 16:08:02 +08:00
|
|
|
|
const isCollapsed = ref(false);
|
|
|
|
|
|
|
|
|
|
|
|
// 切换折叠状态
|
|
|
|
|
|
const toggleCollapse = () => {
|
|
|
|
|
|
isCollapsed.value = !isCollapsed.value;
|
|
|
|
|
|
};
|
2025-07-15 15:16:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 模拟从API获取token
|
|
|
|
|
|
const fetchToken = async () => {
|
|
|
|
|
|
// 实际开发中应替换为真实的 API 请求
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
resolve('pat_' + Math.random().toString(36).substring(2, 15));
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 刷新token
|
|
|
|
|
|
const refreshToken = async () => {
|
|
|
|
|
|
authToken.value = await fetchToken();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-16 18:49:28 +08:00
|
|
|
|
const goChatIndex = async () => {
|
|
|
|
|
|
router.push({
|
|
|
|
|
|
path: '/agent/index',
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-15 15:16:03 +08:00
|
|
|
|
// 动态加载脚本函数
|
|
|
|
|
|
const loadScript = (src) =>
|
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
|
const script = document.createElement('script');
|
|
|
|
|
|
script.src = src;
|
|
|
|
|
|
script.onload = resolve;
|
|
|
|
|
|
script.onerror = reject;
|
|
|
|
|
|
document.head.appendChild(script);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const route = useRoute();
|
2025-07-16 18:49:28 +08:00
|
|
|
|
const id = route.query.id;
|
2025-07-15 15:16:03 +08:00
|
|
|
|
const query = reactive({
|
|
|
|
|
|
id: id,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const cozeInfo = reactive({
|
|
|
|
|
|
title: '',
|
|
|
|
|
|
description: '',
|
|
|
|
|
|
icon_url: '',
|
2025-07-18 10:11:13 +08:00
|
|
|
|
bot_id: '',
|
|
|
|
|
|
auth: {},
|
2025-07-15 15:16:03 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-07-18 10:11:13 +08:00
|
|
|
|
let cozeWebSDK = null;
|
2025-07-15 15:16:03 +08:00
|
|
|
|
const initChat = async () => {
|
|
|
|
|
|
const { code, data } = await getChatAgent(query.id);
|
|
|
|
|
|
if (code != 200) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-18 10:11:13 +08:00
|
|
|
|
|
2025-07-15 15:16:03 +08:00
|
|
|
|
Object.assign(cozeInfo, data.info);
|
2025-07-28 19:59:54 +08:00
|
|
|
|
await loadScript('https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.17/libs/cn/index.js');
|
2025-07-28 13:47:40 +08:00
|
|
|
|
let cozeConfig = await cozeWebSdkConfig(data.info.bot_id, data.info.name, data.info.auth, data.info.user_info);
|
2025-07-18 10:11:13 +08:00
|
|
|
|
cozeWebSDK = cozeWebSDK = new CozeWebSDK.WebChatClient(cozeConfig);
|
2025-07-15 15:16:03 +08:00
|
|
|
|
showChatPage();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-28 13:47:40 +08:00
|
|
|
|
const cozeWebSdkConfig = (botId, name, auth, userInfo) => {
|
2025-07-18 10:11:13 +08:00
|
|
|
|
auth.onRefreshToken = function () {
|
2025-07-24 19:07:46 +08:00
|
|
|
|
return '';
|
2025-07-18 10:11:13 +08:00
|
|
|
|
};
|
2025-07-17 19:32:47 +08:00
|
|
|
|
let config = {
|
|
|
|
|
|
config: {
|
2025-07-18 10:11:13 +08:00
|
|
|
|
botId: botId,
|
2025-07-17 19:32:47 +08:00
|
|
|
|
},
|
|
|
|
|
|
ui: {
|
|
|
|
|
|
chatBot: {
|
|
|
|
|
|
el: document.getElementById('coze-chat-container'),
|
2025-07-28 19:59:54 +08:00
|
|
|
|
width: '95%',
|
2025-07-17 19:32:47 +08:00
|
|
|
|
height: '100%',
|
2025-07-18 10:11:13 +08:00
|
|
|
|
title: name,
|
2025-07-17 19:32:47 +08:00
|
|
|
|
isNeedFunctionCallMessage: true,
|
|
|
|
|
|
},
|
2025-07-25 14:49:38 +08:00
|
|
|
|
base: {
|
|
|
|
|
|
icon: '',
|
|
|
|
|
|
zIndex: 1000,
|
2025-07-28 19:59:54 +08:00
|
|
|
|
lang:'zh-CN'
|
2025-07-25 14:49:38 +08:00
|
|
|
|
},
|
2025-07-28 13:47:40 +08:00
|
|
|
|
footer: {
|
|
|
|
|
|
expressionText: '内容由AI生成,无法确保真实准确,仅供参考。',
|
2025-07-24 19:07:46 +08:00
|
|
|
|
},
|
2025-07-28 19:59:54 +08:00
|
|
|
|
header: {
|
|
|
|
|
|
isShow: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
conversations: {
|
|
|
|
|
|
isNeed: true,
|
|
|
|
|
|
isNeedAddNewConversationBoolean: true,
|
|
|
|
|
|
isNeedQuoteBoolean: true,
|
|
|
|
|
|
}
|
2025-07-16 18:49:28 +08:00
|
|
|
|
},
|
2025-07-18 10:11:13 +08:00
|
|
|
|
auth: auth,
|
2025-07-28 13:47:40 +08:00
|
|
|
|
userInfo: userInfo,
|
2025-07-16 18:49:28 +08:00
|
|
|
|
header: {
|
|
|
|
|
|
isShow: true,
|
2025-07-29 09:55:58 +08:00
|
|
|
|
isNeedClose: false,
|
2025-07-16 18:49:28 +08:00
|
|
|
|
},
|
|
|
|
|
|
};
|
2025-07-18 10:11:13 +08:00
|
|
|
|
return config;
|
2025-07-16 18:49:28 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-07-15 15:16:03 +08:00
|
|
|
|
const showChatPage = () => {
|
|
|
|
|
|
cozeWebSDK.showChatBot();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
initChat();
|
|
|
|
|
|
});
|
2025-07-17 19:32:47 +08:00
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
|
cozeWebSDK.destroy();
|
|
|
|
|
|
});
|
2025-07-15 15:16:03 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
|
|
@import './style.scss';
|
|
|
|
|
|
</style>
|