diff --git a/miniapp/config/index.js b/miniapp/config/index.js index 7794e45..6bdbd8d 100644 --- a/miniapp/config/index.js +++ b/miniapp/config/index.js @@ -23,7 +23,7 @@ const ENV = { } // 当前环境 - 开发时使用 development,打包时改为 production -const CURRENT_ENV = 'development' +const CURRENT_ENV = 'production' // 导出配置 export const config = { diff --git a/miniapp/pages/chat/index.vue b/miniapp/pages/chat/index.vue index 6b4650d..15bc159 100644 --- a/miniapp/pages/chat/index.vue +++ b/miniapp/pages/chat/index.vue @@ -529,6 +529,7 @@ const loadMessages = async (isLoadMore = false) => { } hasMore.value = newMessages.length >= 20 + console.log('[Chat] 消息加载完成,标记会话已读:', sessionId.value) chatStore.markSessionAsRead(sessionId.value) } } catch (error) { @@ -1193,6 +1194,9 @@ const handleReceiveMessage = (message) => { scrollToBottom() }) + // 用户正在查看聊天页面,立即标记消息为已读 + signalR.markSessionAsRead(sessionId.value) + // 播放提示音(可选) // uni.vibrateShort() } diff --git a/miniapp/pages/message/index.vue b/miniapp/pages/message/index.vue index 5f4a0f2..90f352f 100644 --- a/miniapp/pages/message/index.vue +++ b/miniapp/pages/message/index.vue @@ -246,8 +246,14 @@ try { const res = await getSessions() + console.log('[Message] 加载会话列表:', res) if (res?.success || res?.code === 0) { sessions.value = res.data || [] + console.log('[Message] 会话列表数据:', sessions.value.map(s => ({ + sessionId: s.sessionId, + targetNickname: s.targetNickname, + unreadCount: s.unreadCount + }))) chatStore.setSessions(sessions.value) } } catch (error) { diff --git a/miniapp/utils/signalr.js b/miniapp/utils/signalr.js index 9710de2..1744e74 100644 --- a/miniapp/utils/signalr.js +++ b/miniapp/utils/signalr.js @@ -457,6 +457,23 @@ class SignalRClient { } } + /** + * 标记会话消息为已读 + * @param {number} sessionId 会话ID + */ + async markSessionAsRead(sessionId) { + if (!this.isConnected) { + console.warn('[SignalR] 未连接,无法标记已读') + return + } + try { + await this.invoke('MarkSessionAsRead', sessionId) + console.log('[SignalR] 已标记会话已读:', sessionId) + } catch (err) { + console.error('[SignalR] 标记已读失败:', err) + } + } + /** * 获取连接状态 */ diff --git a/server/src/XiangYi.AppApi/Hubs/ChatHub.cs b/server/src/XiangYi.AppApi/Hubs/ChatHub.cs index f8efe4e..05368a1 100644 --- a/server/src/XiangYi.AppApi/Hubs/ChatHub.cs +++ b/server/src/XiangYi.AppApi/Hubs/ChatHub.cs @@ -117,6 +117,35 @@ public class ChatHub : Hub _logger.LogInformation("用户离开会话组: UserId={UserId}, SessionId={SessionId}", userId, sessionId); } + /// + /// 标记会话消息为已读(用户在聊天页面收到新消息时调用) + /// + /// 会话ID + public async Task MarkSessionAsRead(long sessionId) + { + var userId = GetCurrentUserId(); + if (userId <= 0) return; + + // 通过服务容器获取 ChatService + var chatService = Context.GetHttpContext()?.RequestServices.GetService(); + if (chatService == null) + { + _logger.LogWarning("无法获取 ChatService"); + return; + } + + try + { + var readCount = await chatService.MarkMessagesAsReadAsync(userId, sessionId); + _logger.LogInformation("SignalR标记消息已读: UserId={UserId}, SessionId={SessionId}, ReadCount={ReadCount}", + userId, sessionId, readCount); + } + catch (Exception ex) + { + _logger.LogError(ex, "SignalR标记消息已读失败: UserId={UserId}, SessionId={SessionId}", userId, sessionId); + } + } + /// /// 获取当前用户ID ///