| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- 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: 375, // 手机宽度(类似iPhone 12/13)
- height: 667, // 手机高度
- minWidth: 320, // 最小宽度(类似小屏手机)
- minHeight: 480, // 最小高度
- maxWidth: 414, // 最大宽度(类似大屏手机)
- maxHeight: 896, // 最大高度
- 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 = `
- <html>
- <head><title>应用加载失败</title></head>
- <body style="font-family: Arial, sans-serif; padding: 20px;">
- <h1>应用文件加载失败</h1>
- <p>无法找到 index.html 文件,请检查以下路径:</p>
- <ul>
- ${possiblePaths.map(p => `<li>${p}</li>`).join('')}
- </ul>
- <p>请重新安装应用或联系技术支持。</p>
- </body>
- </html>
- `
- mainWindow.loadURL(`data:text/html,${encodeURIComponent(errorHtml)}`)
- }
- }
- // 窗口准备好后显示
- mainWindow.once('ready-to-show', () => {
- mainWindow.show()
- // 设置页面缩放以适应手机尺寸
- mainWindow.webContents.setZoomFactor(0.85) // 85%缩放,让内容更紧凑
- })
- // 窗口关闭时触发
- 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)
- })
- })
|