diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index 6c61782..c58d3fb 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -45,10 +45,10 @@ export default { wsApi.onMessage((cmd, msgInfo) => { if (cmd == 2) { // 异地登录,强制下线 - uni.showModal({ - content: '您已在其他地方登录,将被强制下线', - showCancel: false, - }) + // uni.showModal({ + // content: '您已在其他地方登录,将被强制下线', + // showCancel: false, + // }) this.exit(); } else if (cmd == 3) { if (!this.configStore.appInit || this.chatStore.loading) { diff --git a/im-uniapp/pages/chat/chat-box.vue b/im-uniapp/pages/chat/chat-box.vue index 6c57486..e249620 100644 --- a/im-uniapp/pages/chat/chat-box.vue +++ b/im-uniapp/pages/chat/chat-box.vue @@ -126,7 +126,7 @@ import UNI_APP from '@/.env.js'; export default { data() { return { - chat: {}, + // chat: {}, userInfo: {}, group: {}, groupMembers: [], @@ -1001,6 +1001,24 @@ export default { } }, 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() { return this.userStore.userInfo; }, @@ -1058,10 +1076,10 @@ export default { return this.groupMembers.filter(m => !m.quit).length; }, isGroup() { - return this.chat.type == 'GROUP'; + return this.chat && this.chat.type == 'GROUP'; }, isPrivate() { - return this.chat.type == 'PRIVATE'; + return this.chat && this.chat.type == 'PRIVATE'; }, loading() { return this.chatStore.loading; @@ -1100,50 +1118,122 @@ export default { } } }, - onLoad(options) { - // 聊天数据 - this.chat = this.chatStore.chats[options.chatIdx]; - // 初始状态只显示20条消息 - let size = this.messageSize; - this.showMinIdx = size > 20 ? size - 20 : 0; - // 消息已读 - this.readedMessage() - // 加载好友或群聊信息 - if (this.isGroup) { - this.loadGroup(this.chat.targetId); - } else { - this.loadFriend(this.chat.targetId); - this.loadReaded(this.chat.targetId) - } - // 激活当前会话 - this.chatStore.activeChat(options.chatIdx); - // 复位回执消息 - this.isReceipt = false; - // 清空底部标志 - this.isInBottom = true; - this.newMessageSize = 0; - // 清空消息临时id - this.maxTmpId = 0; - // 监听键盘高度 - this.listenKeyBoard(); - // 计算聊天窗口高度 - this.windowHeight = uni.getSystemInfoSync().windowHeight; - this.screenHeight = uni.getSystemInfoSync().screenHeight; - this.reCalChatMainHeight(); - this.$nextTick(() => { - // 上面获取的windowHeight可能不准,重新计算一次聊天窗口高度 - this.windowHeight = uni.getSystemInfoSync().windowHeight; - this.reCalChatMainHeight(); - this.scrollToBottom(); - // #ifdef H5 - this.initHeight = window.innerHeight; - // 兼容ios的h5:禁止页面滚动 - const chatBox = document.getElementById('chatBox') - chatBox.addEventListener('touchmove', e => { - e.preventDefault() - }, { passive: false }); - // #endif - }); + async onLoad(options) { + const chatIdx = options.chatIdx !== undefined ? parseInt(options.chatIdx) : 0; + + // 显示加载提示 + 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条消息 + let size = this.messageSize; + this.showMinIdx = size > 20 ? size - 20 : 0; + + // 消息已读 + this.readedMessage() + + // 加载好友或群聊信息 + if (this.isGroup) { + this.loadGroup(this.chat.targetId); + } else { + this.loadFriend(this.chat.targetId); + this.loadReaded(this.chat.targetId) + } + + // 激活当前会话 + this.chatStore.activeChat(chatIdx); + + // 复位回执消息 + this.isReceipt = false; + this.isInBottom = true; + this.newMessageSize = 0; + this.maxTmpId = 0; + + // 监听键盘高度 + this.listenKeyBoard(); + + // 计算聊天窗口高度 + this.windowHeight = uni.getSystemInfoSync().windowHeight; + this.screenHeight = uni.getSystemInfoSync().screenHeight; + this.reCalChatMainHeight(); + + this.$nextTick(() => { + this.windowHeight = uni.getSystemInfoSync().windowHeight; + this.reCalChatMainHeight(); + this.scrollToBottom(); + // #ifdef H5 + this.initHeight = window.innerHeight; + const chatBox = document.getElementById('chatBox') + if (chatBox) { + chatBox.addEventListener('touchmove', e => { + e.preventDefault() + }, { passive: false }); + } + // #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() { this.unListenKeyboard(); diff --git a/im-uniapp/pages/friend/friend.vue b/im-uniapp/pages/friend/friend.vue index 713df63..a5a8c72 100644 --- a/im-uniapp/pages/friend/friend.vue +++ b/im-uniapp/pages/friend/friend.vue @@ -1,7 +1,7 @@ diff --git a/im-uniapp/pages/login/login.vue b/im-uniapp/pages/login/login.vue index f645366..f57dca6 100644 --- a/im-uniapp/pages/login/login.vue +++ b/im-uniapp/pages/login/login.vue @@ -87,14 +87,17 @@ export default { url: 'https://api.ipify.org?format=json', method: 'GET', success: (res) => { - console.log("获取IP成功:", res.data); + // console.log("获取IP成功:", res.data); this.dataForm.ip = res.data.ip; - console.log("登录参数:", this.dataForm); + // console.log("登录参数:", this.dataForm); resolve(res.data.ip); // 成功时解析Promise }, fail: (err) => { - console.log("获取IP失败:", err); - reject(err); // 失败时拒绝Promise + // console.log("获取IP失败:", err); + // 获取IP失败时,将ip设为空字符串并继续执行 + this.dataForm.ip = ''; + // console.log("登录参数:", this.dataForm); + resolve(''); // 解析Promise,继续执行后续操作 } }); }); diff --git a/im-web/src/view/Login.vue b/im-web/src/view/Login.vue index 94a0067..f0e474e 100644 --- a/im-web/src/view/Login.vue +++ b/im-web/src/view/Login.vue @@ -78,7 +78,7 @@ export default { this.$refs[formName].validate((valid) => { if (valid) { this.$http({ - url: "/login", + url: "/loginCustom", method: 'post', data: this.loginForm })