fix: 交易圈投顾要是发言已读上报

This commit is contained in:
kaizheng(郑凯) 2025-02-15 17:38:39 +08:00
parent 1e6102b49a
commit 512678dde3
6 changed files with 126 additions and 65 deletions

View File

@ -181,6 +181,10 @@ const props = defineProps({
type: Object, type: Object,
default: () => {}, default: () => {},
}, },
stompClient: {
type: Object,
default: null,
},
}); });
const defaultAvatar = { const defaultAvatar = {
@ -237,6 +241,7 @@ const {
className: `.interact-scroll${props.type}`, className: `.interact-scroll${props.type}`,
id: route.query.id, id: route.query.id,
type: props.type, type: props.type,
stompClient: props.stompClient,
}); });
const maskUserName = (value) => { const maskUserName = (value) => {

View File

@ -1,62 +0,0 @@
import { readMessage } from "@/api/circle";
// type 1 公共互动 2私聊
function getStorageTeacherMsg({
productId,
productType = 41,
type = 1,
userId,
}) {
const getMsgIds = localStorage.getItem(
`read-msg-${productType}-${productId}-${userId}-${type}`
);
if (getMsgIds) {
return JSON.parse(getMsgIds);
} else {
return [];
}
}
// 老师发的言,并且已经上报的缓存起来
export function setStorageTeacherMsg({
productId,
productType = 41,
userId,
type = 1, // type 1 公共互动 2私聊
msg, // 单条消息
msgArr, // 多条消息
}) {
const storageMsgIds = getStorageTeacherMsg({
productId,
productType,
type,
userId,
});
let messageIds = [];
// userType用户类型1投顾2用户3助教4运营人员
if (msg) {
if (!storageMsgIds.includes(msg.id) && msg.userType !== 2) {
messageIds = [msg.id];
}
} else if (msgArr && msgArr.length) {
msgArr.forEach((msg) => {
if (msg.userType !== 2 && !storageMsgIds.includes(msg.id)) {
messageIds.push(msg.id);
}
});
} else {
return;
}
if (messageIds.length) {
setTimeout(async () => {
let ret = await readMessage({ messageIds }).catch(() => {});
if (ret && ret.code === 0) {
localStorage.setItem(
`read-msg-${productType}-${productId}-${userId}-${type}`,
JSON.stringify(messageIds.concat(storageMsgIds))
);
}
}, Math.random() * 3);
}
}

View File

@ -4,13 +4,13 @@ import BScroll from "@better-scroll/core";
import PullDown from "@better-scroll/pull-down"; import PullDown from "@better-scroll/pull-down";
import MouseWheel from "@better-scroll/mouse-wheel"; import MouseWheel from "@better-scroll/mouse-wheel";
import useDisableScroll from "@/hooks/useDisableScroll"; import useDisableScroll from "@/hooks/useDisableScroll";
import { setStorageTeacherMsg } from "./storage"; import useReadMessage from "./useReadMessage";
import store from "@/store/index"; import store from "@/store/index";
BScroll.use(PullDown); BScroll.use(PullDown);
BScroll.use(MouseWheel); BScroll.use(MouseWheel);
export default function useChatData({ id, className, type }) { export default function useChatData({ id, className, type, stompClient }) {
const { addScrollEvent } = useDisableScroll(); const { addScrollEvent } = useDisableScroll();
const hasNext = ref(true); const hasNext = ref(true);
let bs = ref(); let bs = ref();
@ -20,6 +20,8 @@ export default function useChatData({ id, className, type }) {
const msgIdsObj = ref({}); const msgIdsObj = ref({});
const loading = ref(false); const loading = ref(false);
const { setStorageTeacherMsg } = useReadMessage();
const getLiveHisMsg = async () => { const getLiveHisMsg = async () => {
loading.value = true; loading.value = true;
if (isPullingDown.value) return; if (isPullingDown.value) return;
@ -47,6 +49,7 @@ export default function useChatData({ id, className, type }) {
userId: store.state.userInfo.account, userId: store.state.userInfo.account,
msgArr: list, msgArr: list,
type: type === 5 ? 2 : 1, type: type === 5 ? 2 : 1,
stompClient,
}); });
} }
isPullingDown.value = false; isPullingDown.value = false;

View File

@ -0,0 +1,104 @@
// import { readMessage } from "@/api/circle";
export default function useReadMessage() {
// type 1 公共互动 2私聊
const getStorageTeacherMsg = ({
productId,
productType = 41,
type = 1,
userId,
}) => {
const getMsgIds = localStorage.getItem(
`read-msg-${productType}-${productId}-${userId}-${type}`
);
if (getMsgIds) {
return JSON.parse(getMsgIds);
} else {
return [];
}
};
// 老师发的言,并且已经上报的缓存起来
let uploadReadMsgIds = []; // 存储需要上传的ids
let timeout = null;
const uploadReadMsg = ({
productId,
productType,
userId,
type, // type 1 公共互动 2私聊
stompClient,
messageIds,
storageMsgIds,
}) => {
console.log("stompClient", stompClient);
uploadReadMsgIds = Array.from(new Set(uploadReadMsgIds.concat(messageIds)));
clearTimeout(timeout);
timeout = setTimeout(async () => {
stompClient &&
stompClient.publish({
destination: `/chat/group/readMessage`,
headers: {
userId: userId,
messageIds: uploadReadMsgIds,
},
});
localStorage.setItem(
`read-msg-${productType}-${productId}-${userId}-${type}`,
JSON.stringify(uploadReadMsgIds.concat(storageMsgIds).sort())
);
uploadReadMsgIds = [];
}, 5000);
};
const setStorageTeacherMsg = ({
productId,
productType = 41,
userId,
type = 1, // type 1 公共互动 2私聊
msg, // 单条消息
msgArr, // 多条消息
stompClient,
}) => {
const storageMsgIds = getStorageTeacherMsg({
productId,
productType,
type,
userId,
});
let messageIds = [];
// userType用户类型1投顾2用户3助教4运营人员
if (msg) {
if (!storageMsgIds.includes(msg.id) && msg.userType !== 2) {
messageIds = [msg.id];
}
} else if (msgArr && msgArr.length) {
msgArr.forEach((msg) => {
if (msg.userType !== 2 && !storageMsgIds.includes(msg.id)) {
messageIds.push(msg.id);
}
});
} else {
return;
}
if (messageIds.length) {
uploadReadMsg({
productId,
productType,
userId,
type, // type 1 公共互动 2私聊
msg, // 单条消息
msgArr, // 多条消息
stompClient,
messageIds,
storageMsgIds,
});
}
};
return {
setStorageTeacherMsg,
};
}

View File

@ -138,4 +138,7 @@ export default function ({ id, privateMessage, chatMessage }) {
} }
} }
}); });
return {
stompClient,
};
} }

View File

@ -19,6 +19,7 @@
:bsRefresh="active === 0" :bsRefresh="active === 0"
:detail="detail" :detail="detail"
:newMsg="newMsg" :newMsg="newMsg"
:stompClient="stompClient"
/> />
</van-tab> </van-tab>
<van-tab title="老师" :name="1"> <van-tab title="老师" :name="1">
@ -27,6 +28,7 @@
:bsRefresh="active === 1" :bsRefresh="active === 1"
:detail="detail" :detail="detail"
:newMsg="newMsg" :newMsg="newMsg"
:stompClient="stompClient"
/> />
</van-tab> </van-tab>
<van-tab title="私聊" :name="2"> <van-tab title="私聊" :name="2">
@ -35,6 +37,7 @@
:bsRefresh="active === 2" :bsRefresh="active === 2"
:detail="detail" :detail="detail"
:newMsg="privateNewMsg" :newMsg="privateNewMsg"
:stompClient="stompClient"
/> />
</van-tab> </van-tab>
<van-tab title="精选" :name="3"> <van-tab title="精选" :name="3">
@ -43,6 +46,7 @@
:bsRefresh="active === 3" :bsRefresh="active === 3"
:detail="detail" :detail="detail"
:newMsg="newMsg" :newMsg="newMsg"
:stompClient="stompClient"
/> />
</van-tab> </van-tab>
</van-tabs> </van-tabs>
@ -138,7 +142,11 @@ const chatMessage = (msg) => {
detail.value.showMemberCount = data; detail.value.showMemberCount = data;
} }
}; };
useWebSocket({ id: route.query.id, privateMessage, chatMessage }); const { stompClient } = useWebSocket({
id: route.query.id,
privateMessage,
chatMessage,
});
const sendMsg = async () => { const sendMsg = async () => {
let ret = await sendMessage({ let ret = await sendMessage({