@@ -0,0 +1,309 @@
|
||||
"use client";
|
||||
|
||||
import { toast } from "@heroui/react";
|
||||
import { useEffect, useMemo, useState, type ReactNode } from "react";
|
||||
|
||||
import { useVideoContext } from "@/components/analytics/use-playback-analytics";
|
||||
import { openLoginDialog } from "@/components/auth/login-dialog";
|
||||
import { AdBanners, AdIcons } from "@/components/common/ads";
|
||||
import { CommentPanel, videoCommentTarget } from "@/components/common/comments";
|
||||
import { DecryptedImage } from "@/components/common/decrypted-image";
|
||||
import { ActionPill } from "@/components/drama-play/drama-play-page";
|
||||
import { PcPlayer } from "@/components/drama-play/pc-player";
|
||||
import { FloatingActions } from "@/components/home/floating-actions";
|
||||
import { PageBreadcrumb } from "@/components/seo/page-breadcrumb";
|
||||
import { SeoBreadcrumb } from "@/components/seo/seo-breadcrumb";
|
||||
import type { ShortDramaItem } from "@/components/short-drama/data";
|
||||
import { favoriteVideo, getVideoInfo, likeVideo } from "@/lib/api/video-actions";
|
||||
import { handleUnauthorized } from "@/lib/auth/handle-unauthorized";
|
||||
import { ensureGuestSession, getAuthToken } from "@/lib/auth/session";
|
||||
import { IMG_PLACEHOLDER } from "@/lib/image/decrypt";
|
||||
|
||||
import type { VideoDetail } from "./data";
|
||||
import { H5VideoPlay } from "./h5-video-play";
|
||||
import { VideoRelatedAside } from "./related-aside";
|
||||
import { useReportVideoHistory } from "./use-report-video-history";
|
||||
|
||||
async function ensureVideoAuthToken() {
|
||||
let token = getAuthToken();
|
||||
if (!token) {
|
||||
try {
|
||||
await ensureGuestSession();
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
token = getAuthToken();
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
function resolveVideoLikeFavorite(data: Awaited<ReturnType<typeof getVideoInfo>> | null | undefined) {
|
||||
const info = data?.videoInfo;
|
||||
return {
|
||||
isLike: Boolean(data?.isLike ?? info?.isLike),
|
||||
isFavorite: Boolean(data?.isFavorite ?? info?.isFavorite),
|
||||
likeNum: Number(info?.likeNum) || 0,
|
||||
countCollect: Number(info?.countCollect) || 0,
|
||||
};
|
||||
}
|
||||
|
||||
export function VideoPlayPage({
|
||||
detail,
|
||||
src,
|
||||
related = [],
|
||||
relatedAside,
|
||||
promo,
|
||||
adIcons,
|
||||
adGrid,
|
||||
bannerAds,
|
||||
}: {
|
||||
detail: VideoDetail;
|
||||
src: string;
|
||||
related?: ShortDramaItem[];
|
||||
relatedAside?: ReactNode;
|
||||
promo?: ReactNode;
|
||||
adIcons?: ReactNode;
|
||||
adGrid?: ReactNode;
|
||||
bannerAds?: ReactNode;
|
||||
}) {
|
||||
const [liked, setLiked] = useState(Boolean(detail.isLike));
|
||||
const [likeNum, setLikeNum] = useState(Number(detail.likeNum) || 0);
|
||||
const [liking, setLiking] = useState(false);
|
||||
const [collected, setCollected] = useState(Boolean(detail.isFavorite));
|
||||
const [countCollect, setCountCollect] = useState(Number(detail.countCollect) || 0);
|
||||
const [collecting, setCollecting] = useState(false);
|
||||
const reportWatchHistory = useReportVideoHistory(Number(detail.id));
|
||||
const tagName = (detail.tags?.[0]?.tagName || detail.tags?.[0]?.name || "").trim();
|
||||
const videoTrackContext = useMemo(
|
||||
() => ({
|
||||
media_id: String(detail.id || ""),
|
||||
video_id: String(detail.id || ""),
|
||||
video_title: detail.title || "未命名视频",
|
||||
video_type_id: "0",
|
||||
video_type_name: "未分类",
|
||||
video_content_type: "video",
|
||||
recommend_trace_id: "",
|
||||
video_tag_key: tagName ? "tag" : "default",
|
||||
video_tag_name: tagName || "默认标签",
|
||||
video_duration: Math.max(0, Math.round(Number(detail.video?.videoTime) || 0)),
|
||||
}),
|
||||
[detail.id, detail.title, detail.video?.videoTime, tagName],
|
||||
);
|
||||
useVideoContext(videoTrackContext);
|
||||
|
||||
const likeFavoriteKey = `${detail.id}:${detail.isLike}:${detail.likeNum}:${detail.isFavorite}:${detail.countCollect}`;
|
||||
const [likeFavoriteSync, setLikeFavoriteSync] = useState(likeFavoriteKey);
|
||||
if (likeFavoriteSync !== likeFavoriteKey) {
|
||||
setLikeFavoriteSync(likeFavoriteKey);
|
||||
setLiked(Boolean(detail.isLike));
|
||||
setLikeNum(Number(detail.likeNum) || 0);
|
||||
setCollected(Boolean(detail.isFavorite));
|
||||
setCountCollect(Number(detail.countCollect) || 0);
|
||||
}
|
||||
|
||||
// SSR 详情走 "use cache" 无登录态;客户端带 token 再拉一次,纠正已点赞 / 已收藏
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const token = await ensureVideoAuthToken();
|
||||
if (!token || cancelled) return;
|
||||
try {
|
||||
const data = await getVideoInfo(
|
||||
detail.slug?.trim() ? { slug: detail.slug.trim() } : { id: detail.id },
|
||||
token,
|
||||
);
|
||||
if (cancelled) return;
|
||||
const status = resolveVideoLikeFavorite(data);
|
||||
setLiked(status.isLike);
|
||||
setCollected(status.isFavorite);
|
||||
setLikeNum(status.likeNum);
|
||||
setCountCollect(status.countCollect);
|
||||
} catch (error) {
|
||||
console.warn("[video-play/status]", error);
|
||||
}
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [detail.id, detail.slug]);
|
||||
|
||||
const share = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(window.location.href);
|
||||
toast.success("链接已复制");
|
||||
} catch {
|
||||
toast.danger("复制失败,请稍后重试");
|
||||
}
|
||||
};
|
||||
|
||||
const toggleLike = async () => {
|
||||
if (liking) return;
|
||||
const videoId = Number(detail.id);
|
||||
if (!Number.isInteger(videoId) || videoId < 1) return;
|
||||
|
||||
const token = await ensureVideoAuthToken();
|
||||
if (!token) {
|
||||
openLoginDialog();
|
||||
return;
|
||||
}
|
||||
|
||||
const next = !liked;
|
||||
setLiked(next);
|
||||
setLikeNum((value) => Math.max(0, value + (next ? 1 : -1)));
|
||||
setLiking(true);
|
||||
try {
|
||||
const result = await likeVideo({ id: videoId, isLike: next }, token);
|
||||
setLiked(Boolean(result?.isLike ?? next));
|
||||
if (typeof result?.likeNum === "number") setLikeNum(result.likeNum);
|
||||
} catch (error) {
|
||||
setLiked(!next);
|
||||
setLikeNum((value) => Math.max(0, value + (next ? -1 : 1)));
|
||||
handleUnauthorized(error);
|
||||
} finally {
|
||||
setLiking(false);
|
||||
}
|
||||
};
|
||||
|
||||
const toggleCollect = async () => {
|
||||
if (collecting) return;
|
||||
const videoId = Number(detail.id);
|
||||
if (!Number.isInteger(videoId) || videoId < 1) return;
|
||||
|
||||
const token = await ensureVideoAuthToken();
|
||||
if (!token) {
|
||||
openLoginDialog();
|
||||
return;
|
||||
}
|
||||
|
||||
const next = !collected;
|
||||
setCollected(next);
|
||||
setCountCollect((value) => Math.max(0, value + (next ? 1 : -1)));
|
||||
setCollecting(true);
|
||||
try {
|
||||
const result = await favoriteVideo({ id: videoId, isFavorite: next }, token);
|
||||
setCollected(Boolean(result?.isFavorite ?? next));
|
||||
if (typeof result?.countCollect === "number") {
|
||||
setCountCollect(result.countCollect);
|
||||
} else {
|
||||
// 响应无 countCollect 时再拉详情同步
|
||||
try {
|
||||
const data = await getVideoInfo(
|
||||
detail.slug?.trim() ? { slug: detail.slug.trim() } : { id: detail.id },
|
||||
token,
|
||||
);
|
||||
const num = Number(data?.videoInfo?.countCollect);
|
||||
if (Number.isFinite(num) && num >= 0) setCountCollect(num);
|
||||
} catch {
|
||||
/* 保留乐观值 */
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
setCollected(!next);
|
||||
setCountCollect((value) => Math.max(0, value + (next ? -1 : 1)));
|
||||
handleUnauthorized(error);
|
||||
} finally {
|
||||
setCollecting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<main data-component="video-play-page" className="relative flex-1 bg-[#0B0709]">
|
||||
<div
|
||||
aria-hidden="true"
|
||||
className="pointer-events-none absolute inset-x-0 top-0 hidden h-[1200px] overflow-hidden [mask-image:linear-gradient(180deg,#000_70%,transparent_100%)] lg:block"
|
||||
>
|
||||
<DecryptedImage
|
||||
src={detail.coverImg}
|
||||
fallback={IMG_PLACEHOLDER}
|
||||
alt=""
|
||||
className="size-full scale-110 object-cover opacity-70 blur-[12px]"
|
||||
/>
|
||||
<div className="absolute inset-0 bg-[linear-gradient(180deg,rgba(9,5,9,0.32)_0%,rgba(9,5,9,0.56)_55%,rgba(6,5,9,0.94)_100%)]" />
|
||||
</div>
|
||||
|
||||
<div
|
||||
data-pc-scale
|
||||
className="relative mx-auto hidden w-[1700px] flex-col gap-6 pt-6 pb-8 [zoom:var(--page-scale,1)] lg:flex"
|
||||
>
|
||||
<SeoBreadcrumb
|
||||
items={[
|
||||
{ name: "首页", href: "/" },
|
||||
{ name: detail.title || "视频详情" },
|
||||
]}
|
||||
/>
|
||||
<PageBreadcrumb
|
||||
items={[
|
||||
{ name: "首页", href: "/" },
|
||||
{ name: detail.title || "视频详情" },
|
||||
]}
|
||||
/>
|
||||
|
||||
<div className="flex flex-col gap-4">
|
||||
<PcPlayer
|
||||
src={src}
|
||||
poster={detail.coverImg || ""}
|
||||
title={detail.title || ""}
|
||||
locked={false}
|
||||
dramaTitle={detail.title || ""}
|
||||
layout="wide"
|
||||
onPlaySuccess={reportWatchHistory}
|
||||
analyticsKind="video"
|
||||
/>
|
||||
|
||||
{adIcons ?? <AdIcons variant="pc" labels={false} />}
|
||||
|
||||
<section className="flex h-[140px] items-center justify-between gap-10 rounded-xl bg-[rgba(19,15,22,0.9)] px-6 py-5 outline outline-1 -outline-offset-1 outline-[rgba(199,145,89,0.7)]">
|
||||
<div className="flex min-w-0 flex-col gap-1.5">
|
||||
<p className="truncate text-[28px] leading-[34px] font-semibold text-white">{detail.title}</p>
|
||||
<p className="line-clamp-2 text-xs leading-[18px] text-[#9991AB]">{detail.desc}</p>
|
||||
</div>
|
||||
<div className="flex shrink-0 gap-3">
|
||||
<ActionPill
|
||||
icon="/images/icon_like.svg"
|
||||
label={liked ? "已点赞" : "点赞"}
|
||||
active={liked}
|
||||
onClick={liking ? undefined : toggleLike}
|
||||
/>
|
||||
<ActionPill
|
||||
icon="/images/icon_collect.svg"
|
||||
label={collected ? "已收藏" : "收藏"}
|
||||
active={collected}
|
||||
onClick={collecting ? undefined : toggleCollect}
|
||||
/>
|
||||
<ActionPill icon="/images/icon_share.svg" label="分享" onClick={share} />
|
||||
<ActionPill icon="/images/icon_feedback.svg" label="反馈" href="/feedback" />
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div className="flex items-start gap-6">
|
||||
<CommentPanel variant="play" target={videoCommentTarget(detail.id)} />
|
||||
{relatedAside ?? <VideoRelatedAside items={related} />}
|
||||
</div>
|
||||
|
||||
{promo ?? <AdBanners variant="pc" />}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<H5VideoPlay
|
||||
detail={detail}
|
||||
src={src}
|
||||
related={related}
|
||||
liked={liked}
|
||||
likeNum={likeNum}
|
||||
collected={collected}
|
||||
countCollect={countCollect}
|
||||
onLike={liking ? () => undefined : toggleLike}
|
||||
onCollect={collecting ? () => undefined : toggleCollect}
|
||||
onShare={share}
|
||||
commentTarget={videoCommentTarget(detail.id)}
|
||||
onPlaySuccess={reportWatchHistory}
|
||||
adGrid={adGrid}
|
||||
bannerAds={bannerAds}
|
||||
/>
|
||||
|
||||
<div className="max-lg:hidden">
|
||||
<FloatingActions />
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user