86 lines
2.1 KiB
JavaScript
86 lines
2.1 KiB
JavaScript
const { app, BrowserWindow, ipcMain, session } = require('electron')
|
|
const path = require('path')
|
|
const isDev = process.env.NODE_ENV === 'development'
|
|
|
|
// 读取 API 目标地址,优先用环境变量
|
|
const API_TARGET = process.env.VUE_APP_BASE_API || 'http://localhost:4201'
|
|
|
|
// 添加日志记录
|
|
const log = (...args) => {
|
|
console.log('[Electron]', ...args)
|
|
}
|
|
|
|
function createWindow() {
|
|
log('Creating window...')
|
|
|
|
// 创建浏览器窗口
|
|
const mainWindow = new BrowserWindow({
|
|
width: 1200,
|
|
height: 800,
|
|
webPreferences: {
|
|
nodeIntegration: true,
|
|
contextIsolation: false,
|
|
webSecurity: false,
|
|
devTools: true // 启用开发者工具
|
|
},
|
|
frame: false, // 无边框窗口
|
|
titleBarStyle: 'hidden', // 隐藏标题栏
|
|
})
|
|
|
|
// 加载应用
|
|
if (isDev) {
|
|
log('Loading development URL...')
|
|
mainWindow.loadURL('http://localhost:8080')
|
|
mainWindow.webContents.openDevTools()
|
|
} else {
|
|
const indexPath = path.join(__dirname, '../dist/index.html')
|
|
log('Loading production file:', indexPath)
|
|
mainWindow.loadFile(indexPath).catch(err => {
|
|
log('Error loading index.html:', err)
|
|
})
|
|
|
|
// 打开开发者工具(生产环境也打开,方便调试)
|
|
mainWindow.webContents.openDevTools()
|
|
}
|
|
|
|
// 添加错误处理
|
|
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription) => {
|
|
log('Failed to load:', errorCode, errorDescription)
|
|
})
|
|
|
|
// 处理自定义标题栏事件
|
|
ipcMain.on('minimize-window', () => {
|
|
mainWindow.minimize()
|
|
})
|
|
|
|
ipcMain.on('maximize-window', () => {
|
|
if (mainWindow.isMaximized()) {
|
|
mainWindow.unmaximize()
|
|
} else {
|
|
mainWindow.maximize()
|
|
}
|
|
})
|
|
|
|
ipcMain.on('close-window', () => {
|
|
mainWindow.close()
|
|
})
|
|
}
|
|
|
|
app.whenReady().then(() => {
|
|
log('App is ready')
|
|
createWindow()
|
|
|
|
app.on('activate', function () {
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
})
|
|
|
|
app.on('window-all-closed', function () {
|
|
if (process.platform !== 'darwin') app.quit()
|
|
})
|
|
|
|
// 添加未捕获异常处理
|
|
process.on('uncaughtException', (error) => {
|
|
log('Uncaught Exception:', error)
|
|
})
|