diff --git a/electron/main.js b/electron/main.js index d8eb82f..59f050d 100644 --- a/electron/main.js +++ b/electron/main.js @@ -64,6 +64,7 @@ function createWindow() { height: 1920, fullscreen: true, // 全屏 frame: false, // 无边框 + icon: path.join(__dirname, "../resources/icon.ico"), // 设置窗口图标 webPreferences: { preload: path.join(__dirname, "preload.js"), nodeIntegration: false, @@ -195,15 +196,22 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => { } // 创建临时文件 - tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf`); + tempFilePath = path.join( + os.tmpdir(), + `print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf` + ); fs.writeFileSync(tempFilePath, buffer); pdfFilePath = tempFilePath; const fileSize = fs.statSync(tempFilePath).size; - log.info(`PDF written to temp file: ${tempFilePath}, size: ${fileSize} bytes`); + log.info( + `PDF written to temp file: ${tempFilePath}, size: ${fileSize} bytes` + ); if (fileSize < 1024) { - throw new Error(`PDF文件大小异常(${fileSize} bytes),可能数据不完整`); + throw new Error( + `PDF文件大小异常(${fileSize} bytes),可能数据不完整` + ); } } catch (err) { log.error("Base64解码或文件写入失败:", err); @@ -211,32 +219,39 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => { } } // 如果是HTTP/HTTPS URL,先下载 - else if (pdfDataOrUrl.startsWith("http://") || pdfDataOrUrl.startsWith("https://")) { + else if ( + pdfDataOrUrl.startsWith("http://") || + pdfDataOrUrl.startsWith("https://") + ) { log.info("Downloading PDF from URL:", pdfDataOrUrl); const protocol = pdfDataOrUrl.startsWith("https") ? https : http; const buffer = await new Promise((resolve, reject) => { - protocol.get(pdfDataOrUrl, (response) => { - // 处理重定向 - if (response.statusCode === 301 || response.statusCode === 302) { - const redirectUrl = response.headers.location; - protocol.get(redirectUrl, (redirectResponse) => { + protocol + .get(pdfDataOrUrl, (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", () => { + resolve(Buffer.concat(chunks)); + }); + redirectResponse.on("error", reject); + }) + .on("error", reject); + } else { const chunks = []; - redirectResponse.on("data", (chunk) => chunks.push(chunk)); - redirectResponse.on("end", () => { + response.on("data", (chunk) => chunks.push(chunk)); + response.on("end", () => { resolve(Buffer.concat(chunks)); }); - redirectResponse.on("error", reject); - }).on("error", reject); - } else { - const chunks = []; - response.on("data", (chunk) => chunks.push(chunk)); - response.on("end", () => { - resolve(Buffer.concat(chunks)); - }); - response.on("error", reject); - } - }).on("error", reject); + response.on("error", reject); + } + }) + .on("error", reject); }); // 验证PDF文件头 @@ -244,12 +259,17 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => { throw new Error("下载的文件不是有效的PDF"); } - tempFilePath = path.join(os.tmpdir(), `print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf`); + tempFilePath = path.join( + os.tmpdir(), + `print_${Date.now()}_${Math.random().toString(36).substring(7)}.pdf` + ); fs.writeFileSync(tempFilePath, buffer); pdfFilePath = tempFilePath; const fileSize = fs.statSync(tempFilePath).size; - log.info(`PDF downloaded to temp file: ${tempFilePath}, size: ${fileSize} bytes`); + log.info( + `PDF downloaded to temp file: ${tempFilePath}, size: ${fileSize} bytes` + ); } // 如果是本地文件路径 else if (pdfDataOrUrl.startsWith("file://")) { @@ -280,7 +300,11 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => { throw new Error(`PDF文件大小异常(${stats.size} bytes),可能文件损坏`); } - log.info(`准备打印PDF: ${pdfFilePath}, 打印机: ${targetPrinterName || "默认打印机"}`); + log.info( + `准备打印PDF: ${pdfFilePath}, 打印机: ${ + targetPrinterName || "默认打印机" + }` + ); // 使用 pdf-to-printer 打印 const printOptions_ptp = { diff --git a/index.html b/index.html index 1363056..638e875 100644 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ - Electron + React App + 自助签到
diff --git a/package.json b/package.json index 488f580..6f24d4a 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "build": { "appId": "com.yuanhe.checkin", - "productName": "YuanheCheckin", + "productName": "自助签到", "directories": { "output": "release" }, @@ -55,6 +55,7 @@ } ], "win": { + "icon": "resources/icon.ico", "target": [ "nsis", "portable" diff --git a/resources/icon.ico b/resources/icon.ico new file mode 100644 index 0000000..12b4428 Binary files /dev/null and b/resources/icon.ico differ