This commit is contained in:
2025-09-27 17:41:43 +08:00
parent fe3e24b945
commit 96b396500e
10 changed files with 76 additions and 84 deletions

View File

@@ -1,5 +1,5 @@
import {app, BrowserWindow, ipcMain, Menu, screen, dialog} from 'electron';
import { existsSync, createWriteStream, promises as fs, statSync } from 'fs';
import {existsSync, createWriteStream, promises as fs} from 'fs';
import {join, dirname} from 'path';
import {spawn, ChildProcessWithoutNullStreams} from 'child_process';
import * as https from 'https';
@@ -10,17 +10,26 @@ 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 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;
// SpringBoot 启动完成,现在加载前端页面
if (mainWindow) {
if (process.env.NODE_ENV === 'development') {
const rendererPort = process.argv[2] || 8083;
mainWindow.loadURL(`http://localhost:${rendererPort}`);
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
}
mainWindow.show();
mainWindow.focus();
}
if (splashWindow) {
splashWindow.close();
splashWindow = null;
@@ -28,36 +37,24 @@ function openAppIfNotOpened() {
}
function startSpringBoot() {
const jarPath = 'C:/Users/ZiJIe/Desktop/wox/RuoYi-Vue/ruoyi-admin/target/ruoyi-admin.jar';
const jarPath = join(__dirname, '../../public/erp_client_sb-2.4.7.jar');
springProcess = spawn('java', ['-jar', jarPath], {
cwd: dirname(jarPath),
detached: false
});
springProcess.stdout.on('data', (data) => {
console.log(`SpringBoot: ${data}`);
if (data.toString().includes('Started RuoYiApplication')) {
if (data.toString().includes('Started Success')) {
openAppIfNotOpened();
}
});
springProcess.stderr.on('data', (data) => {
console.error(`SpringBoot ERROR: ${data}`);
const errorStr = data.toString();
if (errorStr.includes('APPLICATION FAILED TO START') ||
errorStr.includes('Port') && errorStr.includes('already in use') ||
errorStr.includes('Unable to start embedded Tomcat')) {
app.quit();
}
});
springProcess.on('close', (code) => {
console.log(`SpringBoot exited with code ${code}`);
mainWindow ? mainWindow.close() : app.quit();
});
}
startSpringBoot();
function stopSpringBoot() {
if (!springProcess) return;
try {
@@ -78,7 +75,7 @@ function stopSpringBoot() {
}
}
function createWindow () {
function createWindow() {
mainWindow = new BrowserWindow({
width: 1280,
height: 800,
@@ -111,7 +108,7 @@ function createWindow () {
app.whenReady().then(() => {
createWindow();
const { width: sw, height: sh } = screen.getPrimaryDisplay().workAreaSize;
const {width: sw, height: sh} = screen.getPrimaryDisplay().workAreaSize;
splashWindow = new BrowserWindow({
width: Math.min(Math.floor(sw * 0.8), 1800),
height: Math.min(Math.floor(sh * 0.8), 1200),
@@ -128,7 +125,6 @@ app.whenReady().then(() => {
splashWindow.loadFile(splashPath);
}
setTimeout(() => openAppIfNotOpened(), 1000);
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
@@ -184,8 +180,8 @@ WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " &
}
ipcMain.handle('download-update', async (event, downloadUrl: string) => {
if (isDownloading) return { success: false, error: '正在下载中' };
if (downloadedFilePath === 'completed') return { success: true, filePath: 'already-completed' };
if (isDownloading) return {success: false, error: '正在下载中'};
if (downloadedFilePath === 'completed') return {success: true, filePath: 'already-completed'};
isDownloading = true;
@@ -212,7 +208,7 @@ ipcMain.handle('download-update', async (event, downloadUrl: string) => {
isDownloading = false;
}, 1100);
return { success: true, filePath: 'dev-mode-simulated', dev: true };
return {success: true, filePath: 'dev-mode-simulated', dev: true};
}
const tempPath = join(app.getPath('temp'), 'app.asar.new');
@@ -244,22 +240,23 @@ ipcMain.handle('download-update', async (event, downloadUrl: string) => {
downloadedFilePath = 'completed';
isDownloading = false;
return { success: true, filePath: updateFilePath };
return {success: true, filePath: updateFilePath};
} catch (error: unknown) {
isDownloading = false;
downloadedFilePath = null;
const tempPath = join(app.getPath('temp'), 'app.asar.new');
if (existsSync(tempPath)) {
fs.unlink(tempPath).catch(() => {});
fs.unlink(tempPath).catch(() => {
});
}
return { success: false, error: error instanceof Error ? error.message : '下载失败' };
return {success: false, error: error instanceof Error ? error.message : '下载失败'};
}
});
ipcMain.handle('get-download-progress', () => {
return { ...downloadProgress, isDownloading };
return {...downloadProgress, isDownloading};
});
ipcMain.handle('install-update', async () => {
@@ -268,14 +265,14 @@ ipcMain.handle('install-update', async () => {
if (isRealDev) {
downloadedFilePath = null;
return { success: true, message: '开发环境模拟重启' };
return {success: true, message: '开发环境模拟重启'};
}
const updateFilePath = join(process.resourcesPath, 'app.asar.update');
const hasUpdateFile = existsSync(updateFilePath);
if (!downloadedFilePath || (downloadedFilePath !== 'completed' && !hasUpdateFile)) {
return { success: false, error: '更新文件不存在' };
return {success: false, error: '更新文件不存在'};
}
const appDir = dirname(process.execPath);
@@ -283,7 +280,7 @@ ipcMain.handle('install-update', async () => {
const appAsarPath = join(process.resourcesPath, 'app.asar');
if (!existsSync(helperPath)) {
return { success: false, error: '更新助手不存在' };
return {success: false, error: '更新助手不存在'};
}
const vbsPath = join(app.getPath('temp'), 'update-install.vbs');
@@ -303,28 +300,28 @@ WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " &
app.quit();
}, 500);
return { success: true };
return {success: true};
} catch (error: unknown) {
return { success: false, error: error instanceof Error ? error.message : '重启失败' };
return {success: false, error: error instanceof Error ? error.message : '重启失败'};
}
});
ipcMain.handle('cancel-download', () => {
isDownloading = false;
downloadProgress = { percentage: 0, current: '0 MB', total: '0 MB', speed: '' };
downloadProgress = {percentage: 0, current: '0 MB', total: '0 MB', speed: ''};
downloadedFilePath = null;
return { success: true };
return {success: true};
});
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, isDev};
});
// 添加文件保存对话框处理器
ipcMain.handle('show-save-dialog', async (event, options) => {
if (!mainWindow) {
return { canceled: true };
return {canceled: true};
}
try {
@@ -332,14 +329,14 @@ ipcMain.handle('show-save-dialog', async (event, options) => {
return result;
} catch (error) {
console.error('文件保存对话框错误:', error);
return { canceled: true, error: error instanceof Error ? error.message : '对话框打开失败' };
return {canceled: true, error: error instanceof Error ? error.message : '对话框打开失败'};
}
});
// 添加文件夹选择对话框处理器
ipcMain.handle('show-open-dialog', async (event, options) => {
if (!mainWindow) {
return { canceled: true };
return {canceled: true};
}
try {
@@ -347,7 +344,7 @@ ipcMain.handle('show-open-dialog', async (event, options) => {
return result;
} catch (error) {
console.error('文件夹选择对话框错误:', error);
return { canceled: true, error: error instanceof Error ? error.message : '对话框打开失败' };
return {canceled: true, error: error instanceof Error ? error.message : '对话框打开失败'};
}
});
@@ -377,7 +374,7 @@ async function downloadFile(url: string, filePath: string, onProgress: (progress
const elapsed = (Date.now() - startTime) / 1000;
const speed = elapsed > 0 ? `${((downloadedBytes / elapsed) / 1024 / 1024).toFixed(1)} MB/s` : '';
onProgress({ percentage, current, total, speed });
onProgress({percentage, current, total, speed});
});
response.pipe(fileStream);
@@ -388,10 +385,11 @@ async function downloadFile(url: string, filePath: string, onProgress: (progress
});
fileStream.on('error', (error) => {
fs.unlink(filePath).catch(() => {});
fs.unlink(filePath).catch(() => {
});
reject(error);
});
}).on('error', reject);
});
}
}