zjk
2026-07-31 8665daa7aa863f96d0d8f27c167edae1efc38022
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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()
    }
})