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.

77 lines
2.2 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. const t = i18n.global.t
  21. if (diffHours < 1) {
  22. return `${diffMins}${t('home.difftime.minuteAgo')}`
  23. } else if (diffDays < 1) {
  24. return `${diffHours}${t('home.difftime.hourAgo')}`
  25. } else if (diffDays === 1) {
  26. return t('home.difftime.yesterday')
  27. } else {
  28. return `${msgTime.getFullYear()}-${String(msgTime.getMonth() + 1).padStart(2, '0')}-${String(msgTime.getDate()).padStart(2, '0')}`
  29. }
  30. }
  31. /**
  32. * 按日期分组消息返回扁平化列表每条消息包含group字段
  33. * @param {Array} messages - 原始消息列表
  34. * @returns {Array} 带分组信息的消息列表
  35. */
  36. export function groupMessages(messages) {
  37. const today = new Date()
  38. today.setHours(0, 0, 0, 0)
  39. const yesterday = new Date(today)
  40. yesterday.setDate(yesterday.getDate() - 1)
  41. const t = i18n.global.t
  42. // 直接返回处理后的消息数组(每条消息带group字段)
  43. return messages
  44. .filter(msg => msg.flag !== 1)
  45. .map(msg => {
  46. const msgTime = new Date(msg.czTime)
  47. const formattedTime = formatTime(msg.czTime)
  48. let groupKey
  49. if (msgTime >= today) {
  50. groupKey = 'today'
  51. } else if (msgTime >= yesterday) {
  52. groupKey = 'yesterday'
  53. } else {
  54. groupKey = 'earlier'
  55. }
  56. const group = t(`home.messageGroups.${groupKey}`)
  57. return { ...msg, czTime: formattedTime, groupKey, group }
  58. });
  59. }
  60. // // 异步获取消息
  61. // export const newMessageRes = await API({
  62. // url: '/getMessage',
  63. // method: 'POST',
  64. // data: {}
  65. // });