bug修复
This commit is contained in:
1 parent
ab0245b9a3
commit
7823f67b04
14 files changed
+317
-88
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { ImageOff, Search } from "lucide-react";
|
||||
import { ImageOff, Search, Upload } from "lucide-react";
|
||||
import { API, assetUrl } from "./api";
|
||||
|
||||
const SITE = "SIT0000000000001";
|
||||
@@ -19,30 +19,41 @@ export default function MediaCenter() {
|
||||
const [query, setQuery] = useState("");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState("");
|
||||
const [uploading, setUploading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
/** 加载当前素材类型列表。 */
|
||||
const load = async () => {
|
||||
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));
|
||||
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();
|
||||
}, [type]);
|
||||
|
||||
/** 上传图片或视频到对象存储。 */
|
||||
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); }
|
||||
};
|
||||
|
||||
const filtered = items.filter((item) =>
|
||||
item.originalFileName.toLowerCase().includes(query.trim().toLowerCase()),
|
||||
);
|
||||
@@ -54,6 +65,7 @@ export default function MediaCenter() {
|
||||
<h1>素材中心</h1>
|
||||
<p>集中管理上传过的图片和视频,编辑内容时可直接复用</p>
|
||||
</div>
|
||||
<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>
|
||||
</header>
|
||||
|
||||
<section className="toolbar media-toolbar">
|
||||
|
||||
Reference in new issue
Block a user