118 lines
2.9 KiB
JavaScript
118 lines
2.9 KiB
JavaScript
const { app, BrowserWindow, ipcMain } = require("electron");
|
||
const path = require("path");
|
||
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;
|
||
let mainWindow = 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() {
|
||
const win = new BrowserWindow({
|
||
width: 1080,
|
||
height: 1920,
|
||
webPreferences: {
|
||
preload: path.join(__dirname, "preload.js"),
|
||
nodeIntegration: false,
|
||
contextIsolation: true,
|
||
},
|
||
});
|
||
|
||
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();
|
||
} else {
|
||
win.loadFile(path.join(__dirname, "../dist/index.html"));
|
||
}
|
||
}
|
||
|
||
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) {
|
||
createWindow();
|
||
}
|
||
});
|
||
});
|
||
|
||
app.on("window-all-closed", () => {
|
||
if (idCardWorker) {
|
||
idCardWorker.terminate();
|
||
}
|
||
if (process.platform !== "darwin") {
|
||
app.quit();
|
||
}
|
||
});
|