Merge branch 'develop' of https://gitee.com/yudaocode/yudao-mall-uniapp into develop

pull/125/head^2
YunaiV 2024-11-10 18:50:46 +08:00
commit 01c2dfa30d
5 changed files with 65 additions and 22 deletions

View File

@ -34,10 +34,12 @@
import KeFuApi from '@/sheep/api/promotion/kefu'; import KeFuApi from '@/sheep/api/promotion/kefu';
import { isEmpty } from '@/sheep/helper/utils'; import { isEmpty } from '@/sheep/helper/utils';
import sheep from '@/sheep'; import sheep from '@/sheep';
import { formatDate } from '@/sheep/util';
const sys_navBar = sheep.$platform.navbar; const sys_navBar = sheep.$platform.navbar;
const messageList = ref([]); // const messageList = ref([]); //
const showNewMessageTip = ref(false); // const showNewMessageTip = ref(false); //
const refreshMessage = ref(false); //
const backToTopStyle = reactive({ const backToTopStyle = reactive({
'width': '100px', 'width': '100px',
'background-color': '#fff', 'background-color': '#fff',
@ -48,8 +50,9 @@
'alignItems': 'center', 'alignItems': 'center',
}); // }); //
const queryParams = reactive({ const queryParams = reactive({
pageNo: 1, pageNo: 1, //
pageSize: 10, pageSize: 20,
createTime: undefined,
}); });
const pagingRef = ref(null); // const pagingRef = ref(null); //
const queryList = async (pageNo, pageSize) => { const queryList = async (pageNo, pageSize) => {
@ -61,21 +64,47 @@
}; };
// //
const getMessageList = async () => { const getMessageList = async () => {
const { data } = await KeFuApi.getKefuMessagePage(queryParams); const { data } = await KeFuApi.getKefuMessageList(queryParams);
if (isEmpty(data.list)) { if (isEmpty(data)) {
pagingRef.value.completeByNoMore([], true);
return; return;
} }
pagingRef.value.completeByTotal(data.list, data.total); 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; //
return;
}
// createTime
queryParams.createTime = formatDate(data.at(-1).createTime);
pagingRef.value.completeByNoMore(data, false);
}; };
/** 刷新消息列表 */ /** 刷新消息列表 */
const refreshMessageList = (message = undefined) => { const refreshMessageList = async (message = undefined) => {
if (message !== undefined) { if (typeof message !== 'undefined') {
showNewMessageTip.value = true;
// //
pagingRef.value.addChatRecordData([message], false); pagingRef.value.addChatRecordData([message], false);
return; } else {
queryParams.createTime = undefined;
refreshMessage.value = true;
await getMessageList();
}
//
if (queryParams.pageNo > 1) {
showNewMessageTip.value = true;
} else {
onScrollToUpper();
} }
pagingRef.value.reload();
}; };
/** 滚动到最新消息 */ /** 滚动到最新消息 */
const onBackToTopClick = (event) => { const onBackToTopClick = (event) => {

View File

@ -46,7 +46,7 @@
<!-- 内容 --> <!-- 内容 -->
<template v-if="message.contentType === KeFuMessageContentTypeEnum.TEXT"> <template v-if="message.contentType === KeFuMessageContentTypeEnum.TEXT">
<view class="message-box" :class="{ admin: message.senderType === UserTypeEnum.ADMIN }"> <view class="message-box" :class="{ admin: message.senderType === UserTypeEnum.ADMIN }">
<mp-html :content="replaceEmoji(message.content)" /> <mp-html :content="replaceEmoji(getMessageContent(message).text || message.content)" />
</view> </view>
</template> </template>
<template v-if="message.contentType === KeFuMessageContentTypeEnum.IMAGE"> <template v-if="message.contentType === KeFuMessageContentTypeEnum.IMAGE">
@ -58,9 +58,9 @@
<su-image <su-image
class="message-img" class="message-img"
isPreview isPreview
:previewList="[sheep.$url.cdn(message.content)]" :previewList="[sheep.$url.cdn(getMessageContent(message).picUrl || message.content)]"
:current="0" :current="0"
:src="sheep.$url.cdn(message.content)" :src="sheep.$url.cdn(getMessageContent(message).picUrl || message.content)"
:height="200" :height="200"
:width="200" :width="200"
mode="aspectFill" mode="aspectFill"
@ -101,7 +101,7 @@
import { KeFuMessageContentTypeEnum, UserTypeEnum } from '@/pages/chat/util/constants'; import { KeFuMessageContentTypeEnum, UserTypeEnum } from '@/pages/chat/util/constants';
import { emojiList } from '@/pages/chat/util/emoji'; import { emojiList } from '@/pages/chat/util/emoji';
import sheep from '@/sheep'; import sheep from '@/sheep';
import { formatDate } from '@/sheep/util'; import { formatDate, jsonParse } from '@/sheep/util';
import GoodsItem from '@/pages/chat/components/goods.vue'; import GoodsItem from '@/pages/chat/components/goods.vue';
import OrderItem from '@/pages/chat/components/order.vue'; import OrderItem from '@/pages/chat/components/order.vue';
@ -122,7 +122,7 @@
default: () => [], default: () => [],
}, },
}); });
const getMessageContent = computed(() => (item) => JSON.parse(item.content)); // const getMessageContent = computed(() => (item) => jsonParse(item.content)); //
//======================= ======================= //======================= =======================

View File

@ -34,6 +34,7 @@
import FileApi from '@/sheep/api/infra/file'; import FileApi from '@/sheep/api/infra/file';
import KeFuApi from '@/sheep/api/promotion/kefu'; import KeFuApi from '@/sheep/api/promotion/kefu';
import { useWebSocket } from '@/sheep/hooks/useWebSocket'; import { useWebSocket } from '@/sheep/hooks/useWebSocket';
import { jsonParse } from '@/sheep/util';
const sys_navBar = sheep.$platform.navbar; const sys_navBar = sheep.$platform.navbar;
@ -52,7 +53,7 @@
try { try {
const data = { const data = {
contentType: KeFuMessageContentTypeEnum.TEXT, contentType: KeFuMessageContentTypeEnum.TEXT,
content: chat.msg, content: JSON.stringify({ text: chat.msg }),
}; };
await KeFuApi.sendKefuMessage(data); await KeFuApi.sendKefuMessage(data);
await messageListRef.value.refreshMessageList(); await messageListRef.value.refreshMessageList();
@ -104,7 +105,7 @@
const res = await FileApi.uploadFile(data.tempFiles[0].path); const res = await FileApi.uploadFile(data.tempFiles[0].path);
msg = { msg = {
contentType: KeFuMessageContentTypeEnum.IMAGE, contentType: KeFuMessageContentTypeEnum.IMAGE,
content: res.data, content: JSON.stringify({picUrl: res.data}),
}; };
break; break;
case 'goods': case 'goods':
@ -140,13 +141,13 @@
onMessage: async (data) => { onMessage: async (data) => {
const type = data.type; const type = data.type;
if (!type) { if (!type) {
console.error('未知的消息类型:' + data.value); console.error('未知的消息类型:' + data);
return; return;
} }
// 2.2 KEFU_MESSAGE_TYPE // 2.2 KEFU_MESSAGE_TYPE
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) { if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
// //
await messageListRef.value.refreshMessageList(JSON.parse(data.content)); await messageListRef.value.refreshMessageList(jsonParse(data.content));
return; return;
} }
// 2.3 KEFU_MESSAGE_ADMIN_READ // 2.3 KEFU_MESSAGE_ADMIN_READ

View File

@ -15,9 +15,9 @@ const KeFuApi = {
}, },
}); });
}, },
getKefuMessagePage: (params) => { getKefuMessageList: (params) => {
return request({ return request({
url: '/promotion/kefu-message/page', url: '/promotion/kefu-message/list',
method: 'GET', method: 'GET',
params, params,
custom: { custom: {

View File

@ -131,3 +131,16 @@ export const copyValueToTarget = (target, source) => {
// 更新目标对象值 // 更新目标对象值
Object.assign(target, newObj) Object.assign(target, newObj)
} }
/**
* 解析 JSON 字符串
*
* @param str
*/
export function jsonParse(str) {
try {
return JSON.parse(str)
} catch (e) {
console.error(`str[${str}] 不是一个 JSON 字符串`)
return ''
}
}