2026-11-21 14:52:58 +08:00
|
|
|
import { useEffect, useState } from "react";
|
2026-11-21 17:40:21 +08:00
|
|
|
import { ImageOff, Search } from "lucide-react";
|
|
|
|
|
|
|
|
|
|
const API = "http://localhost:8081/api/admin/v1";
|
|
|
|
|
const SITE = "SIT0000000000001";
|
|
|
|
|
|
|
|
|
|
type Media = {
|
|
|
|
|
mediaCode: string;
|
|
|
|
|
mediaType: string;
|
|
|
|
|
originalFileName: string;
|
|
|
|
|
url: string;
|
|
|
|
|
altText?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-21 14:52:58 +08:00
|
|
|
/** 素材中心页面。 */
|
2026-11-21 17:40:21 +08:00
|
|
|
export default function MediaCenter() {
|
|
|
|
|
const [items, setItems] = useState<Media[]>([]);
|
|
|
|
|
const [type, setType] = useState("ALL");
|
|
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [error, setError] = useState("");
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError("");
|
|
|
|
|
fetch(`${API}/media/list`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ siteCode: SITE, mediaType: type }),
|
|
|
|
|
})
|
|
|
|
|
.then(async (response) => {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const data = await response.json().catch(() => ({}));
|
|
|
|
|
throw new Error(data.message || "素材加载失败");
|
|
|
|
|
}
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then((data) => setItems(data.items || []))
|
|
|
|
|
.catch((reason) => {
|
|
|
|
|
setItems([]);
|
|
|
|
|
setError(reason instanceof Error ? reason.message : "素材加载失败");
|
|
|
|
|
})
|
|
|
|
|
.finally(() => setLoading(false));
|
|
|
|
|
}, [type]);
|
|
|
|
|
|
|
|
|
|
const filtered = items.filter((item) =>
|
|
|
|
|
item.originalFileName.toLowerCase().includes(query.trim().toLowerCase()),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="content-page media-center">
|
|
|
|
|
<header>
|
|
|
|
|
<div>
|
|
|
|
|
<h1>素材中心</h1>
|
|
|
|
|
<p>集中管理上传过的图片和视频,编辑内容时可直接复用</p>
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<section className="toolbar media-toolbar">
|
|
|
|
|
<div className="search">
|
|
|
|
|
<Search size={17} />
|
|
|
|
|
<input
|
|
|
|
|
placeholder="搜索文件名"
|
|
|
|
|
value={query}
|
|
|
|
|
onChange={(event) => setQuery(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<select value={type} onChange={(event) => setType(event.target.value)}>
|
|
|
|
|
<option value="ALL">全部素材</option>
|
|
|
|
|
<option value="IMAGE">图片</option>
|
|
|
|
|
<option value="VIDEO">视频</option>
|
|
|
|
|
</select>
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
{error && <div className="notice">{error}。请检查 Admin 服务和数据库连接。</div>}
|
|
|
|
|
|
|
|
|
|
<section className="media-panel">
|
|
|
|
|
{loading ? (
|
|
|
|
|
<div className="media-empty">
|
|
|
|
|
<strong>正在加载素材</strong>
|
|
|
|
|
</div>
|
|
|
|
|
) : filtered.length === 0 ? (
|
|
|
|
|
<div className="media-empty">
|
|
|
|
|
<ImageOff size={46} strokeWidth={1.5} />
|
|
|
|
|
<strong>{query ? "未找到匹配素材" : "暂无素材"}</strong>
|
|
|
|
|
<p>{query ? "请尝试更换搜索关键词" : "上传图片或视频后,素材会统一展示在这里"}</p>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="media-grid">
|
|
|
|
|
{filtered.map((item) => (
|
|
|
|
|
<article className="media-card" key={item.mediaCode}>
|
|
|
|
|
{item.mediaType === "VIDEO" ? (
|
|
|
|
|
<video src={item.url} controls />
|
|
|
|
|
) : (
|
|
|
|
|
<img
|
|
|
|
|
src={`http://localhost:8081${item.url}`}
|
|
|
|
|
alt={item.altText || item.originalFileName}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<strong title={item.originalFileName}>{item.originalFileName}</strong>
|
|
|
|
|
<small>{item.mediaCode}</small>
|
|
|
|
|
<button className="link" onClick={() => navigator.clipboard.writeText(item.url)}>
|
|
|
|
|
复制地址
|
|
|
|
|
</button>
|
|
|
|
|
</article>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|