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.
 
 
 
 
 
 

168 lines
4.9 KiB

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,<h1>构建文件不存在,请先运行 npm run dev</h1>`);
return;
}
}
// 文件存在,直接加载
loadMainWindow();
function loadMainWindow() {
mainWindow.loadFile(loadPath).then(() => {
console.log('页面加载成功');
// 开发环境下打开开发者工具
if (isDev) {
mainWindow.webContents.openDevTools()
}
}).catch(err => {
console.error('加载失败:', err)
mainWindow.loadURL(`data:text/html,<h1>加载失败: ${err.toString()}</h1>`)
})
}
// 窗口关闭事件
mainWindow.on('closed', () => {
mainWindow = null
})
} catch (error) {
console.error('创建窗口失败:', error)
}
}
app.whenReady().then(() => {
console.log('Electron应用已准备就绪');
createWindow()
// 修复IPC通信问题
ipcMain.on('request-status', (event) => {
try {
const statusData = {
status: 'running',
timestamp: Date.now()
}
event.sender.send('status-update', statusData)
} catch (error) {
console.error('IPC通信错误:', error);
}
})
}).catch(error => {
console.error('应用启动失败:', error)
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
if (mainWindow === null) createWindow()
})
// 添加错误处理
process.on('uncaughtException', (error) => {
console.error('未捕获的异常:', error)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('未处理的Promise拒绝:', reason)
})