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 `
|
<!DOCTYPE html>
|
<html lang="zh-CN">
|
<head>
|
<meta charset="UTF-8" />
|
<title>${APP_TITLE}</title>
|
<style>
|
body {
|
margin: 0;
|
font-family: "Microsoft YaHei", Arial, sans-serif;
|
background: #f5f7fb;
|
color: #1f2937;
|
}
|
.container {
|
min-height: 100vh;
|
display: flex;
|
align-items: center;
|
justify-content: center;
|
padding: 24px;
|
box-sizing: border-box;
|
}
|
.card {
|
width: 100%;
|
max-width: 560px;
|
background: #ffffff;
|
border-radius: 12px;
|
box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
|
padding: 32px;
|
box-sizing: border-box;
|
}
|
h1 {
|
margin: 0 0 12px;
|
font-size: 22px;
|
}
|
p {
|
margin: 0 0 10px;
|
line-height: 1.7;
|
}
|
code {
|
display: inline-block;
|
padding: 2px 6px;
|
background: #f3f4f6;
|
border-radius: 4px;
|
}
|
</style>
|
</head>
|
<body>
|
<div class="container">
|
<div class="card">
|
<h1>${APP_TITLE}</h1>
|
<p>当前没有找到桌面版前端资源。</p>
|
<p>请先执行 <code>npm run build:desktop</code>,再运行 <code>npm run electron:start</code>。</p>
|
</div>
|
</div>
|
</body>
|
</html>
|
`
|
}
|
|
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()
|
}
|
})
|