Files
shoplix/web-admin/src/MediaCenter.tsx
T

124 lines
4.8 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from "react";
2026-11-24 21:32:53 +08:00
import { ImageOff, Search, Upload } from "lucide-react";
2026-11-22 10:14:44 +08:00
import { API, assetUrl } from "./api";
2026-11-21 17:40:21 +08:00
const SITE = "SIT0000000000001";
type Media = {
mediaCode: string;
mediaType: string;
originalFileName: string;
url: string;
altText?: string;
};
/** 素材中心页面。 */
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("");
2026-11-24 21:32:53 +08:00
const [uploading, setUploading] = useState(false);
2026-11-21 17:40:21 +08:00
2026-11-24 21:32:53 +08:00
/** 加载当前素材类型列表。 */
const load = async () => {
2026-11-21 17:40:21 +08:00
setLoading(true);
setError("");
2026-11-24 21:32:53 +08:00
try {
const response = await fetch(`${API}/media/list`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ siteCode: SITE, mediaType: type }) });
if (!response.ok) { const data = await response.json().catch(() => ({})); throw new Error(data.message || "素材加载失败"); }
const data = await response.json();
setItems(data.items || []);
} catch (reason) {
setItems([]);
setError(reason instanceof Error ? reason.message : "素材加载失败");
} finally { setLoading(false); }
};
useEffect(() => {
void load();
2026-11-21 17:40:21 +08:00
}, [type]);
2026-11-24 21:32:53 +08:00
/** 上传图片或视频到对象存储。 */
const upload = async (file?: File) => {
if (!file) return;
try {
setUploading(true); setError("");
const body = new FormData(); body.append("siteCode", SITE); body.append("altText", file.name); body.append("file", file);
const response = await fetch(`${API}/media/upload`, { method: "POST", body });
if (response.status === 401) window.dispatchEvent(new Event("shoplix:unauthorized"));
if (!response.ok) { const data = await response.json().catch(() => ({})); throw new Error(data.message || "素材上传失败"); }
await load();
} catch (reason) { setError(reason instanceof Error ? reason.message : "素材上传失败"); }
finally { setUploading(false); }
};
2026-11-21 17:40:21 +08:00
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>
2026-11-24 21:32:53 +08:00
<label className={`media-upload-button${uploading ? " uploading" : ""}`}><Upload size={17}/>{uploading ? "上传中" : "上传素材"}<input disabled={uploading} type="file" accept="image/jpeg,image/png,image/webp,image/gif,video/mp4,video/webm,video/quicktime,.mov" onChange={(event) => { void upload(event.currentTarget.files?.[0]); event.currentTarget.value = ""; }}/></label>
2026-11-21 17:40:21 +08:00
</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
2026-11-22 10:14:44 +08:00
src={assetUrl(item.url)}
2026-11-21 17:40:21 +08:00
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>
);
}