mirror of
https://github.com/MoonTechLab/LunaTV.git
synced 2026-05-19 12:27:29 +08:00
fix: same SearchResult
This commit is contained in:
@@ -3,20 +3,6 @@ import { SearchResult } from '@/lib/types';
|
|||||||
|
|
||||||
import { getDetailFromApi, searchFromApi } from './downstream';
|
import { getDetailFromApi, searchFromApi } from './downstream';
|
||||||
|
|
||||||
export interface VideoDetail {
|
|
||||||
id: string;
|
|
||||||
title: string;
|
|
||||||
poster: string;
|
|
||||||
episodes: string[];
|
|
||||||
source: string;
|
|
||||||
source_name: string;
|
|
||||||
class?: string;
|
|
||||||
year: string;
|
|
||||||
desc?: string;
|
|
||||||
type_name?: string;
|
|
||||||
douban_id?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface FetchVideoDetailOptions {
|
interface FetchVideoDetailOptions {
|
||||||
source: string;
|
source: string;
|
||||||
id: string;
|
id: string;
|
||||||
@@ -32,7 +18,7 @@ export async function fetchVideoDetail({
|
|||||||
source,
|
source,
|
||||||
id,
|
id,
|
||||||
fallbackTitle = '',
|
fallbackTitle = '',
|
||||||
}: FetchVideoDetailOptions): Promise<VideoDetail> {
|
}: FetchVideoDetailOptions): Promise<SearchResult> {
|
||||||
// 优先通过搜索接口查找精确匹配
|
// 优先通过搜索接口查找精确匹配
|
||||||
const apiSites = getAvailableApiSites();
|
const apiSites = getAvailableApiSites();
|
||||||
const apiSite = apiSites.find((site) => site.key === source);
|
const apiSite = apiSites.find((site) => site.key === source);
|
||||||
@@ -48,19 +34,7 @@ export async function fetchVideoDetail({
|
|||||||
item.id.toString() === id.toString()
|
item.id.toString() === id.toString()
|
||||||
);
|
);
|
||||||
if (exactMatch) {
|
if (exactMatch) {
|
||||||
return {
|
return exactMatch;
|
||||||
id: exactMatch.id,
|
|
||||||
title: exactMatch.title,
|
|
||||||
poster: exactMatch.poster,
|
|
||||||
episodes: exactMatch.episodes,
|
|
||||||
source: exactMatch.source,
|
|
||||||
source_name: exactMatch.source_name,
|
|
||||||
class: exactMatch.class,
|
|
||||||
year: exactMatch.year,
|
|
||||||
desc: exactMatch.desc,
|
|
||||||
type_name: exactMatch.type_name,
|
|
||||||
douban_id: exactMatch.douban_id,
|
|
||||||
} as VideoDetail;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// do nothing
|
// do nothing
|
||||||
@@ -69,17 +43,9 @@ export async function fetchVideoDetail({
|
|||||||
|
|
||||||
// 调用 /api/detail 接口
|
// 调用 /api/detail 接口
|
||||||
const detail = await getDetailFromApi(apiSite, id);
|
const detail = await getDetailFromApi(apiSite, id);
|
||||||
|
if (!detail) {
|
||||||
|
throw new Error('获取视频详情失败');
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return detail;
|
||||||
id: detail.id,
|
|
||||||
title: detail.title,
|
|
||||||
poster: detail.poster || '',
|
|
||||||
episodes: detail.episodes,
|
|
||||||
source: detail.source,
|
|
||||||
source_name: detail.source_name,
|
|
||||||
class: detail.class,
|
|
||||||
year: detail.year || '',
|
|
||||||
desc: detail.desc,
|
|
||||||
type_name: detail.type_name,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
|
|
||||||
import { db } from '@/lib/db';
|
import { db } from '@/lib/db';
|
||||||
import { type VideoDetail, fetchVideoDetail } from '@/lib/fetchVideoDetail';
|
import { fetchVideoDetail } from '@/lib/fetchVideoDetail';
|
||||||
|
import { SearchResult } from '@/lib/types';
|
||||||
|
|
||||||
const STORAGE_TYPE = process.env.NEXT_PUBLIC_STORAGE_TYPE ?? 'localstorage';
|
const STORAGE_TYPE = process.env.NEXT_PUBLIC_STORAGE_TYPE ?? 'localstorage';
|
||||||
|
|
||||||
@@ -16,14 +17,14 @@ async function refreshRecordAndFavorites() {
|
|||||||
users.push(process.env.USERNAME);
|
users.push(process.env.USERNAME);
|
||||||
}
|
}
|
||||||
// 函数级缓存:key 为 `${source}+${id}`,值为 Promise<VideoDetail | null>
|
// 函数级缓存:key 为 `${source}+${id}`,值为 Promise<VideoDetail | null>
|
||||||
const detailCache = new Map<string, Promise<VideoDetail | null>>();
|
const detailCache = new Map<string, Promise<SearchResult | null>>();
|
||||||
|
|
||||||
// 获取详情 Promise(带缓存和错误处理)
|
// 获取详情 Promise(带缓存和错误处理)
|
||||||
const getDetail = async (
|
const getDetail = async (
|
||||||
source: string,
|
source: string,
|
||||||
id: string,
|
id: string,
|
||||||
fallbackTitle: string
|
fallbackTitle: string
|
||||||
): Promise<VideoDetail | null> => {
|
): Promise<SearchResult | null> => {
|
||||||
const key = `${source}+${id}`;
|
const key = `${source}+${id}`;
|
||||||
let promise = detailCache.get(key);
|
let promise = detailCache.get(key);
|
||||||
if (!promise) {
|
if (!promise) {
|
||||||
|
|||||||
Reference in New Issue
Block a user