1
This commit is contained in:
@@ -1,28 +1,29 @@
|
||||
import {app, BrowserWindow, ipcMain, Menu, screen, dialog} from 'electron';
|
||||
import {existsSync, createWriteStream, promises as fs} from 'fs';
|
||||
import {existsSync, createWriteStream, promises as fs, mkdirSync, copyFileSync} from 'fs';
|
||||
import {join, dirname} from 'path';
|
||||
import {spawn, ChildProcess} from 'child_process';
|
||||
import * as https from 'https';
|
||||
import * as http from 'http';
|
||||
|
||||
let springProcess: ChildProcess | null = null;
|
||||
let mainWindow: BrowserWindow | null = null;
|
||||
let splashWindow: BrowserWindow | null = null;
|
||||
let appOpened = false;
|
||||
|
||||
let downloadProgress = {percentage: 0, current: '0 MB', total: '0 MB', speed: ''};
|
||||
let isDownloading = false;
|
||||
let downloadedFilePath: string | null = null;
|
||||
|
||||
function openAppIfNotOpened() {
|
||||
if (appOpened) return;
|
||||
appOpened = true;
|
||||
if (mainWindow) {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.webContents.once('did-finish-load', () => {
|
||||
setTimeout(() => {
|
||||
mainWindow?.show();
|
||||
mainWindow?.focus();
|
||||
if (splashWindow) {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.show();
|
||||
mainWindow.focus();
|
||||
}
|
||||
|
||||
// 安全关闭启动画面
|
||||
if (splashWindow && !splashWindow.isDestroyed()) {
|
||||
splashWindow.close();
|
||||
splashWindow = null;
|
||||
}
|
||||
@@ -61,12 +62,24 @@ function getJarFilePath(): string {
|
||||
return join(__dirname, '../../public/erp_client_sb-2.4.7.jar');
|
||||
}
|
||||
|
||||
const bundledJarPath = join(process.resourcesPath, 'app.asar.unpacked/public/erp_client_sb-2.4.7.jar');
|
||||
if (existsSync(bundledJarPath)) {
|
||||
return bundledJarPath;
|
||||
// 生产环境:需要将JAR包从asar提取到临时位置
|
||||
const tempDir = join(app.getPath('temp'), 'erp-client');
|
||||
const tempJarPath = join(tempDir, 'erp_client_sb-2.4.7.jar');
|
||||
|
||||
// 确保临时目录存在
|
||||
if (!existsSync(tempDir)) {
|
||||
mkdirSync(tempDir, { recursive: true });
|
||||
}
|
||||
|
||||
return join(__dirname, '../../public/erp_client_sb-2.4.7.jar');
|
||||
// 如果临时JAR不存在,从asar中复制
|
||||
if (!existsSync(tempJarPath)) {
|
||||
const asarJarPath = join(__dirname, '../assets/erp_client_sb-2.4.7.jar');
|
||||
if (existsSync(asarJarPath)) {
|
||||
copyFileSync(asarJarPath, tempJarPath);
|
||||
}
|
||||
}
|
||||
|
||||
return tempJarPath;
|
||||
}
|
||||
|
||||
function getSplashPath(): string {
|
||||
@@ -95,9 +108,48 @@ function getIconPath(): string {
|
||||
return join(__dirname, '../renderer/icon/icon.png');
|
||||
}
|
||||
|
||||
function getDataDirectoryPath(): string {
|
||||
// 将用户数据目录放在可写的应用数据目录下
|
||||
const userDataPath = app.getPath('userData');
|
||||
const dataDir = join(userDataPath, 'data');
|
||||
|
||||
// 确保数据目录存在
|
||||
if (!existsSync(dataDir)) {
|
||||
mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
|
||||
return dataDir;
|
||||
}
|
||||
|
||||
function migrateDataFromPublic(): void {
|
||||
// 如果是首次运行,尝试从public/data迁移数据
|
||||
const oldDataPath = join(__dirname, '../../public/data');
|
||||
const newDataPath = getDataDirectoryPath();
|
||||
|
||||
if (process.env.NODE_ENV === 'development' && existsSync(oldDataPath)) {
|
||||
try {
|
||||
const files = require('fs').readdirSync(oldDataPath);
|
||||
for (const file of files) {
|
||||
const srcFile = join(oldDataPath, file);
|
||||
const destFile = join(newDataPath, file);
|
||||
|
||||
if (!existsSync(destFile)) {
|
||||
require('fs').copyFileSync(srcFile, destFile);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('数据迁移失败,使用默认配置');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function startSpringBoot() {
|
||||
// 首先迁移数据(如果需要)
|
||||
migrateDataFromPublic();
|
||||
|
||||
const jarPath = getJarFilePath();
|
||||
const javaPath = getJavaExecutablePath();
|
||||
const dataDir = getDataDirectoryPath();
|
||||
|
||||
if (!existsSync(jarPath)) {
|
||||
dialog.showErrorBox('启动失败', `JAR 文件不存在:\n${jarPath}`);
|
||||
@@ -106,15 +158,31 @@ function startSpringBoot() {
|
||||
}
|
||||
|
||||
try {
|
||||
springProcess = spawn(javaPath, ['-jar', jarPath], {
|
||||
cwd: dirname(jarPath),
|
||||
detached: false
|
||||
// Spring Boot启动参数配置
|
||||
const springArgs = [
|
||||
'-jar', jarPath,
|
||||
`--spring.datasource.url=jdbc:sqlite:${dataDir}/erp-cache.db`,
|
||||
`--logging.file.path=${dataDir}`,
|
||||
`--server.port=8081`
|
||||
];
|
||||
|
||||
// 工作目录设为数据目录,这样Spring Boot会在数据目录下创建临时文件
|
||||
springProcess = spawn(javaPath, springArgs, {
|
||||
cwd: dataDir,
|
||||
detached: false,
|
||||
env: {
|
||||
...process.env,
|
||||
'ERP_DATA_DIR': dataDir,
|
||||
'USER_DATA_DIR': dataDir
|
||||
}
|
||||
});
|
||||
|
||||
let startupCompleted = false;
|
||||
|
||||
springProcess.stdout?.on('data', (data) => {
|
||||
const output = data.toString();
|
||||
console.log('[Spring Boot]', output.trim());
|
||||
|
||||
if (!startupCompleted && (output.includes('Started Success') || output.includes('Started ErpClientSbApplication'))) {
|
||||
startupCompleted = true;
|
||||
openAppIfNotOpened();
|
||||
@@ -184,6 +252,16 @@ function createWindow() {
|
||||
|
||||
Menu.setApplicationMenu(null);
|
||||
mainWindow.setMenuBarVisibility(false);
|
||||
|
||||
// 打开开发者工具
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
mainWindow.webContents.openDevTools();
|
||||
}
|
||||
|
||||
// 监听窗口关闭事件,确保正确清理引用
|
||||
mainWindow.on('closed', () => {
|
||||
mainWindow = null;
|
||||
});
|
||||
|
||||
mainWindow.webContents.once('did-finish-load', () => {
|
||||
setTimeout(() => checkPendingUpdate(), 500);
|
||||
@@ -212,6 +290,11 @@ app.whenReady().then(() => {
|
||||
}
|
||||
});
|
||||
|
||||
// 监听启动窗口关闭事件
|
||||
splashWindow.on('closed', () => {
|
||||
splashWindow = null;
|
||||
});
|
||||
|
||||
const splashPath = getSplashPath();
|
||||
if (existsSync(splashPath)) {
|
||||
splashWindow.loadFile(splashPath);
|
||||
@@ -278,31 +361,6 @@ ipcMain.handle('download-update', async (event, downloadUrl: string) => {
|
||||
isDownloading = true;
|
||||
|
||||
try {
|
||||
const isRealDev = process.env.NODE_ENV === 'development' && !app.isPackaged;
|
||||
|
||||
if (isRealDev) {
|
||||
for (let i = 0; i <= 100; i += 10) {
|
||||
setTimeout(() => {
|
||||
downloadProgress = {
|
||||
percentage: i,
|
||||
current: (i * 0.5).toFixed(1) + ' MB',
|
||||
total: '50.0 MB',
|
||||
speed: '2.5 MB/s'
|
||||
};
|
||||
if (mainWindow) {
|
||||
mainWindow.webContents.send('download-progress', downloadProgress);
|
||||
}
|
||||
}, i * 100);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
downloadedFilePath = 'completed';
|
||||
isDownloading = false;
|
||||
}, 1100);
|
||||
|
||||
return {success: true, filePath: 'dev-mode-simulated', dev: true};
|
||||
}
|
||||
|
||||
const tempPath = join(app.getPath('temp'), 'app.asar.new');
|
||||
|
||||
await downloadFile(downloadUrl, tempPath, (progress) => {
|
||||
@@ -353,13 +411,6 @@ ipcMain.handle('get-download-progress', () => {
|
||||
|
||||
ipcMain.handle('install-update', async () => {
|
||||
try {
|
||||
const isRealDev = process.env.NODE_ENV === 'development' && !app.isPackaged;
|
||||
|
||||
if (isRealDev) {
|
||||
downloadedFilePath = null;
|
||||
return {success: true, message: '开发环境模拟重启'};
|
||||
}
|
||||
|
||||
const updateFilePath = join(process.resourcesPath, 'app.asar.update');
|
||||
const hasUpdateFile = existsSync(updateFilePath);
|
||||
|
||||
@@ -406,38 +457,23 @@ ipcMain.handle('cancel-download', () => {
|
||||
});
|
||||
|
||||
ipcMain.handle('get-update-status', () => {
|
||||
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged || process.defaultApp;
|
||||
return {downloadedFilePath, isDownloading, downloadProgress, isPackaged: app.isPackaged, isDev};
|
||||
return {downloadedFilePath, isDownloading, downloadProgress, isPackaged: app.isPackaged};
|
||||
});
|
||||
|
||||
// 添加文件保存对话框处理器
|
||||
ipcMain.handle('show-save-dialog', async (event, options) => {
|
||||
if (!mainWindow) {
|
||||
return {canceled: true};
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await dialog.showSaveDialog(mainWindow, options);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('文件保存对话框错误:', error);
|
||||
return {canceled: true, error: error instanceof Error ? error.message : '对话框打开失败'};
|
||||
}
|
||||
return await dialog.showSaveDialog(mainWindow!, options);
|
||||
});
|
||||
|
||||
// 添加文件夹选择对话框处理器
|
||||
ipcMain.handle('show-open-dialog', async (event, options) => {
|
||||
if (!mainWindow) {
|
||||
return {canceled: true};
|
||||
}
|
||||
return await dialog.showOpenDialog(mainWindow!, options);
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await dialog.showOpenDialog(mainWindow, options);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('文件夹选择对话框错误:', error);
|
||||
return {canceled: true, error: error instanceof Error ? error.message : '对话框打开失败'};
|
||||
}
|
||||
// 添加文件写入处理器
|
||||
ipcMain.handle('write-file', async (event, filePath: string, data: Uint8Array) => {
|
||||
await fs.writeFile(filePath, Buffer.from(data));
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user