mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-08 04:50:45 +08:00
Merge pull request #689 from nagisa77/feature/daily_bugfix_0822
fix: 消息页面ui重构
This commit is contained in:
@@ -47,10 +47,12 @@
|
|||||||
</div>
|
</div>
|
||||||
</ToolTip>
|
</ToolTip>
|
||||||
|
|
||||||
<ToolTip v-if="isLogin" content="站内信" placement="bottom">
|
<ToolTip v-if="isLogin" content="站内信和频道" placement="bottom">
|
||||||
<div class="messages-icon" @click="goToMessages">
|
<div class="messages-icon" @click="goToMessages">
|
||||||
<i class="fas fa-envelope"></i>
|
<i class="fas fa-comments"></i>
|
||||||
<span v-if="unreadMessageCount > 0" class="unread-badge">{{ unreadMessageCount }}</span>
|
<span v-if="unreadMessageCount > 0" class="unread-badge">{{
|
||||||
|
unreadMessageCount
|
||||||
|
}}</span>
|
||||||
</div>
|
</div>
|
||||||
</ToolTip>
|
</ToolTip>
|
||||||
|
|
||||||
@@ -193,14 +195,14 @@ const refrechData = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const goToMessages = () => {
|
const goToMessages = () => {
|
||||||
navigateTo('/messages');
|
navigateTo('/message-box')
|
||||||
};
|
}
|
||||||
|
|
||||||
const headerMenuItems = computed(() => [
|
const headerMenuItems = computed(() => [
|
||||||
{ text: '设置', onClick: goToSettings },
|
{ text: '设置', onClick: goToSettings },
|
||||||
{ text: '个人主页', onClick: goToProfile },
|
{ text: '个人主页', onClick: goToProfile },
|
||||||
{ text: '退出', onClick: goToLogout },
|
{ text: '退出', onClick: goToLogout },
|
||||||
]);
|
])
|
||||||
|
|
||||||
/** 其余逻辑保持不变 */
|
/** 其余逻辑保持不变 */
|
||||||
const iconClass = computed(() => {
|
const iconClass = computed(() => {
|
||||||
@@ -226,7 +228,7 @@ onMounted(async () => {
|
|||||||
const updateUnread = async () => {
|
const updateUnread = async () => {
|
||||||
if (authState.loggedIn) {
|
if (authState.loggedIn) {
|
||||||
// Initialize the unread count composable
|
// Initialize the unread count composable
|
||||||
fetchUnreadCount();
|
fetchUnreadCount()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -147,7 +147,6 @@ export default {
|
|||||||
.message-editor-container {
|
.message-editor-container {
|
||||||
border: 1px solid var(--border-color);
|
border: 1px solid var(--border-color);
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
margin-top: 20px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.message-bottom-container {
|
.message-bottom-container {
|
||||||
@@ -180,4 +179,4 @@ export default {
|
|||||||
.message-submit:not(.disabled):hover {
|
.message-submit:not(.disabled):hover {
|
||||||
background-color: var(--primary-color-hover);
|
background-color: var(--primary-color-hover);
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
456
frontend_nuxt/pages/message-box/[id].vue
Normal file
456
frontend_nuxt/pages/message-box/[id].vue
Normal file
@@ -0,0 +1,456 @@
|
|||||||
|
<template>
|
||||||
|
<div class="chat-container">
|
||||||
|
<div v-if="!loading && otherParticipant" class="chat-header">
|
||||||
|
<NuxtLink to="/message-box" class="back-button">
|
||||||
|
<i class="fas fa-arrow-left"></i>
|
||||||
|
</NuxtLink>
|
||||||
|
<h2 class="participant-name">{{ otherParticipant.username }}</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="messages-list" ref="messagesListEl">
|
||||||
|
<div v-if="loading" class="loading-container">加载中...</div>
|
||||||
|
<div v-else-if="error" class="error-container">{{ error }}</div>
|
||||||
|
<template v-else>
|
||||||
|
<div class="load-more-container" v-if="hasMoreMessages">
|
||||||
|
<button @click="loadMoreMessages" :disabled="loadingMore" class="load-more-button">
|
||||||
|
{{ loadingMore ? '加载中...' : '查看更多消息' }}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<BaseTimeline :items="messages">
|
||||||
|
<template #item="{ item }">
|
||||||
|
<div class="message-timestamp">
|
||||||
|
{{ TimeManager.format(item.createdAt) }}
|
||||||
|
</div>
|
||||||
|
<div class="message-content">
|
||||||
|
<div class="info-content-text" v-html="renderMarkdown(item.content)"></div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</BaseTimeline>
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="message-input-area">
|
||||||
|
<MessageEditor :loading="sending" @submit="sendMessage" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import {
|
||||||
|
ref,
|
||||||
|
onMounted,
|
||||||
|
onUnmounted,
|
||||||
|
nextTick,
|
||||||
|
computed,
|
||||||
|
watch,
|
||||||
|
onActivated,
|
||||||
|
onDeactivated,
|
||||||
|
} from 'vue'
|
||||||
|
import { useRoute } from 'vue-router'
|
||||||
|
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
||||||
|
import { toast } from '~/main'
|
||||||
|
import { renderMarkdown } from '~/utils/markdown'
|
||||||
|
import MessageEditor from '~/components/MessageEditor.vue'
|
||||||
|
import { useWebSocket } from '~/composables/useWebSocket'
|
||||||
|
import { useUnreadCount } from '~/composables/useUnreadCount'
|
||||||
|
import TimeManager from '~/utils/time'
|
||||||
|
import BaseTimeline from '~/components/BaseTimeline.vue'
|
||||||
|
|
||||||
|
const config = useRuntimeConfig()
|
||||||
|
const route = useRoute()
|
||||||
|
const API_BASE_URL = config.public.apiBaseUrl
|
||||||
|
const { connect, disconnect, subscribe, isConnected } = useWebSocket()
|
||||||
|
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount()
|
||||||
|
let subscription = null
|
||||||
|
|
||||||
|
const messages = ref([])
|
||||||
|
const participants = ref([])
|
||||||
|
const loading = ref(true)
|
||||||
|
const sending = ref(false)
|
||||||
|
const error = ref(null)
|
||||||
|
const conversationId = route.params.id
|
||||||
|
const currentUser = ref(null)
|
||||||
|
const messagesListEl = ref(null)
|
||||||
|
let lastMessageEl = null
|
||||||
|
const currentPage = ref(0)
|
||||||
|
const totalPages = ref(0)
|
||||||
|
const loadingMore = ref(false)
|
||||||
|
let scrollInterval = null
|
||||||
|
|
||||||
|
const hasMoreMessages = computed(() => currentPage.value < totalPages.value - 1)
|
||||||
|
|
||||||
|
const otherParticipant = computed(() => {
|
||||||
|
if (!currentUser.value || participants.value.length === 0) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return participants.value.find((p) => p.id !== currentUser.value.id)
|
||||||
|
})
|
||||||
|
|
||||||
|
function isSentByCurrentUser(message) {
|
||||||
|
return message.sender.id === currentUser.value?.id
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleAvatarError(event) {
|
||||||
|
event.target.src = '/default-avatar.svg'
|
||||||
|
}
|
||||||
|
|
||||||
|
// No changes needed here, as renderMarkdown is now imported.
|
||||||
|
// The old function is removed.
|
||||||
|
|
||||||
|
async function fetchMessages(page = 0) {
|
||||||
|
if (page === 0) {
|
||||||
|
loading.value = true
|
||||||
|
messages.value = []
|
||||||
|
} else {
|
||||||
|
loadingMore.value = true
|
||||||
|
}
|
||||||
|
error.value = null
|
||||||
|
const token = getToken()
|
||||||
|
if (!token) {
|
||||||
|
toast.error('请先登录')
|
||||||
|
loading.value = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(
|
||||||
|
`${API_BASE_URL}/api/messages/conversations/${conversationId}?page=${page}&size=20`,
|
||||||
|
{
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if (!response.ok) throw new Error('无法加载消息')
|
||||||
|
|
||||||
|
const conversationData = await response.json()
|
||||||
|
const pageData = conversationData.messages
|
||||||
|
|
||||||
|
if (page === 0) {
|
||||||
|
participants.value = conversationData.participants
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since the backend sorts by descending, we need to reverse for correct chat order
|
||||||
|
const newMessages = pageData.content.reverse().map((item) => ({
|
||||||
|
...item,
|
||||||
|
src: item.sender.avatar,
|
||||||
|
iconClick: () => {
|
||||||
|
navigateTo(`/users/${item.sender.id}`, { replace: true })
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
const list = messagesListEl.value
|
||||||
|
const oldScrollHeight = list ? list.scrollHeight : 0
|
||||||
|
|
||||||
|
if (page === 0) {
|
||||||
|
messages.value = newMessages
|
||||||
|
} else {
|
||||||
|
messages.value = [...newMessages, ...messages.value]
|
||||||
|
}
|
||||||
|
|
||||||
|
currentPage.value = pageData.number
|
||||||
|
totalPages.value = pageData.totalPages
|
||||||
|
|
||||||
|
// Scrolling is now fully handled by the watcher
|
||||||
|
await nextTick()
|
||||||
|
if (page > 0 && list) {
|
||||||
|
const newScrollHeight = list.scrollHeight
|
||||||
|
list.scrollTop = newScrollHeight - oldScrollHeight
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error.value = e.message
|
||||||
|
toast.error(e.message)
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
loadingMore.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadMoreMessages() {
|
||||||
|
if (hasMoreMessages.value && !loadingMore.value) {
|
||||||
|
await fetchMessages(currentPage.value + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendMessage(content, clearInput) {
|
||||||
|
if (!content.trim()) return
|
||||||
|
|
||||||
|
const recipient = otherParticipant.value
|
||||||
|
if (!recipient) {
|
||||||
|
toast.error('无法确定收信人')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sending.value = true
|
||||||
|
const token = getToken()
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE_URL}/api/messages`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
recipientId: recipient.id,
|
||||||
|
content: content,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
if (!response.ok) throw new Error('发送失败')
|
||||||
|
|
||||||
|
const newMessage = await response.json()
|
||||||
|
messages.value.push(newMessage)
|
||||||
|
clearInput()
|
||||||
|
|
||||||
|
// Use a more reliable scroll approach
|
||||||
|
setTimeout(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
}, 100)
|
||||||
|
} catch (e) {
|
||||||
|
toast.error(e.message)
|
||||||
|
} finally {
|
||||||
|
sending.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function markConversationAsRead() {
|
||||||
|
const token = getToken()
|
||||||
|
if (!token) return
|
||||||
|
try {
|
||||||
|
await fetch(`${API_BASE_URL}/api/messages/conversations/${conversationId}/read`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
})
|
||||||
|
// After marking as read, refresh the global unread count
|
||||||
|
refreshGlobalUnreadCount()
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to mark conversation as read', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function scrollToBottom() {
|
||||||
|
if (messagesListEl.value) {
|
||||||
|
const element = messagesListEl.value
|
||||||
|
// 強制滾動到底部,使用 smooth 行為確保視覺效果
|
||||||
|
element.scrollTop = element.scrollHeight
|
||||||
|
|
||||||
|
// 再次確認滾動位置
|
||||||
|
setTimeout(() => {
|
||||||
|
if (element.scrollTop < element.scrollHeight - element.clientHeight) {
|
||||||
|
element.scrollTop = element.scrollHeight
|
||||||
|
}
|
||||||
|
}, 50)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
messages,
|
||||||
|
async (newMessages) => {
|
||||||
|
if (newMessages.length === 0) return
|
||||||
|
|
||||||
|
await nextTick()
|
||||||
|
|
||||||
|
// Simple, reliable scroll to bottom
|
||||||
|
setTimeout(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
}, 100)
|
||||||
|
},
|
||||||
|
{ deep: true },
|
||||||
|
)
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
currentUser.value = await fetchCurrentUser()
|
||||||
|
if (currentUser.value) {
|
||||||
|
await fetchMessages(0)
|
||||||
|
await markConversationAsRead()
|
||||||
|
const token = getToken()
|
||||||
|
if (token && !isConnected.value) {
|
||||||
|
connect(token)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
toast.error('请先登录')
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
watch(isConnected, (newValue) => {
|
||||||
|
if (newValue) {
|
||||||
|
// 等待一小段时间确保连接稳定
|
||||||
|
setTimeout(() => {
|
||||||
|
subscription = subscribe(`/topic/conversation/${conversationId}`, (message) => {
|
||||||
|
// 避免重复显示当前用户发送的消息
|
||||||
|
if (message.sender.id !== currentUser.value.id) {
|
||||||
|
messages.value.push(message)
|
||||||
|
// 实时收到消息时自动标记为已读
|
||||||
|
markConversationAsRead()
|
||||||
|
setTimeout(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
}, 100)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, 500)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
onActivated(async () => {
|
||||||
|
// This will be called every time the component is activated (navigated to)
|
||||||
|
if (currentUser.value) {
|
||||||
|
await fetchMessages(0)
|
||||||
|
await markConversationAsRead()
|
||||||
|
|
||||||
|
// 確保滾動到底部 - 使用多重延遲策略
|
||||||
|
await nextTick()
|
||||||
|
setTimeout(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
}, 100)
|
||||||
|
setTimeout(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
}, 300)
|
||||||
|
setTimeout(() => {
|
||||||
|
scrollToBottom()
|
||||||
|
}, 500)
|
||||||
|
|
||||||
|
if (!isConnected.value) {
|
||||||
|
const token = getToken()
|
||||||
|
if (token) connect(token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
onDeactivated(() => {
|
||||||
|
if (subscription) {
|
||||||
|
subscription.unsubscribe()
|
||||||
|
subscription = null
|
||||||
|
}
|
||||||
|
disconnect()
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
if (subscription) {
|
||||||
|
subscription.unsubscribe()
|
||||||
|
subscription = null
|
||||||
|
}
|
||||||
|
disconnect()
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.chat-container {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
margin: 0 auto;
|
||||||
|
overflow: auto;
|
||||||
|
height: calc(100vh - var(--header-height));
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.chat-header {
|
||||||
|
display: flex;
|
||||||
|
position: sticky;
|
||||||
|
top: 0;
|
||||||
|
z-index: 100;
|
||||||
|
align-items: center;
|
||||||
|
padding: 10px 20px;
|
||||||
|
border-bottom: 1px solid var(--normal-border-color);
|
||||||
|
background-color: var(--background-color-blur);
|
||||||
|
backdrop-filter: var(--blur-10);
|
||||||
|
}
|
||||||
|
|
||||||
|
.back-button {
|
||||||
|
font-size: 18px;
|
||||||
|
color: var(--text-color-primary);
|
||||||
|
margin-right: 15px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.participant-name {
|
||||||
|
font-size: 18px;
|
||||||
|
font-weight: 600;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.messages-list {
|
||||||
|
overflow-y: auto;
|
||||||
|
padding: 20px;
|
||||||
|
padding-bottom: 100px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 20px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.load-more-container {
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.load-more-button {
|
||||||
|
background-color: var(--bg-color-soft);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
color: var(--text-color-primary);
|
||||||
|
padding: 8px 16px;
|
||||||
|
border-radius: 20px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.load-more-button:hover {
|
||||||
|
background-color: var(--border-color);
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-item {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-avatar {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border-radius: 50%;
|
||||||
|
align-self: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-content {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-timestamp {
|
||||||
|
font-size: 11px;
|
||||||
|
color: var(--text-color-secondary);
|
||||||
|
margin-top: 5px;
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-item.sent {
|
||||||
|
align-self: flex-end;
|
||||||
|
flex-direction: row-reverse;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-item.sent .message-timestamp {
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Received messages */
|
||||||
|
.message-item.received {
|
||||||
|
align-self: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-item.received .message-content {
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-item.received .message-bubble {
|
||||||
|
background-color: var(--bg-color-soft);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-bottom-left-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-input-area {
|
||||||
|
margin-left: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loading-container,
|
||||||
|
.error-container {
|
||||||
|
text-align: center;
|
||||||
|
padding: 50px;
|
||||||
|
color: var(--text-color-secondary);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,108 +1,105 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="messages-container">
|
<div class="messages-container">
|
||||||
<div class="messages-header">
|
<div v-if="loading" class="loading-message">
|
||||||
<h1 class="messages-title">站内信</h1>
|
<l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="loading" class="loading-container">
|
|
||||||
<div class="loading-text">加载中...</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-else-if="error" class="error-container">
|
<div v-else-if="error" class="error-container">
|
||||||
<div class="error-text">{{ error }}</div>
|
<div class="error-text">{{ error }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-else-if="conversations.length === 0" class="empty-container">
|
<div v-else-if="conversations.length === 0" class="empty-container">
|
||||||
<div class="empty-text">暂无会话</div>
|
<div class="empty-text">暂无会话</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-else class="conversations-list">
|
<div
|
||||||
<div
|
v-for="convo in conversations"
|
||||||
v-for="convo in conversations"
|
:key="convo.id"
|
||||||
:key="convo.id"
|
class="conversation-item"
|
||||||
class="conversation-item"
|
@click="goToConversation(convo.id)"
|
||||||
@click="goToConversation(convo.id)"
|
>
|
||||||
>
|
<div class="conversation-avatar">
|
||||||
<div class="conversation-avatar">
|
<img
|
||||||
<img
|
:src="getOtherParticipant(convo)?.avatar || '/default-avatar.svg'"
|
||||||
:src="getOtherParticipant(convo)?.avatar || '/default-avatar.svg'"
|
:alt="getOtherParticipant(convo)?.username || '用户'"
|
||||||
:alt="getOtherParticipant(convo)?.username || '用户'"
|
class="avatar-img"
|
||||||
class="avatar-img"
|
@error="handleAvatarError"
|
||||||
@error="handleAvatarError"
|
/>
|
||||||
/>
|
</div>
|
||||||
</div>
|
|
||||||
|
<div class="conversation-content">
|
||||||
<div class="conversation-content">
|
<div class="conversation-header">
|
||||||
<div class="conversation-header">
|
<div class="participant-name">
|
||||||
<div class="participant-name">
|
{{ getOtherParticipant(convo)?.username || '未知用户' }}
|
||||||
{{ getOtherParticipant(convo)?.username || '未知用户' }}
|
|
||||||
</div>
|
|
||||||
<div class="message-time">
|
|
||||||
{{ formatTime(convo.lastMessage?.createdAt || convo.createdAt) }}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div class="message-time">
|
||||||
<div class="last-message-row">
|
{{ formatTime(convo.lastMessage?.createdAt || convo.createdAt) }}
|
||||||
<div class="last-message">
|
|
||||||
{{ convo.lastMessage ? convo.lastMessage.content : '暂无消息' }}
|
|
||||||
</div>
|
|
||||||
<div v-if="convo.unreadCount > 0" class="unread-count-badge">
|
|
||||||
{{ convo.unreadCount }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="last-message-row">
|
||||||
|
<div class="last-message">
|
||||||
|
{{
|
||||||
|
convo.lastMessage ? stripMarkdownLength(convo.lastMessage.content, 100) : '暂无消息'
|
||||||
|
}}
|
||||||
|
</div>
|
||||||
|
<div v-if="convo.unreadCount > 0" class="unread-count-badge">
|
||||||
|
{{ convo.unreadCount }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, onMounted, onUnmounted, watch, onActivated } from 'vue';
|
import { ref, onUnmounted, watch, onActivated } from 'vue'
|
||||||
import { useRouter } from 'vue-router';
|
import { useRouter } from 'vue-router'
|
||||||
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
import { getToken, fetchCurrentUser } from '~/utils/auth'
|
||||||
import { toast } from '~/main'
|
import { toast } from '~/main'
|
||||||
import { useWebSocket } from '~/composables/useWebSocket';
|
import { useWebSocket } from '~/composables/useWebSocket'
|
||||||
import { useUnreadCount } from '~/composables/useUnreadCount';
|
import { useUnreadCount } from '~/composables/useUnreadCount'
|
||||||
import TimeManager from '~/utils/time'
|
import TimeManager from '~/utils/time'
|
||||||
|
import { stripMarkdownLength } from '~/utils/markdown'
|
||||||
|
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const conversations = ref([]);
|
const conversations = ref([])
|
||||||
const loading = ref(true);
|
const loading = ref(true)
|
||||||
const error = ref(null);
|
const error = ref(null)
|
||||||
const router = useRouter();
|
const router = useRouter()
|
||||||
const currentUser = ref(null);
|
const currentUser = ref(null)
|
||||||
const API_BASE_URL = config.public.apiBaseUrl
|
const API_BASE_URL = config.public.apiBaseUrl
|
||||||
const { connect, disconnect, subscribe, isConnected } = useWebSocket();
|
const { connect, disconnect, subscribe, isConnected } = useWebSocket()
|
||||||
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount();
|
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount()
|
||||||
let subscription = null;
|
let subscription = null
|
||||||
|
|
||||||
async function fetchConversations() {
|
async function fetchConversations() {
|
||||||
const token = getToken();
|
const token = getToken()
|
||||||
if (!token) {
|
if (!token) {
|
||||||
toast.error('请先登录');
|
toast.error('请先登录')
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${API_BASE_URL}/api/messages/conversations`, {
|
const response = await fetch(`${API_BASE_URL}/api/messages/conversations`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
});
|
})
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP error! status: ${response.status}`);
|
throw new Error(`HTTP error! status: ${response.status}`)
|
||||||
}
|
}
|
||||||
const data = await response.json();
|
const data = await response.json()
|
||||||
conversations.value = data;
|
conversations.value = data
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error.value = '无法加载会话列表。';
|
error.value = '无法加载会话列表。'
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false;
|
loading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取对话中的另一个参与者(非当前用户)
|
// 获取对话中的另一个参与者(非当前用户)
|
||||||
function getOtherParticipant(conversation) {
|
function getOtherParticipant(conversation) {
|
||||||
if (!currentUser.value || !conversation.participants) return null
|
if (!currentUser.value || !conversation.participants) return null
|
||||||
return conversation.participants.find(p => p.id !== currentUser.value.id)
|
return conversation.participants.find((p) => p.id !== currentUser.value.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 格式化时间
|
// 格式化时间
|
||||||
@@ -117,57 +114,61 @@ function handleAvatarError(event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onActivated(async () => {
|
onActivated(async () => {
|
||||||
loading.value = true;
|
loading.value = true
|
||||||
currentUser.value = await fetchCurrentUser();
|
currentUser.value = await fetchCurrentUser()
|
||||||
|
|
||||||
if (currentUser.value) {
|
if (currentUser.value) {
|
||||||
await fetchConversations();
|
await fetchConversations()
|
||||||
refreshGlobalUnreadCount(); // Refresh global count when entering the list
|
refreshGlobalUnreadCount() // Refresh global count when entering the list
|
||||||
const token = getToken();
|
const token = getToken()
|
||||||
if (token && !isConnected.value) {
|
if (token && !isConnected.value) {
|
||||||
connect(token);
|
connect(token)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
loading.value = false;
|
loading.value = false
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
watch(isConnected, (newValue) => {
|
watch(isConnected, (newValue) => {
|
||||||
|
|
||||||
if (newValue && currentUser.value) {
|
if (newValue && currentUser.value) {
|
||||||
const destination = `/topic/user/${currentUser.value.id}/messages`;
|
const destination = `/topic/user/${currentUser.value.id}/messages`
|
||||||
|
|
||||||
// 清理旧的订阅
|
// 清理旧的订阅
|
||||||
if (subscription) {
|
if (subscription) {
|
||||||
subscription.unsubscribe();
|
subscription.unsubscribe()
|
||||||
}
|
}
|
||||||
|
|
||||||
subscription = subscribe(destination, (message) => {
|
subscription = subscribe(destination, (message) => {
|
||||||
fetchConversations();
|
fetchConversations()
|
||||||
});
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
if (subscription) {
|
if (subscription) {
|
||||||
subscription.unsubscribe();
|
subscription.unsubscribe()
|
||||||
}
|
}
|
||||||
disconnect();
|
disconnect()
|
||||||
});
|
})
|
||||||
|
|
||||||
function goToConversation(id) {
|
function goToConversation(id) {
|
||||||
router.push(`/messages/${id}`);
|
router.push(`/message-box/${id}`)
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.messages-container {
|
.messages-container {
|
||||||
max-width: 800px;
|
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.loading-message {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 300px;
|
||||||
|
}
|
||||||
|
|
||||||
.messages-header {
|
.messages-header {
|
||||||
margin-bottom: 24px;
|
margin-bottom: 24px;
|
||||||
}
|
}
|
||||||
@@ -179,14 +180,18 @@ function goToConversation(id) {
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.loading-container, .error-container, .empty-container {
|
.loading-container,
|
||||||
|
.error-container,
|
||||||
|
.empty-container {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
min-height: 200px;
|
min-height: 200px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.loading-text, .error-text, .empty-text {
|
.loading-text,
|
||||||
|
.error-text,
|
||||||
|
.empty-text {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
color: #666;
|
color: #666;
|
||||||
}
|
}
|
||||||
@@ -196,61 +201,30 @@ function goToConversation(id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.conversations-list {
|
.conversations-list {
|
||||||
background: #fff;
|
|
||||||
border-radius: 12px;
|
|
||||||
border: 1px solid #e2e8f0;
|
|
||||||
overflow: hidden;
|
|
||||||
max-height: 600px;
|
|
||||||
overflow-y: auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 美化滚动条 */
|
|
||||||
.conversations-list::-webkit-scrollbar {
|
|
||||||
width: 6px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.conversations-list::-webkit-scrollbar-track {
|
|
||||||
background: #f1f5f9;
|
|
||||||
border-radius: 3px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.conversations-list::-webkit-scrollbar-thumb {
|
|
||||||
background: #cbd5e1;
|
|
||||||
border-radius: 3px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.conversations-list::-webkit-scrollbar-thumb:hover {
|
|
||||||
background: #94a3b8;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-item {
|
.conversation-item {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
padding: 16px 20px;
|
padding: 8px 10px;
|
||||||
border-bottom: 1px solid #f7fafc;
|
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background-color 0.2s ease;
|
transition: background-color 0.2s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-item:last-child {
|
|
||||||
border-bottom: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.conversation-item:hover {
|
.conversation-item:hover {
|
||||||
background-color: #f7fafc;
|
background-color: var(--menu-selected-background-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-avatar {
|
.conversation-avatar {
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
margin-right: 16px;
|
margin-right: 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.avatar-img {
|
.avatar-img {
|
||||||
width: 48px;
|
width: 40px;
|
||||||
height: 48px;
|
height: 40px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
border: 2px solid #e2e8f0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-content {
|
.conversation-content {
|
||||||
@@ -268,13 +242,12 @@ function goToConversation(id) {
|
|||||||
.participant-name {
|
.participant-name {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: #2d3748;
|
color: var(--text-color);
|
||||||
truncate: true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.message-time {
|
.message-time {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
color: #a0aec0;
|
color: gray;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
margin-left: 12px;
|
margin-left: 12px;
|
||||||
}
|
}
|
||||||
@@ -287,7 +260,7 @@ function goToConversation(id) {
|
|||||||
|
|
||||||
.last-message {
|
.last-message {
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
color: #4a5568;
|
color: gray;
|
||||||
line-height: 1.4;
|
line-height: 1.4;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
@@ -312,32 +285,32 @@ function goToConversation(id) {
|
|||||||
.messages-container {
|
.messages-container {
|
||||||
padding: 16px 12px;
|
padding: 16px 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.messages-title {
|
.messages-title {
|
||||||
font-size: 24px;
|
font-size: 24px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversations-list {
|
.conversations-list {
|
||||||
max-height: 500px;
|
max-height: 500px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-item {
|
.conversation-item {
|
||||||
padding: 12px 16px;
|
padding: 12px 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.avatar-img {
|
.avatar-img {
|
||||||
width: 40px;
|
width: 40px;
|
||||||
height: 40px;
|
height: 40px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.participant-name {
|
.participant-name {
|
||||||
font-size: 15px;
|
font-size: 15px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.message-time {
|
.message-time {
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.last-message {
|
.last-message {
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
}
|
}
|
||||||
@@ -347,20 +320,20 @@ function goToConversation(id) {
|
|||||||
.messages-container {
|
.messages-container {
|
||||||
padding: 12px 8px;
|
padding: 12px 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversations-list {
|
.conversations-list {
|
||||||
max-height: 400px;
|
max-height: 400px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-item {
|
.conversation-item {
|
||||||
padding: 10px 12px;
|
padding: 10px 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.avatar-img {
|
.avatar-img {
|
||||||
width: 36px;
|
width: 36px;
|
||||||
height: 36px;
|
height: 36px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.conversation-avatar {
|
.conversation-avatar {
|
||||||
margin-right: 12px;
|
margin-right: 12px;
|
||||||
}
|
}
|
||||||
@@ -372,4 +345,4 @@ function goToConversation(id) {
|
|||||||
max-height: 700px;
|
max-height: 700px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -1,490 +0,0 @@
|
|||||||
<template>
|
|
||||||
<div class="chat-container">
|
|
||||||
<div v-if="!loading && otherParticipant" class="chat-header">
|
|
||||||
<NuxtLink to="/messages" class="back-button">
|
|
||||||
<i class="fas fa-arrow-left"></i>
|
|
||||||
</NuxtLink>
|
|
||||||
<h2 class="participant-name">{{ otherParticipant.username }}</h2>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="messages-list" ref="messagesListEl">
|
|
||||||
<div v-if="loading" class="loading-container">加载中...</div>
|
|
||||||
<div v-else-if="error" class="error-container">{{ error }}</div>
|
|
||||||
<template v-else>
|
|
||||||
<div class="load-more-container" v-if="hasMoreMessages">
|
|
||||||
<button @click="loadMoreMessages" :disabled="loadingMore" class="load-more-button">
|
|
||||||
{{ loadingMore ? '加载中...' : '查看更多消息' }}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
v-for="(msg, index) in messages"
|
|
||||||
:key="msg.id"
|
|
||||||
:ref="el => { if (index === messages.length - 1) lastMessageEl = el }"
|
|
||||||
class="message-item"
|
|
||||||
:class="{ sent: isSentByCurrentUser(msg), received: !isSentByCurrentUser(msg) }"
|
|
||||||
>
|
|
||||||
<img
|
|
||||||
:src="msg.sender.avatar"
|
|
||||||
alt="avatar"
|
|
||||||
class="message-avatar"
|
|
||||||
@error="handleAvatarError"
|
|
||||||
/>
|
|
||||||
<div class="message-content">
|
|
||||||
<div class="message-bubble">
|
|
||||||
<div class="message-text" v-html="renderMarkdown(msg.content)"></div>
|
|
||||||
</div>
|
|
||||||
<div class="message-timestamp">
|
|
||||||
{{ TimeManager.format(msg.createdAt) }}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="message-input-area">
|
|
||||||
<MessageEditor
|
|
||||||
:loading="sending"
|
|
||||||
@submit="sendMessage"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script setup>
|
|
||||||
import { ref, onMounted, onUnmounted, nextTick, computed, watch, onActivated, onDeactivated } from 'vue';
|
|
||||||
import { useRoute } from 'vue-router';
|
|
||||||
import { getToken, fetchCurrentUser } from '~/utils/auth';
|
|
||||||
import { toast } from '~/main';
|
|
||||||
import { renderMarkdown } from '~/utils/markdown';
|
|
||||||
import MessageEditor from '~/components/MessageEditor.vue';
|
|
||||||
import { useWebSocket } from '~/composables/useWebSocket';
|
|
||||||
import { useUnreadCount } from '~/composables/useUnreadCount';
|
|
||||||
import TimeManager from '~/utils/time'
|
|
||||||
|
|
||||||
const config = useRuntimeConfig();
|
|
||||||
const route = useRoute();
|
|
||||||
const API_BASE_URL = config.public.apiBaseUrl;
|
|
||||||
const { connect, disconnect, subscribe, isConnected } = useWebSocket();
|
|
||||||
const { fetchUnreadCount: refreshGlobalUnreadCount } = useUnreadCount();
|
|
||||||
let subscription = null;
|
|
||||||
|
|
||||||
const messages = ref([]);
|
|
||||||
const participants = ref([]);
|
|
||||||
const loading = ref(true);
|
|
||||||
const sending = ref(false);
|
|
||||||
const error = ref(null);
|
|
||||||
const conversationId = route.params.id;
|
|
||||||
const currentUser = ref(null);
|
|
||||||
const messagesListEl = ref(null);
|
|
||||||
let lastMessageEl = null;
|
|
||||||
const currentPage = ref(0);
|
|
||||||
const totalPages = ref(0);
|
|
||||||
const loadingMore = ref(false);
|
|
||||||
let scrollInterval = null;
|
|
||||||
|
|
||||||
const hasMoreMessages = computed(() => currentPage.value < totalPages.value - 1);
|
|
||||||
|
|
||||||
const otherParticipant = computed(() => {
|
|
||||||
if (!currentUser.value || participants.value.length === 0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return participants.value.find(p => p.id !== currentUser.value.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
function isSentByCurrentUser(message) {
|
|
||||||
return message.sender.id === currentUser.value?.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleAvatarError(event) {
|
|
||||||
event.target.src = '/default-avatar.svg';
|
|
||||||
}
|
|
||||||
|
|
||||||
// No changes needed here, as renderMarkdown is now imported.
|
|
||||||
// The old function is removed.
|
|
||||||
|
|
||||||
async function fetchMessages(page = 0) {
|
|
||||||
if (page === 0) {
|
|
||||||
loading.value = true;
|
|
||||||
messages.value = [];
|
|
||||||
} else {
|
|
||||||
loadingMore.value = true;
|
|
||||||
}
|
|
||||||
error.value = null;
|
|
||||||
const token = getToken();
|
|
||||||
if (!token) {
|
|
||||||
toast.error('请先登录');
|
|
||||||
loading.value = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${API_BASE_URL}/api/messages/conversations/${conversationId}?page=${page}&size=20`, {
|
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
|
||||||
});
|
|
||||||
if (!response.ok) throw new Error('无法加载消息');
|
|
||||||
|
|
||||||
const conversationData = await response.json();
|
|
||||||
const pageData = conversationData.messages;
|
|
||||||
|
|
||||||
if (page === 0) {
|
|
||||||
participants.value = conversationData.participants;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since the backend sorts by descending, we need to reverse for correct chat order
|
|
||||||
const newMessages = pageData.content.reverse();
|
|
||||||
|
|
||||||
const list = messagesListEl.value;
|
|
||||||
const oldScrollHeight = list ? list.scrollHeight : 0;
|
|
||||||
|
|
||||||
if (page === 0) {
|
|
||||||
messages.value = newMessages;
|
|
||||||
} else {
|
|
||||||
messages.value = [...newMessages, ...messages.value];
|
|
||||||
}
|
|
||||||
|
|
||||||
currentPage.value = pageData.number;
|
|
||||||
totalPages.value = pageData.totalPages;
|
|
||||||
|
|
||||||
// Scrolling is now fully handled by the watcher
|
|
||||||
await nextTick();
|
|
||||||
if (page > 0 && list) {
|
|
||||||
const newScrollHeight = list.scrollHeight;
|
|
||||||
list.scrollTop = newScrollHeight - oldScrollHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
error.value = e.message;
|
|
||||||
toast.error(e.message);
|
|
||||||
} finally {
|
|
||||||
loading.value = false;
|
|
||||||
loadingMore.value = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadMoreMessages() {
|
|
||||||
if (hasMoreMessages.value && !loadingMore.value) {
|
|
||||||
await fetchMessages(currentPage.value + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function sendMessage(content, clearInput) {
|
|
||||||
if (!content.trim()) return;
|
|
||||||
|
|
||||||
const recipient = otherParticipant.value;
|
|
||||||
if (!recipient) {
|
|
||||||
toast.error('无法确定收信人');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
sending.value = true;
|
|
||||||
const token = getToken();
|
|
||||||
try {
|
|
||||||
const response = await fetch(`${API_BASE_URL}/api/messages`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
recipientId: recipient.id,
|
|
||||||
content: content,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
if (!response.ok) throw new Error('发送失败');
|
|
||||||
|
|
||||||
const newMessage = await response.json();
|
|
||||||
messages.value.push(newMessage);
|
|
||||||
clearInput();
|
|
||||||
|
|
||||||
// Use a more reliable scroll approach
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollToBottom();
|
|
||||||
}, 100);
|
|
||||||
|
|
||||||
} catch (e) {
|
|
||||||
toast.error(e.message);
|
|
||||||
} finally {
|
|
||||||
sending.value = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function markConversationAsRead() {
|
|
||||||
const token = getToken();
|
|
||||||
if (!token) return;
|
|
||||||
try {
|
|
||||||
await fetch(`${API_BASE_URL}/api/messages/conversations/${conversationId}/read`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
|
||||||
});
|
|
||||||
// After marking as read, refresh the global unread count
|
|
||||||
refreshGlobalUnreadCount();
|
|
||||||
} catch (e) {
|
|
||||||
console.error('Failed to mark conversation as read', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function scrollToBottom() {
|
|
||||||
if (messagesListEl.value) {
|
|
||||||
const element = messagesListEl.value;
|
|
||||||
// 強制滾動到底部,使用 smooth 行為確保視覺效果
|
|
||||||
element.scrollTop = element.scrollHeight;
|
|
||||||
|
|
||||||
// 再次確認滾動位置
|
|
||||||
setTimeout(() => {
|
|
||||||
if (element.scrollTop < element.scrollHeight - element.clientHeight) {
|
|
||||||
element.scrollTop = element.scrollHeight;
|
|
||||||
}
|
|
||||||
}, 50);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(messages, async (newMessages) => {
|
|
||||||
if (newMessages.length === 0) return;
|
|
||||||
|
|
||||||
await nextTick();
|
|
||||||
|
|
||||||
// Simple, reliable scroll to bottom
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollToBottom();
|
|
||||||
}, 100);
|
|
||||||
}, { deep: true });
|
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
currentUser.value = await fetchCurrentUser();
|
|
||||||
if (currentUser.value) {
|
|
||||||
await fetchMessages(0);
|
|
||||||
await markConversationAsRead();
|
|
||||||
const token = getToken();
|
|
||||||
if (token && !isConnected.value) {
|
|
||||||
connect(token);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
toast.error('请先登录');
|
|
||||||
loading.value = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
watch(isConnected, (newValue) => {
|
|
||||||
if (newValue) {
|
|
||||||
// 等待一小段时间确保连接稳定
|
|
||||||
setTimeout(() => {
|
|
||||||
subscription = subscribe(`/topic/conversation/${conversationId}`, (message) => {
|
|
||||||
// 避免重复显示当前用户发送的消息
|
|
||||||
if (message.sender.id !== currentUser.value.id) {
|
|
||||||
messages.value.push(message);
|
|
||||||
// 实时收到消息时自动标记为已读
|
|
||||||
markConversationAsRead();
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollToBottom();
|
|
||||||
}, 100);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
onActivated(async () => {
|
|
||||||
// This will be called every time the component is activated (navigated to)
|
|
||||||
if (currentUser.value) {
|
|
||||||
await fetchMessages(0);
|
|
||||||
await markConversationAsRead();
|
|
||||||
|
|
||||||
// 確保滾動到底部 - 使用多重延遲策略
|
|
||||||
await nextTick();
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollToBottom();
|
|
||||||
}, 100);
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollToBottom();
|
|
||||||
}, 300);
|
|
||||||
setTimeout(() => {
|
|
||||||
scrollToBottom();
|
|
||||||
}, 500);
|
|
||||||
|
|
||||||
if (!isConnected.value) {
|
|
||||||
const token = getToken();
|
|
||||||
if (token) connect(token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
onDeactivated(() => {
|
|
||||||
if (subscription) {
|
|
||||||
subscription.unsubscribe();
|
|
||||||
subscription = null;
|
|
||||||
}
|
|
||||||
disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
onUnmounted(() => {
|
|
||||||
if (subscription) {
|
|
||||||
subscription.unsubscribe();
|
|
||||||
subscription = null;
|
|
||||||
}
|
|
||||||
disconnect();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.chat-container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
height: calc(100vh - 80px); /* Adjust based on your header/footer height */
|
|
||||||
max-width: 900px;
|
|
||||||
margin: 0 auto;
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
border-radius: 8px;
|
|
||||||
overflow: hidden;
|
|
||||||
background-color: var(--bg-color);
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chat-header {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
padding: 10px 20px;
|
|
||||||
border-bottom: 1px solid var(--border-color);
|
|
||||||
background-color: var(--bg-color-soft);
|
|
||||||
}
|
|
||||||
|
|
||||||
.back-button {
|
|
||||||
font-size: 18px;
|
|
||||||
color: var(--text-color-primary);
|
|
||||||
margin-right: 15px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.participant-name {
|
|
||||||
font-size: 18px;
|
|
||||||
font-weight: 600;
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.messages-list {
|
|
||||||
position: absolute;
|
|
||||||
top: 60px; /* Header height */
|
|
||||||
bottom: 250px; /* Increased space for input area */
|
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
overflow-y: auto;
|
|
||||||
padding: 20px;
|
|
||||||
padding-bottom: 40px; /* Extra padding at bottom */
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.load-more-container {
|
|
||||||
text-align: center;
|
|
||||||
margin-bottom: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.load-more-button {
|
|
||||||
background-color: var(--bg-color-soft);
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
color: var(--text-color-primary);
|
|
||||||
padding: 8px 16px;
|
|
||||||
border-radius: 20px;
|
|
||||||
cursor: pointer;
|
|
||||||
transition: background-color 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.load-more-button:hover {
|
|
||||||
background-color: var(--border-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item {
|
|
||||||
display: flex;
|
|
||||||
gap: 10px;
|
|
||||||
max-width: 75%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-avatar {
|
|
||||||
width: 40px;
|
|
||||||
height: 40px;
|
|
||||||
border-radius: 50%;
|
|
||||||
align-self: flex-end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-content {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-bubble {
|
|
||||||
padding: 10px 15px;
|
|
||||||
border-radius: 18px;
|
|
||||||
max-width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-text {
|
|
||||||
font-size: 15px;
|
|
||||||
line-height: 1.5;
|
|
||||||
word-wrap: break-word;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-text :deep(p) {
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-timestamp {
|
|
||||||
font-size: 11px;
|
|
||||||
color: var(--text-color-secondary);
|
|
||||||
margin-top: 5px;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Sent messages */
|
|
||||||
.message-item.sent {
|
|
||||||
align-self: flex-end;
|
|
||||||
flex-direction: row-reverse;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.sent .message-content {
|
|
||||||
align-items: flex-end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.sent .message-bubble {
|
|
||||||
background-color: var(--primary-color);
|
|
||||||
color: white;
|
|
||||||
border-bottom-right-radius: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.sent .message-timestamp {
|
|
||||||
text-align: right;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Received messages */
|
|
||||||
.message-item.received {
|
|
||||||
align-self: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.received .message-content {
|
|
||||||
align-items: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-item.received .message-bubble {
|
|
||||||
background-color: var(--bg-color-soft);
|
|
||||||
border: 1px solid var(--border-color);
|
|
||||||
border-bottom-left-radius: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.message-input-area {
|
|
||||||
position: absolute;
|
|
||||||
bottom: 0;
|
|
||||||
left: 0;
|
|
||||||
right: 0;
|
|
||||||
min-height: 200px;
|
|
||||||
max-height: 400px;
|
|
||||||
padding: 10px 20px;
|
|
||||||
border-top: 1px solid var(--border-color);
|
|
||||||
background-color: var(--bg-color);
|
|
||||||
box-sizing: border-box;
|
|
||||||
overflow: visible;
|
|
||||||
}
|
|
||||||
|
|
||||||
.loading-container, .error-container {
|
|
||||||
text-align: center;
|
|
||||||
padding: 50px;
|
|
||||||
color: var(--text-color-secondary);
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
@@ -27,15 +27,11 @@
|
|||||||
>
|
>
|
||||||
<i class="fas fa-user-minus"></i>
|
<i class="fas fa-user-minus"></i>
|
||||||
取消关注
|
取消关注
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div v-if="!isMine" class="profile-page-header-subscribe-button" @click="sendMessage">
|
||||||
v-if="!isMine"
|
<i class="fas fa-paper-plane"></i>
|
||||||
class="profile-page-header-subscribe-button"
|
发私信
|
||||||
@click="sendMessage"
|
</div>
|
||||||
>
|
|
||||||
<i class="fas fa-paper-plane"></i>
|
|
||||||
发私信
|
|
||||||
</div>
|
|
||||||
<LevelProgress
|
<LevelProgress
|
||||||
:exp="levelInfo.exp"
|
:exp="levelInfo.exp"
|
||||||
:current-level="levelInfo.currentLevel"
|
:current-level="levelInfo.currentLevel"
|
||||||
@@ -558,14 +554,14 @@ const sendMessage = async () => {
|
|||||||
recipientId: user.value.id,
|
recipientId: user.value.id,
|
||||||
}),
|
}),
|
||||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||||
});
|
})
|
||||||
const result = await response.json();
|
const result = await response.json()
|
||||||
router.push(`/messages/${result.conversationId}`);
|
router.push(`/message-box/${result.conversationId}`)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error('无法发起私信');
|
toast.error('无法发起私信')
|
||||||
console.error(e);
|
console.error(e)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const gotoTag = (tag) => {
|
const gotoTag = (tag) => {
|
||||||
const value = encodeURIComponent(tag.id ?? tag.name)
|
const value = encodeURIComponent(tag.id ?? tag.name)
|
||||||
|
|||||||
Reference in New Issue
Block a user