Merge branch 'main' of https://git.ambigrat.com/hom/yuanhe-checkin-electron
This commit is contained in:
@@ -33,9 +33,11 @@ const UI8: React.FC = () => {
|
||||
const loadPdf = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
window.electronAPI?.log("info", `开始加载PDF: ${PDF_URL}`);
|
||||
|
||||
// 本地文件直接使用,无需通过 fetchPdf
|
||||
if (PDF_URL.startsWith("/") || PDF_URL.startsWith("blob:") || PDF_URL.includes("assets")) {
|
||||
window.electronAPI?.log("info", `检测到本地PDF文件,直接加载: ${PDF_URL}`);
|
||||
setPdfData(PDF_URL);
|
||||
setLoading(false);
|
||||
return;
|
||||
@@ -43,19 +45,26 @@ const UI8: React.FC = () => {
|
||||
|
||||
// 远程 URL 需要通过 Electron 绕过 CORS
|
||||
if (window.electronAPI?.fetchPdf) {
|
||||
window.electronAPI.log("info", `通过Electron下载远程PDF: ${PDF_URL}`);
|
||||
const result = await window.electronAPI.fetchPdf(PDF_URL);
|
||||
if (result.success && result.data) {
|
||||
// 将base64转换为data URL
|
||||
window.electronAPI.log("info", `PDF下载成功,大小: ${result.data.length} bytes (base64)`);
|
||||
setPdfData(`data:application/pdf;base64,${result.data}`);
|
||||
} else {
|
||||
setError(`PDF加载失败: ${result.error || "未知错误"}`);
|
||||
const errorMsg = `PDF下载失败: ${result.error || "未知错误"}`;
|
||||
window.electronAPI.log("error", errorMsg);
|
||||
setError(errorMsg);
|
||||
}
|
||||
} else {
|
||||
// 非Electron环境,直接使用URL
|
||||
window.electronAPI?.log("warn", "非Electron环境,直接使用URL加载");
|
||||
setPdfData(PDF_URL);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("PDF fetch error:", err);
|
||||
const errorMsg = `PDF fetch error: ${err}`;
|
||||
console.error(errorMsg);
|
||||
window.electronAPI?.log("error", errorMsg);
|
||||
setError("PDF加载失败,请检查网络连接");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -72,12 +81,16 @@ const UI8: React.FC = () => {
|
||||
// 打印PDF功能
|
||||
const handleConfirm = async () => {
|
||||
if (!window.electronAPI?.printPdf) {
|
||||
alert("打印功能不可用,请在 Electron 环境中运行");
|
||||
const errorMsg = "打印功能不可用,请在 Electron 环境中运行";
|
||||
window.electronAPI?.log("error", errorMsg);
|
||||
alert(errorMsg);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!pdfData) {
|
||||
alert("PDF 尚未加载完成,请稍候");
|
||||
const errorMsg = "PDF 尚未加载完成,请稍候";
|
||||
window.electronAPI?.log("warn", errorMsg);
|
||||
alert(errorMsg);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -85,12 +98,21 @@ const UI8: React.FC = () => {
|
||||
try {
|
||||
// 本地文件直接传原始路径,远程文件传 base64 data URI
|
||||
const printData = pdfData.startsWith("data:") ? pdfData : PDF_URL;
|
||||
const dataType = pdfData.startsWith("data:") ? "base64数据" : "本地文件路径";
|
||||
window.electronAPI.log("info", `开始打印PDF (${dataType}): ${printData.substring(0, 100)}...`);
|
||||
|
||||
const result = await window.electronAPI.printPdf(printData);
|
||||
if (!result.success) {
|
||||
alert(`打印失败: ${result.error || "未知错误"}`);
|
||||
const errorMsg = `打印失败: ${result.error || "未知错误"}`;
|
||||
window.electronAPI.log("error", errorMsg);
|
||||
alert(errorMsg);
|
||||
} else {
|
||||
window.electronAPI.log("info", "PDF打印请求已发送");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Print error:", error);
|
||||
const errorMsg = `Print error: ${error}`;
|
||||
console.error(errorMsg);
|
||||
window.electronAPI.log("error", errorMsg);
|
||||
alert("打印失败,请重试");
|
||||
} finally {
|
||||
setIsPrinting(false);
|
||||
@@ -98,15 +120,19 @@ const UI8: React.FC = () => {
|
||||
};
|
||||
|
||||
const onDocumentLoadSuccess = ({ numPages }: { numPages: number }) => {
|
||||
window.electronAPI?.log("info", `PDF渲染成功,共 ${numPages} 页`);
|
||||
setNumPages(numPages);
|
||||
setLoading(false);
|
||||
setError("");
|
||||
};
|
||||
|
||||
const onDocumentLoadError = (error: Error) => {
|
||||
const errorMsg = `PDF渲染失败: ${error.message || error}`;
|
||||
console.error("PDF load error:", error);
|
||||
window.electronAPI?.log("error", errorMsg);
|
||||
window.electronAPI?.log("error", `PDF数据: ${pdfData?.substring(0, 100)}...`);
|
||||
setError("PDF 加载失败,请检查网络连接");
|
||||
setLoading(false);
|
||||
console.error("PDF load error:", error);
|
||||
};
|
||||
|
||||
const goToPrevPage = () => {
|
||||
@@ -186,7 +212,7 @@ const UI8: React.FC = () => {
|
||||
<div className="basic-white-block">
|
||||
<div className="basic-content">
|
||||
<div className="ui8-pdf-container" ref={scrollContainerRef}>
|
||||
{/* {loading && <div className="ui8-loading">加载中...</div>} */}
|
||||
{loading && <div className="ui8-loading">PDF加载中...</div>}
|
||||
{error && <div className="ui8-error">{error}</div>}
|
||||
|
||||
{pdfData && (
|
||||
|
||||
Reference in New Issue
Block a user