添加8.9页面
This commit is contained in:
109
electron/main.js
109
electron/main.js
@@ -1,8 +1,12 @@
|
||||
const { app, BrowserWindow } = require("electron");
|
||||
const { app, BrowserWindow, ipcMain } = require("electron");
|
||||
const path = require("path");
|
||||
const https = require("https");
|
||||
const http = require("http");
|
||||
|
||||
let mainWindow;
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1080,
|
||||
height: 1920,
|
||||
webPreferences: {
|
||||
@@ -17,14 +21,109 @@ function createWindow() {
|
||||
const isDev = !app.isPackaged;
|
||||
|
||||
if (isDev) {
|
||||
win.loadURL("http://localhost:5173");
|
||||
mainWindow.loadURL("http://localhost:5173");
|
||||
// 打开开发者工具
|
||||
win.webContents.openDevTools();
|
||||
mainWindow.webContents.openDevTools();
|
||||
} else {
|
||||
win.loadFile(path.join(__dirname, "../dist/index.html"));
|
||||
mainWindow.loadFile(path.join(__dirname, "../dist/index.html"));
|
||||
}
|
||||
}
|
||||
|
||||
// 处理PDF获取请求(绕过CORS)
|
||||
ipcMain.handle("fetch-pdf", async (event, pdfUrl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const protocol = pdfUrl.startsWith("https") ? https : http;
|
||||
|
||||
protocol
|
||||
.get(pdfUrl, (response) => {
|
||||
// 处理重定向
|
||||
if (response.statusCode === 301 || response.statusCode === 302) {
|
||||
const redirectUrl = response.headers.location;
|
||||
protocol
|
||||
.get(redirectUrl, (redirectResponse) => {
|
||||
const chunks = [];
|
||||
redirectResponse.on("data", (chunk) => chunks.push(chunk));
|
||||
redirectResponse.on("end", () => {
|
||||
const buffer = Buffer.concat(chunks);
|
||||
resolve({
|
||||
success: true,
|
||||
data: buffer.toString("base64"),
|
||||
});
|
||||
});
|
||||
redirectResponse.on("error", (error) => {
|
||||
reject({ success: false, error: error.message });
|
||||
});
|
||||
})
|
||||
.on("error", (error) => {
|
||||
reject({ success: false, error: error.message });
|
||||
});
|
||||
} else {
|
||||
const chunks = [];
|
||||
response.on("data", (chunk) => chunks.push(chunk));
|
||||
response.on("end", () => {
|
||||
const buffer = Buffer.concat(chunks);
|
||||
resolve({
|
||||
success: true,
|
||||
data: buffer.toString("base64"),
|
||||
});
|
||||
});
|
||||
response.on("error", (error) => {
|
||||
reject({ success: false, error: error.message });
|
||||
});
|
||||
}
|
||||
})
|
||||
.on("error", (error) => {
|
||||
reject({ success: false, error: error.message });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 处理PDF打印请求
|
||||
ipcMain.handle("print-pdf", async (event, pdfUrl) => {
|
||||
try {
|
||||
// 创建一个隐藏的窗口用于加载PDF
|
||||
const printWindow = new BrowserWindow({
|
||||
width: 800,
|
||||
height: 600,
|
||||
show: false,
|
||||
webPreferences: {
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
},
|
||||
});
|
||||
|
||||
// 加载PDF
|
||||
await printWindow.loadURL(pdfUrl);
|
||||
|
||||
// 等待页面加载完成
|
||||
await new Promise((resolve) => {
|
||||
printWindow.webContents.on("did-finish-load", resolve);
|
||||
});
|
||||
|
||||
// 静默打印(直接调用系统打印对话框)
|
||||
printWindow.webContents.print(
|
||||
{
|
||||
silent: false, // 显示打印对话框
|
||||
printBackground: true,
|
||||
margins: {
|
||||
marginType: "none",
|
||||
},
|
||||
},
|
||||
(success, errorType) => {
|
||||
printWindow.close();
|
||||
if (!success) {
|
||||
console.error("Print failed:", errorType);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Print error:", error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
});
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
const { contextBridge, ipcRenderer } = require("electron");
|
||||
|
||||
contextBridge.exposeInMainWorld("electronAPI", {
|
||||
// 在这里暴露安全的 API 给渲染进程
|
||||
// example: sendMessage: (message) => ipcRenderer.send('message', message)
|
||||
// 获取PDF(绕过CORS)
|
||||
fetchPdf: (pdfUrl) => ipcRenderer.invoke("fetch-pdf", pdfUrl),
|
||||
// 打印PDF
|
||||
printPdf: (pdfUrl) => ipcRenderer.invoke("print-pdf", pdfUrl),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user