task-103(任务与重复分析中心): 实现视频任务列表/卡片加载

新增 image-video-model.ts(snake 任务行解析、videos 安全地址与展平为视频卡片)
与 image-video-api.ts(GET /api/admin/image-video-tasks 加载适配)。

TDD: task-103.test.ts 8 用例先 RED 后 GREEN。
This commit is contained in:
2026-09-05 17:05:28 +08:00
parent 602c2118d3
commit 841430cd6c
3 changed files with 256 additions and 0 deletions
@@ -0,0 +1,17 @@
/** 视频任务列表加载适配(任务 103):GET /api/admin/image-video-tasks + 筛选归一与解析。 */
import { http } from '@/api/http'
import { parseImageVideoPage, type ImageVideoPageResult } from './image-video-model.ts'
import { toImageVideoQuery, type ImageVideoFilter } from './image-video-filter.ts'
export const IMAGE_VIDEO_TASKS_ENDPOINT = '/api/admin/image-video-tasks'
export async function fetchImageVideoTasks(
filter: ImageVideoFilter,
page: number,
pageSize: number,
): Promise<ImageVideoPageResult> {
const { data } = await http.get<unknown>(IMAGE_VIDEO_TASKS_ENDPOINT, {
params: toImageVideoQuery(filter, page, pageSize),
})
return parseImageVideoPage(data)
}
@@ -0,0 +1,141 @@
/** 视频任务列表/卡片模型(任务 103/104):解析 /api/admin/image-video-tasks snake 行并展平为视频卡片,纯逻辑。 */
import { unwrap } from '../../api/envelope.ts'
import { IMAGE_VIDEO_DEFAULT_PAGE_SIZE } from './image-video-filter.ts'
import { normalizeTaskStatus, type TaskId, type TaskStatus } from './task-model.ts'
export const IMAGE_VIDEO_PAGE_SIZE_DEFAULT = IMAGE_VIDEO_DEFAULT_PAGE_SIZE
export interface ImageVideoVideo {
sourceUrl: string
archivedUrl: string
displayUrl: string
objectKey: string
archiveStatus: string
archiveError: string
}
export interface ImageVideoRow {
taskId: TaskId
userId?: number | null
username: string
groupName: string
status: TaskStatus
cozeStatus: string
cozeExecuteId: string
videos: ImageVideoVideo[]
videoUrl: string
debugUrl: string
archiveStatus: string
archiveError: string
submittedAt?: string
completedAt?: string
}
export interface ImageVideoCard {
key: string
task: ImageVideoRow
video: ImageVideoVideo | null
videoIndex: number
}
export interface ImageVideoPageResult {
items: ImageVideoRow[]
total: number
page: number
pageSize: number
}
export function emptyImageVideoPage(): ImageVideoPageResult {
return { items: [], total: 0, page: 1, pageSize: IMAGE_VIDEO_PAGE_SIZE_DEFAULT }
}
function text(value: unknown): string {
return typeof value === 'string' ? value.trim() : ''
}
function numberOrNull(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
}
/** 安全管理端地址:仅 http(s) 视为可访问。 */
export function safeAdminUrl(value: unknown): string {
const url = text(value)
return /^https?:\/\//i.test(url) ? url : ''
}
/** 解析单条视频对象(display_url 等)。 */
export function toImageVideoVideo(raw: unknown): ImageVideoVideo | null {
if (!raw || typeof raw !== 'object') return null
const r = raw as Record<string, unknown>
return {
sourceUrl: safeAdminUrl(r.source_url ?? r.sourceUrl),
archivedUrl: safeAdminUrl(r.archived_url ?? r.archivedUrl),
displayUrl: safeAdminUrl(r.display_url ?? r.displayUrl),
objectKey: text(r.object_key ?? r.objectKey),
archiveStatus: text(r.archive_status ?? r.archiveStatus),
archiveError: text(r.archive_error ?? r.archiveError),
}
}
/** 解析单条任务行;缺 task_id 视为无效。 */
export function toImageVideoRow(raw: unknown): ImageVideoRow | null {
if (!raw || typeof raw !== 'object') return null
const r = raw as Record<string, unknown>
const taskId = numberOrNull(r.task_id ?? r.taskId)
if (taskId === null || taskId < 1) return null
const task: ImageVideoRow = {
taskId: String(taskId) as TaskId,
username: text(r.username),
groupName: text(r.group_name ?? r.groupName),
status: normalizeTaskStatus(r.status),
cozeStatus: text(r.coze_status ?? r.cozeStatus),
cozeExecuteId: text(r.coze_execute_id ?? r.cozeExecuteId),
videos: Array.isArray(r.videos)
? r.videos.map((v) => toImageVideoVideo(v)).filter((v): v is ImageVideoVideo => v !== null)
: [],
videoUrl: safeAdminUrl(r.video_url ?? r.videoUrl),
debugUrl: safeAdminUrl(r.debug_url ?? r.debugUrl),
archiveStatus: text(r.archive_status ?? r.archiveStatus),
archiveError: text(r.archive_error ?? r.archiveError),
}
const userId = numberOrNull(r.user_id ?? r.userId)
if (userId !== null && userId >= 1) task.userId = userId
const submittedAt = text(r.submitted_at ?? r.submittedAt)
if (submittedAt) task.submittedAt = submittedAt
const completedAt = text(r.completed_at ?? r.completedAt)
if (completedAt) task.completedAt = completedAt
return task
}
/** 把任务行展平为“任务:视频下标”卡片;无 videos 的任务展为一张空卡。 */
export function flattenImageVideoTasks(items: readonly ImageVideoRow[]): ImageVideoCard[] {
const cards: ImageVideoCard[] = []
for (const task of items) {
if (!task.videos.length) {
cards.push({ key: `${task.taskId}:empty`, task, video: null, videoIndex: 0 })
continue
}
task.videos.forEach((video, index) => {
cards.push({ key: `${task.taskId}:${index}`, task, video, videoIndex: index })
})
}
return cards
}
/** 归一化视频任务分页负载为前端结果;缺省字段回默认。 */
export function parseImageVideoPage(payload: unknown): ImageVideoPageResult {
const out = emptyImageVideoPage()
const core = unwrap<unknown>(payload)
if (!core || typeof core !== 'object') return out
const record = core as Record<string, unknown>
if (Array.isArray(record.items)) {
out.items = record.items
.map((raw) => toImageVideoRow(raw))
.filter((item): item is ImageVideoRow => item !== null)
}
if (typeof record.total === 'number') out.total = Math.floor(record.total)
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
const rawSize = record.page_size ?? record.pageSize
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
return out
}