Files
web_hghl_h5/src/components/video/data.ts
T
2026-10-28 17:07:32 +07:00

72 lines
2.0 KiB
TypeScript

import { pickVideoCardTags } from "@/components/common/card-tags";
import { sorts, type ShortDramaItem, type Sort } from "@/components/short-drama/data";
import { formatHeat } from "@/components/ranking/data";
import type { VideoCategoryListItem, VideoListItem, VideoListResp } from "@/lib/api/types";
export const VIDEO_PAGE_SIZE = 30;
export const videoSorts = sorts;
export type VideoSort = Sort;
export function videoSortType(sort: VideoSort): 1 | 2 {
return sort === "latest" ? 2 : 1;
}
export function mapVideoListItem(item: VideoListItem): ShortDramaItem | null {
// 信息流广告(type=2)不展示:网页只保留后台 5 个站点广告位
if (item.type === 2) return null;
if (!item.id) return null;
const slug = (item.slug || "").trim();
return {
kind: "drama",
id: String(item.id),
slug: slug || undefined,
title: (item.title || "").trim() || "未命名",
subtitle: "",
tags: pickVideoCardTags(item),
heat: formatHeat(item.watchNum),
episodes: "",
cover: (item.coverImg || "").trim(),
date: item.addedTime || undefined,
};
}
export type VideoListResult = {
items: ShortDramaItem[];
page: number;
pageSize: number;
totalCount: number;
totalPages: number;
sort: VideoSort;
categories: VideoCategoryListItem[];
categorySlug?: string;
};
export function buildVideoList(
data: VideoListResp | null | undefined,
sort: VideoSort,
fallbackPage = 1,
fallbackPageSize = VIDEO_PAGE_SIZE,
options?: { categories?: VideoCategoryListItem[]; categorySlug?: string },
): VideoListResult {
const page = data?.pageNum || fallbackPage;
const pageSize = data?.pageSize || fallbackPageSize;
const totalCount = data?.totalCount || 0;
const items = (data?.list ?? [])
.map(mapVideoListItem)
.filter((item): item is ShortDramaItem => Boolean(item));
return {
items,
page,
pageSize,
totalCount,
totalPages: Math.max(1, Math.ceil(totalCount / pageSize) || 1),
sort,
categories: options?.categories ?? [],
categorySlug: options?.categorySlug,
};
}