fix: photo link http proxy

This commit is contained in:
JOYCEQL
2025-02-08 12:21:45 +08:00
committed by qingchen
parent 6dd02adcd1
commit 475c8f3dc0
+29 -13
View File
@@ -3,6 +3,7 @@ import React, { useState, useRef, useEffect } from "react";
import { motion } from "framer-motion"; import { motion } from "framer-motion";
import { Upload, X } from "lucide-react"; import { Upload, X } from "lucide-react";
import { useTranslations } from "next-intl"; import { useTranslations } from "next-intl";
import { toast } from "sonner";
import { import {
Drawer, Drawer,
DrawerContent, DrawerContent,
@@ -118,16 +119,31 @@ const PhotoConfigDrawer: React.FC<Props> = ({
} }
}; };
const handleUrlChange = (e: string) => { const handleUrlChange = async (e: string) => {
const url = e; const url = e;
setImageUrl(url); try {
setPreviewUrl(url); // images.weserv.nl proxy
updateBasicInfo({ const proxyUrl = `https://images.weserv.nl/?url=${encodeURIComponent(
photo: url, url
}); )}`;
const blob = new Blob([url], { type: "image/png" });
const blobUrl = URL.createObjectURL(blob); const img = new Image();
localStorage.setItem("photo", blobUrl);
await new Promise((resolve, reject) => {
img.onload = resolve;
img.onerror = reject;
img.src = proxyUrl;
});
setImageUrl(proxyUrl);
setPreviewUrl(proxyUrl);
updateBasicInfo({
photo: proxyUrl,
});
} catch (error) {
console.error("图片加载失败:", error);
toast.error("图片链接无效或无法访问,请尝试使用其他图片链接");
}
}; };
const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => { const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => {
@@ -419,10 +435,10 @@ const PhotoConfigDrawer: React.FC<Props> = ({
{radius === "none" {radius === "none"
? "无" ? "无"
: radius === "medium" : radius === "medium"
? "中等" ? "中等"
: radius === "full" : radius === "full"
? "圆形" ? "圆形"
: "自定义"} : "自定义"}
</Button> </Button>
) )
)} )}