const { app, BrowserWindow, ipcMain } = require('electron') const path = require('path') const fs = require('fs') // 获取项目根目录绝对路径 const appRoot = path.resolve(__dirname, '../..') console.log('Application root:', appRoot) let mainWindow // 检查是否为开发环境 const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged console.log('开发环境:', isDev) // 开发环境热重载 - 只在非watch模式下启用 if (isDev && !process.argv.includes('--watch')) { try { // 修复热重载配置 require('electron-reload')(appRoot, { electron: path.join(__dirname, '..', '..', 'node_modules', 'electron', 'dist', 'electron.exe'), hardResetMethod: 'exit' }); console.log('热重载已启用'); } catch (error) { console.log('热重载启用失败:', error.message); } } // 监听文件变化(用于watch模式) if (isDev && process.argv.includes('--watch')) { console.log('Watch模式已启用,监听文件变化...'); // 监听dist目录变化 const distPath = path.join(appRoot, 'dist/renderer'); if (fs.existsSync(distPath)) { fs.watch(distPath, { recursive: true }, (eventType, filename) => { if (filename && mainWindow && !mainWindow.isDestroyed()) { console.log(`文件变化: ${filename}, 重新加载页面...`); mainWindow.reload(); } }); } } function createWindow() { try { mainWindow = new BrowserWindow({ width: 1200, height: 800, autoHideMenuBar: true, webPreferences: { nodeIntegration: false, contextIsolation: true, enableRemoteModule: false, preload: path.join(appRoot, 'src/preload.js'), // 强化安全配置 webSecurity: true, allowRunningInsecureContent: false, experimentalFeatures: false, // 禁用Node.js集成 nodeIntegrationInWorker: false, nodeIntegrationInSubFrames: false, // 沙箱模式 sandbox: false, // 保持false以支持preload脚本 // 禁用eval相关功能 enableBlinkFeatures: '', disableBlinkFeatures: 'Auxclick' } }) console.log('主窗口已创建'); // 开发环境下抑制安全警告 if (isDev) { mainWindow.webContents.on('did-frame-finish-load', () => { // 移除有问题的JavaScript执行,避免IPC通信错误 console.log('页面加载完成,开发者工具已准备就绪'); }); } const loadPath = path.join(appRoot, 'dist/renderer/index.html') console.log('Loading:', loadPath) // 检查文件是否存在 if (!fs.existsSync(loadPath)) { console.log('文件不存在,等待构建完成...'); // 在watch模式下等待文件构建完成 if (isDev && process.argv.includes('--watch')) { const checkFile = () => { if (fs.existsSync(loadPath)) { console.log('文件已构建完成,开始加载...'); loadMainWindow(); } else { setTimeout(checkFile, 1000); // 每秒检查一次 } }; checkFile(); return; // 重要:如果文件不存在,直接返回,不执行后续代码 } else { // 非watch模式,直接显示错误 console.error('文件不存在:', loadPath); mainWindow.loadURL(`data:text/html,