| | |
| | | |
| | | // 加载消息列表 |
| | | loadMessages() { |
| | | // 检查用户是否已登录 |
| | | const userInfo = app.globalData.userInfo |
| | | // 检查用户是否已登录,如果globalData中没有,尝试从本地存储恢复 |
| | | let userInfo = app.globalData.userInfo |
| | | if (!userInfo || !userInfo.userId) { |
| | | console.log('globalData中没有userInfo,尝试从本地存储恢复') |
| | | try { |
| | | const storedUserInfo = wx.getStorageSync('userInfo') |
| | | if (storedUserInfo && storedUserInfo.userId) { |
| | | console.log('从本地存储恢复userInfo成功') |
| | | app.globalData.userInfo = storedUserInfo |
| | | userInfo = storedUserInfo |
| | | } |
| | | } catch (error) { |
| | | console.error('从本地存储恢复userInfo失败:', error) |
| | | } |
| | | } |
| | | |
| | | if (!userInfo || !userInfo.userId) { |
| | | console.error('用户未登录或userId不存在') |
| | | wx.showToast({ |
| | |
| | | }) |
| | | }, |
| | | |
| | | // 格式化消息时间 |
| | | formatMessageTime(timeStr) { |
| | | if (!timeStr) return '' |
| | | |
| | | try { |
| | | const date = new Date(timeStr) |
| | | const now = new Date() |
| | | const diff = now.getTime() - date.getTime() |
| | | |
| | | // 如果是今天 |
| | | if (diff < 24 * 60 * 60 * 1000) { |
| | | const hours = date.getHours().toString().padStart(2, '0') |
| | | const minutes = date.getMinutes().toString().padStart(2, '0') |
| | | return `${hours}:${minutes}` |
| | | } |
| | | |
| | | // 如果是昨天 |
| | | if (diff < 48 * 60 * 60 * 1000) { |
| | | const hours = date.getHours().toString().padStart(2, '0') |
| | | const minutes = date.getMinutes().toString().padStart(2, '0') |
| | | return `昨天 ${hours}:${minutes}` |
| | | } |
| | | |
| | | // 其他日期 |
| | | const month = (date.getMonth() + 1).toString().padStart(2, '0') |
| | | const day = date.getDate().toString().padStart(2, '0') |
| | | const hours = date.getHours().toString().padStart(2, '0') |
| | | const minutes = date.getMinutes().toString().padStart(2, '0') |
| | | return `${month}-${day} ${hours}:${minutes}` |
| | | } catch (error) { |
| | | console.error('时间格式化失败:', error) |
| | | return timeStr |
| | | } |
| | | } |
| | | }) |