You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
66 lines
1.7 KiB
const { app, BrowserWindow, ipcMain } = require('electron') |
|
const path = require('path') |
|
|
|
// 获取项目根目录绝对路径 |
|
const appRoot = path.resolve(__dirname, '../..') |
|
console.log('Application root:', appRoot) |
|
|
|
let mainWindow |
|
|
|
// 开发环境热重载 |
|
if (process.env.NODE_ENV === 'development') { |
|
require('electron-reload')(__dirname, { |
|
electron: path.join(__dirname, 'node_modules', '.bin', 'electron'), |
|
hardResetMethod: 'exit' |
|
}); |
|
} |
|
|
|
function createWindow() { |
|
mainWindow = new BrowserWindow({ |
|
width: 1200, |
|
height: 800, |
|
autoHideMenuBar: true, |
|
webPreferences: { |
|
nodeIntegration: false, |
|
contextIsolation: true, |
|
enableRemoteModule: false, |
|
preload: path.join(appRoot, 'src/preload.js') |
|
} |
|
}) |
|
|
|
// 开发环境下抑制安全警告 |
|
if (process.env.NODE_ENV === 'development') { |
|
mainWindow.webContents.on('did-frame-finish-load', () => { |
|
mainWindow.webContents.executeJavaScript(` |
|
console.warn = console.error = () => {}; |
|
`).catch(err => console.error('抑制警告失败:', err)); |
|
}); |
|
} |
|
|
|
const loadPath = path.join(appRoot, 'dist/renderer/index.html') |
|
console.log('Loading:', loadPath) |
|
|
|
mainWindow.loadFile(loadPath).catch(err => { |
|
console.error('加载失败:', err) |
|
mainWindow.loadURL(`data:text/html,<h1>加载失败: ${err.toString()}</h1>`) |
|
}) |
|
|
|
mainWindow.webContents.openDevTools() |
|
} |
|
|
|
app.whenReady().then(() => { |
|
createWindow() |
|
|
|
ipcMain.on('request-status', (event) => { |
|
const statusData = { /* 你的数据 */ } |
|
event.sender.send('status-update', statusData) |
|
}) |
|
}) |
|
|
|
app.on('window-all-closed', () => { |
|
if (process.platform !== 'darwin') app.quit() |
|
}) |
|
|
|
app.on('activate', () => { |
|
if (mainWindow === null) createWindow() |
|
})
|
|
|