2024-07-07 01:58:23 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="chat-container">
|
2024-07-14 16:42:38 +08:00
|
|
|
<div v-if="state.isSidebarOpen" class="conversations">
|
|
|
|
|
<div class="actions">
|
2024-07-25 20:30:28 +08:00
|
|
|
<!-- <div class="action new" @click="addNewConv"><FormOutlined /></div> -->
|
2024-07-26 14:13:05 +08:00
|
|
|
<span style="font-weight: bold;">对话历史</span>
|
2024-07-14 16:42:38 +08:00
|
|
|
<div class="action close" @click="state.isSidebarOpen = false"><MenuOutlined /></div>
|
2024-07-07 01:58:23 +08:00
|
|
|
</div>
|
2024-07-14 16:42:38 +08:00
|
|
|
<div class="conversation"
|
|
|
|
|
v-for="(state, index) in convs"
|
|
|
|
|
:key="index"
|
|
|
|
|
:class="{ active: curConvId === index }"
|
|
|
|
|
@click="goToConversation(index)">
|
|
|
|
|
<div class="conversation__title">{{ state.title }}</div>
|
|
|
|
|
<div class="conversation__delete" @click.prevent="delConv(index)"><DeleteOutlined /></div>
|
2024-07-07 01:58:23 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-07-14 16:42:38 +08:00
|
|
|
<ChatComponent
|
|
|
|
|
:conv="convs[curConvId]"
|
|
|
|
|
:state="state"
|
|
|
|
|
@rename-title="renameTitle"
|
|
|
|
|
@newconv="addNewConv"/>
|
2024-07-07 01:58:23 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2024-07-14 16:42:38 +08:00
|
|
|
import { reactive, ref, watch, onMounted } from 'vue'
|
|
|
|
|
import { FormOutlined, MenuOutlined, DeleteOutlined } from '@ant-design/icons-vue'
|
|
|
|
|
import ChatComponent from '@/components/ChatComponent.vue'
|
|
|
|
|
|
|
|
|
|
const convs = reactive(JSON.parse(localStorage.getItem('chat-convs')) || [
|
|
|
|
|
{
|
|
|
|
|
id: 0,
|
|
|
|
|
title: '新对话',
|
|
|
|
|
history: [],
|
|
|
|
|
messages: [],
|
|
|
|
|
inputText: ''
|
|
|
|
|
},
|
|
|
|
|
])
|
2024-07-07 01:58:23 +08:00
|
|
|
|
|
|
|
|
const state = reactive({
|
2024-07-14 16:42:38 +08:00
|
|
|
isSidebarOpen: true,
|
2024-07-07 01:58:23 +08:00
|
|
|
})
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
const curConvId = ref(0)
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
const generateRandomHash = (length) => {
|
|
|
|
|
let chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
|
|
|
let hash = '';
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
|
hash += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
|
|
|
}
|
|
|
|
|
return hash;
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
const renameTitle = (newTitle) => {
|
|
|
|
|
convs[curConvId.value].title = newTitle
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
const goToConversation = (index) => {
|
|
|
|
|
curConvId.value = index
|
|
|
|
|
console.log(convs[curConvId.value])
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
const addNewConv = () => {
|
|
|
|
|
curConvId.value = 0
|
|
|
|
|
if (convs.length > 0 && convs[0].messages.length === 0) {
|
|
|
|
|
return
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
2024-07-14 16:42:38 +08:00
|
|
|
convs.unshift({
|
|
|
|
|
id: generateRandomHash(8),
|
|
|
|
|
title: `新对话`,
|
|
|
|
|
history: [],
|
|
|
|
|
messages: [],
|
|
|
|
|
inputText: ''
|
2024-07-07 01:58:23 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
const delConv = (index) => {
|
|
|
|
|
convs.splice(index, 1)
|
|
|
|
|
if (index === curConvId.value) {
|
|
|
|
|
curConvId.value = 0
|
|
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
if (convs.length === 0) {
|
|
|
|
|
addNewConv()
|
|
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
// Watch convs and save to localStorage
|
|
|
|
|
watch(
|
|
|
|
|
() => convs,
|
|
|
|
|
(newStates) => {
|
|
|
|
|
localStorage.setItem('chat-convs', JSON.stringify(newStates))
|
|
|
|
|
},
|
|
|
|
|
{ deep: true }
|
|
|
|
|
)
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
// Load convs from localStorage on mount
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
const savedSonvs = JSON.parse(localStorage.getItem('chat-convs'))
|
|
|
|
|
if (savedSonvs) {
|
|
|
|
|
for (let i = 0; i < savedSonvs.length; i++) {
|
|
|
|
|
convs[i] = savedSonvs[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="less" scoped>
|
2024-07-14 16:42:38 +08:00
|
|
|
@import '@/assets/main.css';
|
|
|
|
|
|
2024-07-07 01:58:23 +08:00
|
|
|
.chat-container {
|
|
|
|
|
display: flex;
|
2024-07-14 16:42:38 +08:00
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
position: relative;
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
.chat-container .conversations {
|
|
|
|
|
flex: 1 1 auto;
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-14 16:42:38 +08:00
|
|
|
.conversations {
|
2024-07-07 01:58:23 +08:00
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
2024-07-14 16:42:38 +08:00
|
|
|
width: 100px;
|
|
|
|
|
height: 100%;
|
2024-07-07 01:58:23 +08:00
|
|
|
overflow-y: auto;
|
2024-07-26 14:13:05 +08:00
|
|
|
border-right: 1px solid var(--main-light-3);
|
2024-07-14 16:42:38 +08:00
|
|
|
min-width: var(--min-sider-width);
|
|
|
|
|
max-width: 200px;
|
2024-07-22 00:01:01 +08:00
|
|
|
background-color: #FAFCFD;
|
2024-07-14 16:42:38 +08:00
|
|
|
|
|
|
|
|
& .actions {
|
|
|
|
|
height: var(--header-height);
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 16px;
|
|
|
|
|
position: sticky;
|
|
|
|
|
top: 0;
|
2024-07-22 00:01:01 +08:00
|
|
|
background-color: #FAFCFD;
|
2024-07-14 16:42:38 +08:00
|
|
|
z-index: 9;
|
|
|
|
|
|
|
|
|
|
.action {
|
|
|
|
|
font-size: 1.2rem;
|
|
|
|
|
width: 2.5rem;
|
|
|
|
|
height: 2.5rem;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
align-items: center;
|
|
|
|
|
border-radius: 8px;
|
2024-07-25 20:30:28 +08:00
|
|
|
color: var(--c-black-light-2);
|
2024-07-14 16:42:38 +08:00
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
&:hover {
|
2024-07-26 14:13:05 +08:00
|
|
|
background-color: var(--main-light-3);
|
2024-07-14 16:42:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 20:30:28 +08:00
|
|
|
.conversation {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 16px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
width: 100%;
|
|
|
|
|
user-select: none;
|
|
|
|
|
|
|
|
|
|
&__title {
|
|
|
|
|
color: var(--c-black-light-3);
|
|
|
|
|
white-space: nowrap; /* 禁止换行 */
|
|
|
|
|
overflow: hidden; /* 超出部分隐藏 */
|
|
|
|
|
text-overflow: ellipsis; /* 显示省略号 */
|
|
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-25 20:30:28 +08:00
|
|
|
&__delete {
|
|
|
|
|
display: none;
|
|
|
|
|
color: #7D7D7D;
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-25 20:30:28 +08:00
|
|
|
&:hover {
|
|
|
|
|
color: #F93A37;
|
|
|
|
|
background-color: #EEE;
|
|
|
|
|
}
|
2024-07-14 16:42:38 +08:00
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-25 20:30:28 +08:00
|
|
|
&.active {
|
|
|
|
|
border-left: 3px solid var(--main-color);
|
2024-07-26 14:13:05 +08:00
|
|
|
padding-left: 13px;
|
|
|
|
|
background-color: var(--main-light-3);
|
2024-07-07 01:58:23 +08:00
|
|
|
|
2024-07-25 20:30:28 +08:00
|
|
|
& .conversation__title {
|
|
|
|
|
color: var(--c-black-light-1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-09 05:04:20 +08:00
|
|
|
|
2024-07-25 20:30:28 +08:00
|
|
|
&:hover {
|
2024-07-26 14:13:05 +08:00
|
|
|
background-color: var(--main-light-3);
|
2024-07-25 20:30:28 +08:00
|
|
|
|
|
|
|
|
& .conversation__delete {
|
|
|
|
|
display: block;
|
|
|
|
|
}
|
2024-07-14 16:42:38 +08:00
|
|
|
}
|
2024-07-09 05:04:20 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-14 16:42:38 +08:00
|
|
|
|
2024-07-31 20:22:05 +08:00
|
|
|
.conversations::-webkit-scrollbar {
|
|
|
|
|
position: absolute;
|
|
|
|
|
width: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.conversations::-webkit-scrollbar-track {
|
|
|
|
|
background: transparent;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.conversations::-webkit-scrollbar-thumb {
|
|
|
|
|
background: var(--c-text-dark-2);
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.conversations::-webkit-scrollbar-thumb:hover {
|
|
|
|
|
background: rgb(100, 100, 100);
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.conversations::-webkit-scrollbar-thumb:active {
|
|
|
|
|
background: rgb(68, 68, 68);
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-07-27 14:32:57 +08:00
|
|
|
@media (max-width: 520px) {
|
|
|
|
|
.conversations {
|
|
|
|
|
position: absolute;
|
|
|
|
|
z-index: 101;
|
|
|
|
|
width: 300px;
|
|
|
|
|
border-radius: 0 16px 16px 0;
|
|
|
|
|
box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.05);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-07 01:58:23 +08:00
|
|
|
</style>
|