Files
crawler-plugin/admin-frontend-vue/src/components/NotificationBell.vue
T
huangzd1997 d268ecdf6b feat(通知): task_failed 带 ref_json 定位锚点,明细可点直达任务详情
用户原话「都没体现哪个任务id/店铺/用户,咋排查」:通知表只有 title/content,
失败任务又按「用户×模块×自然日」聚合成一条,明细被 5 个 taskNo 预览 + 512 字截断吃掉。

- V162 给 biz_user_notification 加 ref_json(TEXT,可重跑迁移,存量不回填)
- 扫描器把桶内每个失败任务写成锚点(taskId/taskNo/moduleType/reason/updatedAt/
  deviceId),上限 20 条;品牌任务无 task_no 用 #<id> 兜底,保证至少一条可定位
- 详情接口回传 refs;同键刷新时锚点变化也更新,且不重新点亮已读
- 后台铃铛「展开明细」渲染成可点行,跳任务中心 ?taskNo= 自动开详情抽屉
- 新增判据 taskFailedAdminNotificationCarriesUidAndTaskNoAnchor:管理端正文必须含
  uid、每个桶的 ref_json 至少一条非空 taskNo
2026-10-22 10:28:12 +08:00

782 lines
19 KiB
Vue
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.
<template>
<div ref="rootRef" class="admin-notification-bell">
<button
type="button"
class="bell-trigger"
:class="{ 'bell-trigger--active': panelOpen }"
:title="unreadCount > 0 ? `通知(${unreadCount} 条未读)` : '通知'"
aria-label="通知"
@click="togglePanel"
>
<svg
class="bell-icon"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M18 8a6 6 0 0 0-12 0c0 7-3 9-3 9h18s-3-2-3-9" />
<path d="M13.73 21a2 2 0 0 1-3.46 0" />
</svg>
<span v-if="badgeText" class="bell-badge">{{ badgeText }}</span>
</button>
<!-- 面板挂到 body 上(fixed 定位):顶栏内绝对定位时会被列表页 el-select 等页面内容压住,提 z-index 无效 -->
<Teleport to="body">
<div
v-if="panelOpen"
ref="panelRef"
class="bell-panel"
:style="panelStyle"
role="dialog"
aria-label="通知列表"
>
<div class="bell-panel-head">
<span class="bell-panel-title">通知</span>
<button
v-if="unreadCount > 0"
type="button"
class="bell-read-all"
:disabled="markingAll"
@click="readAll"
>
{{ markingAll ? '处理中...' : '全部已读' }}
</button>
</div>
<div class="bell-search">
<input
v-model="keyword"
type="text"
class="bell-search-input"
placeholder="搜索标题或内容"
aria-label="搜索通知"
@input="onKeywordInput"
/>
<div class="bell-search-days">
<el-date-picker
:model-value="dateRangeValue"
type="daterange"
value-format="YYYY-MM-DD"
start-placeholder="开始日期"
end-placeholder="结束日期"
range-separator="至"
size="small"
class="bell-date-picker"
popper-class="bell-date-popper"
:clearable="true"
@update:model-value="onDateRangeChange"
@change="reload"
/>
<button v-if="hasFilter" type="button" class="bell-search-reset" @click="resetFilters">
重置
</button>
</div>
<div class="bell-sort-chips" role="group" aria-label="通知排序">
<span class="bell-sort-label">排序</span>
<button
type="button"
class="bell-sort-chip"
:class="{ 'is-active': sortMode === 'severity' }"
@click="setSortMode('severity')"
>
严重度
</button>
<button
type="button"
class="bell-sort-chip"
:class="{ 'is-active': sortMode === 'time' }"
@click="setSortMode('time')"
>
时间
</button>
</div>
</div>
<div v-if="loading && !groups.length" class="bell-empty">正在加载...</div>
<div v-else-if="loadError && !groups.length" class="bell-empty bell-empty--error">{{ loadError }}</div>
<div v-else-if="!groups.length" class="bell-empty">{{ hasFilter ? '没有符合条件的通知' : '暂无通知' }}</div>
<ul v-else class="bell-list">
<template v-for="(group, index) in groups" :key="notificationGroupKey(group)">
<!-- 严重度排序会跨天交错,日期头只在时间排序下有意义 -->
<li v-if="sortMode === 'time' && startsNewDay(index)" class="bell-day-head">{{ formatDayLabel(group.day) }}</li>
<li
class="bell-group"
:class="[`bell-item--${group.level || 'warning'}`, { 'bell-group--open': isExpanded(group) }]"
@click="toggleGroup(group)"
>
<div class="bell-item-head">
<span class="bell-severity-tag" :class="`bell-severity-tag--${itemSeverity(group)}`">
{{ severityLabel(group) }}
</span>
<span class="bell-item-title">{{ group.title }}</span>
<span class="bell-item-time">{{ formatTime(group.createdAt) }}</span>
</div>
<div class="bell-item-content">{{ group.content }}</div>
<div class="bell-group-foot">
<span class="bell-group-count">{{ formatGroupCounts(group.rowCount, group.unreadCount) }}</span>
<span class="bell-group-toggle">{{ isExpanded(group) ? '收起明细' : '展开明细' }}</span>
</div>
</li>
<!-- 已读只能进明细逐条点:聚合行上的点击只做展开,避免"看一眼顺手把没看过的全标成已读" -->
<template v-if="isExpanded(group)">
<li v-if="detailLoading" class="bell-empty bell-empty--detail">正在加载明细...</li>
<li v-else-if="!details.length" class="bell-empty bell-empty--detail">这一组里没有通知</li>
<li
v-for="item in details"
:key="item.id"
class="bell-item bell-item--detail"
:class="{ 'bell-item--unread': !item.read }"
@click="onItemClick(item)"
>
<div class="bell-item-head">
<span class="bell-severity-tag" :class="`bell-severity-tag--${itemSeverity(item)}`">
{{ severityLabel(item) }}
</span>
<span class="bell-item-title">{{ item.title }}</span>
<span class="bell-item-time">{{ formatTime(item.createdAt) }}</span>
</div>
<div class="bell-item-content">{{ item.content }}</div>
<div v-if="item.refs && item.refs.length" class="bell-ref-list">
<button
v-for="ref in item.refs"
:key="`${item.id}-${ref.taskNo}`"
type="button"
class="bell-ref-link"
:title="ref.reason || `查看任务 ${ref.taskNo}`"
@click.stop="openTaskRef(item, ref)"
>
{{ ref.taskNo }}<span v-if="ref.reason" class="bell-ref-reason">:{{ ref.reason }}</span>
</button>
</div>
</li>
<li v-if="!detailLoading && detailTotal > details.length" class="bell-detail-more">
另有 {{ detailTotal - details.length }} 条未列出,可缩小日期范围后查看
</li>
</template>
</template>
</ul>
<div class="bell-panel-foot">
<button
type="button"
class="bell-page-btn"
:disabled="page <= 1 || loading"
@click="goPage(page - 1)"
>
上一页
</button>
<span class="bell-page-info">{{ page }} / {{ totalPages }}</span>
<button
type="button"
class="bell-page-btn"
:disabled="page >= totalPages || loading"
@click="goPage(page + 1)"
>
下一页
</button>
<span class="bell-page-total">共 {{ total }} 组 · 明细 {{ rowCount }} 条</span>
</div>
</div>
</Teleport>
</div>
</template>
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAdminSessionStore } from '@/stores/admin-session'
import type { AdminNotificationItem, AdminNotificationRef } from '@/api/notifications'
import {
NOTIFICATION_POLL_INTERVAL_MS,
formatDayLabel,
formatGroupCounts,
formatNotificationTime,
notificationGroupKey,
taskCenterRouteForRef,
} from '@/layout/notification-bell-model'
import { todayDateInput } from '@/utils/datetime'
import {
itemLevel,
itemSeverity,
severityLabel,
startsNewDay as groupStartsNewDay,
useNotificationBellPanel,
} from '@/layout/notification-bell-panel'
const session = useAdminSessionStore()
const router = useRouter()
/**
* 面板的数据流(聚合组、明细、两个计数、防抖与取数)整块在 notification-bell-panel.ts。
* 那一层是真函数,`npm test` 能直接跑判据;组件只留 DOM 相关的一半:
* 面板开合、fixed 定位、外部点击关闭、60s 轮询定时器。
*/
const {
badgeText,
dateRangeValue,
detailLoading,
detailTotal,
details,
goPage,
groups,
hasFilter,
isExpanded,
keyword,
loadError,
loading,
markingAll,
onItemClick,
onDateRangeChange,
onKeywordInput,
page,
readAll,
refreshSummary,
reload,
resetFilters,
rowCount,
setSortMode,
sortMode,
toggleGroup,
total,
totalPages,
unreadCount,
} = useNotificationBellPanel({ uid: () => session.user?.id ?? 0, today: () => todayDateInput() })
/** 点锚点:先按原逻辑标已读,再跳任务中心(路由带 taskNo,页面进来自动开详情抽屉)。 */
function openTaskRef(item: AdminNotificationItem, ref: AdminNotificationRef): void {
const target = taskCenterRouteForRef(ref)
if (!target) {
return
}
void onItemClick(item)
void router.push(target)
}
const rootRef = ref<HTMLElement | null>(null)
const panelRef = ref<HTMLElement | null>(null)
const panelStyle = ref<Record<string, string>>({})
const panelOpen = ref(false)
/** 模板里仍是 startsNewDay(index):组序列在面板层,这里只补一次取值。 */
function startsNewDay(index: number): boolean {
return groupStartsNewDay(groups.value, index)
}
function formatTime(value: string | null) {
return formatNotificationTime(value)
}
/** 面板挂到 body 后用 fixed 定位,位置按触发器实时算(右对齐在铃铛下方)。 */
function syncPanelPosition() {
const trigger = rootRef.value
if (!trigger) return
const rect = trigger.getBoundingClientRect()
panelStyle.value = {
top: `${Math.round(rect.bottom + 10)}px`,
right: `${Math.max(8, Math.round(window.innerWidth - rect.right))}px`,
}
}
function togglePanel() {
panelOpen.value = !panelOpen.value
if (panelOpen.value) {
syncPanelPosition()
reload()
}
}
/** 点击组件外部关闭面板(面板已 Teleport 到 body,需连同面板自身一起判断)。 */
function onDocumentMouseDown(event: MouseEvent) {
if (!panelOpen.value) return
const target = event.target
if (!(target instanceof Node)) return
if (rootRef.value?.contains(target) || panelRef.value?.contains(target)) return
panelOpen.value = false
}
/** 窗口尺寸变化时重新对齐面板。 */
function onWindowResize() {
if (panelOpen.value) {
syncPanelPosition()
}
}
/** 页面回到前台时立即刷新一次未读数。 */
function onVisibilityChange() {
if (typeof document !== 'undefined' && document.visibilityState === 'visible') {
refreshSummary()
}
}
let pollTimer: number | null = null
onMounted(() => {
document.addEventListener('mousedown', onDocumentMouseDown)
document.addEventListener('visibilitychange', onVisibilityChange)
window.addEventListener('resize', onWindowResize)
refreshSummary()
pollTimer = window.setInterval(() => {
if (typeof document !== 'undefined' && document.visibilityState !== 'visible') {
return
}
refreshSummary()
}, NOTIFICATION_POLL_INTERVAL_MS)
})
onUnmounted(() => {
document.removeEventListener('mousedown', onDocumentMouseDown)
document.removeEventListener('visibilitychange', onVisibilityChange)
window.removeEventListener('resize', onWindowResize)
if (pollTimer != null) {
window.clearInterval(pollTimer)
pollTimer = null
}
})
</script>
<style scoped>
.admin-notification-bell {
position: relative;
display: inline-flex;
align-items: center;
}
.bell-trigger {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
padding: 0;
border: 1px solid transparent;
border-radius: 9px;
background: transparent;
color: var(--admin-muted, #5b6f83);
cursor: pointer;
transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.bell-trigger:hover,
.bell-trigger--active {
background: #edf5fb;
border-color: #cbd9e6;
color: var(--admin-primary-strong, #2f5d8b);
}
.bell-icon {
width: 20px;
height: 20px;
}
.bell-badge {
position: absolute;
top: 1px;
right: 0;
min-width: 16px;
height: 16px;
padding: 0 4px;
box-sizing: border-box;
border-radius: 999px;
background: #d64545;
color: #ffffff;
font-size: 10px;
font-weight: 700;
line-height: 16px;
text-align: center;
pointer-events: none;
}
.bell-panel {
position: fixed;
z-index: 3200;
width: 380px;
max-width: calc(100vw - 32px);
max-height: 520px;
display: flex;
flex-direction: column;
border: 1px solid var(--admin-border, #d8e3ee);
border-radius: 12px;
background: #ffffff;
box-shadow: 0 18px 48px rgba(39, 67, 94, 0.22);
overflow: hidden;
}
.bell-panel-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 14px;
border-bottom: 1px solid #edf1f5;
}
.bell-panel-title {
color: var(--admin-text, #24384d);
font-size: 14px;
font-weight: 700;
}
.bell-read-all {
border: none;
background: transparent;
color: var(--admin-primary-strong, #2f5d8b);
font-size: 12px;
cursor: pointer;
padding: 0;
}
.bell-read-all:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.bell-empty {
padding: 36px 0;
color: #8a99a8;
font-size: 13px;
text-align: center;
}
.bell-empty--error {
color: #b23c3c;
}
.bell-list {
flex: 1;
margin: 0;
padding: 0;
list-style: none;
overflow-y: auto;
}
.bell-item {
padding: 11px 14px;
border-bottom: 1px solid #f0f3f7;
cursor: pointer;
transition: background 0.12s ease;
}
.bell-item:hover {
background: #f7f9fc;
}
/* ---- 聚合行(默认视图的一级)与明细行(展开后的二级)---- */
.bell-group {
padding: 11px 14px 8px;
border-bottom: 1px solid #f0f3f7;
cursor: pointer;
transition: background 0.12s ease;
}
.bell-group:hover {
background: #f7f9fc;
}
.bell-group--open {
background: #f4f8fd;
}
.bell-group-foot {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 6px;
}
.bell-group-count {
color: #7c8b9a;
font-size: 11px;
}
.bell-group-toggle {
color: var(--admin-primary-strong, #2f5d8b);
font-size: 11px;
font-weight: 700;
}
/* 明细缩进 + 左侧一道竖线,让"这是上面那一组里的东西"看得出来,而不是又一批同级通知 */
.bell-item--detail {
padding-left: 26px;
background: #fcfdfe;
border-bottom-color: #f5f8fb;
}
.bell-item--detail::before {
content: '';
display: inline-block;
width: 2px;
height: 26px;
margin: 0 0 -8px -14px;
border-radius: 2px;
background: #dce7f2;
}
.bell-empty--detail {
padding: 14px 0;
font-size: 12px;
}
.bell-detail-more {
padding: 8px 14px;
color: #8a99a8;
font-size: 11px;
text-align: center;
background: #fcfdfe;
}
.bell-item--unread {
background: #f2f6ff;
}
.bell-item--unread .bell-item-title::before {
content: '';
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
background: #d64545;
vertical-align: 1px;
}
.bell-item-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
}
.bell-item-title {
flex: 1;
color: var(--admin-text, #24384d);
font-size: 13px;
font-weight: 700;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.bell-severity-tag {
flex-shrink: 0;
padding: 1px 6px;
border: 1px solid currentColor;
border-radius: 999px;
font-size: 10.5px;
font-weight: 600;
line-height: 1.5;
}
.bell-severity-tag--urgent {
background: #fdecec;
color: #c92a2a;
}
.bell-severity-tag--high {
background: #fdf3e2;
color: #b26a12;
}
.bell-severity-tag--medium {
background: #eaf2fb;
color: #2f6fb3;
}
.bell-severity-tag--low {
background: #f1f4f7;
color: #6b7c8e;
}
.bell-item--error .bell-item-title {
color: #b23c3c;
}
.bell-item--warning .bell-item-title {
color: #a8793e;
}
.bell-item--info .bell-item-title {
color: var(--admin-primary-strong, #2f5d8b);
}
.bell-item-time {
flex-shrink: 0;
color: #9aa6b3;
font-size: 11px;
}
.bell-item-content {
margin-top: 4px;
color: #667085;
font-size: 12px;
line-height: 1.55;
word-break: break-all;
}
/* 定位锚点:任务号可点,直达任务详情。压低视觉权重,不与正文抢注意力。 */
.bell-ref-list { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.bell-ref-link { padding: 0; border: none; background: none; color: var(--admin-primary, #2f5d8b); font-family: inherit; font-size: 12px; line-height: 1.5; text-align: left; word-break: break-all; cursor: pointer; }
.bell-ref-link:hover { text-decoration: underline; }
.bell-ref-reason { color: #8a94a6; }
.bell-search {
display: flex;
flex-direction: column;
gap: 6px;
padding: 10px 12px;
border-bottom: 1px solid #edf1f5;
background: #fafcfe;
}
.bell-search-input,
.bell-date-input {
height: 28px;
padding: 0 8px;
box-sizing: border-box;
border: 1px solid #d8e3ee;
border-radius: 6px;
background: #ffffff;
color: var(--admin-text, #24384d);
font-size: 12px;
outline: none;
}
.bell-search-input {
width: 100%;
}
.bell-search-input:focus,
.bell-date-input:focus {
border-color: var(--admin-primary-strong, #2f5d8b);
}
.bell-search-days {
display: flex;
align-items: center;
gap: 6px;
}
.bell-date-input {
flex: 1;
min-width: 0;
}
.bell-date-sep {
color: #8a99a8;
font-size: 12px;
}
.bell-search-reset {
flex-shrink: 0;
padding: 0 8px;
height: 28px;
border: 1px solid #d8e3ee;
border-radius: 6px;
background: #ffffff;
color: var(--admin-primary-strong, #2f5d8b);
font-size: 12px;
cursor: pointer;
}
.bell-search-reset:hover {
background: #edf5fb;
}
.bell-sort-chips {
display: flex;
align-items: center;
gap: 6px;
}
.bell-sort-label {
color: #8a99a8;
font-size: 11px;
}
.bell-sort-chip {
height: 24px;
padding: 0 10px;
border: 1px solid #d8e3ee;
border-radius: 12px;
background: #ffffff;
color: #667085;
font-size: 12px;
line-height: 1;
cursor: pointer;
transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.bell-sort-chip:hover {
border-color: var(--admin-primary-strong, #2f5d8b);
color: var(--admin-primary-strong, #2f5d8b);
}
.bell-sort-chip.is-active {
border-color: var(--admin-primary-strong, #2f5d8b);
background: #edf5fb;
color: var(--admin-primary-strong, #2f5d8b);
font-weight: 700;
}
.bell-day-head {
padding: 6px 14px;
background: #f4f7fa;
color: #7c8b9a;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
}
.bell-panel-foot {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
padding: 7px 12px;
border-top: 1px solid #edf1f5;
}
.bell-page-btn {
padding: 3px 10px;
border: 1px solid #d8e3ee;
border-radius: 6px;
background: #ffffff;
color: var(--admin-primary-strong, #2f5d8b);
font-size: 12px;
cursor: pointer;
}
.bell-page-btn:hover:not(:disabled) {
background: #edf5fb;
}
.bell-page-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.bell-page-info {
color: var(--admin-text, #24384d);
font-size: 12px;
font-weight: 600;
}
.bell-page-total {
color: #9aa6b3;
font-size: 11px;
}
</style>
<style>
/* 日期选择面板与通知面板同为 body 级浮层,须压在通知面板(3200)之上:
Element 弹层只自 2000 起递增,不抬就会被面板挡住,表现为「日期点不动」。 */
.bell-date-popper {
z-index: 3300 !important;
}
</style>