Skip to content

Commit 6550983

Browse files
committed
【代码评审】Mall:客服的会话列表
1 parent 0051264 commit 6550983

File tree

3 files changed

+52
-24
lines changed

3 files changed

+52
-24
lines changed

src/views/mall/promotion/kefu/components/KeFuConversationBox.vue

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
22
<div class="kefu">
3+
<!-- TODO @puhui999:item => conversation 会不会更容易理解 -->
34
<div
45
v-for="(item, index) in conversationList"
56
:key="item.id"
@@ -9,7 +10,9 @@
910
@contextmenu.prevent="rightClick($event as PointerEvent, item)"
1011
>
1112
<div class="flex justify-center items-center w-100%">
13+
<!-- TODO style 换成 unocss -->
1214
<div class="flex justify-center items-center" style="width: 50px; height: 50px">
15+
<!-- 头像 + 未读 -->
1316
<el-badge
1417
:hidden="item.adminUnreadMessageCount === 0"
1518
:max="99"
@@ -41,7 +44,8 @@
4144
</div>
4245
</div>
4346
</div>
44-
<!-- 通过右击获取到的坐标定位 -->
47+
48+
<!-- 右键,进行操作(类似微信) -->
4549
<ul v-show="showRightMenu" :style="rightMenuStyle" class="right-menu-ul">
4650
<li
4751
v-show="!selectedConversation.adminPinned"
@@ -78,23 +82,30 @@ import { formatDate } from '@/utils/formatTime'
7882
import { KeFuMessageContentTypeEnum } from './tools/constants'
7983
8084
defineOptions({ name: 'KeFuConversationBox' })
81-
const message = useMessage()
85+
86+
const message = useMessage() // 消息弹窗
87+
8288
const { replaceEmoji } = useEmoji()
83-
const activeConversationIndex = ref(-1) // 选中的会话
8489
const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
90+
const activeConversationIndex = ref(-1) // 选中的会话 index 位置 TODO @puhui999:这个可以改成 activeConversationId 么?因为一般是选中的对话编号
91+
92+
/** 加载会话列表 */
8593
const getConversationList = async () => {
8694
conversationList.value = await KeFuConversationApi.getConversationList()
8795
}
8896
defineExpose({ getConversationList })
97+
98+
/** 打开右侧的消息列表 */
8999
const emits = defineEmits<{
90100
(e: 'change', v: KeFuConversationRespVO): void
91101
}>()
92-
// 打开右侧消息
93102
const openRightMessage = (item: KeFuConversationRespVO, index: number) => {
94103
activeConversationIndex.value = index
95104
emits('change', item)
96105
}
97-
// 获得消息类型
106+
107+
// TODO @puhui999:这个,是不是改成 getConversationDisplayText,获取会话的展示文本。然后,把文本消息类型,也统一处理(包括上面的 replaceEmoji)。这样,更统一。
108+
/** 获得消息类型 */
98109
const getContentType = computed(() => (lastMessageContentType: number) => {
99110
switch (lastMessageContentType) {
100111
case KeFuMessageContentTypeEnum.SYSTEM:
@@ -117,8 +128,9 @@ const getContentType = computed(() => (lastMessageContentType: number) => {
117128
//======================= 右键菜单 =======================
118129
const showRightMenu = ref(false) // 显示右键菜单
119130
const rightMenuStyle = ref<any>({}) // 右键菜单 Style
120-
const selectedConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 右键选中的会话对象
121-
// 右键菜单
131+
const selectedConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 右键选中的会话对象 TODO puhui999:这个是不是叫 rightClickConversation 会好点。因为 selected 容易和选中的对话,定义上有点重叠
132+
133+
/** 打开右键菜单 */
122134
const rightClick = (mouseEvent: PointerEvent, item: KeFuConversationRespVO) => {
123135
selectedConversation.value = item
124136
// 显示右键菜单
@@ -128,11 +140,12 @@ const rightClick = (mouseEvent: PointerEvent, item: KeFuConversationRespVO) => {
128140
left: mouseEvent.clientX - 80 + 'px'
129141
}
130142
}
131-
// 关闭菜单
143+
/** 关闭右键菜单 */
132144
const closeRightMenu = () => {
133145
showRightMenu.value = false
134146
}
135-
// 置顶会话
147+
148+
/** 置顶会话 */
136149
const updateConversationPinned = async (adminPinned: boolean) => {
137150
// 1. 会话置顶/取消置顶
138151
await KeFuConversationApi.updateConversationPinned({
@@ -144,7 +157,8 @@ const updateConversationPinned = async (adminPinned: boolean) => {
144157
closeRightMenu()
145158
await getConversationList()
146159
}
147-
// 删除会话
160+
161+
/** 删除会话 */
148162
const deleteConversation = async () => {
149163
// 1. 删除会话
150164
await message.confirm('您确定要删除该会话吗?')
@@ -153,6 +167,8 @@ const deleteConversation = async () => {
153167
closeRightMenu()
154168
await getConversationList()
155169
}
170+
171+
/** 监听右键菜单的显示状态,添加点击事件监听器 */
156172
watch(showRightMenu, (val) => {
157173
if (val) {
158174
document.body.addEventListener('click', closeRightMenu)

src/views/mall/promotion/kefu/components/tools/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const KeFuMessageContentTypeEnum = {
99
PRODUCT: 10, // 商品消息
1010
ORDER: 11 // 订单消息"
1111
}
12+
1213
// Promotion 的 WebSocket 消息类型枚举类
1314
export const WebSocketMessageTypeConstants = {
1415
KEFU_MESSAGE_TYPE: 'kefu_message_type', // 客服消息类型

src/views/mall/promotion/kefu/index.vue

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<template>
22
<el-row :gutter="10">
3+
<!-- TODO @puhui999:KeFuConversationBox => KeFuConversationList ;KeFuChatBox => KeFuMessageList -->
4+
<!-- 会话列表 -->
35
<el-col :span="8">
46
<ContentWrap>
57
<KeFuConversationBox ref="keFuConversationRef" @change="handleChange" />
68
</ContentWrap>
79
</el-col>
10+
<!-- 会话详情(选中会话的消息列表) -->
811
<el-col :span="16">
912
<ContentWrap>
1013
<KeFuChatBox ref="keFuChatBoxRef" @change="getConversationList" />
@@ -21,15 +24,10 @@ import { getAccessToken } from '@/utils/auth'
2124
import { useWebSocket } from '@vueuse/core'
2225
2326
defineOptions({ name: 'KeFu' })
24-
const message = useMessage()
2527
26-
// 加载消息
27-
const keFuChatBoxRef = ref<InstanceType<typeof KeFuChatBox>>()
28-
const handleChange = (conversation: KeFuConversationRespVO) => {
29-
keFuChatBoxRef.value?.getMessageList(conversation)
30-
}
28+
const message = useMessage() // 消息弹窗
3129
32-
//======================= websocket start=======================
30+
// ======================= WebSocket start =======================
3331
const server = ref(
3432
(import.meta.env.VITE_BASE_URL + '/infra/ws/').replace('http', 'ws') +
3533
'?token=' +
@@ -38,9 +36,11 @@ const server = ref(
3836
3937
/** 发起 WebSocket 连接 */
4038
const { data, close, open } = useWebSocket(server.value, {
41-
autoReconnect: false,
39+
autoReconnect: false, // TODO @puhui999:重连要加下
4240
heartbeat: true
4341
})
42+
43+
/** 监听 WebSocket 数据 */
4444
watchEffect(() => {
4545
if (!data.value) {
4646
return
@@ -75,17 +75,28 @@ watchEffect(() => {
7575
console.error(error)
7676
}
7777
})
78-
//======================= websocket end=======================
79-
// 加载会话列表
78+
// ======================= WebSocket end =======================
79+
80+
/** 加载会话列表 */
8081
const keFuConversationRef = ref<InstanceType<typeof KeFuConversationBox>>()
8182
const getConversationList = () => {
8283
keFuConversationRef.value?.getConversationList()
8384
}
85+
86+
/** 加载指定会话的消息列表 */
87+
const keFuChatBoxRef = ref<InstanceType<typeof KeFuChatBox>>()
88+
const handleChange = (conversation: KeFuConversationRespVO) => {
89+
keFuChatBoxRef.value?.getMessageList(conversation)
90+
}
91+
92+
/** 初始化 */
8493
onMounted(() => {
8594
getConversationList()
8695
// 打开 websocket 连接
8796
open()
8897
})
98+
99+
/** 销毁 */
89100
onBeforeUnmount(() => {
90101
// 关闭 websocket 连接
91102
close()
@@ -104,17 +115,17 @@ onBeforeUnmount(() => {
104115
height: 6px;
105116
}
106117
107-
/*定义滚动条轨道 内阴影+圆角*/
118+
/* 定义滚动条轨道 内阴影+圆角 */
108119
::-webkit-scrollbar-track {
109-
box-shadow: inset 0 0 0px rgba(240, 240, 240, 0.5);
120+
box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
110121
border-radius: 10px;
111122
background-color: #fff;
112123
}
113124
114-
/*定义滑块 内阴影+圆角*/
125+
/* 定义滑块 内阴影+圆角 */
115126
::-webkit-scrollbar-thumb {
116127
border-radius: 10px;
117-
box-shadow: inset 0 0 0px rgba(240, 240, 240, 0.5);
128+
box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
118129
background-color: rgba(240, 240, 240, 0.5);
119130
}
120131
</style>

0 commit comments

Comments
 (0)