Skip to content

Commit 1a6afa3

Browse files
committed
【代码优化】AI:聊天对话 index.vue 代码梳理 40%(message 部分)
1 parent 2b07891 commit 1a6afa3

File tree

4 files changed

+61
-98
lines changed

4 files changed

+61
-98
lines changed

src/api/ai/chat/conversation/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface ChatConversationVO {
1212
temperature: number // 温度参数
1313
maxTokens: number // 单条回复的最大 Token 数量
1414
maxContexts: number // 上下文的最大 Message 数量
15+
createTime?: Date // 创建时间
1516
// 额外字段
1617
systemMessage?: string // 角色设定
1718
modelName?: string // 模型名字

src/views/ai/chat/index/components/message/MessageList.vue

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<div ref="messageContainer" style="height: 100%; overflow-y: auto; position: relative">
2+
<div ref="messageContainer" class="h-100% overflow-y relative">
33
<div class="chat-list" v-for="(item, index) in list" :key="index">
4-
<!-- 靠左 message -->
4+
<!-- 靠左 message:system、assistant 类型 -->
55
<div class="left-message message-item" v-if="item.type !== 'user'">
66
<div class="avatar">
77
<el-avatar :src="roleAvatar" />
@@ -14,16 +14,16 @@
1414
<MarkdownView class="left-text" :content="item.content" />
1515
</div>
1616
<div class="left-btns">
17-
<el-button class="btn-cus" link @click="noCopy(item.content)">
17+
<el-button class="btn-cus" link @click="copyContent(item.content)">
1818
<img class="btn-image" src="@/assets/ai/copy.svg" />
1919
</el-button>
2020
<el-button v-if="item.id > 0" class="btn-cus" link @click="onDelete(item.id)">
21-
<img class="btn-image" src="@/assets/ai/delete.svg" style="height: 17px" />
21+
<img class="btn-image h-17px" src="@/assets/ai/delete.svg" />
2222
</el-button>
2323
</div>
2424
</div>
2525
</div>
26-
<!-- 靠右 message -->
26+
<!-- 靠右 message:user 类型 -->
2727
<div class="right-message message-item" v-if="item.type === 'user'">
2828
<div class="avatar">
2929
<el-avatar :src="userAvatar" />
@@ -36,15 +36,11 @@
3636
<div class="right-text">{{ item.content }}</div>
3737
</div>
3838
<div class="right-btns">
39-
<el-button class="btn-cus" link @click="noCopy(item.content)">
39+
<el-button class="btn-cus" link @click="copyContent(item.content)">
4040
<img class="btn-image" src="@/assets/ai/copy.svg" />
4141
</el-button>
4242
<el-button class="btn-cus" link @click="onDelete(item.id)">
43-
<img
44-
class="btn-image"
45-
src="@/assets/ai/delete.svg"
46-
style="height: 17px; margin-right: 12px"
47-
/>
43+
<img class="btn-image h-17px mr-12px" src="@/assets/ai/delete.svg" />
4844
</el-button>
4945
<el-button class="btn-cus" link @click="onRefresh(item)">
5046
<el-icon size="17"><RefreshRight /></el-icon>
@@ -63,23 +59,25 @@
6359
</div>
6460
</template>
6561
<script setup lang="ts">
62+
import { PropType } from 'vue'
6663
import { formatDate } from '@/utils/formatTime'
6764
import MarkdownView from '@/components/MarkdownView/index.vue'
68-
import { ChatMessageApi, ChatMessageVO } from '@/api/ai/chat/message'
6965
import { useClipboard } from '@vueuse/core'
70-
import { PropType } from 'vue'
7166
import { ArrowDownBold, Edit, RefreshRight } from '@element-plus/icons-vue'
67+
import { ChatMessageApi, ChatMessageVO } from '@/api/ai/chat/message'
7268
import { ChatConversationVO } from '@/api/ai/chat/conversation'
7369
import { useUserStore } from '@/store/modules/user'
7470
import userAvatarDefaultImg from '@/assets/imgs/avatar.gif'
7571
import roleAvatarDefaultImg from '@/assets/ai/gpt.svg'
7672
73+
const message = useMessage() // 消息弹窗
7774
const { copy } = useClipboard() // 初始化 copy 到粘贴板
78-
// 判断 消息列表 滚动的位置(用于判断是否需要滚动到消息最下方)
75+
const userStore = useUserStore()
76+
77+
// 判断“消息列表”滚动的位置(用于判断是否需要滚动到消息最下方)
7978
const messageContainer: any = ref(null)
8079
const isScrolling = ref(false) //用于判断用户是否在滚动
8180
82-
const userStore = useUserStore()
8381
const userAvatar = computed(() => userStore.user.avatar ?? userAvatarDefaultImg)
8482
const roleAvatar = computed(() => props.conversation.roleAvatar ?? roleAvatarDefaultImg)
8583
@@ -95,12 +93,16 @@ const props = defineProps({
9593
}
9694
})
9795
96+
const { list } = toRefs(props) // 消息列表
97+
98+
const emits = defineEmits(['onDeleteSuccess', 'onRefresh', 'onEdit']) // 定义 emits
99+
98100
// ============ 处理对话滚动 ==============
99101
102+
/** 滚动到底部 */
100103
const scrollToBottom = async (isIgnore?: boolean) => {
104+
// 注意要使用 nextTick 以免获取不到dom
101105
await nextTick(() => {
102-
// TODO @fan:中文写作习惯,中英文之间要有空格;另外,nextick 哈,idea 如果有绿色波兰线,可以关注下
103-
//注意要使用nexttick以免获取不到dom
104106
if (isIgnore || !isScrolling.value) {
105107
messageContainer.value.scrollTop =
106108
messageContainer.value.scrollHeight - messageContainer.value.offsetHeight
@@ -122,75 +124,48 @@ function handleScroll() {
122124
}
123125
}
124126
125-
/**
126-
* 复制
127-
*/
128-
const noCopy = async (content) => {
129-
copy(content)
130-
ElMessage({
131-
message: '复制成功!',
132-
type: 'success'
133-
})
127+
/** 回到底部 */
128+
const handleGoBottom = async () => {
129+
const scrollContainer = messageContainer.value
130+
scrollContainer.scrollTop = scrollContainer.scrollHeight
131+
}
132+
133+
/** 回到顶部 */
134+
const handlerGoTop = async () => {
135+
const scrollContainer = messageContainer.value
136+
scrollContainer.scrollTop = 0
137+
}
138+
139+
defineExpose({ scrollToBottom, handlerGoTop }) // 提供方法给 parent 调用
140+
141+
// ============ 处理消息操作 ==============
142+
143+
/** 复制 */
144+
const copyContent = async (content) => {
145+
await copy(content)
146+
message.success('复制成功!')
134147
}
135148
136-
/**
137-
* 删除
138-
*/
149+
/** 删除 */
139150
const onDelete = async (id) => {
140151
// 删除 message
141152
await ChatMessageApi.deleteChatMessage(id)
142-
ElMessage({
143-
message: '删除成功!',
144-
type: 'success'
145-
})
153+
message.success('删除成功!')
146154
// 回调
147155
emits('onDeleteSuccess')
148156
}
149157
150-
/**
151-
* 刷新
152-
*/
158+
/** 刷新 */
153159
const onRefresh = async (message: ChatMessageVO) => {
154160
emits('onRefresh', message)
155161
}
156162
157-
/**
158-
* 编辑
159-
*/
163+
/** 编辑 */
160164
const onEdit = async (message: ChatMessageVO) => {
161165
emits('onEdit', message)
162166
}
163167
164-
/**
165-
* 回到底部
166-
*/
167-
const handleGoBottom = async () => {
168-
const scrollContainer = messageContainer.value
169-
scrollContainer.scrollTop = scrollContainer.scrollHeight
170-
}
171-
172-
/**
173-
* 回到顶部
174-
*/
175-
const handlerGoTop = async () => {
176-
const scrollContainer = messageContainer.value
177-
scrollContainer.scrollTop = 0
178-
}
179-
180-
// 监听 list
181-
// TODO @fan:这个木有,是不是删除啦
182-
const { list, conversationId } = toRefs(props)
183-
watch(list, async (newValue, oldValue) => {
184-
console.log('watch list', list)
185-
})
186-
187-
// 提供方法给 parent 调用
188-
defineExpose({ scrollToBottom, handlerGoTop })
189-
190-
// 定义 emits
191-
const emits = defineEmits(['onDeleteSuccess', 'onRefresh', 'onEdit'])
192-
193-
// onMounted
168+
/** 初始化 */
194169
onMounted(async () => {
195170
messageContainer.value.addEventListener('scroll', handleScroll)
196171
})
@@ -199,15 +174,7 @@ onMounted(async () => {
199174
<style scoped lang="scss">
200175
.message-container {
201176
position: relative;
202-
//top: 0;
203-
//bottom: 0;
204-
//left: 0;
205-
//right: 0;
206-
//width: 100%;
207-
//height: 100%;
208177
overflow-y: scroll;
209-
//padding: 0 15px;
210-
//z-index: -1;
211178
}
212179
213180
// 中间
@@ -231,11 +198,6 @@ onMounted(async () => {
231198
justify-content: flex-start;
232199
}
233200
234-
.avatar {
235-
//height: 170px;
236-
//width: 170px;
237-
}
238-
239201
.message {
240202
display: flex;
241203
flex-direction: column;
@@ -272,7 +234,6 @@ onMounted(async () => {
272234
color: #fff;
273235
display: inline;
274236
background-color: #267fff;
275-
color: #fff;
276237
box-shadow: 0 0 0 1px #267fff;
277238
border-radius: 10px;
278239
padding: 10px;

src/views/ai/chat/index/components/message/MessageListEmpty.vue

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1+
<!-- 消息列表为空时,展示 prompt 列表 -->
12
<template>
23
<div class="chat-empty">
3-
4-
<!-- title -->
4+
<!-- title -->
55
<div class="center-container">
6-
<div class="title">芋艿 AI</div>
6+
<div class="title">芋道 AI</div>
77
<div class="role-list">
8-
<div class="role-item" v-for="prompt in promptList" :key="prompt.prompt" @click="handlerPromptClick(prompt)">
9-
{{prompt.prompt}}
8+
<div
9+
class="role-item"
10+
v-for="prompt in promptList"
11+
:key="prompt.prompt"
12+
@click="handlerPromptClick(prompt)"
13+
>
14+
{{ prompt.prompt }}
1015
</div>
1116
</div>
1217
</div>
1318
</div>
1419
</template>
1520
<script setup lang="ts">
16-
17-
const promptList = ref<any[]>() // 角色列表
18-
promptList.value = [
21+
const promptList = [
1922
{
20-
"prompt": "今天气怎么样?",
23+
prompt: '今天气怎么样?'
2124
},
2225
{
23-
"prompt": "写一首好听的诗歌?",
26+
prompt: '写一首好听的诗歌?'
2427
}
25-
]
28+
] // prompt 列表
2629
2730
const emits = defineEmits(['onPrompt'])
2831
32+
/** 选中 prompt 点击 */
2933
const handlerPromptClick = async ({ prompt }) => {
3034
emits('onPrompt', prompt)
3135
}

src/views/ai/chat/index/components/message/MessageNewConversation.vue

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
</div>
1010
</div>
1111
</template>
12-
1312
<script setup lang="ts">
1413
const emits = defineEmits(['onNewConversation'])
1514
16-
/**
17-
* 新建 conversation 聊天对话
18-
*/
15+
/** 新建 conversation 聊天对话 */
1916
const handlerNewChat = () => {
2017
emits('onNewConversation')
2118
}

0 commit comments

Comments
 (0)