kongdeqiang
2026-10-12 285bf234bd50ce54fcae7bb777f8fa579b91266e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * 会话列表的时间处理与分组。
 *
 * 「今天 / 昨天 / 更早」这三个分组的判定看起来简单,但有几个必须处理好的点:
 *
 *   1. **按本地日历日比较,不是按 24 小时差**
 *      凌晨 1 点看到「昨天 23:00 的对话」,如果按「相差不到 24 小时」算,
 *      它会被归到「今天」,与用户的直觉不符。
 *   2. **时间是 ISO 字符串且不带时区**
 *      后端返回的是 LocalDateTime(如 2026-10-09T10:32:24),
 *      `new Date('2026-10-09T10:32:24')` 在浏览器里按**本地时区**解析,
 *      行为正好符合预期;但如果哪天后端改成带 Z 的格式,
 *      这里的比较逻辑需要跟着调整(所以集中在本文件里处理)。
 *   3. **置顶会话单独成组**
 *      它们已经按「置顶先后」排好序,再做时间分组反而会把顺序打乱。
 */
 
const DAY_MS = 24 * 60 * 60 * 1000
 
/** 解析后端时间;无法解析时返回 null,由调用方兜底 */
export function parseTime(value) {
  if (!value) return null
  if (value instanceof Date) return value
  const date = new Date(value)
  return Number.isNaN(date.getTime()) ? null : date
}
 
function startOfDay(date) {
  return new Date(date.getFullYear(), date.getMonth(), date.getDate())
}
 
/**
 * 相差几个「日历日」。
 * 0=今天,1=昨天,2=前天…… 只按日期算,忽略具体时刻。
 */
export function daysAgo(date, now = new Date()) {
  if (!date) return Number.MAX_SAFE_INTEGER
  const diff = startOfDay(now).getTime() - startOfDay(date).getTime()
  return Math.round(diff / DAY_MS)
}
 
function pad(n) {
  return String(n).padStart(2, '0')
}
 
/**
 * 列表项上显示的日期文本 —— 用户明确要求「列表里要能看到日期」,
 * 所以这里始终带上日期,而不是只给一个 HH:mm。
 *
 *   今天   → 今天 10:32
 *   昨天   → 昨天 10:32
 *   今年   → 10-09 10:32
 *   往年   → 2025-10-09 10:32
 */
export function formatChatTime(value, now = new Date()) {
  const date = parseTime(value)
  if (!date) return ''
 
  const time = `${pad(date.getHours())}:${pad(date.getMinutes())}`
  const ago = daysAgo(date, now)
  if (ago === 0) return `今天 ${time}`
  if (ago === 1) return `昨天 ${time}`
  if (date.getFullYear() === now.getFullYear()) {
    return `${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${time}`
  }
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${time}`
}
 
/** 完整时间,用于 hover 提示 */
export function formatFullTime(value) {
  const date = parseTime(value)
  if (!date) return ''
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
    `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
}
 
/** 相对时间,用于归档列表这类不强调精确时刻的地方 */
export function formatRelative(value, now = new Date()) {
  const date = parseTime(value)
  if (!date) return ''
  const ago = daysAgo(date, now)
  if (ago === 0) return '今天'
  if (ago === 1) return '昨天'
  if (ago < 30) return `${ago} 天前`
  if (date.getFullYear() === now.getFullYear()) {
    return `${date.getMonth() + 1} 月 ${date.getDate()} 日`
  }
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
}
 
/**
 * 把会话列表按「置顶 / 今天 / 昨天 / 更早」分组。
 *
 * @param {Array} chats 后端已排好序的列表(置顶在前,其余按最近活跃倒序)
 * @returns {Array<{key:string,label:string,chats:Array}>} 只返回非空分组
 *
 * 注意:组内顺序**完全沿用后端顺序**,这里只做分桶,不排序。
 */
export function groupChats(chats, now = new Date()) {
  const list = Array.isArray(chats) ? chats : []
 
  const pinned = []
  const today = []
  const yesterday = []
  const earlier = []
 
  for (const chat of list) {
    if (chat.pinned) {
      pinned.push(chat)
      continue
    }
    // 分组依据用「最近活跃时间」:用户对「这段对话是什么时候的」的记忆
    // 通常来自最后一次交谈,而不是创建时间
    const ago = daysAgo(parseTime(chat.updatedAt || chat.createdAt), now)
    if (ago <= 0) today.push(chat)
    else if (ago === 1) yesterday.push(chat)
    else earlier.push(chat)
  }
 
  const groups = []
  if (pinned.length) groups.push({ key: 'pinned', label: '置顶', chats: pinned })
  if (today.length) groups.push({ key: 'today', label: '今天', chats: today })
  if (yesterday.length) groups.push({ key: 'yesterday', label: '昨天', chats: yesterday })
  if (earlier.length) groups.push({ key: 'earlier', label: '更早', chats: earlier })
  return groups
}