You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
9.8 KiB

4 weeks ago
const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const fs = require('fs')
// 设置进程编码,确保中文正常显示
process.env.LANG = 'zh_CN.UTF-8'
process.env.LC_ALL = 'zh_CN.UTF-8'
4 weeks ago
// 获取项目根目录绝对路径
const appRoot = path.resolve(__dirname, '../..')
console.log('Application root:', appRoot)
let mainWindow
// 检查是否为开发环境
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged
console.log('Development environment:', isDev)
// 开发环境热重载 - 只在非watch模式下启用
if (isDev && !process.argv.includes('--watch')) {
try {
// 修复热重载配置
require('electron-reload')(appRoot, {
electron: path.join(__dirname, '..', '..', 'node_modules', 'electron', 'dist', 'electron.exe'),
hardResetMethod: 'exit',
// 添加更稳定的配置
forceHardReset: true,
ignored: [
/node_modules|[\/\\]\./,
/dist|[\/\\]\./,
/\.git|[\/\\]\./
]
});
console.log('Hot reload enabled');
} catch (error) {
console.log('Hot reload failed:', error.message);
}
}
// 监听文件变化(用于watch模式)
if (isDev && process.argv.includes('--watch')) {
console.log('Watch mode enabled, monitoring file changes...');
// 监听dist目录变化
const distPath = path.join(appRoot, 'dist/renderer');
if (fs.existsSync(distPath)) {
let reloadTimeout = null;
fs.watch(distPath, { recursive: true }, (eventType, filename) => {
if (filename && mainWindow && !mainWindow.isDestroyed()) {
// 添加防抖机制,避免频繁重载
if (reloadTimeout) {
clearTimeout(reloadTimeout);
}
reloadTimeout = setTimeout(() => {
try {
console.log(`File changed: ${filename}, reloading page...`);
mainWindow.reload();
} catch (error) {
console.log('Page reload failed:', error.message);
}
}, 500); // 500ms防抖延迟
}
});
}
4 weeks ago
}
function createWindow() {
try {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
preload: path.join(appRoot, 'src/preload.js'),
// 强化安全配置
webSecurity: true,
allowRunningInsecureContent: false,
experimentalFeatures: false,
// 禁用Node.js集成
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
// 沙箱模式
sandbox: false, // 保持false以支持preload脚本
// 禁用eval相关功能
enableBlinkFeatures: '',
disableBlinkFeatures: 'Auxclick'
},
// 添加字体配置,确保中文正常显示
titleBarStyle: 'default',
show: false // 先隐藏窗口,等加载完成后再显示
})
console.log('Main window created');
// 开发环境下抑制安全警告
if (isDev) {
mainWindow.webContents.on('did-frame-finish-load', () => {
// 移除有问题的JavaScript执行,避免IPC通信错误
console.log('Page loaded, DevTools ready');
// 简化JavaScript注入,避免序列化错误
mainWindow.webContents.executeJavaScript(`
// 重写console.warn和console.error来抑制特定警告
const originalWarn = console.warn;
const originalError = console.error;
console.warn = function(...args) {
const message = args.join(' ');
if (message.includes('Electron Security Warning') ||
message.includes('Insecure Content-Security-Policy') ||
message.includes('unsafe-eval')) {
return; // 抑制这些警告
}
originalWarn.apply(console, args);
};
console.error = function(...args) {
const message = args.join(' ');
if (message.includes('Electron Security Warning') ||
message.includes('Insecure Content-Security-Policy') ||
message.includes('unsafe-eval')) {
return; // 抑制这些警告
}
originalError.apply(console, args);
};
console.log('Console warning suppression enabled');
`).catch(err => {
console.log('JavaScript injection failed:', err.message);
// 注入失败不影响应用运行
});
});
// 添加页面加载错误处理
mainWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
console.log('Page load failed:', errorDescription);
// 可以在这里添加重试逻辑
});
// 添加渲染进程崩溃处理
mainWindow.webContents.on('render-process-gone', (event, details) => {
console.log('Render process gone:', details.reason);
// 可以在这里添加恢复逻辑
});
// 抑制Electron安全警告
mainWindow.webContents.on('console-message', (event, level, message, line, sourceId) => {
// 抑制Autofill相关警告
if (message.includes('Autofill.enable failed') ||
message.includes('Autofill.setAddresses failed') ||
message.includes('Request Autofill')) {
event.preventDefault();
return false;
}
// 抑制Electron安全警告
if (message.includes('Electron Security Warning') ||
message.includes('Insecure Content-Security-Policy') ||
message.includes('unsafe-eval')) {
event.preventDefault();
return false;
}
});
}
const loadPath = path.join(appRoot, 'dist/renderer/index.html')
console.log('Loading:', loadPath)
// 检查文件是否存在
if (!fs.existsSync(loadPath)) {
console.log('File not found, waiting for build completion...');
// 在watch模式下等待文件构建完成
if (isDev && process.argv.includes('--watch')) {
const checkFile = () => {
if (fs.existsSync(loadPath)) {
console.log('Build completed, starting to load...');
loadMainWindow();
} else {
setTimeout(checkFile, 1000); // 每秒检查一次
}
};
checkFile();
return; // 重要:如果文件不存在,直接返回,不执行后续代码
} else {
// 非watch模式,直接显示错误
console.error('File not found:', loadPath);
mainWindow.loadURL(`data:text/html,<h1>Build file not found, please run npm run dev first</h1>`);
return;
}
}
4 weeks ago
// 文件存在,直接加载
loadMainWindow();
function loadMainWindow() {
mainWindow.loadFile(loadPath).then(() => {
console.log('Page loaded successfully');
// 开发环境下打开开发者工具
if (isDev) {
mainWindow.webContents.openDevTools()
}
// 页面加载完成后显示窗口
mainWindow.show()
}).catch(err => {
console.error('Load failed:', err)
mainWindow.loadURL(`data:text/html,<h1>Load failed: ${err.toString()}</h1>`)
// 即使加载失败也要显示窗口
mainWindow.show()
})
}
// 窗口关闭事件
mainWindow.on('closed', () => {
mainWindow = null
})
4 weeks ago
} catch (error) {
console.error('Failed to create window:', error)
}
}
4 weeks ago
app.whenReady().then(() => {
console.log('Electron app ready');
4 weeks ago
createWindow()
// 修复IPC通信问题
4 weeks ago
ipcMain.on('request-status', (event) => {
try {
const statusData = {
status: 'running',
timestamp: Date.now()
}
event.sender.send('status-update', statusData)
} catch (error) {
console.error('IPC communication error:', error);
}
4 weeks ago
})
// 添加获取进程内存信息的API
ipcMain.handle('get-process-memory-info', async (event) => {
try {
// 使用 process.memoryUsage 获取内存信息
const memoryUsage = process.memoryUsage();
// 获取更详细的系统内存信息
let systemMemoryInfo = {};
try {
// 尝试使用 require('os') 获取系统内存信息
const os = require('os');
systemMemoryInfo = {
totalMemory: os.totalmem(),
freeMemory: os.freemem(),
usedMemory: os.totalmem() - os.freemem()
};
} catch (osError) {
console.warn('Cannot get system memory info:', osError.message);
}
return {
// 进程内存使用情况
privateBytes: memoryUsage.rss, // Resident Set Size - 进程占用的物理内存
sharedBytes: memoryUsage.external, // 外部内存使用
heapUsed: memoryUsage.heapUsed, // JavaScript堆内存使用
heapTotal: memoryUsage.heapTotal, // JavaScript堆内存总量
// 系统内存信息
systemTotal: systemMemoryInfo.totalMemory || 0,
systemFree: systemMemoryInfo.freeMemory || 0,
systemUsed: systemMemoryInfo.usedMemory || 0
};
} catch (error) {
console.error('Failed to get memory info:', error);
// 返回默认值
return {
privateBytes: 0,
sharedBytes: 0,
heapUsed: 0,
heapTotal: 0,
systemTotal: 0,
systemFree: 0,
systemUsed: 0
};
}
});
}).catch(error => {
console.error('App startup failed:', error)
4 weeks ago
})
app.on('window-all-closed', () => {
4 weeks ago
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', () => {
4 weeks ago
if (mainWindow === null) createWindow()
})
// 添加错误处理
process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled promise rejection:', reason)
})