详情替换产品图预览改用dataURL
This commit is contained in:
parent
627a9132c5
commit
b6ed76758c
@ -99,19 +99,11 @@ export function SceneFusionView({ onBack, initialProductImage, initialProductNam
|
||||
// 文件上传 Input 引用
|
||||
const productInputRef = useRef<HTMLInputElement>(null);
|
||||
const sceneInputRef = useRef<HTMLInputElement>(null);
|
||||
const productPreviewObjectUrlRef = useRef<string | null>(null);
|
||||
const productUploadIdRef = useRef(0);
|
||||
|
||||
// 如果外部传入了初始产品图,则在首次挂载或传入值变化时自动带入
|
||||
const updateProductPreview = (previewUrl: string | null, isObjectUrl = false) => {
|
||||
if (productPreviewObjectUrlRef.current) {
|
||||
URL.revokeObjectURL(productPreviewObjectUrlRef.current);
|
||||
productPreviewObjectUrlRef.current = null;
|
||||
}
|
||||
const updateProductPreview = (previewUrl: string | null) => {
|
||||
setProductImagePreview(previewUrl);
|
||||
if (isObjectUrl && previewUrl) {
|
||||
productPreviewObjectUrlRef.current = previewUrl;
|
||||
}
|
||||
};
|
||||
|
||||
const clearProductImage = () => {
|
||||
@ -122,7 +114,7 @@ export function SceneFusionView({ onBack, initialProductImage, initialProductNam
|
||||
};
|
||||
|
||||
const handleProductPreviewError = () => {
|
||||
if (!productPreviewObjectUrlRef.current && productImage && productImagePreview !== productImage) {
|
||||
if (productImage && productImagePreview !== productImage) {
|
||||
updateProductPreview(productImage);
|
||||
}
|
||||
};
|
||||
@ -134,15 +126,6 @@ export function SceneFusionView({ onBack, initialProductImage, initialProductNam
|
||||
}
|
||||
}, [initialProductImage]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (productPreviewObjectUrlRef.current) {
|
||||
URL.revokeObjectURL(productPreviewObjectUrlRef.current);
|
||||
productPreviewObjectUrlRef.current = null;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 处理产品图上传
|
||||
const uploadProductImageFile = async (file: File): Promise<string> => {
|
||||
const formData = new FormData();
|
||||
@ -182,8 +165,7 @@ export function SceneFusionView({ onBack, initialProductImage, initialProductNam
|
||||
}
|
||||
|
||||
setProductImage(null);
|
||||
const previewUrl = URL.createObjectURL(file);
|
||||
updateProductPreview(previewUrl, true);
|
||||
updateProductPreview(null);
|
||||
setIsProductUploading(true);
|
||||
const uploadId = ++productUploadIdRef.current;
|
||||
|
||||
@ -192,6 +174,7 @@ export function SceneFusionView({ onBack, initialProductImage, initialProductNam
|
||||
if (uploadId !== productUploadIdRef.current) return;
|
||||
const result = e.target?.result;
|
||||
if (typeof result === "string") {
|
||||
updateProductPreview(result);
|
||||
setProductImage(result);
|
||||
} else {
|
||||
toast.error("图片读取失败,请重试");
|
||||
@ -210,9 +193,6 @@ export function SceneFusionView({ onBack, initialProductImage, initialProductNam
|
||||
const uploadedUrl = await uploadProductImageFile(file);
|
||||
if (uploadId === productUploadIdRef.current) {
|
||||
setProductImage(uploadedUrl);
|
||||
if (!productPreviewObjectUrlRef.current) {
|
||||
updateProductPreview(uploadedUrl);
|
||||
}
|
||||
toast.success("产品图上传成功");
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user