|
|
|
@ -59,7 +59,7 @@ |
|
|
|
<script setup name="planNoticeMessage"> |
|
|
|
import { showToast } from 'vant'; |
|
|
|
import 'vant/es/toast/style'; |
|
|
|
import { getPlanNoticeMessageList, addPlanNoticeMessage } from '@/api/enterprise/planNotice'; |
|
|
|
import { getPlanNoticeMessageList, addPlanNoticeMessage, markMessageViewed } from '@/api/enterprise/planNotice'; |
|
|
|
|
|
|
|
const router = useRouter(); |
|
|
|
const route = useRoute(); |
|
|
|
@ -72,11 +72,52 @@ const loading = ref(false); |
|
|
|
const inputMessage = ref(''); |
|
|
|
const sending = ref(false); |
|
|
|
const messageAreaRef = ref(null); |
|
|
|
let pollTimer = null; |
|
|
|
|
|
|
|
onMounted(() => { |
|
|
|
loadMessages(); |
|
|
|
// 打开页面时标记所有消息为企业已读 |
|
|
|
if (noticeId.value) { |
|
|
|
markMessageViewed(noticeId.value); |
|
|
|
} |
|
|
|
// 启动15秒轮询 |
|
|
|
startPolling(); |
|
|
|
}); |
|
|
|
|
|
|
|
onUnmounted(() => { |
|
|
|
stopPolling(); |
|
|
|
}); |
|
|
|
|
|
|
|
const startPolling = () => { |
|
|
|
stopPolling(); |
|
|
|
pollTimer = setInterval(() => { |
|
|
|
pollMessages(); |
|
|
|
}, 15000); |
|
|
|
}; |
|
|
|
|
|
|
|
const stopPolling = () => { |
|
|
|
if (pollTimer) { |
|
|
|
clearInterval(pollTimer); |
|
|
|
pollTimer = null; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// 轮询加载新消息(静默加载,不显示loading) |
|
|
|
const pollMessages = () => { |
|
|
|
if (!noticeId.value) return; |
|
|
|
getPlanNoticeMessageList({ noticeId: noticeId.value }, (res) => { |
|
|
|
if (res.code === 200 && Array.isArray(res.data)) { |
|
|
|
const oldCount = messageList.value.length; |
|
|
|
messageList.value = res.data; |
|
|
|
// 有新消息时滚动到底部,并标记已读 |
|
|
|
if (res.data.length > oldCount) { |
|
|
|
scrollToBottom(); |
|
|
|
markMessageViewed(noticeId.value); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
const loadMessages = () => { |
|
|
|
if (!noticeId.value) return; |
|
|
|
loading.value = true; |
|
|
|
|