const path = require('path')
const fs = require('fs')
const { app, BrowserWindow, shell } = require('electron')
const APP_TITLE = process.env.ELECTRON_APP_TITLE || '访客机自助登记管理平台'
const rendererUrl = process.env.ELECTRON_RENDERER_URL || ''
const iconPath = path.join(__dirname, '../public/logo.ico')
const preloadPath = path.join(__dirname, 'preload.js')
const desktopIndexPath = path.join(__dirname, '../dist-desktop/index.html')
let mainWindow = null
app.name = APP_TITLE
function createFallbackPage() {
return `
${APP_TITLE}
${APP_TITLE}
当前没有找到桌面版前端资源。
请先执行 npm run build:desktop,再运行 npm run electron:start。
`
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1440,
height: 900,
minWidth: 1280,
minHeight: 760,
show: false,
autoHideMenuBar: true,
title: APP_TITLE,
icon: iconPath,
webPreferences: {
preload: preloadPath,
contextIsolation: true,
nodeIntegration: false
}
})
mainWindow.once('ready-to-show', () => {
mainWindow.show()
if (rendererUrl) {
mainWindow.webContents.openDevTools({ mode: 'detach' })
}
})
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url)
return { action: 'deny' }
})
if (rendererUrl) {
mainWindow.loadURL(rendererUrl)
return
}
if (fs.existsSync(desktopIndexPath)) {
mainWindow.loadFile(desktopIndexPath)
return
}
mainWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(createFallbackPage())}`)
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})