refactor(Agent/Chat): 使用 cozeInfo 替代 botId 获取聊天记录
- 移除了 HistoryChat 组件中的 botId属性 - 使用 cozeInfo.bot_id 替代 botId 获取历史聊天数据
This commit is contained in:
89
src/views/agent/chat/components/HistoryChat.vue
Normal file
89
src/views/agent/chat/components/HistoryChat.vue
Normal file
@ -0,0 +1,89 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="logo">
|
||||
<img :src="cozeInfo?.icon_url" class="agent-img" />
|
||||
</div>
|
||||
|
||||
<a-menu mode="inline" theme="light">
|
||||
<a-menu-item key="1">
|
||||
<span class="menu-title">{{ cozeInfo.name }}</span>
|
||||
<span style="color: #8492ff; font-size: 12px">{{ cozeInfo.type == 1 ? '智能体' : '对话式' }}</span>
|
||||
<span style="float: right">{{ cozeInfo.views }}次使用</span>
|
||||
<p>{{ cozeInfo.description }}</p>
|
||||
</a-menu-item>
|
||||
|
||||
<div v-for="(item, index) in conversations" :key="index">
|
||||
<a-dropdown-button>
|
||||
<span> {{ item.content }} </span>
|
||||
<template #content>
|
||||
<a-doption>重命名</a-doption>
|
||||
<a-popconfirm
|
||||
content="确认删除对话吗?删除后,聊天记录将不可恢复。"
|
||||
@ok="delMessage(item.chat_id, item.conversation_id)"
|
||||
type="error"
|
||||
>
|
||||
<a-button>删除</a-button>
|
||||
</a-popconfirm>
|
||||
</template>
|
||||
</a-dropdown-button>
|
||||
</div>
|
||||
</a-menu>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { defineProps } from 'vue';
|
||||
import { delAgentMessage, getHistoryChat } from '@/api/all/agent';
|
||||
|
||||
const props = defineProps({
|
||||
cozeInfo: {
|
||||
type: Object as () => any,
|
||||
default: () => ({}),
|
||||
}
|
||||
});
|
||||
|
||||
const delMessage = async (chatId, conversationId) => {
|
||||
const { code, data } = await delAgentMessage({ chat_id: chatId, conversation_id: conversationId });
|
||||
if (code === 200) {
|
||||
console.log(data, 'data');
|
||||
}
|
||||
};
|
||||
|
||||
const conversations = ref([]);
|
||||
|
||||
const getHistoryChatData = async (botId) => {
|
||||
const { code, data } = await getHistoryChat({ bot_id: botId });
|
||||
if (code === 200) {
|
||||
conversations.value = data.list;
|
||||
}
|
||||
};
|
||||
const truncateText = (text: string, maxLength = 30) => {
|
||||
if (text.length <= maxLength) return text;
|
||||
return text.slice(0, maxLength) + '...';
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
getHistoryChatData(props.cozeInfo.bot_id);
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.logo {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.menu-title {
|
||||
color: var(--Text-1, #211f24);
|
||||
font-size: 18px;
|
||||
font-family: Alibaba PuHuiTi;
|
||||
font-weight: 400;
|
||||
line-height: 26px;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
.agent-img {
|
||||
width: 100%;
|
||||
height: 230px;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
</style>
|
||||
138
src/views/agent/chat/index.vue
Normal file
138
src/views/agent/chat/index.vue
Normal file
@ -0,0 +1,138 @@
|
||||
<template>
|
||||
<div class="chat-wrap">
|
||||
<span class="" @click="goChatIndex"> <icon-left /> 返回空间 </span>
|
||||
<div class="chat-contain">
|
||||
<a-layout>
|
||||
<a-layout-sider width="15%" style="background: #fff">
|
||||
<HistoryChat v-if="cozeInfo?.bot_id" :cozeInfo="cozeInfo" />
|
||||
</a-layout-sider>
|
||||
<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>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue';
|
||||
import { getChatAgent } from '@/api/all/agent';
|
||||
import HistoryChat from './components/HistoryChat.vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
// 存储认证令牌
|
||||
const authToken = ref('');
|
||||
|
||||
// 模拟从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();
|
||||
};
|
||||
|
||||
const goChatIndex = async () => {
|
||||
router.push({
|
||||
path: '/agent/index',
|
||||
});
|
||||
};
|
||||
|
||||
// 动态加载脚本函数
|
||||
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();
|
||||
const id = route.query.id;
|
||||
const query = reactive({
|
||||
id: id,
|
||||
});
|
||||
|
||||
const cozeInfo = reactive({
|
||||
title: '',
|
||||
description: '',
|
||||
icon_url: '',
|
||||
bot_id: '',
|
||||
auth: {},
|
||||
});
|
||||
|
||||
let cozeWebSDK = null;
|
||||
const initChat = async () => {
|
||||
const { code, data } = await getChatAgent(query.id);
|
||||
if (code != 200) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Object.assign(cozeInfo, data.info);
|
||||
await loadScript('https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.10/libs/cn/index.js');
|
||||
let cozeConfig = await cozeWebSdkConfig(data.info.bot_id, data.info.name, data.info.auth);
|
||||
cozeWebSDK = cozeWebSDK = new CozeWebSDK.WebChatClient(cozeConfig);
|
||||
showChatPage();
|
||||
};
|
||||
|
||||
const cozeWebSdkConfig = (botId, name, auth) => {
|
||||
auth.onRefreshToken = function () {
|
||||
return '';
|
||||
};
|
||||
let config = {
|
||||
config: {
|
||||
botId: botId,
|
||||
},
|
||||
ui: {
|
||||
chatBot: {
|
||||
el: document.getElementById('coze-chat-container'),
|
||||
width: '80%',
|
||||
height: '100%',
|
||||
title: name,
|
||||
isNeedFunctionCallMessage: true,
|
||||
},
|
||||
footer:{
|
||||
expressionText:"内容由AI生成,无法确保真实准确,仅供参考。",
|
||||
},
|
||||
},
|
||||
auth: auth,
|
||||
base: {
|
||||
icon: '',
|
||||
zIndex: 1000,
|
||||
},
|
||||
header: {
|
||||
isShow: true,
|
||||
isNeedClose: false,
|
||||
},
|
||||
};
|
||||
return config;
|
||||
};
|
||||
|
||||
const showChatPage = () => {
|
||||
cozeWebSDK.showChatBot();
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
initChat();
|
||||
});
|
||||
onUnmounted(() => {
|
||||
cozeWebSDK.destroy();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
@import './style.scss';
|
||||
</style>
|
||||
27
src/views/agent/chat/style.scss
Normal file
27
src/views/agent/chat/style.scss
Normal file
@ -0,0 +1,27 @@
|
||||
.chat-wrap {
|
||||
.chat-contain {
|
||||
font-family: Avenir, Helvetica, Arial, sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
color: #2c3e50;
|
||||
margin-top: 24px;
|
||||
|
||||
.span-back {
|
||||
color: var(--Text-2, #3C4043);
|
||||
font-size: 14px;
|
||||
font-family: Alibaba PuHuiTi;
|
||||
font-weight: 400;
|
||||
line-height: 62px;
|
||||
word-wrap: break-word
|
||||
}
|
||||
}
|
||||
|
||||
.span-back {
|
||||
color: var(--Text-2, #3C4043);
|
||||
font-size: 14px;
|
||||
font-family: Alibaba PuHuiTi;
|
||||
font-weight: 400;
|
||||
line-height: 22px;
|
||||
word-wrap: break-word
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user