Skip to content

Commit 39a5af5

Browse files
committed
11.30
1 parent 1fa91f0 commit 39a5af5

File tree

4 files changed

+145
-16
lines changed

4 files changed

+145
-16
lines changed

src/components/InputSection.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { useUserSentMessagesStore } from '../stores/userSentMessages'
44
const userSentMessagesStore = useUserSentMessagesStore()
55
const handleSent = async () => {
66
if (!userSentMessagesStore.userMessage.trim()) return
7-
87
console.log('用户输入的内容', userSentMessagesStore.userMessage)
98
await userSentMessagesStore.addUserMessage(userSentMessagesStore.userMessage)
109
}

src/components/SideDrawer.vue

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,78 @@
1+
<script lang="ts" setup>
2+
import { ref } from 'vue'
3+
import { useUserSentMessagesStore } from '../stores/userSentMessages'
4+
5+
const drawer = ref(false)
6+
const store = useUserSentMessagesStore()
7+
8+
const handleNewChat = () => {
9+
store.createNewChat()
10+
}
11+
const handleChatSelect = (chatId) => {
12+
store.switchChat(chatId)
13+
}
14+
</script>
15+
116
<template>
217
<el-button type="primary" size="large" style="margin-left: 16px" @click="drawer = true">
3-
open
18+
历史聊天
419
</el-button>
5-
6-
<el-drawer v-model="drawer" title="I am the title" :with-header="false">
7-
<span>Hi there!</span>
20+
<el-drawer v-model="drawer" title="历史聊天">
21+
<div class="drawer-content">
22+
<el-button type="primary" @click="handleNewChat" style="width: 100%; margin-bottom: 20px">
23+
新建对话
24+
</el-button>
25+
<el-card
26+
v-for="chat in store.chatHistory"
27+
:key="chat.id"
28+
class="chat-card"
29+
:class="{ active: chat.id === store.currentChatId }"
30+
@click="handleChatSelect(chat.id)"
31+
>
32+
</el-card>
33+
</div>
834
</el-drawer>
935
</template>
1036

11-
<script lang="ts" setup>
12-
import { ref } from 'vue'
13-
const drawer = ref(false)
14-
</script>
37+
<style scoped>
38+
.drawer-content {
39+
padding: 20px;
40+
}
41+
42+
.chat-card {
43+
margin-bottom: 10px;
44+
cursor: pointer;
45+
transition: all 0.3s;
46+
}
47+
48+
.chat-card:hover {
49+
transform: translateX(5px);
50+
}
51+
52+
.chat-card.active {
53+
border-left: 4px solid #409eff;
54+
}
55+
56+
.chat-info {
57+
display: flex;
58+
flex-direction: column;
59+
gap: 5px;
60+
}
61+
62+
.chat-title {
63+
font-weight: bold;
64+
}
65+
66+
.chat-last-message {
67+
font-size: 0.9em;
68+
color: #666;
69+
overflow: hidden;
70+
text-overflow: ellipsis;
71+
white-space: nowrap;
72+
}
73+
74+
.chat-time {
75+
font-size: 0.8em;
76+
color: #999;
77+
}
78+
</style>

src/services/coze-service.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1-
const COZE_API_URL = 'https://api.coze.cn/v3/chat?conversation_id=7442211537055678499'
1+
const COZE_API_CREATCONVERSATION_URL = 'https://api.coze.cn/v1/conversation/create'
22
const COZE_KEY = 'pat_7fkzMiEFmB0gcEjrFiM6Exa2O317TsmgWDoq0lbjr7dre3zDGCIihARO2Lp64XEJ'
33
const BOT_ID = '7442210806965813258'
44

5+
//创建会话接口
6+
export async function createConversation() {
7+
try {
8+
const response = await fetch(COZE_API_CREATCONVERSATION_URL, {
9+
method: 'POST',
10+
headers: {
11+
'Content-Type': 'application/json',
12+
Authorization: `Bearer ${COZE_KEY}`,
13+
},
14+
})
15+
if (!response.ok) {
16+
throw new Error(`API1调用失败: ${response.status}`)
17+
}
18+
const data = await response.json()
19+
const id = data.data.id
20+
console.log('返回的会话ID:', id)
21+
console.log('API1响应状态:', response.status)
22+
return id
23+
} catch (error) {
24+
console.error('Coze API1调用错误:', error)
25+
throw error
26+
}
27+
}
28+
29+
const COZE_API_URL = `https://api.coze.cn/v3/chat?conversation_id=7442211537055678499`
30+
531
export async function chatWithCoze(message, onChunk) {
632
try {
733
const response = await fetch(COZE_API_URL, {
@@ -26,10 +52,10 @@ export async function chatWithCoze(message, onChunk) {
2652
})
2753

2854
if (!response.ok) {
29-
throw new Error(`API调用失败: ${response.status}`)
55+
throw new Error(`API2调用失败: ${response.status}`)
3056
}
3157

32-
console.log('API响应状态:', response.status)
58+
console.log('API2响应状态:', response.status)
3359

3460
const reader = response.body.getReader()
3561
const decoder = new TextDecoder()

src/stores/userSentMessages.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,59 @@
11
import { defineStore } from 'pinia'
22
import { ref } from 'vue'
3-
import { chatWithCoze } from '../services/coze-service'
3+
import { chatWithCoze, createConversation } from '../services/coze-service'
44

55
export const useUserSentMessagesStore = defineStore('userSentMessages', () => {
66
const messages = ref([])
77
const userMessage = ref('')
8+
const chatHistory = ref([
9+
{
10+
id: 1,
11+
title: '对话1',
12+
messages: [],
13+
lastMessage: '这是最后一条消息',
14+
timestamp: '2024-01-21 12:00',
15+
},
16+
])
17+
const currentChatId = ref(1)
18+
var conversation_id = ref('')
19+
function createNewChat() {
20+
conversation_id.value = createConversation() //??????????????????????????返回值有对于的数据
21+
const newChat = {
22+
id: Date.now(),
23+
title: `对话${chatHistory.value.length + 1}`,
24+
messages: [],
25+
lastMessage: '',
26+
timestamp: new Date().toLocaleString,
27+
}
28+
chatHistory.value.push(newChat)
29+
currentChatId.value = newChat.id
30+
messages.value = []
31+
console.log('Date.now()', Date.now())
32+
console.log('conversation_id', conversation_id.value)
33+
}
34+
35+
console.log('conversation_id', conversation_id)
36+
37+
function switchChat(chatId) {
38+
currentChatId.value = chatId
39+
const chat = chatHistory.value.find((c) => c.id === chatId)
40+
if (chat) {
41+
messages.value = chat.messages
42+
}
43+
}
844

945
async function addUserMessage(message) {
1046
// 先保存消息内容
1147
const currentMessage = message
1248
// 立即清空输入框
1349
userMessage.value = ''
1450

15-
// 添加用户消息
16-
messages.value.push({
51+
const newUserMessage = {
1752
type: 'user',
1853
content: currentMessage,
1954
content_type: 'text',
20-
})
55+
}
56+
messages.value.push(newUserMessage)
2157

2258
try {
2359
// 准备 AI 消息
@@ -55,5 +91,9 @@ export const useUserSentMessagesStore = defineStore('userSentMessages', () => {
5591
messages,
5692
userMessage,
5793
addUserMessage,
94+
chatHistory,
95+
currentChatId,
96+
createNewChat,
97+
switchChat,
5898
}
5999
})

0 commit comments

Comments
 (0)