// 所有导入放在顶部 import API from '@/util/http.js' import { ref } from 'vue' import i18n from '@/components/locales' // 声明变量 const messageList1 = ref() /** * 格式化时间为相对时间 * @param {String} timeStr - 原始时间字符串 * @returns {String} 格式化后的时间 */ function formatTime(timeStr) { // 函数逻辑不变... const now = new Date(); const msgTime = new Date(timeStr); const diffMs = now - msgTime; const diffMins = Math.floor(diffMs / (1000 * 60)); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24); if (diffHours < 1) { return `${diffMins}分钟前` } else if (diffDays < 1) { return `${diffHours}小时前` } else if (diffDays === 1) { return '昨天' } else { return `${msgTime.getFullYear()}-${String(msgTime.getMonth() + 1).padStart(2, '0')}-${String(msgTime.getDate()).padStart(2, '0')}` } } /** * 按日期分组消息(返回扁平化列表,每条消息包含group字段) * @param {Array} messages - 原始消息列表 * @returns {Array} 带分组信息的消息列表 */ export function groupMessages(messages) { const today = new Date() today.setHours(0, 0, 0, 0) const yesterday = new Date(today) yesterday.setDate(yesterday.getDate() - 1) const t = i18n.global.t // 直接返回处理后的消息数组(每条消息带group字段) return messages .filter(msg => msg.flag !== 1) .map(msg => { const msgTime = new Date(msg.czTime) const formattedTime = formatTime(msg.czTime) let groupKey if (msgTime >= today) { groupKey = 'today' } else if (msgTime >= yesterday) { groupKey = 'yesterday' } else { groupKey = 'earlier' } const group = t(`home.messageGroups.${groupKey}`) return { ...msg, czTime: formattedTime, groupKey, group } }); } // // 异步获取消息 // export const newMessageRes = await API({ // url: '/getMessage', // method: 'POST', // data: {} // });