96 lines
2.2 KiB
JavaScript
96 lines
2.2 KiB
JavaScript
const { app, BrowserWindow, ipcMain } = require('electron');
|
||
const path = require('path');
|
||
const { Worker } = require('worker_threads');
|
||
|
||
let idCardWorker = null;
|
||
let mainWindow = null;
|
||
|
||
function createIdCardWorker() {
|
||
if (idCardWorker) return;
|
||
|
||
idCardWorker = new Worker(path.join(__dirname, 'idcard-worker.js'));
|
||
|
||
idCardWorker.on('message', (msg) => {
|
||
if (!mainWindow) return;
|
||
|
||
if (msg.type === 'data') {
|
||
mainWindow.webContents.send('idcard-data', { payload: msg.payload });
|
||
} else if (msg.type === 'error') {
|
||
mainWindow.webContents.send('idcard-error', { payload: msg.payload });
|
||
}
|
||
});
|
||
|
||
idCardWorker.on('error', (err) => {
|
||
console.error('Worker error:', err);
|
||
});
|
||
|
||
idCardWorker.on('exit', (code) => {
|
||
if (code !== 0) console.error(`Worker stopped with exit code ${code}`);
|
||
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();
|
||
}
|
||
});
|