import CloudUploadOutlined from "@mui/icons-material/CloudUploadOutlined"; import Alert from "@mui/material/Alert"; import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; import CircularProgress from "@mui/material/CircularProgress"; import Stack from "@mui/material/Stack"; import Typography from "@mui/material/Typography"; import { useRef, useState } from "react"; import { uploadExternalImage } from "../api/business.js"; export function ExternalImageUploadField({ disabled, label, onChange, value }) { const inputRef = useRef(null); const [error, setError] = useState(""); const [uploading, setUploading] = useState(false); const handleFile = async (event) => { const file = event.target.files?.[0]; event.target.value = ""; if (!file) { return; } if (!file.type.startsWith("image/")) { setError("请选择图片文件"); return; } if (file.size > 10 * 1024 * 1024) { setError("图片不能超过 10MB"); return; } setError(""); setUploading(true); try { const url = await uploadExternalImage(file); if (!url) { throw new Error("上传结果缺少图片地址"); } onChange(url); } catch (uploadError) { setError(uploadError.message || "图片上传失败"); } finally { setUploading(false); } }; return ( {label} {value ? {`${label}预览`} : 暂无图片} {error ? {error} : null} ); }