Merge branch 'main' of https://git.ambigrat.com/hom/yuanhe-checkin-electron
This commit is contained in:
@@ -3,7 +3,57 @@ const path = require("path");
|
||||
const https = require("https");
|
||||
const http = require("http");
|
||||
|
||||
let mainWindow;
|
||||
let mainWindow = null;
|
||||
const { Worker } = require("worker_threads");
|
||||
const log = require("electron-log");
|
||||
|
||||
// 配置日志输出
|
||||
log.transports.file.level = "info";
|
||||
log.transports.file.resolvePath = () =>
|
||||
path.join(path.dirname(app.getPath("exe")), "logs", "main.log");
|
||||
log.info("App starting...");
|
||||
|
||||
// 监听渲染进程日志
|
||||
ipcMain.on("log-message", (event, { level, message }) => {
|
||||
if (log[level]) {
|
||||
log[level](`[Renderer] ${message}`);
|
||||
} else {
|
||||
log.info(`[Renderer] ${message}`);
|
||||
}
|
||||
});
|
||||
|
||||
let idCardWorker = null;
|
||||
|
||||
function createIdCardWorker() {
|
||||
if (idCardWorker) return;
|
||||
|
||||
log.info("Creating IDCard Worker...");
|
||||
idCardWorker = new Worker(path.join(__dirname, "idcard-worker.js"));
|
||||
|
||||
idCardWorker.on("message", (msg) => {
|
||||
if (!mainWindow) return;
|
||||
|
||||
if (msg.type === "data") {
|
||||
log.info("IDCard data received");
|
||||
mainWindow.webContents.send("idcard-data", { payload: msg.payload });
|
||||
} else if (msg.type === "error") {
|
||||
log.error("IDCard worker error message:", msg.payload);
|
||||
mainWindow.webContents.send("idcard-error", { payload: msg.payload });
|
||||
} else if (msg.type === "log") {
|
||||
log.info("[Worker]", msg.payload);
|
||||
}
|
||||
});
|
||||
|
||||
idCardWorker.on("error", (err) => {
|
||||
log.error("Worker thread error:", err);
|
||||
});
|
||||
|
||||
idCardWorker.on("exit", (code) => {
|
||||
if (code !== 0) log.error(`Worker stopped with exit code ${code}`);
|
||||
else log.info("Worker stopped gracefully");
|
||||
idCardWorker = null;
|
||||
});
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
@@ -16,11 +66,16 @@ function createWindow() {
|
||||
},
|
||||
});
|
||||
|
||||
// 开发环境下加载 Vite 开发服务器地址
|
||||
// 生产环境下加载打包后的 index.html
|
||||
mainWindow = win;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD> Vite <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD> index.html
|
||||
const isDev = !app.isPackaged;
|
||||
|
||||
if (isDev) {
|
||||
win.loadURL("http://localhost:5173");
|
||||
// <20><EFBFBD><F2BFAABF><EFBFBD><EFBFBD>߹<EFBFBD><DFB9><EFBFBD>
|
||||
win.webContents.openDevTools();
|
||||
mainWindow.loadURL("http://localhost:5173");
|
||||
// 打开开发者工具
|
||||
mainWindow.webContents.openDevTools();
|
||||
@@ -126,6 +181,25 @@ ipcMain.handle("print-pdf", async (event, pdfUrl) => {
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow();
|
||||
createIdCardWorker();
|
||||
|
||||
// IPC <20><><EFBFBD><EFBFBD>
|
||||
ipcMain.handle("start_idcard_listen", () => {
|
||||
if (idCardWorker) {
|
||||
idCardWorker.postMessage("start");
|
||||
} else {
|
||||
createIdCardWorker();
|
||||
idCardWorker.postMessage("start");
|
||||
}
|
||||
return "started";
|
||||
});
|
||||
|
||||
ipcMain.handle("stop_idcard_listen", () => {
|
||||
if (idCardWorker) {
|
||||
idCardWorker.postMessage("stop");
|
||||
}
|
||||
return "stopped";
|
||||
});
|
||||
|
||||
app.on("activate", () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
@@ -135,6 +209,9 @@ app.whenReady().then(() => {
|
||||
});
|
||||
|
||||
app.on("window-all-closed", () => {
|
||||
if (idCardWorker) {
|
||||
idCardWorker.terminate();
|
||||
}
|
||||
if (process.platform !== "darwin") {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user