123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- const { app, BrowserWindow, ipcMain } = require('electron')
- const path = require('path')
- const fs = require('fs')
- // 设置环境变量
- process.env.NODE_ENV = process.env.NODE_ENV || 'development'
- // 命令行参数
- const argv = process.argv.slice(2);
- // 保存主窗口引用
- let mainWindow
- // 设置文件路径
- const settingsPath = path.join(app.getPath('userData'), 'settings.json');
- // 加载设置
- function loadSettings() {
- try {
- if (fs.existsSync(settingsPath)) {
- const data = fs.readFileSync(settingsPath, 'utf8');
- console.log('读取到设置文件:', data);
- try {
- return JSON.parse(data);
- } catch (parseErr) {
- console.error('解析设置文件失败:', parseErr);
- }
- } else {
- console.log('设置文件不存在:', settingsPath);
- }
- } catch (err) {
- console.error('加载设置失败:', err);
- }
- return { defaultLoad: false };
- }
- // 保存设置
- function saveSettings(settings) {
- try {
- // 确保目录存在
- const settingsDir = path.dirname(settingsPath);
- if (!fs.existsSync(settingsDir)) {
- fs.mkdirSync(settingsDir, { recursive: true });
- }
-
- const data = JSON.stringify(settings, null, 2);
- console.log('保存设置:', data, '到路径:', settingsPath);
- fs.writeFileSync(settingsPath, data, 'utf8');
- return true;
- } catch (err) {
- console.error('保存设置失败:', err);
- return false;
- }
- }
- function createWindow() {
- // 加载设置
- const settings = loadSettings();
- console.log('当前设置:', settings);
-
- // 判断是否直接加载应用
- // 命令行参数优先,其次是设置,最后是生产环境默认值
- const shouldDirectLoad = argv.includes('--direct-load') ||
- settings.defaultLoad === true ||
- process.env.NODE_ENV === 'production' ||
- process.env.FORCE_DIRECT_LOAD === 'true';
-
- console.log('是否直接加载应用:', shouldDirectLoad,
- '(命令行:', argv.includes('--direct-load'),
- ', 设置:', settings.defaultLoad,
- ', 环境:', process.env.NODE_ENV, ')');
-
- // 创建浏览器窗口
- mainWindow = new BrowserWindow({
- width: 1200,
- height: 800,
- webPreferences: {
- nodeIntegration: false,
- contextIsolation: true,
- preload: path.join(__dirname, 'preload.cjs'),
- webSecurity: process.env.NODE_ENV === 'production',
- devTools: true
- }
- });
- // 添加控制台消息监听
- mainWindow.webContents.on('console-message', (event, level, message, line, sourceId) => {
- console.log(`[WebContents] ${message}`);
- });
- // 打开开发者工具
- mainWindow.webContents.openDevTools();
- if (shouldDirectLoad) {
- // 直接加载Vue应用并导航到报表页面
- console.log('直接加载报表页面...');
- loadVueApp('/report');
- } else {
- // 加载测试页面
- const testPath = path.join(__dirname, 'test.html');
- console.log('加载测试页面...');
- mainWindow.loadFile(testPath);
- }
- // 添加错误处理
- mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
- console.error('页面加载失败:', errorCode, errorDescription);
- const testPath = path.join(__dirname, 'test.html');
- if (fs.existsSync(testPath)) {
- mainWindow.loadFile(testPath);
- }
- });
- // 在加载页面前添加
- mainWindow.webContents.on('dom-ready', () => {
- console.log('DOM已准备就绪');
- });
- // 在页面加载后执行脚本
- mainWindow.webContents.on('did-finish-load', () => {
- console.log('页面加载完成');
- });
- }
- // 当Electron完成初始化并准备创建浏览器窗口时调用此方法
- app.whenReady().then(() => {
- createWindow()
-
- // 设置IPC监听器
- ipcMain.on('load-app', (event, route) => {
- console.log('收到加载应用请求, 路由:', route || '默认');
- loadVueApp(route);
- });
-
- // 添加保存设置的IPC监听器
- ipcMain.on('save-settings', (event, settings) => {
- console.log('收到保存设置请求:', settings);
- const success = saveSettings(settings);
- console.log('保存设置结果:', success ? '成功' : '失败');
- });
-
- // 添加获取设置的IPC处理器
- ipcMain.handle('get-settings', () => {
- return loadSettings();
- });
-
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow()
- }
- })
- })
- // 加载Vue应用的函数
- function loadVueApp(route) {
- if (!mainWindow) return;
-
- const indexPath = path.join(__dirname, '../dist/index.html');
- console.log('尝试加载Vue应用:', indexPath);
-
- if (fs.existsSync(indexPath)) {
- console.log('Vue应用文件存在,正在加载...');
-
- // 构建URL,如果有指定路由则添加
- let url = `file://${indexPath}?t=${Date.now()}`;
- if (route) {
- url += `#${route}`; // 使用hash模式路由
- }
-
- console.log('加载URL:', url);
-
- // 在加载前清除缓存
- mainWindow.webContents.session.clearCache().then(() => {
- mainWindow.loadURL(url);
-
- // 监听加载完成事件
- mainWindow.webContents.once('did-finish-load', () => {
- console.log('Vue应用加载完成');
- });
- });
- } else {
- console.error('Vue应用文件不存在:', indexPath);
- mainWindow.webContents.executeJavaScript(`
- alert('Vue应用文件不存在: ${indexPath}');
- `).catch(err => console.error('执行脚本失败:', err));
- }
- }
- // 关闭所有窗口时退出应用
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit()
- }
- })
- // 添加全局未捕获异常处理
- process.on('uncaughtException', (error) => {
- console.error('未捕获的异常:', error)
- })
|