2024-07-15 03:53:35 +00:00
|
|
|
|
<template>
|
2024-07-16 08:23:49 +00:00
|
|
|
|
<!-- 聊天虚拟列表 -->
|
|
|
|
|
<z-paging ref="pagingRef" v-model="messageList" use-chat-record-mode use-virtual-list
|
|
|
|
|
cell-height-mode="dynamic" default-page-size="20" :auto-clean-list-when-reload="false"
|
|
|
|
|
safe-area-inset-bottom bottom-bg-color="#f8f8f8" :back-to-top-style="backToTopStyle"
|
|
|
|
|
:auto-show-back-to-top="showNewMessageTip" @backToTopClick="onBackToTopClick"
|
|
|
|
|
@scrolltoupper="onScrollToUpper" @query="queryList">
|
2024-07-17 08:45:03 +00:00
|
|
|
|
<template #top>
|
|
|
|
|
<!-- 撑一下顶部导航 -->
|
2024-09-01 09:37:26 +00:00
|
|
|
|
<view :style="{ height: sys_navBar + 'px' }"></view>
|
2024-07-17 08:45:03 +00:00
|
|
|
|
</template>
|
2024-07-16 08:23:49 +00:00
|
|
|
|
<!-- style="transform: scaleY(-1)"必须写,否则会导致列表倒置!!! -->
|
|
|
|
|
<!-- 注意不要直接在chat-item组件标签上设置style,因为在微信小程序中是无效的,请包一层view -->
|
|
|
|
|
<template #cell="{item,index}">
|
|
|
|
|
<view style="transform: scaleY(-1)">
|
2024-07-15 03:53:35 +00:00
|
|
|
|
<!-- 消息渲染 -->
|
2024-07-16 08:23:49 +00:00
|
|
|
|
<MessageListItem :message="item" :message-index="index" :message-list="messageList"></MessageListItem>
|
2024-07-15 03:53:35 +00:00
|
|
|
|
</view>
|
2024-07-16 08:23:49 +00:00
|
|
|
|
</template>
|
|
|
|
|
<!-- 底部聊天输入框 -->
|
|
|
|
|
<template #bottom>
|
|
|
|
|
<slot name="bottom"></slot>
|
|
|
|
|
</template>
|
2024-07-15 03:53:35 +00:00
|
|
|
|
<!-- 查看最新消息 -->
|
2024-07-16 08:23:49 +00:00
|
|
|
|
<template #backToTop>
|
2024-07-15 03:53:35 +00:00
|
|
|
|
<text>有新消息</text>
|
2024-07-16 08:23:49 +00:00
|
|
|
|
</template>
|
|
|
|
|
</z-paging>
|
2024-07-15 03:53:35 +00:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2024-07-16 08:23:49 +00:00
|
|
|
|
import MessageListItem from '@/pages/chat/components/messageListItem.vue';
|
|
|
|
|
import { reactive, ref } from 'vue';
|
2024-07-15 03:53:35 +00:00
|
|
|
|
import KeFuApi from '@/sheep/api/promotion/kefu';
|
2024-07-16 08:23:49 +00:00
|
|
|
|
import { isEmpty } from '@/sheep/helper/utils';
|
2024-09-01 09:37:26 +00:00
|
|
|
|
import sheep from '@/sheep';
|
2024-11-10 09:56:58 +00:00
|
|
|
|
import { formatDate } from '@/sheep/util';
|
|
|
|
|
|
2024-09-01 09:37:26 +00:00
|
|
|
|
const sys_navBar = sheep.$platform.navbar;
|
2024-07-15 03:53:35 +00:00
|
|
|
|
const messageList = ref([]); // 消息列表
|
2024-07-15 10:05:59 +00:00
|
|
|
|
const showNewMessageTip = ref(false); // 显示有新消息提示
|
2024-11-10 09:56:58 +00:00
|
|
|
|
const refreshMessage = ref(false); // 更新消息列表
|
2024-07-16 08:23:49 +00:00
|
|
|
|
const backToTopStyle = reactive({
|
|
|
|
|
'width': '100px',
|
|
|
|
|
'background-color': '#fff',
|
|
|
|
|
'border-radius': '30px',
|
|
|
|
|
'box-shadow': '0 2px 4px rgba(0, 0, 0, 0.1)',
|
|
|
|
|
'display': 'flex',
|
|
|
|
|
'justifyContent': 'center',
|
|
|
|
|
'alignItems': 'center',
|
|
|
|
|
}); // 返回顶部样式
|
2024-07-15 03:53:35 +00:00
|
|
|
|
const queryParams = reactive({
|
2024-11-10 09:56:58 +00:00
|
|
|
|
pageNo: 1, // 只用于触底计算
|
|
|
|
|
pageSize: 20,
|
|
|
|
|
createTime: undefined,
|
2024-07-15 03:53:35 +00:00
|
|
|
|
});
|
2024-07-16 08:23:49 +00:00
|
|
|
|
const pagingRef = ref(null); // 虚拟列表
|
|
|
|
|
const queryList = async (pageNo, pageSize) => {
|
|
|
|
|
// 组件加载时会自动触发此方法,因此默认页面加载时会自动触发,无需手动调用
|
|
|
|
|
// 这里的pageNo和pageSize会自动计算好,直接传给服务器即可
|
|
|
|
|
queryParams.pageNo = pageNo;
|
|
|
|
|
queryParams.pageSize = pageSize;
|
|
|
|
|
await getMessageList();
|
|
|
|
|
};
|
2024-07-15 03:53:35 +00:00
|
|
|
|
// 获得消息分页列表
|
|
|
|
|
const getMessageList = async () => {
|
2024-11-10 09:56:58 +00:00
|
|
|
|
const { data } = await KeFuApi.getKefuMessageList(queryParams);
|
|
|
|
|
if (isEmpty(data)) {
|
|
|
|
|
pagingRef.value.completeByNoMore([], true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (queryParams.pageNo > 1 && refreshMessage.value) {
|
|
|
|
|
const newMessageList = [];
|
|
|
|
|
for (const message of data) {
|
|
|
|
|
if (messageList.value.some((val) => val.id === message.id)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
newMessageList.push(message);
|
|
|
|
|
}
|
|
|
|
|
// 新消息追加到开头
|
|
|
|
|
messageList.value = [...newMessageList, ...messageList.value];
|
|
|
|
|
pagingRef.value.updateCache(); // 更新缓存
|
|
|
|
|
refreshMessage.value = false; // 更新好后重置状态
|
2024-07-15 03:53:35 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-18 08:54:55 +00:00
|
|
|
|
if (data.slice(-1).length > 0) {
|
|
|
|
|
// 设置最后一次历史查询的最后一条消息的 createTime
|
|
|
|
|
queryParams.createTime = formatDate(data.slice(-1)[0].createTime);
|
|
|
|
|
}
|
2024-11-10 09:56:58 +00:00
|
|
|
|
pagingRef.value.completeByNoMore(data, false);
|
2024-07-15 03:53:35 +00:00
|
|
|
|
};
|
|
|
|
|
/** 刷新消息列表 */
|
2024-11-10 09:56:58 +00:00
|
|
|
|
const refreshMessageList = async (message = undefined) => {
|
|
|
|
|
if (typeof message !== 'undefined') {
|
2024-07-16 08:23:49 +00:00
|
|
|
|
// 追加数据
|
|
|
|
|
pagingRef.value.addChatRecordData([message], false);
|
2024-11-10 09:56:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
queryParams.createTime = undefined;
|
|
|
|
|
refreshMessage.value = true;
|
|
|
|
|
await getMessageList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 若已是第一页则不做处理
|
|
|
|
|
if (queryParams.pageNo > 1) {
|
|
|
|
|
showNewMessageTip.value = true;
|
|
|
|
|
} else {
|
|
|
|
|
onScrollToUpper();
|
2024-07-15 03:53:35 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2024-07-16 08:23:49 +00:00
|
|
|
|
/** 滚动到最新消息 */
|
|
|
|
|
const onBackToTopClick = (event) => {
|
|
|
|
|
event(false); // 禁用默认操作
|
|
|
|
|
pagingRef.value.scrollToBottom();
|
2024-07-15 03:53:35 +00:00
|
|
|
|
};
|
2024-07-16 08:23:49 +00:00
|
|
|
|
/** 监听滚动到底部事件(因为 scroll 翻转了顶就是底) */
|
|
|
|
|
const onScrollToUpper = () => {
|
|
|
|
|
// 若已是第一页则不做处理
|
|
|
|
|
if (queryParams.pageNo === 1) {
|
2024-07-15 03:53:35 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-07-16 08:23:49 +00:00
|
|
|
|
showNewMessageTip.value = false;
|
2024-07-15 03:53:35 +00:00
|
|
|
|
};
|
2024-07-16 08:23:49 +00:00
|
|
|
|
defineExpose({ getMessageList, refreshMessageList });
|
2024-07-15 03:53:35 +00:00
|
|
|
|
</script>
|