Files
crawler-plugin/admin-frontend-vue/src/layout/notification-bell-model.ts
T
huangzd1997 1a486617b5 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 与桌面组件各一条用例钉着。
2026-10-13 08:34:18 +08:00

204 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 后台铃铛通知纯逻辑:未读徽标文案、新通知判定、「已提醒过」去重记录、时间展示。
* 轮询调度与渲染在 NotificationBell.vue;这里只放可单测的纯函数与存储读写。
*/
/** 未读轮询间隔(60 秒)。 */
export const NOTIFICATION_POLL_INTERVAL_MS = 60_000
const NOTIFIED_KEY_PREFIX = 'admin-notification:last-notified-id'
/** 未读徽标文案:0 或非法值显示空串,>99 显示 99+。 */
export function formatUnreadBadge(count: number | null | undefined): string {
const value = Number(count ?? 0)
if (!Number.isFinite(value) || value <= 0) {
return ''
}
return value > 99 ? '99+' : String(Math.floor(value))
}
/** 是否出现新通知:latestId 大于上次已提醒过的 id。 */
export function hasNewNotification(
latestId: number | null | undefined,
lastNotifiedId: number | null | undefined,
): boolean {
const latest = Number(latestId ?? 0)
const notified = Number(lastNotifiedId ?? 0)
if (!Number.isFinite(latest) || latest <= 0) {
return false
}
return latest > (Number.isFinite(notified) && notified > 0 ? notified : 0)
}
function storageKey(uid: string | number | null | undefined): string {
const normalized = uid === null || uid === undefined || String(uid).trim() === '' ? '0' : String(uid)
return `${NOTIFIED_KEY_PREFIX}:${normalized}`
}
/** 读取当前管理员「已提醒过的最大通知 id」。 */
export function readLastNotifiedId(uid: string | number | null | undefined): number {
try {
const raw = window.localStorage.getItem(storageKey(uid))
const value = Number(raw ?? 0)
return Number.isFinite(value) && value > 0 ? value : 0
} catch {
return 0
}
}
export function writeLastNotifiedId(uid: string | number | null | undefined, id: number): void {
try {
if (Number.isFinite(id) && id > 0) {
window.localStorage.setItem(storageKey(uid), String(Math.floor(id)))
}
} catch {
/* 存储不可用时静默忽略(退化为每次都提醒,不影响功能) */
}
}
/** 通知所属日期键(本地时区 yyyy-MM-dd);时间缺失/非法返回空串。 */
export function notificationDayKey(value: string | null | undefined): string {
if (!value) {
return ''
}
const date = new Date(value)
if (Number.isNaN(date.getTime())) {
return ''
}
const pad = (input: number) => String(input).padStart(2, '0')
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
}
/** 日期组头文案:今天 / 昨天 / 2026年9月11日;空键兜底「未知时间」。 */
export function formatDayLabel(day: string, now: Date = new Date()): string {
if (!day) {
return '未知时间'
}
if (day === notificationDayKey(now.toISOString())) {
return '今天'
}
const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)
if (day === notificationDayKey(yesterday.toISOString())) {
return '昨天'
}
const [year, month, date] = day.split('-').map((part) => Number(part))
if (!Number.isFinite(year) || !Number.isFinite(month) || !Number.isFinite(date)) {
return day
}
return `${year}年${month}月${date}日`
}
/** 通知时间展示:今天只显示 HH:mm;昨天显示「昨天 HH:mm」;更早显示 MM-DD HH:mm。 */
export function formatNotificationTime(
value: string | null | undefined,
now: Date = new Date(),
): string {
if (!value) {
return ''
}
const date = new Date(value)
if (Number.isNaN(date.getTime())) {
return ''
}
const pad = (input: number) => String(input).padStart(2, '0')
const hourMinute = `${pad(date.getHours())}:${pad(date.getMinutes())}`
const sameDay =
date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate()
if (sameDay) {
return hourMinute
}
const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)
const isYesterday =
date.getFullYear() === yesterday.getFullYear() &&
date.getMonth() === yesterday.getMonth() &&
date.getDate() === yesterday.getDate()
if (isYesterday) {
return `昨天 ${hourMinute}`
}
return `${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${hourMinute}`
}
/**
* 聚合组的稳定键:场景 × 自然日 × 事件主体用户,与后端的分组键逐字对应。
* 主体为空(存量未标注那一组)要参与拼键,否则两组不同主体的组会撞成同一个 key,
* 展开态就会串台——点开 A 组、展开的却是 B 组。
*/
export function notificationGroupKey(group: {
scene?: string | null
day?: string | null
subjectUserId?: number | string | null
}): string {
const subject =
group.subjectUserId === null || group.subjectUserId === undefined ? 'none' : String(group.subjectUserId)
return `${group.scene ?? ''}|${group.day ?? ''}|${subject}`
}
/** 组行上的计数徽语:折叠前有几条、其中几条没读。只报一个数就会被读成"一共就这些通知"。 */
export function formatGroupCounts(rowCount: number | null | undefined, unreadCount: number | null | undefined): string {
const total = Number(rowCount ?? 0)
const unread = Number(unreadCount ?? 0)
return unread > 0 ? `${total} 条 · ${unread} 未读` : `${total} 条`
}
/**
* 失败反馈的文案与「同场故障只提示一次」的闸门(四面9分口径 §2 后管「错误静默」)。
*
* <p>改前 NotificationBell.vue 的两处 catch 只 console.warn:未读数接口挂了红点静默消失、
* 点通知标已读失败界面纹丝不动,使用者无从判断是"没有通知"还是"接口没通"(全仓 src 无
* errorHandler/unhandledrejection 兜底)。文案在这里出、可见出口由组件注入
* ({@code showAdminFeedback}),模型层不引 element-plus ⇒ 这一族判据能用 node --test 直接跑真函数,
* 不必退化成读源码的钉子。</p>
*/
export type BellFeedbackKind = 'success' | 'error' | 'warning'
/** 可见提示的出口:组件把 admin-feedback-ui 的 showAdminFeedback 递进来。 */
export type BellFeedbackSink = (message: string, kind: BellFeedbackKind) => void
function failureReason(error: unknown): string {
if (error instanceof Error) {
return error.message.trim()
}
return String(error ?? '').trim()
}
/** 未读数刷新失败的提示文案:带上原因,便于区分"接口挂了"与"没权限"。 */
export function summaryFailureMessage(error: unknown): string {
const reason = failureReason(error)
return reason ? `通知未读数刷新失败:${reason}` : '通知未读数刷新失败'
}
/** 标记已读失败的提示文案:点了必须有回应,失败也要有回应。 */
export function markReadFailureMessage(error: unknown): string {
const reason = failureReason(error)
return reason ? `这条通知没能标记为已读:${reason}` : '这条通知没能标记为已读'
}
/**
* 轮询失败的提示闸门:一轮 60s,同一场故障只提示一次,成功一轮后重新武装。
* 不设闸等于把「静默失败」换成「每分钟打扰一次」,两个都是错的口径。
*/
export function createSummaryFailureAlerter(feedback: BellFeedbackSink): {
reportFailure(error: unknown): void
reportSuccess(): void
isArmed(): boolean
} {
let alerted = false
return {
reportFailure(error: unknown): void {
if (alerted) {
return
}
alerted = true
feedback(summaryFailureMessage(error), 'warning')
},
reportSuccess(): void {
alerted = false
},
isArmed(): boolean {
return alerted
},
}
}