|
|
@ -126,7 +126,7 @@ import UNI_APP from '@/.env.js'; |
|
|
export default { |
|
|
export default { |
|
|
data() { |
|
|
data() { |
|
|
return { |
|
|
return { |
|
|
chat: {}, |
|
|
// chat: {}, |
|
|
userInfo: {}, |
|
|
userInfo: {}, |
|
|
group: {}, |
|
|
group: {}, |
|
|
groupMembers: [], |
|
|
groupMembers: [], |
|
|
@ -1001,6 +1001,24 @@ export default { |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
computed: { |
|
|
computed: { |
|
|
|
|
|
chat() { |
|
|
|
|
|
// 根据当前激活的索引从 store 获取最新的 chat |
|
|
|
|
|
if (!this.chatStore.chats || this.chatStore.chats.length === 0) { |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
const idx = this.activeChatIdx; |
|
|
|
|
|
return this.chatStore.chats[idx] || this.chatStore.chats[0]; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
activeChatIdx: { |
|
|
|
|
|
get() { |
|
|
|
|
|
return this._activeChatIdx || 0; |
|
|
|
|
|
}, |
|
|
|
|
|
set(val) { |
|
|
|
|
|
this._activeChatIdx = val; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
mine() { |
|
|
mine() { |
|
|
return this.userStore.userInfo; |
|
|
return this.userStore.userInfo; |
|
|
}, |
|
|
}, |
|
|
@ -1058,10 +1076,10 @@ export default { |
|
|
return this.groupMembers.filter(m => !m.quit).length; |
|
|
return this.groupMembers.filter(m => !m.quit).length; |
|
|
}, |
|
|
}, |
|
|
isGroup() { |
|
|
isGroup() { |
|
|
return this.chat.type == 'GROUP'; |
|
|
return this.chat && this.chat.type == 'GROUP'; |
|
|
}, |
|
|
}, |
|
|
isPrivate() { |
|
|
isPrivate() { |
|
|
return this.chat.type == 'PRIVATE'; |
|
|
return this.chat && this.chat.type == 'PRIVATE'; |
|
|
}, |
|
|
}, |
|
|
loading() { |
|
|
loading() { |
|
|
return this.chatStore.loading; |
|
|
return this.chatStore.loading; |
|
|
@ -1100,14 +1118,63 @@ export default { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
onLoad(options) { |
|
|
async onLoad(options) { |
|
|
// 聊天数据 |
|
|
const chatIdx = options.chatIdx !== undefined ? parseInt(options.chatIdx) : 0; |
|
|
this.chat = this.chatStore.chats[options.chatIdx]; |
|
|
|
|
|
|
|
|
// 显示加载提示 |
|
|
|
|
|
uni.showLoading({ |
|
|
|
|
|
title: '加载中...', |
|
|
|
|
|
mask: true |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// 等待配置初始化 |
|
|
|
|
|
let retryCount = 0; |
|
|
|
|
|
const maxRetry = 30; // 3秒 |
|
|
|
|
|
|
|
|
|
|
|
// 等待 chats 数据加载 |
|
|
|
|
|
while ((!this.chatStore.chats || this.chatStore.chats.length === 0) && retryCount < maxRetry) { |
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 100)); |
|
|
|
|
|
retryCount++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果还是没有数据,主动加载 |
|
|
|
|
|
if (!this.chatStore.chats || this.chatStore.chats.length === 0) { |
|
|
|
|
|
if (this.chatStore.loadChat) { |
|
|
|
|
|
await this.chatStore.loadChat(); |
|
|
|
|
|
} |
|
|
|
|
|
// 再次等待 |
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 500)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 再次检查 |
|
|
|
|
|
if (!this.chatStore.chats || this.chatStore.chats.length === 0) { |
|
|
|
|
|
// 尝试从本地缓存读取 |
|
|
|
|
|
const cached = uni.getStorageSync('chat_chats'); |
|
|
|
|
|
if (cached && cached.length > 0) { |
|
|
|
|
|
this.chatStore.chats = cached; |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new Error('暂无聊天数据'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取聊天数据 |
|
|
|
|
|
this.chat = this.chatStore.chats[chatIdx] || this.chatStore.chats[0]; |
|
|
|
|
|
|
|
|
|
|
|
if (!this.chat || !this.chat.targetId) { |
|
|
|
|
|
throw new Error('会话不存在'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uni.hideLoading(); |
|
|
|
|
|
|
|
|
|
|
|
// 其余代码保持不变... |
|
|
// 初始状态只显示20条消息 |
|
|
// 初始状态只显示20条消息 |
|
|
let size = this.messageSize; |
|
|
let size = this.messageSize; |
|
|
this.showMinIdx = size > 20 ? size - 20 : 0; |
|
|
this.showMinIdx = size > 20 ? size - 20 : 0; |
|
|
|
|
|
|
|
|
// 消息已读 |
|
|
// 消息已读 |
|
|
this.readedMessage() |
|
|
this.readedMessage() |
|
|
|
|
|
|
|
|
// 加载好友或群聊信息 |
|
|
// 加载好友或群聊信息 |
|
|
if (this.isGroup) { |
|
|
if (this.isGroup) { |
|
|
this.loadGroup(this.chat.targetId); |
|
|
this.loadGroup(this.chat.targetId); |
|
|
@ -1115,35 +1182,58 @@ export default { |
|
|
this.loadFriend(this.chat.targetId); |
|
|
this.loadFriend(this.chat.targetId); |
|
|
this.loadReaded(this.chat.targetId) |
|
|
this.loadReaded(this.chat.targetId) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 激活当前会话 |
|
|
// 激活当前会话 |
|
|
this.chatStore.activeChat(options.chatIdx); |
|
|
this.chatStore.activeChat(chatIdx); |
|
|
|
|
|
|
|
|
// 复位回执消息 |
|
|
// 复位回执消息 |
|
|
this.isReceipt = false; |
|
|
this.isReceipt = false; |
|
|
// 清空底部标志 |
|
|
|
|
|
this.isInBottom = true; |
|
|
this.isInBottom = true; |
|
|
this.newMessageSize = 0; |
|
|
this.newMessageSize = 0; |
|
|
// 清空消息临时id |
|
|
|
|
|
this.maxTmpId = 0; |
|
|
this.maxTmpId = 0; |
|
|
|
|
|
|
|
|
// 监听键盘高度 |
|
|
// 监听键盘高度 |
|
|
this.listenKeyBoard(); |
|
|
this.listenKeyBoard(); |
|
|
|
|
|
|
|
|
// 计算聊天窗口高度 |
|
|
// 计算聊天窗口高度 |
|
|
this.windowHeight = uni.getSystemInfoSync().windowHeight; |
|
|
this.windowHeight = uni.getSystemInfoSync().windowHeight; |
|
|
this.screenHeight = uni.getSystemInfoSync().screenHeight; |
|
|
this.screenHeight = uni.getSystemInfoSync().screenHeight; |
|
|
this.reCalChatMainHeight(); |
|
|
this.reCalChatMainHeight(); |
|
|
|
|
|
|
|
|
this.$nextTick(() => { |
|
|
this.$nextTick(() => { |
|
|
// 上面获取的windowHeight可能不准,重新计算一次聊天窗口高度 |
|
|
|
|
|
this.windowHeight = uni.getSystemInfoSync().windowHeight; |
|
|
this.windowHeight = uni.getSystemInfoSync().windowHeight; |
|
|
this.reCalChatMainHeight(); |
|
|
this.reCalChatMainHeight(); |
|
|
this.scrollToBottom(); |
|
|
this.scrollToBottom(); |
|
|
// #ifdef H5 |
|
|
// #ifdef H5 |
|
|
this.initHeight = window.innerHeight; |
|
|
this.initHeight = window.innerHeight; |
|
|
// 兼容ios的h5:禁止页面滚动 |
|
|
|
|
|
const chatBox = document.getElementById('chatBox') |
|
|
const chatBox = document.getElementById('chatBox') |
|
|
|
|
|
if (chatBox) { |
|
|
chatBox.addEventListener('touchmove', e => { |
|
|
chatBox.addEventListener('touchmove', e => { |
|
|
e.preventDefault() |
|
|
e.preventDefault() |
|
|
}, { passive: false }); |
|
|
}, { passive: false }); |
|
|
|
|
|
} |
|
|
// #endif |
|
|
// #endif |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
uni.hideLoading(); |
|
|
|
|
|
console.error('进入聊天页面失败:', error); |
|
|
|
|
|
uni.showToast({ |
|
|
|
|
|
title: error.message || '加载失败,请稍后重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
uni.navigateBack({ |
|
|
|
|
|
delta: 1, |
|
|
|
|
|
fail: () => { |
|
|
|
|
|
uni.switchTab({ |
|
|
|
|
|
url: "/pages/chat/chat" |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}, 1500); |
|
|
|
|
|
} |
|
|
}, |
|
|
}, |
|
|
onUnload() { |
|
|
onUnload() { |
|
|
this.unListenKeyboard(); |
|
|
this.unListenKeyboard(); |
|
|
|