This commit is contained in:
wangdongbo 2026-01-28 17:51:31 +08:00
parent b33c4357e8
commit af3b59f8e2
4 changed files with 230 additions and 122 deletions

View File

@ -44,12 +44,6 @@
"navigationBarTitleText": "新增档案" "navigationBarTitleText": "新增档案"
} }
}, },
{
"path": "pages/article/article-list",
"style": {
"navigationBarTitleText": "健康宣教"
}
},
{ {
"path": "pages/health/list", "path": "pages/health/list",
"style": { "style": {
@ -99,5 +93,25 @@
"navigationBarBackgroundColor": "#065bd6", "navigationBarBackgroundColor": "#065bd6",
"backgroundColor": "#065bd6" "backgroundColor": "#065bd6"
}, },
"tabBar": {
"color": "#666666",
"selectedColor": "#007aff",
"backgroundColor": "#ffffff",
"borderStyle": "white",
"list": [
{
"pagePath": "pages/home/home",
"iconPath": "static/tabbar/home.png",
"selectedIconPath": "static/tabbar/home_selected.png",
"text": "消息"
},
{
"pagePath": "pages/message/message",
"iconPath": "static/tabbar/cart.png",
"selectedIconPath": "static/tabbar/cart_selected.png",
"text": "咨询"
}
]
},
"uniIdRouter": {} "uniIdRouter": {}
} }

View File

@ -400,6 +400,25 @@ const initTIMCallbacks = async () => {
nextTick(() => { nextTick(() => {
scrollToBottom(true); scrollToBottom(true);
}); });
// 0
if (timChatManager.tim && timChatManager.isLoggedIn && chatInfo.value.conversationID) {
timChatManager.tim
.setMessageRead({
conversationID: chatInfo.value.conversationID,
})
.then(() => {
console.log("✓ 收到新消息后已标记为已读");
// 0
timChatManager.triggerCallback('onConversationListUpdated', {
conversationID: chatInfo.value.conversationID,
unreadCount: 0
});
})
.catch((error) => {
console.error("✗ 标记已读失败:", error);
});
}
} }
}); });
@ -538,21 +557,27 @@ const loadMessageList = async () => {
timChatManager.enterConversation(chatInfo.value.conversationID || "test1"); timChatManager.enterConversation(chatInfo.value.conversationID || "test1");
// // -
if ( if (
timChatManager.tim && timChatManager.tim &&
timChatManager.isLoggedIn && timChatManager.isLoggedIn &&
chatInfo.value.conversationID chatInfo.value.conversationID
) { ) {
console.log("标记会话为已读:", chatInfo.value.conversationID);
timChatManager.tim timChatManager.tim
.setMessageRead({ .setMessageRead({
conversationID: chatInfo.value.conversationID, conversationID: chatInfo.value.conversationID,
}) })
.then(() => { .then(() => {
console.log("会话已标记为已读:", chatInfo.value.conversationID); console.log("✓ 会话已标记为已读:", chatInfo.value.conversationID);
//
timChatManager.triggerCallback('onConversationListUpdated', {
conversationID: chatInfo.value.conversationID,
unreadCount: 0
});
}) })
.catch((error) => { .catch((error) => {
console.error("标记会话已读失败:", error); console.error("标记会话已读失败:", error);
}); });
} }
}; };

View File

@ -16,7 +16,6 @@
> >
<text class="loading-text">加载中...</text> <text class="loading-text">加载中...</text>
</view> </view>
<!-- 消息列表项 --> <!-- 消息列表项 -->
<view <view
v-for="conversation in conversationList" v-for="conversation in conversationList"
@ -171,50 +170,6 @@ const loadConversationList = async () => {
} }
}; };
//
const extractMessagePreview = (message) => {
if (!message) return "暂无消息";
const payload = message.payload;
if (!payload) return "暂无消息";
//
if (message.type === "TIMTextElem") {
return payload.text || "暂无消息";
}
//
if (message.type === "TIMImageElem") {
return "[图片]";
}
//
if (message.type === "TIMSoundElem") {
return "[语音]";
}
//
if (message.type === "TIMVideoFileElem") {
return "[视频]";
}
//
if (message.type === "TIMFileElem") {
return "[文件]";
}
//
if (message.type === "TIMCustomElem") {
const description = payload.description;
if (description === "SYSTEM_NOTIFICATION") {
return "[系统消息]";
}
return "[消息]";
}
return "暂无消息";
};
// //
const setupConversationListener = () => { const setupConversationListener = () => {
if (!globalTimChatManager) return; if (!globalTimChatManager) return;
@ -223,58 +178,74 @@ const setupConversationListener = () => {
globalTimChatManager.setCallback("onConversationListUpdated", (eventData) => { globalTimChatManager.setCallback("onConversationListUpdated", (eventData) => {
console.log("会话列表更新事件:", eventData); console.log("会话列表更新事件:", eventData);
// //
if ( if (eventData && !Array.isArray(eventData) && eventData.conversationID) {
eventData.reason === "NEW_MESSAGE_RECEIVED_IN_CURRENT_CONVERSATION" || const conversationID = eventData.conversationID;
eventData.reason === "NEW_MESSAGE_RECEIVED" const existingIndex = conversationList.value.findIndex(
) {
const conversation = eventData.conversation;
if (!conversation) return;
const conversationID = conversation.conversationID;
const conversationIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversationID (conv) => conv.conversationID === conversationID
); );
if (conversationIndex !== -1) { if (existingIndex !== -1) {
// //
const existingConversation = conversationList.value[conversationIndex]; if (eventData.unreadCount !== undefined) {
existingConversation.lastMessage = conversationList.value[existingIndex].unreadCount =
conversation.lastMessage || "暂无消息"; eventData.unreadCount;
existingConversation.lastMessageTime = console.log(
conversation.lastMessageTime || Date.now(); `已清空会话未读数: ${conversationList.value[existingIndex].name}, unreadCount: ${eventData.unreadCount}`
existingConversation.unreadCount = conversation.unreadCount || 0; );
}
//
const [updatedConversation] = conversationList.value.splice(
conversationIndex,
1
);
conversationList.value.unshift(updatedConversation);
console.log("已更新会话:", existingConversation.name);
} else {
//
conversationList.value.unshift({
conversationID: conversationID,
groupID: conversation.groupID || conversationID.replace("GROUP", ""),
name: conversation.name || "问诊群聊",
avatar: conversation.avatar || "/static/default-avatar.png",
lastMessage: conversation.lastMessage || "暂无消息",
lastMessageTime: conversation.lastMessageTime || Date.now(),
unreadCount: conversation.unreadCount || 0,
});
console.log("已添加新会话");
} }
return;
} }
// eventData
if (!eventData || !Array.isArray(eventData)) {
console.warn("会话列表更新事件数据格式错误");
return;
}
//
const groupConversations = eventData.filter(
(conv) => conv.conversationID && conv.conversationID.startsWith("GROUP")
);
console.log(`收到 ${groupConversations.length} 个群聊会话更新`);
// - 使 tim-chat.js
groupConversations.forEach((updatedConv) => {
const conversationID = updatedConv.conversationID;
const existingIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversationID
);
// 使 TimChatManager
const conversationData =
globalTimChatManager.formatConversationData(updatedConv);
if (existingIndex !== -1) {
//
conversationList.value[existingIndex] = conversationData;
console.log(
`已更新会话: ${conversationData.name}, unreadCount: ${conversationData.unreadCount}`
);
} else {
//
conversationList.value.push(conversationData);
console.log(`已添加新会话: ${conversationData.name}`);
}
});
//
conversationList.value.sort(
(a, b) => b.lastMessageTime - a.lastMessageTime
);
}); });
// //
globalTimChatManager.setCallback("onMessageReceived", (message) => { globalTimChatManager.setCallback("onMessageReceived", (message) => {
console.log("消息列表页面收到新消息:", message); console.log("消息列表页面收到新消息:", message);
// //
const conversationID = message.conversationID; const conversationID = message.conversationID;
const conversationIndex = conversationList.value.findIndex( const conversationIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversationID (conv) => conv.conversationID === conversationID
@ -282,9 +253,27 @@ const setupConversationListener = () => {
if (conversationIndex !== -1) { if (conversationIndex !== -1) {
const conversation = conversationList.value[conversationIndex]; const conversation = conversationList.value[conversationIndex];
// onConversationListUpdated
//
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const isViewingConversation =
currentPage?.route === "pages/message/index";
//
if (isViewingConversation) {
console.log("用户正在查看该会话,不增加未读数");
return;
}
//
conversation.unreadCount = (conversation.unreadCount || 0) + 1; conversation.unreadCount = (conversation.unreadCount || 0) + 1;
console.log("已更新会话未读数:", conversation.name); console.log(
"已更新会话未读数:",
conversation.name,
"unreadCount:",
conversation.unreadCount
);
} }
}); });
}; };
@ -335,6 +324,15 @@ const formatMessageTime = (timestamp) => {
const handleClickConversation = (conversation) => { const handleClickConversation = (conversation) => {
console.log("点击会话:", conversation); console.log("点击会话:", conversation);
//
const conversationIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversation.conversationID
);
if (conversationIndex !== -1) {
conversationList.value[conversationIndex].unreadCount = 0;
console.log("已清空本地未读数:", conversation.name);
}
// //
uni.navigateTo({ uni.navigateTo({
url: `/pages/message/index?conversationID=${conversation.conversationID}&groupID=${conversation.groupID}`, url: `/pages/message/index?conversationID=${conversation.conversationID}&groupID=${conversation.groupID}`,

View File

@ -1017,14 +1017,12 @@ class TimChatManager {
reject(new Error('TIM实例不存在')) reject(new Error('TIM实例不存在'))
return return
} }
if (!this.isLoggedIn) { if (!this.isLoggedIn) {
console.log('SDK未ready等待SDK初始化...') console.log('SDK未ready等待SDK初始化...')
let waitTime = 0 let waitTime = 0
const maxWaitTime = 30000 // 最多等待30秒 const maxWaitTime = 30000 // 最多等待30秒
const checkInterval = 1000 // 每秒检查一次 const checkInterval = 1000 // 每秒检查一次
let timeoutHandle = null let timeoutHandle = null
const checkSDKReady = () => { const checkSDKReady = () => {
if (this.isLoggedIn) { if (this.isLoggedIn) {
console.log('SDK已ready开始获取群聊列表') console.log('SDK已ready开始获取群聊列表')
@ -1036,15 +1034,12 @@ class TimChatManager {
reject(new Error('SDK初始化超时请检查网络连接')) reject(new Error('SDK初始化超时请检查网络连接'))
} else { } else {
waitTime += checkInterval waitTime += checkInterval
console.log(`等待SDK就绪... (${Math.floor(waitTime / 1000)}/${Math.floor(maxWaitTime / 1000)}秒)`)
timeoutHandle = setTimeout(checkSDKReady, checkInterval) timeoutHandle = setTimeout(checkSDKReady, checkInterval)
} }
} }
checkSDKReady() checkSDKReady()
return return
} }
this.getGroupListInternal().then(resolve).catch(reject) this.getGroupListInternal().then(resolve).catch(reject)
}) })
} }
@ -2488,32 +2483,22 @@ class TimChatManager {
return message return message
} }
// 格式化最后一条消息(支持更多消息类型)
formatLastMessage(message) { formatLastMessage(message) {
try { try {
switch (message.type) { switch (message.type) {
case 'TIMTextElem': case 'TIMTextElem':
return message.payload.text || '[文本消息]' return message.payload?.text || '[文本消息]'
case 'TIMImageElem': case 'TIMImageElem':
return '[图片]' return '[图片]'
case 'TIMSoundElem': case 'TIMSoundElem':
return '[语音]' return '[语音]'
case 'TIMVideoFileElem':
return '[视频]'
case 'TIMFileElem':
return '[文件]'
case 'TIMCustomElem': case 'TIMCustomElem':
try { return this.formatCustomMessage(message.payload)
const customData = JSON.parse(message.payload.data)
if (customData.messageType === 'symptom') {
return '[病情描述]'
} else if (customData.messageType === 'prescription') {
return '[处方单]'
} else if (customData.messageType === 'refill') {
return '[续方申请]'
} else if (customData.messageType === 'survey') {
return '[问卷调查]'
} else {
return customData.content || '[自定义消息]'
}
} catch (error) {
return '[自定义消息]'
}
default: default:
return '[未知消息类型]' return '[未知消息类型]'
} }
@ -2523,6 +2508,85 @@ class TimChatManager {
} }
} }
// 格式化自定义消息
formatCustomMessage(payload) {
try {
if (!payload || !payload.data) {
return '[自定义消息]'
}
const customData = typeof payload.data === 'string'
? JSON.parse(payload.data)
: payload.data
const messageType = customData.messageType || customData.type
const messageTypeMap = {
system_message: '[系统消息]',
symptom: '[病情描述]',
prescription: '[处方单]',
refill: '[续方申请]',
survey: '[问卷调查]',
article: '[文章]',
consult_pending: '患者向团队发起咨询请在1小时内接诊',
consult_rejected: '咨询已被拒绝',
consult_timeout: '咨询已超时自动关闭',
consult_accepted: '已接诊,会话已开始',
consult_ended: '已结束当前会话',
}
return messageTypeMap[messageType] || customData.content || '[自定义消息]'
} catch (error) {
console.error('格式化自定义消息失败:', error)
return '[自定义消息]'
}
}
// 格式化会话数据(用于会话列表)
formatConversationData(conversation) {
try {
const conversationID = conversation.conversationID
const groupName = conversation.groupProfile?.name || ''
const [doctorId, patientName] = groupName.split('|')
const groupID = conversationID.replace('GROUP', '')
// 解析最后一条消息
let lastMessage = '暂无消息'
let lastMessageTime = Date.now()
if (conversation.lastMessage) {
const msg = conversation.lastMessage
lastMessageTime = (msg.lastTime || msg.time || 0) * 1000
lastMessage = this.formatLastMessage(msg)
}
return {
conversationID,
groupID,
name: patientName ? `${patientName}的问诊` : groupName || '问诊群聊',
avatar: '/static/default-avatar.png',
lastMessage,
lastMessageTime,
unreadCount: conversation.unreadCount || 0,
doctorId,
patientName,
}
} catch (error) {
console.error('格式化会话数据失败:', error)
return {
conversationID: conversation.conversationID,
groupID: conversation.conversationID?.replace('GROUP', '') || '',
name: '问诊群聊',
avatar: '/static/default-avatar.png',
lastMessage: '暂无消息',
lastMessageTime: Date.now(),
unreadCount: 0,
doctorId: '',
patientName: '',
}
}
}
getImageUrl(imageFile) { getImageUrl(imageFile) {
// 处理 tempFiles 数组格式 // 处理 tempFiles 数组格式
if (imageFile?.tempFiles?.length > 0) { if (imageFile?.tempFiles?.length > 0) {
@ -2600,7 +2664,10 @@ class TimChatManager {
// 标记会话为已读 // 标记会话为已读
markConversationAsRead(conversationID) { markConversationAsRead(conversationID) {
if (!this.tim || !this.isLoggedIn) return if (!this.tim || !this.isLoggedIn) {
console.log('⚠️ TIM未初始化或未登录无法标记会话已读');
return;
}
try { try {
let formattedConversationID = conversationID let formattedConversationID = conversationID
@ -2608,18 +2675,22 @@ class TimChatManager {
formattedConversationID = `GROUP${conversationID}` formattedConversationID = `GROUP${conversationID}`
} }
console.log('📖 标记会话为已读:', formattedConversationID);
this.tim.setMessageRead({ this.tim.setMessageRead({
conversationID: formattedConversationID conversationID: formattedConversationID
}).then(() => { }).then(() => {
console.log('✓ 会话已标记为已读:', formattedConversationID);
// 触发会话列表更新回调,通知消息列表页面清空未读数
this.triggerCallback('onConversationListUpdated', { this.triggerCallback('onConversationListUpdated', {
conversationID: formattedConversationID, conversationID: formattedConversationID,
unreadCount: 0 unreadCount: 0
}) })
}).catch(error => { }).catch(error => {
console.error('标记会话已读失败:', error) console.error('标记会话已读失败:', error)
}) })
} catch (error) { } catch (error) {
console.error('标记会话已读异常:', error) console.error('标记会话已读异常:', error)
} }
} }