feat: move config to ts

This commit is contained in:
shinya
2025-06-24 10:30:36 +08:00
parent e47cc27f49
commit b60d069c02
6 changed files with 97 additions and 170 deletions

View File

@@ -1,58 +0,0 @@
import Image, { ImageProps } from 'next/image';
import * as React from 'react';
import { cn } from '@/lib/utils';
type NextImageProps = {
useSkeleton?: boolean;
classNames?: {
image?: string;
blur?: string;
};
alt: string;
} & (
| { width: string | number; height: string | number }
| { layout: 'fill'; width?: string | number; height?: string | number }
) &
ImageProps;
/**
*
* @description Must set width using `w-` className
* @param useSkeleton add background with pulse animation, don't use it if image is transparent
*/
export default function NextImage({
useSkeleton = false,
src,
width,
height,
alt,
className,
classNames,
...rest
}: NextImageProps) {
const [status, setStatus] = React.useState(
useSkeleton ? 'loading' : 'complete'
);
const widthIsSet = className?.includes('w-') ?? false;
return (
<figure
style={!widthIsSet ? { width: `${width}px` } : undefined}
className={className}
>
<Image
className={cn(
classNames?.image,
status === 'loading' && cn('animate-pulse', classNames?.blur)
)}
src={src}
width={width}
height={height}
alt={alt}
onLoadingComplete={() => setStatus('complete')}
{...rest}
/>
</figure>
);
}

View File

@@ -1,6 +1,3 @@
import fs from 'fs';
import path from 'path';
export interface ApiSite {
key: string;
api: string;
@@ -8,6 +5,13 @@ export interface ApiSite {
detail?: string;
}
// 配置文件中的 API 站点类型(不包含 key 属性)
export interface ApiSiteConfig {
api: string;
name: string;
detail?: string;
}
export interface StorageConfig {
type: 'localstorage' | 'database';
database?: {
@@ -22,7 +26,7 @@ export interface StorageConfig {
export interface Config {
cache_time?: number;
api_site: {
[key: string]: ApiSite;
[key: string]: ApiSiteConfig;
};
storage?: StorageConfig;
}
@@ -48,28 +52,103 @@ export const API_CONFIG = {
},
};
let config: Config | null = null;
// 配置数据,直接嵌入不再从文件读取
const CONFIG_DATA: Config = {
cache_time: 7200,
api_site: {
dyttzy: {
api: 'http://caiji.dyttzyapi.com/api.php/provide/vod',
name: '电影天堂资源',
detail: 'http://caiji.dyttzyapi.com',
},
ruyi: {
api: 'https://cj.rycjapi.com/api.php/provide/vod',
name: '如意资源',
},
bfzy: {
api: 'https://bfzyapi.com/api.php/provide/vod',
name: '暴风资源',
},
tyyszy: {
api: 'https://tyyszy.com/api.php/provide/vod',
name: '天涯资源',
},
ffzy: {
api: 'http://ffzy5.tv/api.php/provide/vod',
name: '非凡影视',
detail: 'http://ffzy5.tv',
},
heimuer: {
api: 'https://json.heimuer.xyz/api.php/provide/vod',
name: '黑木耳',
detail: 'https://heimuer.tv',
},
zy360: {
api: 'https://360zy.com/api.php/provide/vod',
name: '360资源',
},
iqiyi: {
api: 'https://www.iqiyizyapi.com/api.php/provide/vod',
name: 'iqiyi资源',
},
wolong: {
api: 'https://wolongzyw.com/api.php/provide/vod',
name: '卧龙资源',
},
hwba: {
api: 'https://cjhwba.com/api.php/provide/vod',
name: '华为吧资源',
},
jisu: {
api: 'https://jszyapi.com/api.php/provide/vod',
name: '极速资源',
detail: 'https://jszyapi.com',
},
dbzy: {
api: 'https://dbzy.tv/api.php/provide/vod',
name: '豆瓣资源',
},
mozhua: {
api: 'https://mozhuazy.com/api.php/provide/vod',
name: '魔爪资源',
},
mdzy: {
api: 'https://www.mdzyapi.com/api.php/provide/vod',
name: '魔都资源',
},
zuid: {
api: 'https://api.zuidapi.com/api.php/provide/vod',
name: '最大资源',
},
yinghua: {
api: 'https://m3u8.apiyhzy.com/api.php/provide/vod',
name: '樱花资源',
},
wujin: {
api: 'https://api.wujinapi.me/api.php/provide/vod',
name: '无尽资源',
},
wwzy: {
api: 'https://wwzy.tv/api.php/provide/vod',
name: '旺旺短剧',
},
ikun: {
api: 'https://ikunzyapi.com/api.php/provide/vod',
name: 'iKun资源',
},
},
};
export function getConfig(): Config {
if (config) {
return config;
}
const configPath = path.join(process.cwd(), 'config.json');
const configContent = fs.readFileSync(configPath, 'utf-8');
const parsedConfig = JSON.parse(configContent) as Config;
config = parsedConfig;
return parsedConfig;
return CONFIG_DATA;
}
export function getCacheTime(): number {
const config = getConfig();
return config.cache_time || 300; // 默认5分钟缓存
return CONFIG_DATA.cache_time || 300; // 默认5分钟缓存
}
export function getApiSites(): ApiSite[] {
const config = getConfig();
return Object.entries(config.api_site).map(([key, site]) => ({
return Object.entries(CONFIG_DATA.api_site).map(([key, site]) => ({
...site,
key,
}));