192 lines
5.7 KiB
Vue
192 lines
5.7 KiB
Vue
<script setup lang="ts">
|
|
import Hls from "hls.js";
|
|
|
|
interface Episode {
|
|
cid: number;
|
|
idx: number;
|
|
title: string;
|
|
playable: boolean;
|
|
media?: string;
|
|
locked_reason?: string;
|
|
is_preview?: boolean;
|
|
}
|
|
|
|
const props = defineProps<{
|
|
token: string;
|
|
device: object | null;
|
|
dramaId: string | number;
|
|
apiBase?: string;
|
|
}>();
|
|
const apiBase = computed(() => props.apiBase || "/api");
|
|
|
|
const episodes = ref<Episode[]>([]);
|
|
const current = ref<number | null>(null);
|
|
const loading = ref(false);
|
|
const error = ref("");
|
|
const videoRef = ref<HTMLVideoElement | null>(null);
|
|
let hls: Hls | null = null;
|
|
|
|
const currentTitle = computed(() => episodes.value.find((item) => item.cid === current.value)?.title || "");
|
|
|
|
async function load() {
|
|
loading.value = true;
|
|
error.value = "";
|
|
try {
|
|
const response = await fetch(`${apiBase.value}/play/episodes`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ token: props.token, device: props.device, vid: props.dramaId }),
|
|
});
|
|
const payload = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
error.value = typeof payload.detail === "string" ? payload.detail : "没有读到分集";
|
|
return;
|
|
}
|
|
episodes.value = Array.isArray(payload.episodes) ? payload.episodes : [];
|
|
const first = episodes.value.find((item) => item.playable);
|
|
if (!first) {
|
|
error.value = episodes.value.find((item) => item.locked_reason)?.locked_reason || "没有可播放的集";
|
|
return;
|
|
}
|
|
select(first);
|
|
} catch (exc) {
|
|
error.value = exc instanceof Error ? exc.message : "没有读到分集";
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
function select(episode: Episode) {
|
|
if (!episode.playable || episode.cid === current.value) return;
|
|
current.value = episode.cid;
|
|
error.value = "";
|
|
void nextTick(() => attach(episode.cid));
|
|
}
|
|
|
|
function attach(cid: number) {
|
|
const video = videoRef.value;
|
|
if (!video) return;
|
|
stop();
|
|
const episode = episodes.value.find((item) => item.cid === cid);
|
|
if (episode?.media === "file") {
|
|
video.src = `${apiBase.value}/play/${props.dramaId}/${cid}/file`;
|
|
video.play().catch(() => undefined);
|
|
return;
|
|
}
|
|
const url = `${apiBase.value}/play/${props.dramaId}/${cid}/index.m3u8`;
|
|
if (Hls.isSupported()) {
|
|
const player = new Hls();
|
|
hls = player;
|
|
player.loadSource(url);
|
|
player.attachMedia(video);
|
|
player.on(Hls.Events.MANIFEST_PARSED, () => {
|
|
video.play().catch(() => undefined);
|
|
});
|
|
player.on(Hls.Events.ERROR, (_event, data) => {
|
|
if (data.fatal) error.value = "这一集播放失败";
|
|
});
|
|
return;
|
|
}
|
|
video.src = url;
|
|
video.play().catch(() => undefined);
|
|
}
|
|
|
|
function stop() {
|
|
hls?.destroy();
|
|
hls = null;
|
|
if (videoRef.value) {
|
|
videoRef.value.removeAttribute("src");
|
|
videoRef.value.load();
|
|
}
|
|
}
|
|
|
|
onMounted(load);
|
|
onUnmounted(stop);
|
|
</script>
|
|
|
|
<template>
|
|
<div class="play-page">
|
|
<div class="play-stage">
|
|
<div class="play-video-wrap">
|
|
<video ref="videoRef" class="play-video" controls playsinline></video>
|
|
<div v-if="loading || error" class="play-status">{{ loading ? "正在读取分集" : error }}</div>
|
|
</div>
|
|
<div v-if="currentTitle" class="play-now">{{ currentTitle }}</div>
|
|
</div>
|
|
<aside class="play-side">
|
|
<div class="play-side-title">选集</div>
|
|
<div class="play-episodes">
|
|
<button
|
|
v-for="episode in episodes"
|
|
:key="episode.cid"
|
|
type="button"
|
|
class="play-episode"
|
|
:class="{ current: episode.cid === current }"
|
|
:disabled="!episode.playable"
|
|
@click="select(episode)"
|
|
>
|
|
<span class="play-episode-title">{{ episode.title }}</span>
|
|
<span v-if="!episode.playable" class="play-episode-lock">{{ episode.locked_reason || "无地址" }}</span>
|
|
<span v-else-if="episode.is_preview" class="play-episode-lock">试看</span>
|
|
</button>
|
|
<div v-if="!loading && episodes.length === 0" class="play-empty">没有分集</div>
|
|
</div>
|
|
</aside>
|
|
</div>
|
|
</template>
|
|
|
|
<style>
|
|
.play-page { height: 100%; min-height: 0; display: flex; overflow: hidden; background: #101014; }
|
|
.play-stage { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
|
|
.play-video-wrap { position: relative; flex: 1; min-height: 0; background: #000; }
|
|
.play-video { display: block; width: 100%; height: 100%; background: #000; }
|
|
.play-status {
|
|
position: absolute;
|
|
left: 16px;
|
|
right: 16px;
|
|
bottom: 56px;
|
|
color: rgba(255, 255, 255, 0.8);
|
|
text-align: center;
|
|
}
|
|
.play-now {
|
|
flex: none;
|
|
padding: 8px 16px;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
color: rgba(255, 255, 255, 0.72);
|
|
}
|
|
.play-side {
|
|
width: 280px;
|
|
flex: none;
|
|
min-height: 0;
|
|
display: flex;
|
|
flex-direction: column;
|
|
border-left: 1px solid rgba(255, 255, 255, 0.08);
|
|
}
|
|
.play-side-title { flex: none; padding: 12px 16px 8px; font-weight: 600; }
|
|
.play-episodes { flex: 1; min-height: 0; overflow: auto; padding: 0 8px 12px; }
|
|
.play-episode {
|
|
width: 100%;
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
gap: 4px 8px;
|
|
min-height: 36px;
|
|
margin: 0 0 4px;
|
|
padding: 6px 10px;
|
|
border: 0;
|
|
border-radius: 6px;
|
|
background: transparent;
|
|
color: inherit;
|
|
font: inherit;
|
|
text-align: left;
|
|
cursor: pointer;
|
|
}
|
|
.play-episode.current { background: rgba(99, 226, 183, 0.16); }
|
|
.play-episode:disabled { color: rgba(255, 255, 255, 0.35); cursor: default; }
|
|
.play-episode-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
.play-episode-lock { flex: 1 1 100%; font-size: 12px; line-height: 1.3; white-space: normal; }
|
|
.play-empty { padding: 8px 10px; color: rgba(255, 255, 255, 0.45); }
|
|
</style>
|