更新名称和图标

This commit is contained in:
yuchenglong
2025-12-04 11:34:12 +08:00
parent 00a4d9966b
commit 1e0822c1a0
4 changed files with 52 additions and 27 deletions

View File

@@ -64,6 +64,7 @@ function createWindow() {
height: 1920, height: 1920,
fullscreen: true, // 全屏 fullscreen: true, // 全屏
frame: false, // 无边框 frame: false, // 无边框
icon: path.join(__dirname, "../resources/icon.ico"), // 设置窗口图标
webPreferences: { webPreferences: {
preload: path.join(__dirname, "preload.js"), preload: path.join(__dirname, "preload.js"),
nodeIntegration: false, 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); fs.writeFileSync(tempFilePath, buffer);
pdfFilePath = tempFilePath; pdfFilePath = tempFilePath;
const fileSize = fs.statSync(tempFilePath).size; 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) { if (fileSize < 1024) {
throw new Error(`PDF文件大小异常${fileSize} bytes可能数据不完整`); throw new Error(
`PDF文件大小异常${fileSize} bytes可能数据不完整`
);
} }
} catch (err) { } catch (err) {
log.error("Base64解码或文件写入失败:", err); log.error("Base64解码或文件写入失败:", err);
@@ -211,23 +219,29 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
} }
} }
// 如果是HTTP/HTTPS URL先下载 // 如果是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); log.info("Downloading PDF from URL:", pdfDataOrUrl);
const protocol = pdfDataOrUrl.startsWith("https") ? https : http; const protocol = pdfDataOrUrl.startsWith("https") ? https : http;
const buffer = await new Promise((resolve, reject) => { const buffer = await new Promise((resolve, reject) => {
protocol.get(pdfDataOrUrl, (response) => { protocol
.get(pdfDataOrUrl, (response) => {
// 处理重定向 // 处理重定向
if (response.statusCode === 301 || response.statusCode === 302) { if (response.statusCode === 301 || response.statusCode === 302) {
const redirectUrl = response.headers.location; const redirectUrl = response.headers.location;
protocol.get(redirectUrl, (redirectResponse) => { protocol
.get(redirectUrl, (redirectResponse) => {
const chunks = []; const chunks = [];
redirectResponse.on("data", (chunk) => chunks.push(chunk)); redirectResponse.on("data", (chunk) => chunks.push(chunk));
redirectResponse.on("end", () => { redirectResponse.on("end", () => {
resolve(Buffer.concat(chunks)); resolve(Buffer.concat(chunks));
}); });
redirectResponse.on("error", reject); redirectResponse.on("error", reject);
}).on("error", reject); })
.on("error", reject);
} else { } else {
const chunks = []; const chunks = [];
response.on("data", (chunk) => chunks.push(chunk)); response.on("data", (chunk) => chunks.push(chunk));
@@ -236,7 +250,8 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
}); });
response.on("error", reject); response.on("error", reject);
} }
}).on("error", reject); })
.on("error", reject);
}); });
// 验证PDF文件头 // 验证PDF文件头
@@ -244,12 +259,17 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
throw new Error("下载的文件不是有效的PDF"); 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); fs.writeFileSync(tempFilePath, buffer);
pdfFilePath = tempFilePath; pdfFilePath = tempFilePath;
const fileSize = fs.statSync(tempFilePath).size; 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://")) { else if (pdfDataOrUrl.startsWith("file://")) {
@@ -280,7 +300,11 @@ ipcMain.handle("print-pdf", async (event, pdfDataOrUrl, printOptions = {}) => {
throw new Error(`PDF文件大小异常${stats.size} bytes可能文件损坏`); throw new Error(`PDF文件大小异常${stats.size} bytes可能文件损坏`);
} }
log.info(`准备打印PDF: ${pdfFilePath}, 打印机: ${targetPrinterName || "默认打印机"}`); log.info(
`准备打印PDF: ${pdfFilePath}, 打印机: ${
targetPrinterName || "默认打印机"
}`
);
// 使用 pdf-to-printer 打印 // 使用 pdf-to-printer 打印
const printOptions_ptp = { const printOptions_ptp = {

View File

@@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Electron + React App</title> <title>自助签到</title>
</head> </head>
<body> <body>
<div id="root"></div> <div id="root"></div>

View File

@@ -40,7 +40,7 @@
}, },
"build": { "build": {
"appId": "com.yuanhe.checkin", "appId": "com.yuanhe.checkin",
"productName": "YuanheCheckin", "productName": "自助签到",
"directories": { "directories": {
"output": "release" "output": "release"
}, },
@@ -55,6 +55,7 @@
} }
], ],
"win": { "win": {
"icon": "resources/icon.ico",
"target": [ "target": [
"nsis", "nsis",
"portable" "portable"

BIN
resources/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB