feat: 接入黄豆短剧独立清单与下载

This commit is contained in:
Nixevol
2026-10-21 22:54:56 +08:00
parent e31e80384c
commit 5b4a948e5f
25 changed files with 2093 additions and 132 deletions
+97 -25
View File
@@ -1,8 +1,12 @@
<script setup lang="ts">
interface Drama {
id: number;
id: string | number;
title: string;
image: string;
payType?: string;
corner?: string;
locked?: boolean;
limitedFree?: boolean;
}
interface Section {
@@ -16,11 +20,14 @@ interface Section {
const props = defineProps<{
token: string;
device: object | null;
apiBase?: string;
remoteSearch?: boolean;
}>();
const apiBase = computed(() => props.apiBase || "/api");
const sections = ref<Section[]>([]);
const expanded = ref<string[]>([]);
const selected = ref<Set<number>>(new Set());
const selected = ref<Set<string>>(new Set());
const busy = ref(false);
const error = ref("");
const query = ref("");
@@ -40,7 +47,7 @@ const scrollEl = ref<HTMLElement | null>(null);
function coverOf(image: string) {
if (!image) return "";
if (image.startsWith("/api/covers/")) return image;
if (image.startsWith("/api/")) return image;
if (image.startsWith("http")) return image;
return `https://apis.raptdrama.com${image.startsWith("/") ? image : `/${image}`}`;
}
@@ -56,7 +63,7 @@ function toggleExpanded(key: string) {
}
function selectedCount(section: Section) {
return section.items.filter((item) => selected.value.has(item.id)).length;
return section.items.filter((item) => selected.value.has(dramaKey(item.id))).length;
}
function allChecked(section: Section) {
@@ -68,18 +75,24 @@ function someChecked(section: Section) {
return count > 0 && count < section.items.length;
}
function toggleDrama(id: number, checked: boolean) {
function dramaKey(id: string | number) {
return String(id);
}
function toggleDrama(id: string | number, checked: boolean) {
const next = new Set(selected.value);
if (checked) next.add(id);
else next.delete(id);
const key = dramaKey(id);
if (checked) next.add(key);
else next.delete(key);
selected.value = next;
}
function toggleSection(section: Section, checked: boolean) {
const next = new Set(selected.value);
for (const item of section.items) {
if (checked) next.add(item.id);
else next.delete(item.id);
const key = dramaKey(item.id);
if (checked) next.add(key);
else next.delete(key);
}
selected.value = next;
}
@@ -89,7 +102,7 @@ function clearSelection() {
}
function pruneSelection() {
const ids = new Set(sections.value.flatMap((section) => section.items.map((item) => item.id)));
const ids = new Set(sections.value.flatMap((section) => section.items.map((item) => dramaKey(item.id))));
selected.value = new Set([...selected.value].filter((id) => ids.has(id)));
}
@@ -122,7 +135,7 @@ async function reload(refresh = false) {
error.value = "";
busy.value = true;
try {
const response = await fetch("/api/catalog", {
const response = await fetch(`${apiBase.value}/catalog`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: props.token, device: props.device, refresh }),
@@ -173,13 +186,13 @@ async function reload(refresh = false) {
}
}
function cardKey(sectionKey: string, id: number) {
return `${sectionKey}-${id}`;
function cardKey(sectionKey: string, id: string | number) {
return `${sectionKey}-${dramaKey(id)}`;
}
function matches() {
const keyword = query.value.trim().toLowerCase();
const found: { sectionKey: string; id: number }[] = [];
const found: { sectionKey: string; id: string | number }[] = [];
if (!keyword) return found;
for (const section of sections.value) {
for (const item of section.items) {
@@ -250,7 +263,12 @@ async function find(keyword = query.value) {
searchHint.value = "请输入名称";
return;
}
const found = matches();
let found = matches();
if (!found.length && props.remoteSearch) {
await searchRemote(text, generation);
if (generation !== revealGeneration) return;
found = matches();
}
if (!found.length) {
searchHint.value = "没有找到";
flashKey.value = "";
@@ -289,8 +307,44 @@ onMounted(() => {
});
defineExpose({ reload, busy, find, searchHint, clearSearch, loadDirectory });
async function searchRemote(keyword: string, generation: number) {
searchHint.value = "正在搜索";
try {
const response = await fetch(`${apiBase.value}/search`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: props.token, device: props.device, keyword }),
});
const payload = await response.json().catch(() => ({}));
if (generation !== revealGeneration) return;
if (!response.ok) {
searchHint.value = typeof payload.detail === "string" ? payload.detail : "搜索失败";
return;
}
const items = Array.isArray(payload.items) ? payload.items : [];
const key = "search";
const existing = sections.value.find((section) => section.key === key);
if (existing) {
existing.items = items;
existing.loading = false;
existing.note = items.length ? "" : "没有搜索结果";
} else {
sections.value.unshift({
key,
name: "搜索结果",
note: items.length ? "" : "没有搜索结果",
items,
loading: false,
});
}
if (!isExpanded(key)) expanded.value = [...expanded.value, key];
} catch {
if (generation === revealGeneration) searchHint.value = "搜索失败";
}
}
async function loadDirectory() {
const response = await fetch("/api/downloads");
const response = await fetch(`${apiBase.value}/downloads`);
if (!response.ok) return;
const payload = await response.json();
directory.value = payload.directory || "";
@@ -298,7 +352,7 @@ async function loadDirectory() {
async function chooseDirectory() {
footError.value = "";
const response = await fetch("/api/downloads/pick", { method: "POST" });
const response = await fetch(`${apiBase.value}/downloads/pick`, { method: "POST" });
if (!response.ok) {
const payload = await response.json().catch(() => ({}));
const detail = typeof payload.detail === "string" ? payload.detail : "";
@@ -310,20 +364,21 @@ async function chooseDirectory() {
}
async function startDownload() {
const seen = new Set<number>();
const items: { id: number; title: string; image: string }[] = [];
const seen = new Set<string>();
const items: { id: string; title: string; image: string }[] = [];
for (const section of sections.value) {
for (const item of section.items) {
if (!selected.value.has(item.id) || seen.has(item.id)) continue;
seen.add(item.id);
items.push({ id: item.id, title: item.title, image: item.image });
const key = dramaKey(item.id);
if (!selected.value.has(key) || seen.has(key)) continue;
seen.add(key);
items.push({ id: key, title: item.title, image: item.image });
}
}
if (!items.length) return;
starting.value = true;
footError.value = "";
try {
const response = await fetch("/api/downloads", {
const response = await fetch(`${apiBase.value}/downloads`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: props.token, device: props.device, items }),
@@ -384,10 +439,11 @@ async function startDownload() {
:key="item.id"
class="drama-card"
:data-card="cardKey(section.key, item.id)"
@click="toggleDrama(item.id, !selected.has(item.id))"
@click="toggleDrama(item.id, !selected.has(dramaKey(item.id)))"
>
<div class="cover">
<img v-if="item.image" :src="coverOf(item.image)" :alt="item.title" />
<span v-if="item.corner || item.locked || item.limitedFree" class="cover-tag">{{ item.corner || (item.limitedFree ? "限免" : item.payType || "付费") }}</span>
<div class="cover-play-layer">
<n-tooltip placement="top">
<template #trigger>
@@ -402,7 +458,7 @@ async function startDownload() {
<div class="pick" @click.stop>
<n-tooltip placement="left">
<template #trigger>
<n-checkbox :checked="selected.has(item.id)" @update:checked="toggleDrama(item.id, $event)" />
<n-checkbox :checked="selected.has(dramaKey(item.id))" @update:checked="toggleDrama(item.id, $event)" />
</template>
{{ selected.has(item.id) ? "取消选择" : "选择" }}
</n-tooltip>
@@ -496,6 +552,22 @@ async function startDownload() {
cursor: pointer;
}
.cover { position: relative; background: #2a2a2a; }
.cover-tag {
position: absolute;
left: 6px;
top: 6px;
z-index: 2;
max-width: calc(100% - 12px);
padding: 1px 6px;
border-radius: 4px;
background: rgba(0, 0, 0, 0.62);
color: #fff;
font-size: 11px;
line-height: 16px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.cover img { display: block; width: 100%; aspect-ratio: 3 / 4; object-fit: cover; }
.cover:not(:has(img)) { aspect-ratio: 3 / 4; }
.cover-play-layer {