Skip to content

Commit 32d6ac0

Browse files
author
puhui999
committed
【优化】根据代码评审优化 mall 客服
1 parent e53786e commit 32d6ac0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+106
-131
lines changed

src/views/mall/promotion/kefu/components/KeFuConversationBox.vue renamed to src/views/mall/promotion/kefu/components/KeFuConversationList.vue

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<template>
22
<div class="kefu">
33
<div
4-
v-for="(item, index) in conversationList"
4+
v-for="item in conversationList"
55
:key="item.id"
6-
:class="{ active: index === activeConversationIndex, pinned: item.adminPinned }"
6+
:class="{ active: item.id === activeConversationId, pinned: item.adminPinned }"
77
class="kefu-conversation flex items-center"
8-
@click="openRightMessage(item, index)"
8+
@click="openRightMessage(item)"
99
@contextmenu.prevent="rightClick($event as PointerEvent, item)"
1010
>
1111
<div class="flex justify-center items-center w-100%">
12-
<!-- TODO style 换成 unocss -->
13-
<div class="flex justify-center items-center" style="width: 50px; height: 50px">
12+
<div class="flex justify-center items-center w-50px h-50px">
1413
<!-- 头像 + 未读 -->
1514
<el-badge
1615
:hidden="item.adminUnreadMessageCount === 0"
@@ -27,35 +26,29 @@
2726
{{ formatDate(item.lastMessageTime) }}
2827
</span>
2928
</div>
30-
<!-- 文本消息 -->
31-
<template v-if="KeFuMessageContentTypeEnum.TEXT === item.lastMessageContentType">
32-
<div
33-
v-dompurify-html="replaceEmoji(item.lastMessageContent)"
34-
class="last-message flex items-center color-[#989EA6]"
35-
></div>
36-
</template>
37-
<!-- 图片消息 -->
38-
<template v-else>
39-
<div class="last-message flex items-center color-[#989EA6]">
40-
{{ getContentType(item.lastMessageContentType) }}
41-
</div>
42-
</template>
29+
<!-- 最后聊天内容 -->
30+
<div
31+
v-dompurify-html="
32+
getConversationDisplayText(item.lastMessageContentType, item.lastMessageContent)
33+
"
34+
class="last-message flex items-center color-[#989EA6]"
35+
></div>
4336
</div>
4437
</div>
4538
</div>
4639

4740
<!-- 右键,进行操作(类似微信) -->
4841
<ul v-show="showRightMenu" :style="rightMenuStyle" class="right-menu-ul">
4942
<li
50-
v-show="!selectedConversation.adminPinned"
43+
v-show="!rightClickConversation.adminPinned"
5144
class="flex items-center"
5245
@click.stop="updateConversationPinned(true)"
5346
>
5447
<Icon class="mr-5px" icon="ep:top" />
5548
置顶会话
5649
</li>
5750
<li
58-
v-show="selectedConversation.adminPinned"
51+
v-show="rightClickConversation.adminPinned"
5952
class="flex items-center"
6053
@click.stop="updateConversationPinned(false)"
6154
>
@@ -86,7 +79,7 @@ const message = useMessage() // 消息弹窗
8679
8780
const { replaceEmoji } = useEmoji()
8881
const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
89-
const activeConversationIndex = ref(-1) // 选中的会话 index 位置 TODO @puhui999:这个可以改成 activeConversationId 么?因为一般是选中的对话编号
82+
const activeConversationId = ref(-1) // 选中的会话
9083
9184
/** 加载会话列表 */
9285
const getConversationList = async () => {
@@ -98,40 +91,43 @@ defineExpose({ getConversationList })
9891
const emits = defineEmits<{
9992
(e: 'change', v: KeFuConversationRespVO): void
10093
}>()
101-
const openRightMessage = (item: KeFuConversationRespVO, index: number) => {
102-
activeConversationIndex.value = index
94+
const openRightMessage = (item: KeFuConversationRespVO) => {
95+
activeConversationId.value = item.id
10396
emits('change', item)
10497
}
10598
106-
// TODO @puhui999:这个,是不是改成 getConversationDisplayText,获取会话的展示文本。然后,把文本消息类型,也统一处理(包括上面的 replaceEmoji)。这样,更统一。
10799
/** 获得消息类型 */
108-
const getContentType = computed(() => (lastMessageContentType: number) => {
109-
switch (lastMessageContentType) {
110-
case KeFuMessageContentTypeEnum.SYSTEM:
111-
return '[系统消息]'
112-
case KeFuMessageContentTypeEnum.VIDEO:
113-
return '[视频消息]'
114-
case KeFuMessageContentTypeEnum.IMAGE:
115-
return '[图片消息]'
116-
case KeFuMessageContentTypeEnum.PRODUCT:
117-
return '[商品消息]'
118-
case KeFuMessageContentTypeEnum.ORDER:
119-
return '[订单消息]'
120-
case KeFuMessageContentTypeEnum.VOICE:
121-
return '[语音消息]'
122-
default:
123-
return ''
100+
const getConversationDisplayText = computed(
101+
() => (lastMessageContentType: number, lastMessageContent: string) => {
102+
switch (lastMessageContentType) {
103+
case KeFuMessageContentTypeEnum.SYSTEM:
104+
return '[系统消息]'
105+
case KeFuMessageContentTypeEnum.VIDEO:
106+
return '[视频消息]'
107+
case KeFuMessageContentTypeEnum.IMAGE:
108+
return '[图片消息]'
109+
case KeFuMessageContentTypeEnum.PRODUCT:
110+
return '[商品消息]'
111+
case KeFuMessageContentTypeEnum.ORDER:
112+
return '[订单消息]'
113+
case KeFuMessageContentTypeEnum.VOICE:
114+
return '[语音消息]'
115+
case KeFuMessageContentTypeEnum.TEXT:
116+
return replaceEmoji(lastMessageContent)
117+
default:
118+
return ''
119+
}
124120
}
125-
})
121+
)
126122
127123
//======================= 右键菜单 =======================
128124
const showRightMenu = ref(false) // 显示右键菜单
129125
const rightMenuStyle = ref<any>({}) // 右键菜单 Style
130-
const selectedConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 右键选中的会话对象 TODO puhui999:这个是不是叫 rightClickConversation 会好点。因为 selected 容易和选中的对话,定义上有点重叠
126+
const rightClickConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 右键选中的会话对象
131127
132128
/** 打开右键菜单 */
133129
const rightClick = (mouseEvent: PointerEvent, item: KeFuConversationRespVO) => {
134-
selectedConversation.value = item
130+
rightClickConversation.value = item
135131
// 显示右键菜单
136132
showRightMenu.value = true
137133
rightMenuStyle.value = {
@@ -148,7 +144,7 @@ const closeRightMenu = () => {
148144
const updateConversationPinned = async (adminPinned: boolean) => {
149145
// 1. 会话置顶/取消置顶
150146
await KeFuConversationApi.updateConversationPinned({
151-
id: selectedConversation.value.id,
147+
id: rightClickConversation.value.id,
152148
adminPinned
153149
})
154150
message.notifySuccess(adminPinned ? '置顶成功' : '取消置顶成功')
@@ -161,7 +157,7 @@ const updateConversationPinned = async (adminPinned: boolean) => {
161157
const deleteConversation = async () => {
162158
// 1. 删除会话
163159
await message.confirm('您确定要删除该会话吗?')
164-
await KeFuConversationApi.deleteConversation(selectedConversation.value.id)
160+
await KeFuConversationApi.deleteConversation(rightClickConversation.value.id)
165161
// 2. 关闭右键菜单,更新会话列表
166162
closeRightMenu()
167163
await getConversationList()

src/views/mall/promotion/kefu/components/KeFuChatBox.vue renamed to src/views/mall/promotion/kefu/components/KeFuMessageList.vue

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<template>
22
<el-container v-if="showChatBox" class="kefu">
33
<el-header>
4-
<!-- TODO @puhui999:keFuConversation => conversation -->
5-
<div class="kefu-title">{{ keFuConversation.userNickname }}</div>
4+
<div class="kefu-title">{{ conversation.userNickname }}</div>
65
</el-header>
7-
<!-- TODO @puhui999:unocss -->
8-
<el-main class="kefu-content" style="overflow: visible">
6+
<el-main class="kefu-content overflow-visible">
97
<!-- 加载历史消息 -->
108
<div
119
v-show="loadingMore"
@@ -48,7 +46,7 @@
4846
>
4947
<el-avatar
5048
v-if="item.senderType === UserTypeEnum.MEMBER"
51-
:src="keFuConversation.userAvatar"
49+
:src="conversation.userAvatar"
5250
alt="avatar"
5351
/>
5452
<div
@@ -127,7 +125,7 @@ const message = ref('') // 消息弹窗
127125
128126
const messageTool = useMessage()
129127
const messageList = ref<KeFuMessageRespVO[]>([]) // 消息列表
130-
const keFuConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 用户会话
128+
const conversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 用户会话
131129
const showNewMessageTip = ref(false) // 显示有新消息提示
132130
const queryParams = reactive({
133131
pageNo: 1,
@@ -136,9 +134,9 @@ const queryParams = reactive({
136134
const total = ref(0) // 消息总条数
137135
138136
/** 获得消息列表 */
139-
const getMessageList = async (conversation: KeFuConversationRespVO) => {
140-
keFuConversation.value = conversation
141-
queryParams.conversationId = conversation.id
137+
const getMessageList = async (val: KeFuConversationRespVO) => {
138+
conversation.value = val
139+
queryParams.conversationId = val.id
142140
const messageTotal = messageList.value.length
143141
if (total.value > 0 && messageTotal > 0 && messageTotal === total.value) {
144142
return
@@ -162,20 +160,20 @@ const getMessageList0 = computed(() => {
162160
163161
/** 刷新消息列表 */
164162
const refreshMessageList = async () => {
165-
if (!keFuConversation.value) {
163+
if (!conversation.value) {
166164
return
167165
}
168166
169167
queryParams.pageNo = 1
170-
await getMessageList(keFuConversation.value)
168+
await getMessageList(conversation.value)
171169
if (loadHistory.value) {
172170
// 有下角显示有新消息提示
173171
showNewMessageTip.value = true
174172
}
175173
}
176174
177175
defineExpose({ getMessageList, refreshMessageList })
178-
const showChatBox = computed(() => !isEmpty(keFuConversation.value)) // 是否显示聊天区域
176+
const showChatBox = computed(() => !isEmpty(conversation.value)) // 是否显示聊天区域
179177
180178
/** 处理表情选择 */
181179
const handleEmojiSelect = (item: Emoji) => {
@@ -186,7 +184,7 @@ const handleEmojiSelect = (item: Emoji) => {
186184
const handleSendPicture = async (picUrl: string) => {
187185
// 组织发送消息
188186
const msg = {
189-
conversationId: keFuConversation.value.id,
187+
conversationId: conversation.value.id,
190188
contentType: KeFuMessageContentTypeEnum.IMAGE,
191189
content: picUrl
192190
}
@@ -202,7 +200,7 @@ const handleSendMessage = async () => {
202200
}
203201
// 2. 组织发送消息
204202
const msg = {
205-
conversationId: keFuConversation.value.id,
203+
conversationId: conversation.value.id,
206204
contentType: KeFuMessageContentTypeEnum.TEXT,
207205
content: message.value
208206
}
@@ -215,7 +213,7 @@ const sendMessage = async (msg: any) => {
215213
await KeFuMessageApi.sendKeFuMessage(msg)
216214
message.value = ''
217215
// 加载消息列表
218-
await getMessageList(keFuConversation.value)
216+
await getMessageList(conversation.value)
219217
// 滚动到最新消息处
220218
await scrollToBottom()
221219
}
@@ -233,7 +231,7 @@ const scrollToBottom = async () => {
233231
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
234232
showNewMessageTip.value = false
235233
// 2.2 消息已读
236-
await KeFuMessageApi.updateKeFuMessageReadStatus(keFuConversation.value.id)
234+
await KeFuMessageApi.updateKeFuMessageReadStatus(conversation.value.id)
237235
}
238236
239237
/** 查看新消息 */
@@ -257,7 +255,7 @@ const handleOldMessage = async () => {
257255
loadHistory.value = true
258256
// 加载消息列表
259257
queryParams.pageNo += 1
260-
await getMessageList(keFuConversation.value)
258+
await getMessageList(conversation.value)
261259
loadingMore.value = false
262260
// TODO puhui999: 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
263261
}

0 commit comments

Comments
 (0)