From 77555bb92b6cb2678483b3e122e278bb6556eaef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Sat, 20 Dec 2025 11:33:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=81=8A=E5=A4=A9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E7=9B=B8=E5=85=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/chat-detail/index.js | 15 +++- pages/chat-detail/index.wxss | 5 ++ pages/chat/index.js | 56 +++++++++--- pages/chat/index.wxss | 4 + pages/index/index.js | 156 ++++++++++++++++++++++++++++++++ pages/message-list/index.js | 167 ++++++++++++++++++++++++++++++++++- 6 files changed, 389 insertions(+), 14 deletions(-) diff --git a/pages/chat-detail/index.js b/pages/chat-detail/index.js index 0d598dd..8ec16c8 100644 --- a/pages/chat-detail/index.js +++ b/pages/chat-detail/index.js @@ -9,7 +9,8 @@ Page({ loading: false, chatTitle: '聊天对象', managerPhone: null, - timer: null + timer: null, + lastLoadTime: 0 // 用于节流的时间戳 }, onLoad: function (options) { @@ -93,6 +94,18 @@ Page({ }, loadMessages: function () { + // 节流逻辑:避免短时间内多次调用 + const currentTime = Date.now(); + if (currentTime - this.data.lastLoadTime < 2000) { // 2秒内不重复调用 + console.log('API调用节流中,距离上次调用仅' + (currentTime - this.data.lastLoadTime) + 'ms'); + return Promise.resolve(); + } + + // 更新上次加载时间 + this.setData({ + lastLoadTime: currentTime + }); + return new Promise((resolve, reject) => { this.setData({ loading: true }); diff --git a/pages/chat-detail/index.wxss b/pages/chat-detail/index.wxss index c82494c..1f20228 100644 --- a/pages/chat-detail/index.wxss +++ b/pages/chat-detail/index.wxss @@ -123,6 +123,11 @@ padding: 16rpx 24rpx; border-top: 1rpx solid #e8e8e8; gap: 20rpx; + position: fixed; + bottom: 0; + left: 0; + right: 0; + z-index: 100; } .message-input { diff --git a/pages/chat/index.js b/pages/chat/index.js index 0f39ac8..b0f8e12 100644 --- a/pages/chat/index.js +++ b/pages/chat/index.js @@ -8,7 +8,8 @@ Page({ filteredChatList: [], timer: null, debugCount: 0, // 调试信息输出计数器 - loginToastShown: false // 登录提示是否已显示 + loginToastShown: false, // 登录提示是否已显示 + lastLoadTime: 0 // 用于节流的时间戳 }, onLoad: function (options) { @@ -68,21 +69,37 @@ Page({ }, startTimer: function () { - // 设置5秒刷新定时器 + // 确保只有一个定时器在运行 + if (this.data.timer) { + clearInterval(this.data.timer); + this.setData({ timer: null }); + } + + // 设置30秒刷新定时器 this.setData({ timer: setInterval(() => { this.loadChatList(); - }, 5000) + }, 30000) }); }, loadChatList: function () { + // 节流逻辑:避免短时间内多次调用 + const currentTime = Date.now(); + if (currentTime - this.data.lastLoadTime < 2000) { // 2秒内不重复调用 + console.log('API调用节流中,距离上次调用仅' + (currentTime - this.data.lastLoadTime) + 'ms'); + return; + } + + // 更新上次加载时间 + this.setData({ + lastLoadTime: currentTime + }); + // 增加调试信息计数器 const newDebugCount = this.data.debugCount + 1; this.setData({ debugCount: newDebugCount }); - - // 获取用户手机号 - 增强版,增加更多获取途径和调试信息 const users = wx.getStorageSync('users') || {}; const userId = wx.getStorageSync('userId'); @@ -186,16 +203,30 @@ Page({ // 调用API获取聊天列表 API.getChatList(userPhone).then(res => { if (res && Array.isArray(res)) { + // 创建业务员信息缓存,避免重复调用API + const personnelCache = {}; + // 处理每个聊天项,获取业务员信息 const chatListPromises = res.map(async (chatItem) => { if (chatItem.manager_phone) { try { - // 获取业务员信息 - const personnelInfo = await API.getSalesPersonnelInfo(chatItem.manager_phone); - if (personnelInfo) { - // 使用业务员的alias作为显示名称 - chatItem.name = personnelInfo.alias || chatItem.manager_phone; + // 先检查缓存中是否已有该业务员信息 + if (personnelCache[chatItem.manager_phone]) { + const cachedInfo = personnelCache[chatItem.manager_phone]; + chatItem.name = cachedInfo.alias || chatItem.manager_phone; chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; + console.log('使用缓存的业务员信息:', chatItem.manager_phone); + } else { + // 获取业务员信息 + const personnelInfo = await API.getSalesPersonnelInfo(chatItem.manager_phone); + if (personnelInfo) { + // 缓存业务员信息 + personnelCache[chatItem.manager_phone] = personnelInfo; + // 使用业务员的alias作为显示名称 + chatItem.name = personnelInfo.alias || chatItem.manager_phone; + chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; + console.log('获取并缓存业务员信息:', chatItem.manager_phone); + } } } catch (error) { console.error('获取业务员信息失败:', error); @@ -209,13 +240,14 @@ Page({ chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; } - // 确保消息内容存在 + // 修复:对所有没有消息内容的聊天项获取最新消息内容 if (!chatItem.content) { - // 如果没有消息内容,尝试获取该聊天会话的最新消息 try { const messages = await API.getChatMessages(chatItem.manager_phone, userPhone, { limit: 1 }); if (messages.length > 0) { chatItem.content = messages[0].content || '暂无消息内容'; + // 同时更新时间为最新消息的时间 + chatItem.time = messages[0].created_at || null; } else { chatItem.content = '暂无消息内容'; } diff --git a/pages/chat/index.wxss b/pages/chat/index.wxss index 99abb1f..7654e3c 100644 --- a/pages/chat/index.wxss +++ b/pages/chat/index.wxss @@ -96,6 +96,10 @@ font-size: 32rpx; font-weight: 500; color: #333; + max-width: 70%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; } /* 时间 */ diff --git a/pages/index/index.js b/pages/index/index.js index fb3c659..f254c19 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -8,6 +8,8 @@ Page({ name: '', showAuthModal: false, showOneKeyLoginModal: false, + userInfo: {}, + needPhoneAuth: false, // 测试模式开关,用于在未完成微信认证时进行测试 testMode: true }, @@ -65,6 +67,8 @@ Page({ onLoad() { console.log('首页初始化') + // 检查本地缓存并恢复登录状态 + this.checkAndRestoreLoginStatus() }, onShow: function () { @@ -78,6 +82,9 @@ Page({ // 更新全局tab状态 const app = getApp(); app.updateCurrentTab('index'); + + // 检查并恢复登录状态 + this.checkAndRestoreLoginStatus() }, // 跳转到估价页面 @@ -873,6 +880,155 @@ Page({ wx.hideLoading() }, + // 检查本地缓存并恢复登录状态 + checkAndRestoreLoginStatus() { + console.log('开始检查并恢复登录状态') + const app = getApp() + + // 从本地存储获取用户信息 + const localUserInfo = wx.getStorageSync('userInfo') || {} + const userId = wx.getStorageSync('userId') + const openid = wx.getStorageSync('openid') + console.log('恢复登录状态 - userId:', userId, 'openid:', openid ? '已获取' : '未获取') + + // 优先使用全局用户信息,如果没有则使用本地存储的用户信息 + if (app.globalData.userInfo) { + this.setData({ + userInfo: app.globalData.userInfo, + needPhoneAuth: !app.globalData.userInfo.phoneNumber + }) + } else { + app.globalData.userInfo = localUserInfo + this.setData({ + userInfo: localUserInfo, + needPhoneAuth: !localUserInfo.phoneNumber + }) + } + + if (userId && openid) { + // 确保users存储结构存在 + let users = wx.getStorageSync('users') + if (!users) { + users = {} + wx.setStorageSync('users', users) + } + + if (!users[userId]) { + users[userId] = { type: '' } + wx.setStorageSync('users', users) + } + + // 先显示本地存储的用户类型,但会被服务器返回的最新值覆盖 + const user = users[userId] + const currentType = user.type + this.setData({ currentUserType: currentType }) + console.log('恢复登录状态 - 当前本地存储的用户类型:', currentType) + + // 从服务器获取最新的用户信息,确保身份由数据库决定 + this.refreshUserInfoFromServer(openid, userId) + } else { + console.log('未找到有效的本地登录信息') + } + }, + + // 从服务器刷新用户信息并同步身份数据 + refreshUserInfoFromServer(openid, userId) { + const API = require('../../utils/api.js') + + API.getUserInfo(openid).then(res => { + console.log('从服务器获取用户信息成功:', res) + + if (res.success && res.data) { + const serverUserInfo = res.data + + // 更新本地用户信息 + const app = getApp() + const updatedUserInfo = { + ...app.globalData.userInfo, + ...serverUserInfo + } + + app.globalData.userInfo = updatedUserInfo + wx.setStorageSync('userInfo', updatedUserInfo) + this.setData({ userInfo: updatedUserInfo }) + + // 同步更新用户身份信息(当前身份由数据库决定) + if (serverUserInfo.type) { + this.syncUserTypeFromServer(userId, serverUserInfo.type) + } + + console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type) + } + }).catch(err => { + console.error('从服务器获取用户信息失败:', err) + // 如果getUserInfo失败,尝试使用validateUserLogin作为备选 + API.validateUserLogin().then(res => { + console.log('使用validateUserLogin获取用户信息成功:', res) + + if (res.success && res.data) { + const serverUserInfo = res.data + + // 更新本地用户信息 + const app = getApp() + const updatedUserInfo = { + ...app.globalData.userInfo, + ...serverUserInfo + } + + app.globalData.userInfo = updatedUserInfo + wx.setStorageSync('userInfo', updatedUserInfo) + this.setData({ userInfo: updatedUserInfo }) + + // 同步更新用户身份信息(当前身份由数据库决定) + if (serverUserInfo.type) { + this.syncUserTypeFromServer(userId, serverUserInfo.type) + } + + console.log('用户信息已更新(备选方案):', updatedUserInfo) + } + }).catch(validateErr => { + console.error('从服务器获取用户信息失败(包括备选方案):', validateErr) + // 如果服务器请求失败,继续使用本地缓存的信息 + }) + }) + }, + + // 从服务器同步用户身份信息 + syncUserTypeFromServer(userId, serverType) { + if (!userId || !serverType) { + console.error('同步用户身份信息失败: 参数不完整') + return + } + + console.log('从服务器同步用户身份信息:', { userId, serverType }) + + // 更新本地存储的用户身份 + let users = wx.getStorageSync('users') || {} + if (!users[userId]) { + users[userId] = {} + } + + // 移除serverType中的customer(如果存在) + let processedServerType = serverType.replace(/,?customer/g, '').replace(/^,|,$/g, '') + + // 构建新的用户类型 + let newUserType = processedServerType + + // 只有当新构建的用户类型与本地不同时才更新 + if (users[userId].type !== newUserType) { + users[userId].type = newUserType + wx.setStorageSync('users', users) + + // 更新全局用户类型 + const app = getApp() + app.globalData.userType = newUserType + + console.log('用户身份已从服务器同步并保留客服标识:', newUserType) + } else { + console.log('用户身份与服务器一致,无需更新:', newUserType) + } + }, + // 上传用户信息到服务器 async uploadUserInfoToServer(userInfo, userId, type) { // 引入API服务 diff --git a/pages/message-list/index.js b/pages/message-list/index.js index 97c1ac2..a971513 100644 --- a/pages/message-list/index.js +++ b/pages/message-list/index.js @@ -1,13 +1,29 @@ // pages/message-list/index.js +const API = require('../../utils/api.js'); Page({ data: { - messageList: [] + messageList: [], + userInfo: {}, + needPhoneAuth: false }, onLoad: function (options) { + // 检查本地缓存并恢复登录状态 + this.checkAndRestoreLoginStatus(); this.loadMessageList(); }, + onShow: function () { + // 页面显示时再次检查登录状态 + this.checkAndRestoreLoginStatus(); + // 更新自定义tabBar状态 + if (typeof this.getTabBar === 'function' && this.getTabBar()) { + this.getTabBar().setData({ + selected: 3 // 假设消息中心是第4个tab(索引3) + }); + } + }, + loadMessageList: function () { // 模拟加载消息列表 const messageList = [ @@ -55,5 +71,154 @@ Page({ onPullDownRefresh: function () { this.loadMessageList(); wx.stopPullDownRefresh(); + }, + + // 检查本地缓存并恢复登录状态 + checkAndRestoreLoginStatus() { + console.log('开始检查并恢复登录状态') + const app = getApp() + + // 从本地存储获取用户信息 + const localUserInfo = wx.getStorageSync('userInfo') || {} + const userId = wx.getStorageSync('userId') + const openid = wx.getStorageSync('openid') + console.log('恢复登录状态 - userId:', userId, 'openid:', openid ? '已获取' : '未获取') + + // 优先使用全局用户信息,如果没有则使用本地存储的用户信息 + if (app.globalData.userInfo) { + this.setData({ + userInfo: app.globalData.userInfo, + needPhoneAuth: !app.globalData.userInfo.phoneNumber + }) + } else { + app.globalData.userInfo = localUserInfo + this.setData({ + userInfo: localUserInfo, + needPhoneAuth: !localUserInfo.phoneNumber + }) + } + + if (userId && openid) { + // 确保users存储结构存在 + let users = wx.getStorageSync('users') + if (!users) { + users = {} + wx.setStorageSync('users', users) + } + + if (!users[userId]) { + users[userId] = { type: '' } + wx.setStorageSync('users', users) + } + + // 先显示本地存储的用户类型,但会被服务器返回的最新值覆盖 + const user = users[userId] + const currentType = user.type + this.setData({ userType: currentType }) + console.log('恢复登录状态 - 当前本地存储的用户类型:', currentType) + + // 从服务器获取最新的用户信息,确保身份由数据库决定 + this.refreshUserInfoFromServer(openid, userId) + } else { + console.log('未找到有效的本地登录信息') + } + }, + + // 从服务器刷新用户信息并同步身份数据 + refreshUserInfoFromServer(openid, userId) { + const API = require('../../utils/api.js') + + API.getUserInfo(openid).then(res => { + console.log('从服务器获取用户信息成功:', res) + + if (res.success && res.data) { + const serverUserInfo = res.data + + // 更新本地用户信息 + const app = getApp() + const updatedUserInfo = { + ...app.globalData.userInfo, + ...serverUserInfo + } + + app.globalData.userInfo = updatedUserInfo + wx.setStorageSync('userInfo', updatedUserInfo) + this.setData({ userInfo: updatedUserInfo }) + + // 同步更新用户身份信息(当前身份由数据库决定) + if (serverUserInfo.type) { + this.syncUserTypeFromServer(userId, serverUserInfo.type) + } + + console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type) + } + }).catch(err => { + console.error('从服务器获取用户信息失败:', err) + // 如果getUserInfo失败,尝试使用validateUserLogin作为备选 + API.validateUserLogin().then(res => { + console.log('使用validateUserLogin获取用户信息成功:', res) + + if (res.success && res.data) { + const serverUserInfo = res.data + + // 更新本地用户信息 + const app = getApp() + const updatedUserInfo = { + ...app.globalData.userInfo, + ...serverUserInfo + } + + app.globalData.userInfo = updatedUserInfo + wx.setStorageSync('userInfo', updatedUserInfo) + this.setData({ userInfo: updatedUserInfo }) + + // 同步更新用户身份信息(当前身份由数据库决定) + if (serverUserInfo.type) { + this.syncUserTypeFromServer(userId, serverUserInfo.type) + } + + console.log('用户信息已更新(备选方案):', updatedUserInfo) + } + }).catch(validateErr => { + console.error('从服务器获取用户信息失败(包括备选方案):', validateErr) + // 如果服务器请求失败,继续使用本地缓存的信息 + }) + }) + }, + + // 从服务器同步用户身份信息 + syncUserTypeFromServer(userId, serverType) { + if (!userId || !serverType) { + console.error('同步用户身份信息失败: 参数不完整') + return + } + + console.log('从服务器同步用户身份信息:', { userId, serverType }) + + // 更新本地存储的用户身份 + let users = wx.getStorageSync('users') || {} + if (!users[userId]) { + users[userId] = {} + } + + // 移除serverType中的customer(如果存在) + let processedServerType = serverType.replace(/,?customer/g, '').replace(/^,|,$/g, '') + + // 构建新的用户类型 + let newUserType = processedServerType + + // 只有当新构建的用户类型与本地不同时才更新 + if (users[userId].type !== newUserType) { + users[userId].type = newUserType + wx.setStorageSync('users', users) + + // 更新全局用户类型 + const app = getApp() + app.globalData.userType = newUserType + + console.log('用户身份已从服务器同步并保留客服标识:', newUserType) + } else { + console.log('用户身份与服务器一致,无需更新:', newUserType) + } } }); \ No newline at end of file