const { app, BrowserWindow, Menu } = require('electron') const path = require('path') const isDev = process.env.NODE_ENV === 'development' // 保持对窗口对象的全局引用,避免被垃圾回收 let mainWindow function createWindow() { // 创建浏览器窗口 mainWindow = new BrowserWindow({ width: 600, height: 800, minWidth: 100, minHeight: 300, webPreferences: { nodeIntegration: false, contextIsolation: true, webSecurity: false, // 禁用webSecurity以允许跨域请求 allowRunningInsecureContent: true, // 允许不安全内容 enableRemoteModule: false, // 禁用远程模块,提高安全性 webgl: true, images: true }, icon: path.join(__dirname, '../../dist/build/h5/static/pc.png'), // 应用图标 show: false // 先隐藏窗口,等加载完成再显示 }) // 设置Content Security Policy mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => { callback({ responseHeaders: { ...details.responseHeaders, 'Content-Security-Policy': [ "default-src * 'unsafe-inline' 'unsafe-eval' data: blob:; " + "script-src * 'unsafe-inline' 'unsafe-eval'; " + "connect-src * 'unsafe-inline'; " + "img-src * data: blob: 'unsafe-inline'; " + "frame-src *; " + "style-src * 'unsafe-inline'; " + "font-src * data:; " + "media-src *" ] } }) }) // 加载应用 if (isDev) { // 开发环境:加载本地H5开发服务器 mainWindow.loadURL('http://localhost:9090') // 打开开发者工具 mainWindow.webContents.openDevTools() } else { // 生产环境:加载打包后的文件 const fs = require('fs') let indexPath // 尝试多种可能的路径 const possiblePaths = [ // 打包后路径1:resources/app/dist/build/h5 process.resourcesPath ? path.join(process.resourcesPath, 'app', 'dist', 'build', 'h5', 'index.html') : null, // 打包后路径2:resources/app process.resourcesPath ? path.join(process.resourcesPath, 'app', 'index.html') : null, // 打包后路径3:直接位于应用目录 path.join(__dirname, '..', 'dist', 'build', 'h5', 'index.html'), // 开发环境路径 path.join(__dirname, '..', '..', 'dist', 'build', 'h5', 'index.html'), // 备用路径 path.join(__dirname, 'dist', 'build', 'h5', 'index.html') ].filter(Boolean) // 查找存在的文件 for (const testPath of possiblePaths) { console.log('Testing path:', testPath) if (fs.existsSync(testPath)) { indexPath = testPath console.log('Found index file at:', indexPath) break } } if (indexPath && fs.existsSync(indexPath)) { // 使用 loadFile 方法加载文件 mainWindow.loadFile(indexPath) console.log('Successfully loaded index file from:', indexPath) } else { console.error('Index file not found in any of the following paths:') possiblePaths.forEach(p => console.error(' -', p)) // 显示详细的错误信息 const errorHtml = `
无法找到 index.html 文件,请检查以下路径:
请重新安装应用或联系技术支持。
` mainWindow.loadURL(`data:text/html,${encodeURIComponent(errorHtml)}`) } } // 窗口准备好后显示 mainWindow.once('ready-to-show', () => { mainWindow.show() }) // 窗口关闭时触发 mainWindow.on('closed', () => { mainWindow = null }) // 设置菜单(可选) const template = [ { label: '文件', submenu: [ { label: '退出', accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q', click() { app.quit() } } ] }, { label: '视图', submenu: [ { role: 'reload', label: '重新加载' }, { role: 'forceReload', label: '强制重新加载' }, { role: 'toggleDevTools', label: '开发者工具' }, { type: 'separator' }, { role: 'resetZoom', label: '实际大小' }, { role: 'zoomIn', label: '放大' }, { role: 'zoomOut', label: '缩小' }, { type: 'separator' }, { role: 'togglefullscreen', label: '切换全屏' } ] } ] const menu = Menu.buildFromTemplate(template) Menu.setApplicationMenu(menu) } // Electron 初始化完成时触发 app.whenReady().then(createWindow) // 所有窗口关闭时退出应用(macOS除外) app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() } }) // macOS 应用激活时重新创建窗口 app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow() } }) // 安全设置:阻止新窗口创建 app.on('web-contents-created', (event, contents) => { contents.on('new-window', (event, navigationUrl) => { event.preventDefault() // 在默认浏览器中打开外部链接 require('electron').shell.openExternal(navigationUrl) }) })