2025-07-10 19:05:38 +08:00
|
|
|
<template>
|
2025-07-11 18:30:44 +08:00
|
|
|
<!-- -->
|
|
|
|
|
<div>
|
|
|
|
|
<a-row class="grid-demo">
|
|
|
|
|
<a-col :span="24">
|
|
|
|
|
<div>24 - 100%</div>
|
|
|
|
|
</a-col>
|
|
|
|
|
</a-row>
|
|
|
|
|
|
|
|
|
|
<a-row class="grid-demo">
|
|
|
|
|
<a-col :span="4">
|
|
|
|
|
<a-space direction="vertical">
|
|
|
|
|
<a-space class="w-240px">1</a-space>
|
|
|
|
|
<a-space>2</a-space>
|
|
|
|
|
|
|
|
|
|
</a-space>
|
|
|
|
|
</a-col>
|
|
|
|
|
<a-col :span="20">
|
|
|
|
|
<div>
|
|
|
|
|
<div id="coze-chat-container"></div>
|
|
|
|
|
</div>
|
|
|
|
|
</a-col>
|
|
|
|
|
</a-row>
|
2025-07-10 19:05:38 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
|
|
|
|
import { ref, onMounted } from 'vue';
|
2025-07-11 14:40:19 +08:00
|
|
|
import { getChatAgent } from '@/api/all/agent';
|
2025-07-10 19:05:38 +08:00
|
|
|
|
2025-07-11 14:40:19 +08:00
|
|
|
// 存储认证令牌
|
|
|
|
|
const authToken = ref('pat_tuIM7jubM1hLXaIWzbWg1U15lBe66AlYwu9BkXMQXInh8VdPszRFTwlTPmdziHwg');
|
2025-07-10 19:05:38 +08:00
|
|
|
|
2025-07-11 14:40:19 +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-10 19:05:38 +08:00
|
|
|
|
2025-07-11 14:40:19 +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);
|
|
|
|
|
});
|
2025-07-10 19:05:38 +08:00
|
|
|
|
2025-07-11 14:40:19 +08:00
|
|
|
const query = reactive({
|
2025-07-11 18:30:44 +08:00
|
|
|
id: 1,
|
2025-07-11 14:40:19 +08:00
|
|
|
});
|
2025-07-10 19:05:38 +08:00
|
|
|
|
2025-07-11 14:40:19 +08:00
|
|
|
let cozeWebSDKConfig = reactive({
|
|
|
|
|
config: {},
|
|
|
|
|
auth: {},
|
|
|
|
|
userInfo: {},
|
|
|
|
|
ui: {},
|
|
|
|
|
});
|
2025-07-11 18:30:44 +08:00
|
|
|
let cozeWebSDK = null;
|
2025-07-11 14:40:19 +08:00
|
|
|
const initChat = async () => {
|
|
|
|
|
const { code, data } = await getChatAgent(query.id);
|
2025-07-11 18:30:44 +08:00
|
|
|
if (code != 200) {
|
|
|
|
|
return false;
|
2025-07-11 14:40:19 +08:00
|
|
|
}
|
2025-07-11 18:30:44 +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');
|
|
|
|
|
let config = data;
|
|
|
|
|
config.ui.chatBot.el = document.getElementById('coze-chat-container');
|
|
|
|
|
config.ui.width = '900px';
|
|
|
|
|
config.auth.onRefreshToken = function () {
|
|
|
|
|
return 'pat_tuIM7jubM1hLXaIWzbWg1U15lBe66AlYwu9BkXMQXInh8VdPszRFTwlTPmdziHwg';
|
|
|
|
|
};
|
|
|
|
|
cozeWebSDK = cozeWebSDK = new CozeWebSDK.WebChatClient(config);
|
|
|
|
|
showChatPage();
|
2025-07-10 19:05:38 +08:00
|
|
|
};
|
2025-07-11 14:40:19 +08:00
|
|
|
|
|
|
|
|
const showChatPage = () => {
|
|
|
|
|
// 显示聊天页面
|
|
|
|
|
console.log(cozeWebSDK, 'chatClient');
|
|
|
|
|
cozeWebSDK.showChatBot();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
initChat();
|
|
|
|
|
});
|
2025-07-10 19:05:38 +08:00
|
|
|
</script>
|
|
|
|
|
|
2025-07-11 14:40:19 +08:00
|
|
|
<style scoped>
|
2025-07-10 19:05:38 +08:00
|
|
|
.app-container {
|
|
|
|
|
max-width: 800px;
|
|
|
|
|
margin: 0 auto;
|
|
|
|
|
padding: 20px;
|
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.refresh-btn {
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
padding: 10px 20px;
|
|
|
|
|
background: #2ecc71;
|
|
|
|
|
color: white;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
transition: background 0.3s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.refresh-btn:hover {
|
|
|
|
|
background: #27ae60;
|
|
|
|
|
}
|
|
|
|
|
</style>
|