feat(通知): 列表按 场景×自然日×主体用户 聚合成两级,红点按组数亮而明细条数同屏;后管日期面板不再被弹窗压住

V158 给 biz_user_notification 加 subject_user_id(事件主体用户,与收件人 user_id 分家):
一行通知是给某个收件人看的,被通知的那个 uid 原先只活在文案字符串里,没有列就没法按用户成组,
也不肯用正则去解历史文案凑分组键。存量不回填(定案)⇒ 老行 subject_user_id 为 NULL,自成「未标注主体」一组。
写入侧 pushOrRefresh 第 8 参带主体,AUDIENCE_USER 时主体=收件人(故新增 7 参便捷档,68 处调用一行没改);
pushToAdminsAnyOf 传 null 并注明理由:一把密钥被 N 个账号共用,不挑一个 uid 冒充。

新增 GET /api/notifications/groups 与 /api/admin/notifications/groups:Java 侧分组(复用 baseWrapper 一份筛选实现),
分页按组切,同屏返回 total=组数 与 rowCount=明细条数;unreadGroupCount 走
COUNT(DISTINCT scene, DATE(created_at), IFNULL(subject_user_id,-1)) —— IFNULL 不能省,
MySQL 的 COUNT(DISTINCT a,b,c) 会跳过任一表达式为 NULL 的行,不兜就把存量那一组整个数没。
红点是新增 unreadGroupCount 而不动 unreadCount 语义:已装机老客户端读 unreadCount 画徽标,
换语义会让老客户端出现「红点 3、列表 217 条」对不上。

铃铛两级(后管+桌面各一份):聚合行点击只展开、不替用户标已读,已读必须进明细逐条点;
明细一次最多 50 条,超出显示「另有 N 条未列出」;下钻带 scene+day+subjectUserId(主体为空改下发
subjectUnlabelled 旗标走 IS NULL),少一维就会把同场景同天别主体的行混进这一组的明细——桌面端最初就漏了这一维。
死码随两级视图退场:groupNotificationsByDay/NotificationDayGroup 删掉,用例换成 formatDayLabel/notificationGroupKey/formatGroupCounts。

后管日期选择面板 z-index 抬到 3300(面板 3200)之上:Element 弹层只自 2000 起递增,不抬就被面板压住,表现为「日期点不动」。
playwright 加 E2E_PORT+strictPort:实测 5174/5176 被别的项目的 vite 占着,reuseExistingServer 让 e2e 连上别人的应用,
当时 5 条全红是端口归属问题而不是回归。

验证=后端通知面 66/66(-Dtest='Notification*' 63 + AdminNotificationControllerTest 3)BUILD SUCCESS;
后管 node --test 1819/1819、真浏览器 e2e 10/10(含遮挡与「展开只取本组」);
桌面 node --test 834/834、组件挂载 vitest 8/8;两前端 vue-tsc --noEmit rc=0;ops 值班侧 18/18。
记账三处=后管 source-pin 逐文件基线 notification-bell.test.ts 用例 6→7(该文件钉子仍 0,占比不动);
桌面 catch 棘轮 blocks 298→299(新增那支是 toggleGroup 明细失败写进面板 loadError,非静默,silent 84 一格没涨);
桌面 endpoints 台账补 /api/notifications/groups。
变异自证 7 条全 KILLED:红点按条数/组内没读完就减组数/聚合行直接标已读/明细截断不报数/下钻丢主体/
丢 IS NULL 旗标/null 主体当值下发。其中「组内没读完就减组数」第一轮 SURVIVED——用例只钉了徽标,
而徽标在 unreadGroupCount 归零时会退回条数兜底,把这条回归盖住了;改成同时钉「N 组未读」chip 才杀掉。

已知盲区(下次补,别当已验):①桌面两级只在 happy-dom 跑过真组件,布局与层级没在真浏览器点过
(桌面日期弹层的层级修复本就在 HEAD 里,不是这批引入);②后管 e2e 夹具没有 subjectUserId=null 的行,
「存量自成一组」在后管 UI 层没被点过,只有后端 WHERE 与桌面组件各一条用例钉着。
This commit is contained in:
2026-10-13 08:34:18 +08:00
parent 1e3bcc9688
commit 5d2a1c6974
31 changed files with 1938 additions and 344 deletions
+143 -13
View File
@@ -152,33 +152,93 @@ const TUTORIAL_PACKAGES = [
]
/* ===================== 站内通知夹具(铃铛面板:搜索/按天分组/分页验收用) ===================== */
/**
* 通知夹具。两条约定不能破:
* ① id 与 createdAt 同为插入时单调(09-10 最早 ⇒ id 最小)——与真库一致;倒挂会让
* "按 id 倒序即按天倒序"这种隐藏假设在 mock 里成立、上线就翻车。
* ② 场景显式给,不从 id 取模推——否则换一批 id,「余额不足」命中数与聚合组数一起漂,
* 用例断言就成了跟着夹具跑的活字。
* 共 14 行、5 行密钥告警;按 场景×自然日×主体用户 折成 10 组,其中 2 组未读、3 行未读。
*/
const NOTIFICATIONS = [
...[6, 5, 4, 3, 2, 1].map((seq) => notif(seq, '2026-09-13', 22, seq, seq <= 3)),
...[4, 3, 2, 1].map((seq) => notif(20 + seq, '2026-09-12', 20, seq, true)),
...[4, 3, 2, 1].map((seq) => notif(30 + seq, '2026-09-10', 18, seq, true)),
]
['2026-09-10', 18, 1, true, 'task_failed'],
['2026-09-10', 18, 2, true, 'task_failed'],
['2026-09-10', 18, 3, true, 'secret_balance'],
['2026-09-10', 18, 4, true, 'task_failed'],
['2026-09-12', 20, 1, true, 'secret_balance'],
['2026-09-12', 20, 2, true, 'task_failed'],
['2026-09-12', 20, 3, true, 'task_failed'],
['2026-09-12', 20, 4, true, 'secret_balance'],
['2026-09-13', 22, 1, true, 'task_failed'],
['2026-09-13', 22, 2, true, 'task_failed'],
['2026-09-13', 22, 3, true, 'secret_balance'],
['2026-09-13', 22, 4, false, 'task_failed'],
['2026-09-13', 22, 5, false, 'task_failed'],
['2026-09-13', 22, 6, false, 'secret_balance'],
].map(([day, hour, seq, read, scene], index) => notif(index + 1, day, hour, seq, read, scene))
function notif(id, day, hour, seq, read) {
const NOTIFICATION_SEED = structuredClone(NOTIFICATIONS)
function notif(id, day, hour, seq, read, scene) {
const minutes = String(seq * 7).padStart(2, '0')
const secret = scene === 'secret_balance'
return {
id,
scene: id % 3 === 0 ? 'secret_balance' : 'task_failed',
level: id % 3 === 0 ? 'error' : 'warning',
title: id % 3 === 0 ? `用户密钥异常:测试用户${seq}` : `跟价任务失败`,
content: id % 3 === 0 ? `用户 测试用户${seq}(uid=${1100 + seq})的代理设置对应服务商余额不足` : `用户 测试用户${seq} 有 ${seq} 个跟价任务失败`,
scene,
level: secret ? 'error' : 'warning',
title: secret ? `用户密钥异常:测试用户${seq}` : `跟价任务失败`,
content: secret
? `用户 测试用户${seq}(uid=${1100 + seq})的代理设置对应服务商余额不足`
: `用户 测试用户${seq} 有 ${seq} 个跟价任务失败`,
// 事件主体用户:与后端 V158 那列同义,聚合分组键的第三维。seq>3 归 1196、其余归 1160,
// 让"同一天两个用户"在夹具里真的存在(否则聚合视图测不到按用户拆组这一维)。
subjectUserId: seq > 3 ? 1196 : 1160,
read,
readAt: read ? `${day} ${hour}:${minutes}:00` : null,
createdAt: `${day} ${hour}:${minutes}:00`,
}
}
/** 与后端 groups 出口同形的聚合:场景 × 自然日 × 主体用户。 */
function groupRows(rows) {
const byKey = new Map()
for (const item of [...rows].sort((a, b) => b.id - a.id)) {
const day = item.createdAt.slice(0, 10)
const key = `${item.scene}|${day}|${item.subjectUserId ?? 'none'}`
let group = byKey.get(key)
if (!group) {
group = {
latestId: item.id,
scene: item.scene,
category: item.scene === 'secret_balance' ? 'config_error' : 'task_error',
day,
subjectUserId: item.subjectUserId ?? null,
rowCount: 0,
unreadCount: 0,
level: item.level,
title: item.title,
content: item.content,
createdAt: item.createdAt,
}
byKey.set(key, group)
}
group.rowCount += 1
if (!item.read) group.unreadCount += 1
if (item.level === 'error') group.level = 'error'
}
return [...byKey.values()]
}
/** 通知列表:关键字匹配标题/内容,日期按年月日区间(与 Java 侧同语义)。 */
function notificationPage(searchParams) {
/** 列表与聚合共用的筛选:关键字 / 日期区间 / 展开某一组(场景 + 自然日 + 主体用户)。 */
function filteredRows(searchParams) {
const keyword = (searchParams.get('keyword') || '').trim()
const startDate = searchParams.get('startDate') || ''
const endDate = searchParams.get('endDate') || ''
const page = Math.max(1, Number(searchParams.get('page') || 1))
const pageSize = Math.min(100, Math.max(1, Number(searchParams.get('pageSize') || 20)))
const scene = searchParams.get('scene') || ''
const day = searchParams.get('day') || ''
const subjectUserId = searchParams.get('subjectUserId')
const subjectUnlabelled = searchParams.get('subjectUnlabelled') === 'true'
let rows = NOTIFICATIONS
if (keyword) {
rows = rows.filter((item) => `${item.title}${item.content}`.includes(keyword))
@@ -189,7 +249,29 @@ function notificationPage(searchParams) {
if (endDate) {
rows = rows.filter((item) => item.createdAt.slice(0, 10) <= endDate)
}
const start = (page - 1) * pageSize
if (scene) {
rows = rows.filter((item) => item.scene === scene)
}
if (day) {
rows = rows.filter((item) => item.createdAt.slice(0, 10) === day)
}
if (subjectUserId) {
rows = rows.filter((item) => String(item.subjectUserId) === String(subjectUserId))
} else if (subjectUnlabelled) {
rows = rows.filter((item) => item.subjectUserId === null || item.subjectUserId === undefined)
}
return rows
}
function readPaging(searchParams) {
const page = Math.max(1, Number(searchParams.get('page') || 1))
const pageSize = Math.min(100, Math.max(1, Number(searchParams.get('pageSize') || 20)))
return { page, pageSize, start: (page - 1) * pageSize }
}
function notificationPage(searchParams) {
const rows = filteredRows(searchParams)
const { page, pageSize, start } = readPaging(searchParams)
return {
success: true,
data: {
@@ -202,6 +284,25 @@ function notificationPage(searchParams) {
}
}
/** 聚合出口:分页按组切,total=组数、rowCount=折叠前的明细条数,两个都给前端。 */
function notificationGroupsPage(searchParams) {
const rows = filteredRows(searchParams)
const groups = groupRows(rows)
const { page, pageSize, start } = readPaging(searchParams)
return {
success: true,
data: {
groups: groups.slice(start, start + pageSize),
total: groups.length,
rowCount: rows.length,
page,
pageSize,
unreadGroupCount: groups.filter((group) => group.unreadCount > 0).length,
unreadCount: rows.filter((item) => !item.read).length,
},
}
}
const server = createServer((req, res) => {
const url = (req.url || '').split('?')[0]
const method = req.method || 'GET'
@@ -410,15 +511,44 @@ const server = createServer((req, res) => {
data: { pending: false, scanned_at: '2026-09-05 08:30:00', items: ALL_ROWS, total: ALL_ROWS.length, page: 1, page_size: 20 },
})
}
// 已读标记会改内存里的夹具(不是幂等只读口)⇒ 用例之间要靠下面这条重置口隔离,
// 否则同一份 mock 服务被 reuseExistingServer 复用时,第二次跑就会带着上一次的已读态。
if (method === 'POST' && url === '/api/admin/notifications/__reset') {
NOTIFICATIONS.splice(0, NOTIFICATIONS.length, ...structuredClone(NOTIFICATION_SEED))
return json(res, { success: true, data: NOTIFICATIONS.length })
}
if (method === 'POST' && /^\/api\/admin\/notifications\/\d+\/read$/.test(url)) {
const id = Number(url.split('/')[3])
const item = NOTIFICATIONS.find((row) => row.id === id)
if (item && !item.read) {
item.read = true
item.readAt = `${item.createdAt.slice(0, 10)} 23:59:59`
}
return json(res, { success: true, data: true })
}
if (method === 'POST' && url === '/api/admin/notifications/read-all') {
for (const item of NOTIFICATIONS) {
if (!item.read) {
item.read = true
item.readAt = `${item.createdAt.slice(0, 10)} 23:59:59`
}
}
return json(res, { success: true, data: NOTIFICATIONS.length })
}
if (url === '/api/admin/notifications/summary') {
const groups = groupRows(NOTIFICATIONS)
return json(res, {
success: true,
data: {
unreadCount: NOTIFICATIONS.filter((item) => !item.read).length,
unreadGroupCount: groups.filter((group) => group.unreadCount > 0).length,
latestId: NOTIFICATIONS.reduce((max, item) => Math.max(max, item.id), 0),
},
})
}
if (url === '/api/admin/notifications/groups') {
return json(res, notificationGroupsPage(new URL(req.url || '/', 'http://127.0.0.1').searchParams))
}
if (url === '/api/admin/notifications') {
return json(res, notificationPage(new URL(req.url || '/', 'http://127.0.0.1').searchParams))
}