2025-07-10 19:05:38 +08:00
|
|
|
<template>
|
2025-07-15 15:16:03 +08:00
|
|
|
<div class="chat-wrap">
|
2025-07-16 18:49:28 +08:00
|
|
|
<span class="" @click="goChatIndex"> <icon-left /> 返回空间 </span>
|
2025-07-15 15:16:03 +08:00
|
|
|
<div class="chat-contain">
|
|
|
|
|
<a-layout>
|
2025-07-16 18:49:28 +08:00
|
|
|
<a-layout-sider width="15%" style="background: #fff">
|
|
|
|
|
<HistoryChat
|
|
|
|
|
v-if="cozeWebSDKConfig?.config?.bot_id"
|
|
|
|
|
:cozeInfo="cozeInfo"
|
|
|
|
|
:botId="cozeWebSDKConfig?.config?.bot_id"
|
|
|
|
|
/>
|
|
|
|
|
</a-layout-sider>
|
2025-07-15 15:16:03 +08:00
|
|
|
<a-layout>
|
|
|
|
|
<a-layout-content style="padding: 24px; background: #fff; min-height: 280px">
|
|
|
|
|
<a-card :bordered="false">
|
|
|
|
|
<div id="coze-chat-container" style="width: 100%; margin-left: 100px"></div>
|
|
|
|
|
</a-card>
|
|
|
|
|
</a-layout-content>
|
|
|
|
|
</a-layout>
|
|
|
|
|
</a-layout>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-07-10 19:05:38 +08:00
|
|
|
</template>
|
2025-07-15 15:16:03 +08:00
|
|
|
|
|
|
|
|
<script setup>
|
2025-07-17 19:32:47 +08:00
|
|
|
import { ref, onMounted, onUnmounted } 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-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
|
|
|
// 存储认证令牌
|
|
|
|
|
const authToken = ref('pat_tuIM7jubM1hLXaIWzbWg1U15lBe66AlYwu9BkXMQXInh8VdPszRFTwlTPmdziHwg');
|
|
|
|
|
|
|
|
|
|
// 模拟从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 cozeWebSDKConfig = reactive({
|
|
|
|
|
config: {},
|
|
|
|
|
auth: {},
|
|
|
|
|
userInfo: {},
|
|
|
|
|
ui: {},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const cozeInfo = reactive({
|
|
|
|
|
title: '',
|
|
|
|
|
description: '',
|
|
|
|
|
icon_url: '',
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-17 19:32:47 +08:00
|
|
|
const cozeWebSDK = null;
|
2025-07-15 15:16:03 +08:00
|
|
|
const initChat = async () => {
|
|
|
|
|
console.log(2323);
|
|
|
|
|
const { code, data } = await getChatAgent(query.id);
|
|
|
|
|
if (code != 200) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Object.assign(cozeWebSDKConfig, data.cozeConfig);
|
|
|
|
|
Object.assign(cozeInfo, data.info);
|
2025-07-16 18:49:28 +08:00
|
|
|
console.log(cozeWebSDKConfig?.config?.bot_id, 'cozeWebSDKConfig?.config?.bot_id');
|
2025-07-15 15:16:03 +08:00
|
|
|
await loadScript('https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.10/libs/cn/index.js');
|
2025-07-17 19:32:47 +08:00
|
|
|
let config = await getChatAgent(data.info.bot_id, data.info.title);
|
2025-07-16 18:49:28 +08:00
|
|
|
config.ui = editUi(data);
|
2025-07-15 15:16:03 +08:00
|
|
|
config.auth.onRefreshToken = function () {
|
|
|
|
|
return 'pat_tuIM7jubM1hLXaIWzbWg1U15lBe66AlYwu9BkXMQXInh8VdPszRFTwlTPmdziHwg';
|
|
|
|
|
};
|
2025-07-17 19:32:47 +08:00
|
|
|
cozeWebSDK = new CozeWebSDK.WebChatClient(config);
|
2025-07-15 15:16:03 +08:00
|
|
|
showChatPage();
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-17 19:32:47 +08:00
|
|
|
//扣子配置
|
|
|
|
|
const cozeWebSdkConfig = (botId, title) => {
|
|
|
|
|
let config = {
|
|
|
|
|
config: {
|
|
|
|
|
bot_id: botId,
|
|
|
|
|
},
|
|
|
|
|
ui: {
|
|
|
|
|
chatBot: {
|
|
|
|
|
el: document.getElementById('coze-chat-container'),
|
|
|
|
|
width: '70%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
title: title,
|
|
|
|
|
isNeedFunctionCallMessage: true,
|
|
|
|
|
},
|
|
|
|
|
base: {
|
|
|
|
|
icon: '',
|
|
|
|
|
zIndex: 1000,
|
|
|
|
|
},
|
|
|
|
|
header: {
|
|
|
|
|
isShow: true,
|
|
|
|
|
isNeedClose: false,
|
|
|
|
|
},
|
|
|
|
|
feedback: {
|
|
|
|
|
isNeedFeedback: true,
|
|
|
|
|
feedbackPanel: {
|
|
|
|
|
title: '您对这个回答有什么看法?请告诉我们',
|
|
|
|
|
placeholder: '请详细描述您的问题...',
|
|
|
|
|
tags: [
|
|
|
|
|
{
|
|
|
|
|
label: '信息不正确',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: '敏感涉及 信息',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
return config;
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-16 18:49:28 +08:00
|
|
|
const editUi = (config) => {
|
|
|
|
|
let uiConfig = {
|
|
|
|
|
chatBot: {
|
|
|
|
|
el: document.getElementById('coze-chat-container'),
|
|
|
|
|
width: '70%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
title: config?.info?.title,
|
|
|
|
|
isNeedFunctionCallMessage: true,
|
|
|
|
|
},
|
|
|
|
|
base: {
|
|
|
|
|
icon: '',
|
|
|
|
|
zIndex: 1000,
|
|
|
|
|
},
|
|
|
|
|
header: {
|
|
|
|
|
isShow: true,
|
|
|
|
|
isNeedClose: false,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
return uiConfig;
|
|
|
|
|
};
|
|
|
|
|
|
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>
|