Files
crawler-plugin/admin-frontend-vue/src/api/notifications.ts
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

161 lines
5.2 KiB
TypeScript

import { http } from './http'
import { unwrap } from './envelope'
/** 业务严重度:紧急 / 高危 / 中危 / 低危(后端按 scene + level 推导)。 */
export type AdminNotificationSeverity = 'urgent' | 'high' | 'medium' | 'low'
export type AdminNotificationSortMode = 'severity' | 'time'
export const ADMIN_NOTIFICATION_SEVERITY_LABELS: Readonly<Record<AdminNotificationSeverity, string>> = {
urgent: '紧急',
high: '高危',
medium: '中危',
low: '低危',
}
/** 通知里的结构化定位锚点(当前只有 task_failed 使用:桶内每个失败任务一项)。 */
export interface AdminNotificationRef {
taskId: number | null
/** 任务号;品牌任务为 #<id>。任务详情接口按它查。 */
taskNo: string
moduleType?: string | null
reason?: string | null
updatedAt?: string | null
deviceId?: string | null
}
/** 单条通知(后台铃铛)。 */
export interface AdminNotificationItem {
id: number
scene: string
level: string
/** 业务严重度;旧后端不返回时前端按 level 兜底。 */
severity?: string
title: string
content: string
/** 可点的定位锚点;旧后端/历史行没有这一列时为空。 */
refs?: AdminNotificationRef[] | null
read: boolean
readAt: string | null
createdAt: string | null
}
export interface AdminNotificationPage {
items: AdminNotificationItem[]
total: number
page: number
pageSize: number
unreadCount: number
}
export interface AdminNotificationSummary {
unreadCount: number
/** 有未读的聚合组数;徽标用它。unreadCount 仍是"条数",给老明细视图与对账用。 */
unreadGroupCount?: number
latestId: number
}
/** 聚合组:一个「场景 × 自然日 × 事件主体用户」折成的一行。 */
export interface AdminNotificationGroup {
latestId: number
scene: string
category: string
day: string
subjectUserId: number | null
rowCount: number
unreadCount: number
level: string
/** 组内最高严重度。 */
severity?: string
title: string
content: string
createdAt: string | null
}
export interface AdminNotificationGroupPage {
groups: AdminNotificationGroup[]
/** 组数(分页分母)。 */
total: number
/** 折叠前的明细条数——必须与 total 同屏,否则「共 12 条」会被读成只有 12 条通知。 */
rowCount: number
page: number
pageSize: number
unreadGroupCount: number
unreadCount: number
}
/** 铃铛轮询:未读数 + 最新通知 id。 */
export async function fetchNotificationSummary(): Promise<AdminNotificationSummary> {
const { data } = await http.get('/api/admin/notifications/summary')
return unwrap<AdminNotificationSummary>(data)
}
/** 列表查询参数:关键字匹配标题/内容;startDate/endDate 为年月日闭区间(yyyy-MM-dd)。 */
export interface AdminNotificationListParams {
page?: number
pageSize?: number
onlyUnread?: boolean
keyword?: string
startDate?: string
endDate?: string
/** 排序:severity(默认)或 time;默认值不下发。 */
sort?: AdminNotificationSortMode
/** 展开聚合组:三个条件取自该组自己返回的值,原样回传。 */
scene?: string
day?: string
subjectUserId?: number | null
/** true 表示展开「未区分用户」那一组(subject_user_id 为空的存量行)。 */
subjectUnlabelled?: boolean
}
/** 聚合列表(铃铛默认视图)。分页按组切。 */
export async function fetchNotificationGroups(
params: AdminNotificationListParams = {},
): Promise<AdminNotificationGroupPage> {
const keyword = (params.keyword ?? '').trim()
const { data } = await http.get('/api/admin/notifications/groups', {
params: {
page: params.page ?? 1,
pageSize: params.pageSize ?? 20,
onlyUnread: params.onlyUnread ? 'true' : 'false',
keyword: keyword || undefined,
startDate: params.startDate || undefined,
endDate: params.endDate || undefined,
sort: params.sort === 'time' ? 'time' : undefined,
},
})
return unwrap<AdminNotificationGroupPage>(data)
}
export async function fetchNotificationList(
params: AdminNotificationListParams = {},
): Promise<AdminNotificationPage> {
const keyword = (params.keyword ?? '').trim()
const { data } = await http.get('/api/admin/notifications', {
params: {
page: params.page ?? 1,
pageSize: params.pageSize ?? 20,
onlyUnread: params.onlyUnread ? 'true' : 'false',
keyword: keyword || undefined,
startDate: params.startDate || undefined,
endDate: params.endDate || undefined,
sort: params.sort === 'time' ? 'time' : undefined,
scene: params.scene || undefined,
day: params.day || undefined,
subjectUserId: params.subjectUserId ?? undefined,
subjectUnlabelled: params.subjectUnlabelled ? 'true' : undefined,
},
})
return unwrap<AdminNotificationPage>(data)
}
export async function markNotificationRead(id: number): Promise<boolean> {
const { data } = await http.post(`/api/admin/notifications/${id}/read`)
return unwrap<boolean>(data)
}
export async function markAllNotificationsRead(): Promise<number> {
const { data } = await http.post('/api/admin/notifications/read-all')
return unwrap<number>(data)
}