You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

  1. // 所有导入放在顶部
  2. import API from '@/util/http.js'
  3. import { ref } from 'vue'
  4. import i18n from '@/components/locales'
  5. // 声明变量
  6. const messageList1 = ref()
  7. /**
  8. * 格式化时间为相对时间
  9. * @param {String} timeStr - 原始时间字符串
  10. * @returns {String} 格式化后的时间
  11. */
  12. function formatTime(timeStr) {
  13. // 函数逻辑不变...
  14. const now = new Date();
  15. const msgTime = new Date(timeStr);
  16. const diffMs = now - msgTime;
  17. const diffMins = Math.floor(diffMs / (1000 * 60));
  18. const diffHours = Math.floor(diffMins / 60);
  19. const diffDays = Math.floor(diffHours / 24);
  20. if (diffHours < 1) {
  21. return `${diffMins}分钟前`
  22. } else if (diffDays < 1) {
  23. return `${diffHours}小时前`
  24. } else if (diffDays === 1) {
  25. return '昨天'
  26. } else {
  27. return `${msgTime.getFullYear()}-${String(msgTime.getMonth() + 1).padStart(2, '0')}-${String(msgTime.getDate()).padStart(2, '0')}`
  28. }
  29. }
  30. /**
  31. * 按日期分组消息返回扁平化列表每条消息包含group字段
  32. * @param {Array} messages - 原始消息列表
  33. * @returns {Array} 带分组信息的消息列表
  34. */
  35. export function groupMessages(messages) {
  36. const today = new Date()
  37. today.setHours(0, 0, 0, 0)
  38. const yesterday = new Date(today)
  39. yesterday.setDate(yesterday.getDate() - 1)
  40. const t = i18n.global.t
  41. // 直接返回处理后的消息数组(每条消息带group字段)
  42. return messages
  43. .filter(msg => msg.flag !== 1)
  44. .map(msg => {
  45. const msgTime = new Date(msg.czTime)
  46. const formattedTime = formatTime(msg.czTime)
  47. let groupKey
  48. if (msgTime >= today) {
  49. groupKey = 'today'
  50. } else if (msgTime >= yesterday) {
  51. groupKey = 'yesterday'
  52. } else {
  53. groupKey = 'earlier'
  54. }
  55. const group = t(`home.messageGroups.${groupKey}`)
  56. return { ...msg, czTime: formattedTime, groupKey, group }
  57. });
  58. }
  59. // // 异步获取消息
  60. // export const newMessageRes = await API({
  61. // url: '/getMessage',
  62. // method: 'POST',
  63. // data: {}
  64. // });