fix(client): 设备移除逻辑与认证流程优化
- 修改设备移除时的本地清理方法,统一调用 clearLocalAuth - 优化设备数量限制校验逻辑,避免重复计算当前设备- 移除冗余的设备状态检查,简化设备移除流程- 调整 Redis 连接超时与等待时间,提升连接稳定性- 增强 MySQL 数据库连接配置,添加自动重连机制 -优化 Druid 连接池参数,提高数据库连接性能 - 简化客户端认证与数据上报逻辑,提升处理效率 - 移除过期设备状态更新逻辑,减少不必要的数据库操作- 调整慢 SQL 记录阈值,便于及时发现性能问题-优化版本分布与数据类型统计查询逻辑,提高响应速度
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
import {app, BrowserWindow, ipcMain, Menu, screen, dialog} from 'electron';
|
||||
import {existsSync, createWriteStream, promises as fs, mkdirSync, copyFileSync} from 'fs';
|
||||
import {join, dirname} from 'path';
|
||||
import {existsSync, createWriteStream, promises as fs, mkdirSync, copyFileSync, readdirSync, writeFileSync} from 'fs';
|
||||
import {join, dirname, basename} from 'path';
|
||||
import {spawn, ChildProcess} from 'child_process';
|
||||
import * as https from 'https';
|
||||
import * as http from 'http';
|
||||
|
||||
const isDev = process.env.NODE_ENV === 'development';
|
||||
|
||||
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 downloadProgress = {percentage: 0, current: '0 MB', total: '0 MB'};
|
||||
let isDownloading = false;
|
||||
let downloadedFilePath: string | null = null;
|
||||
let downloadedAsarPath: string | null = null;
|
||||
let downloadedJarPath: string | null = null;
|
||||
function openAppIfNotOpened() {
|
||||
if (appOpened) return;
|
||||
appOpened = true;
|
||||
@@ -20,8 +25,8 @@ function openAppIfNotOpened() {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.show();
|
||||
mainWindow.focus();
|
||||
if (isDev) mainWindow.webContents.openDevTools();
|
||||
}
|
||||
// 安全关闭启动画面
|
||||
if (splashWindow && !splashWindow.isDestroyed()) {
|
||||
splashWindow.close();
|
||||
splashWindow = null;
|
||||
@@ -29,141 +34,75 @@ function openAppIfNotOpened() {
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
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'));
|
||||
}
|
||||
isDev
|
||||
? mainWindow.loadURL(`http://localhost:${process.argv[2] || 8083}`)
|
||||
: mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
|
||||
}
|
||||
}
|
||||
|
||||
// 通用资源路径获取函数
|
||||
function getResourcePath(devPath: string, prodPath: string, fallbackPath?: string): string {
|
||||
if (isDev) return join(__dirname, devPath);
|
||||
const bundledPath = join(process.resourcesPath, 'app.asar.unpacked', prodPath);
|
||||
if (existsSync(bundledPath)) return bundledPath;
|
||||
return fallbackPath ? join(__dirname, fallbackPath) : join(__dirname, prodPath);
|
||||
}
|
||||
|
||||
function getJavaExecutablePath(): string {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return 'java';
|
||||
}
|
||||
|
||||
const bundledJavaPath = join(process.resourcesPath, 'app.asar.unpacked/public/jre/bin/java.exe');
|
||||
if (existsSync(bundledJavaPath)) {
|
||||
return bundledJavaPath;
|
||||
}
|
||||
|
||||
const devJavaPath = join(__dirname, '../../public/jre/bin/java.exe');
|
||||
if (existsSync(devJavaPath)) {
|
||||
return devJavaPath;
|
||||
}
|
||||
|
||||
return 'java';
|
||||
if (isDev) return 'java';
|
||||
const javaPath = getResourcePath('', 'public/jre/bin/java.exe');
|
||||
return existsSync(javaPath) ? javaPath : 'java';
|
||||
}
|
||||
|
||||
function findJarFile(directory: string): string {
|
||||
if (!existsSync(directory)) return '';
|
||||
|
||||
const files = require('fs').readdirSync(directory);
|
||||
const jarFile = files.find((f: string) => f.startsWith('erp_client_sb-') && f.endsWith('.jar'));
|
||||
|
||||
const jarFile = readdirSync(directory).find((f: string) => f.startsWith('erp_client_sb-') && f.endsWith('.jar'));
|
||||
return jarFile ? join(directory, jarFile) : '';
|
||||
}
|
||||
|
||||
function extractVersionFromJar(jarPath: string): string {
|
||||
if (!jarPath) return '';
|
||||
const match = require('path').basename(jarPath).match(/erp_client_sb-(\d+\.\d+\.\d+)\.jar/);
|
||||
return match?.[1] || '';
|
||||
}
|
||||
|
||||
function getJarFilePath(): string {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return findJarFile(join(__dirname, '../../public'));
|
||||
}
|
||||
|
||||
const tempDir = join(app.getPath('temp'), 'erp-client');
|
||||
if (!existsSync(tempDir)) mkdirSync(tempDir, { recursive: true });
|
||||
|
||||
const asarJarPath = findJarFile(join(__dirname, '../assets'));
|
||||
if (!asarJarPath) return '';
|
||||
|
||||
const asarFileName = require('path').basename(asarJarPath);
|
||||
const tempJarPath = join(tempDir, asarFileName);
|
||||
|
||||
// 如果临时目录版本不同,删除旧版本并复制新版本
|
||||
const existingJar = findJarFile(tempDir);
|
||||
if (existingJar && require('path').basename(existingJar) !== asarFileName) {
|
||||
require('fs').unlinkSync(existingJar);
|
||||
}
|
||||
|
||||
if (!existsSync(tempJarPath)) {
|
||||
copyFileSync(asarJarPath, tempJarPath);
|
||||
}
|
||||
|
||||
return tempJarPath;
|
||||
if (isDev) return findJarFile(join(__dirname, '../../public'));
|
||||
return findJarFile(process.resourcesPath);
|
||||
}
|
||||
|
||||
function getSplashPath(): string {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return join(__dirname, '../../public/splash.html');
|
||||
}
|
||||
|
||||
const bundledSplashPath = join(process.resourcesPath, 'app.asar.unpacked/public/splash.html');
|
||||
if (existsSync(bundledSplashPath)) {
|
||||
return bundledSplashPath;
|
||||
}
|
||||
|
||||
return join(__dirname, '../../public/splash.html');
|
||||
}
|
||||
function getIconPath(): string {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return join(__dirname, '../../public/icon/icon.png');
|
||||
}
|
||||
const bundledIconPath = join(process.resourcesPath, 'app.asar.unpacked/public/icon/icon.png');
|
||||
if (existsSync(bundledIconPath)) {
|
||||
return bundledIconPath;
|
||||
}
|
||||
|
||||
return join(__dirname, '../renderer/icon/icon.png');
|
||||
}
|
||||
const getSplashPath = () => getResourcePath('../../public/splash.html', 'public/splash.html');
|
||||
const getIconPath = () => getResourcePath('../../public/icon/icon.png', 'public/icon/icon.png', '../renderer/icon/icon.png');
|
||||
const getLogbackConfigPath = () => getResourcePath('../../public/config/logback.xml', 'public/config/logback.xml');
|
||||
|
||||
function getDataDirectoryPath(): string {
|
||||
// 将用户数据目录放在可写的应用数据目录下
|
||||
const userDataPath = app.getPath('userData');
|
||||
const dataDir = join(userDataPath, 'data');
|
||||
|
||||
// 确保数据目录存在
|
||||
if (!existsSync(dataDir)) {
|
||||
mkdirSync(dataDir, { recursive: true });
|
||||
}
|
||||
|
||||
const dataDir = join(app.getPath('userData'), 'data');
|
||||
if (!existsSync(dataDir)) mkdirSync(dataDir, {recursive: true});
|
||||
return dataDir;
|
||||
}
|
||||
|
||||
function migrateDataFromPublic(): void {
|
||||
// 如果是首次运行,尝试从public/data迁移数据
|
||||
if (!isDev) return;
|
||||
|
||||
const oldDataPath = join(__dirname, '../../public/data');
|
||||
if (!existsSync(oldDataPath)) return;
|
||||
|
||||
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);
|
||||
}
|
||||
try {
|
||||
readdirSync(oldDataPath).forEach(file => {
|
||||
const destFile = join(newDataPath, file);
|
||||
if (!existsSync(destFile)) {
|
||||
copyFileSync(join(oldDataPath, file), destFile);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('数据迁移失败,使用默认配置');
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('数据迁移失败,使用默认配置');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function startSpringBoot() {
|
||||
migrateDataFromPublic();
|
||||
|
||||
const jarPath = getJarFilePath();
|
||||
const javaPath = getJavaExecutablePath();
|
||||
const dataDir = getDataDirectoryPath();
|
||||
|
||||
const logbackConfigPath = getLogbackConfigPath();
|
||||
|
||||
if (!existsSync(jarPath)) {
|
||||
dialog.showErrorBox('启动失败', `JAR 文件不存在:\n${jarPath}`);
|
||||
app.quit();
|
||||
@@ -175,19 +114,14 @@ function startSpringBoot() {
|
||||
const springArgs = [
|
||||
'-jar', jarPath,
|
||||
`--spring.datasource.url=jdbc:sqlite:${dataDir}/erp-cache.db`,
|
||||
`--logging.file.path=${dataDir}`,
|
||||
`--server.port=8081`
|
||||
`--server.port=8081`,
|
||||
`--logging.config=file:${logbackConfigPath}`
|
||||
];
|
||||
|
||||
// 工作目录设为数据目录,这样Spring Boot会在数据目录下创建临时文件
|
||||
springProcess = spawn(javaPath, springArgs, {
|
||||
cwd: dataDir,
|
||||
detached: false,
|
||||
env: {
|
||||
...process.env,
|
||||
'ERP_DATA_DIR': dataDir,
|
||||
'USER_DATA_DIR': dataDir
|
||||
}
|
||||
detached: false
|
||||
});
|
||||
|
||||
let startupCompleted = false;
|
||||
@@ -202,6 +136,16 @@ function startSpringBoot() {
|
||||
}
|
||||
});
|
||||
|
||||
springProcess.stderr?.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();
|
||||
}
|
||||
});
|
||||
|
||||
springProcess.on('close', (code) => {
|
||||
mainWindow ? mainWindow.close() : app.quit();
|
||||
});
|
||||
@@ -226,7 +170,9 @@ function startSpringBoot() {
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
startSpringBoot();
|
||||
|
||||
startSpringBoot();
|
||||
|
||||
function stopSpringBoot() {
|
||||
if (!springProcess) return;
|
||||
try {
|
||||
@@ -264,11 +210,6 @@ function createWindow() {
|
||||
Menu.setApplicationMenu(null);
|
||||
mainWindow.setMenuBarVisibility(false);
|
||||
|
||||
// 打开开发者工具
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
mainWindow.webContents.openDevTools();
|
||||
}
|
||||
|
||||
// 监听窗口关闭事件,确保正确清理引用
|
||||
mainWindow.on('closed', () => {
|
||||
mainWindow = null;
|
||||
@@ -334,14 +275,29 @@ ipcMain.on('message', (event, message) => {
|
||||
console.log(message);
|
||||
});
|
||||
|
||||
ipcMain.handle('get-jar-version', () => extractVersionFromJar(getJarFilePath()));
|
||||
ipcMain.handle('get-jar-version', () => {
|
||||
const jarPath = getJarFilePath();
|
||||
const match = jarPath ? basename(jarPath).match(/erp_client_sb-(\d+\.\d+\.\d+)\.jar/) : null;
|
||||
return match?.[1] || '';
|
||||
});
|
||||
|
||||
function checkPendingUpdate() {
|
||||
try {
|
||||
const updateFilePath = join(process.resourcesPath, 'app.asar.update');
|
||||
const asarUpdatePath = join(process.resourcesPath, 'app.asar.update');
|
||||
const appAsarPath = join(process.resourcesPath, 'app.asar');
|
||||
|
||||
if (!existsSync(updateFilePath)) return;
|
||||
// 查找jar更新文件
|
||||
let jarUpdatePath = '';
|
||||
if (process.resourcesPath && existsSync(process.resourcesPath)) {
|
||||
const files = readdirSync(process.resourcesPath);
|
||||
const jarUpdateFile = files.find(f => f.startsWith('erp_client_sb-') && f.endsWith('.jar.update'));
|
||||
if (jarUpdateFile) {
|
||||
jarUpdatePath = join(process.resourcesPath, jarUpdateFile);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有任何更新文件,直接返回
|
||||
if (!existsSync(asarUpdatePath) && !jarUpdatePath) return;
|
||||
|
||||
const appDir = dirname(process.execPath);
|
||||
const helperPath = join(appDir, 'update-helper.bat');
|
||||
@@ -353,9 +309,9 @@ function checkPendingUpdate() {
|
||||
|
||||
const vbsPath = join(app.getPath('temp'), 'update-silent.vbs');
|
||||
const vbsContent = `Set WshShell = CreateObject("WScript.Shell")
|
||||
WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${appAsarPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${updateFilePath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${process.execPath.replace(/\\/g, '\\\\')}" & Chr(34), 0, False`;
|
||||
WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${appAsarPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${asarUpdatePath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${jarUpdatePath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${process.execPath.replace(/\\/g, '\\\\')}" & Chr(34), 0, False`;
|
||||
|
||||
require('fs').writeFileSync(vbsPath, vbsContent);
|
||||
writeFileSync(vbsPath, vbsContent);
|
||||
|
||||
spawn('wscript.exe', [vbsPath], {
|
||||
detached: true,
|
||||
@@ -369,53 +325,86 @@ WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " &
|
||||
}
|
||||
}
|
||||
|
||||
ipcMain.handle('download-update', async (event, downloadUrl: string) => {
|
||||
ipcMain.handle('download-update', async (event, downloadUrls: {asarUrl?: string, jarUrl?: string}) => {
|
||||
if (isDownloading) return {success: false, error: '正在下载中'};
|
||||
if (downloadedFilePath === 'completed') return {success: true, filePath: 'already-completed'};
|
||||
|
||||
isDownloading = true;
|
||||
let totalDownloaded = 0;
|
||||
let totalSize = 0;
|
||||
|
||||
try {
|
||||
const tempPath = join(app.getPath('temp'), 'app.asar.new');
|
||||
// 获取总大小
|
||||
const sizes = await Promise.all([
|
||||
downloadUrls.asarUrl ? getFileSize(downloadUrls.asarUrl) : 0,
|
||||
downloadUrls.jarUrl ? getFileSize(downloadUrls.jarUrl) : 0
|
||||
]);
|
||||
totalSize = sizes[0] + sizes[1];
|
||||
|
||||
await downloadFile(downloadUrl, tempPath, (progress) => {
|
||||
downloadProgress = progress;
|
||||
if (mainWindow) {
|
||||
mainWindow.webContents.send('download-progress', progress);
|
||||
// 下载asar文件
|
||||
if (downloadUrls.asarUrl) {
|
||||
const tempAsarPath = join(app.getPath('temp'), 'app.asar.new');
|
||||
const asarSize = sizes[0];
|
||||
|
||||
await downloadFile(downloadUrls.asarUrl, tempAsarPath, (progress) => {
|
||||
const combinedProgress = {
|
||||
percentage: Math.round(((totalDownloaded + progress.downloaded) / totalSize) * 100),
|
||||
current: `${((totalDownloaded + progress.downloaded) / 1024 / 1024).toFixed(1)} MB`,
|
||||
total: `${(totalSize / 1024 / 1024).toFixed(1)} MB`
|
||||
};
|
||||
downloadProgress = combinedProgress;
|
||||
if (mainWindow) mainWindow.webContents.send('download-progress', combinedProgress);
|
||||
});
|
||||
|
||||
const asarUpdatePath = join(process.resourcesPath, 'app.asar.update');
|
||||
await fs.copyFile(tempAsarPath, asarUpdatePath);
|
||||
await fs.unlink(tempAsarPath);
|
||||
downloadedAsarPath = asarUpdatePath;
|
||||
totalDownloaded += asarSize;
|
||||
}
|
||||
|
||||
// 下载jar文件
|
||||
if (downloadUrls.jarUrl) {
|
||||
let jarFileName = basename(downloadUrls.jarUrl);
|
||||
if (!jarFileName.match(/^erp_client_sb-[\d.]+\.jar$/)) {
|
||||
const currentJar = getJarFilePath();
|
||||
const versionMatch = currentJar ? basename(currentJar).match(/erp_client_sb-([\d.]+)\.jar/) : null;
|
||||
if (versionMatch && versionMatch[1]) {
|
||||
const versionParts = versionMatch[1].split('.');
|
||||
versionParts[2] = String(Number(versionParts[2]) + 1);
|
||||
jarFileName = `erp_client_sb-${versionParts.join('.')}.jar`;
|
||||
} else {
|
||||
jarFileName = 'erp_client_sb-2.4.7.jar';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const tempJarPath = join(app.getPath('temp'), jarFileName);
|
||||
|
||||
if (!existsSync(tempPath)) {
|
||||
throw new Error('下载文件不存在');
|
||||
}
|
||||
await downloadFile(downloadUrls.jarUrl, tempJarPath, (progress) => {
|
||||
const combinedProgress = {
|
||||
percentage: Math.round(((totalDownloaded + progress.downloaded) / totalSize) * 100),
|
||||
current: `${((totalDownloaded + progress.downloaded) / 1024 / 1024).toFixed(1)} MB`,
|
||||
total: `${(totalSize / 1024 / 1024).toFixed(1)} MB`
|
||||
};
|
||||
downloadProgress = combinedProgress;
|
||||
if (mainWindow) mainWindow.webContents.send('download-progress', combinedProgress);
|
||||
});
|
||||
|
||||
const fileStats = await fs.stat(tempPath);
|
||||
if (fileStats.size < 1000) {
|
||||
throw new Error('下载文件过小');
|
||||
}
|
||||
|
||||
const updateFilePath = join(process.resourcesPath, 'app.asar.update');
|
||||
await fs.copyFile(tempPath, updateFilePath);
|
||||
await fs.unlink(tempPath);
|
||||
|
||||
if (!existsSync(updateFilePath)) {
|
||||
throw new Error('更新文件保存失败');
|
||||
const jarUpdatePath = join(process.resourcesPath, jarFileName + '.update');
|
||||
await fs.copyFile(tempJarPath, jarUpdatePath);
|
||||
await fs.unlink(tempJarPath);
|
||||
downloadedJarPath = jarUpdatePath;
|
||||
}
|
||||
|
||||
downloadedFilePath = 'completed';
|
||||
isDownloading = false;
|
||||
|
||||
return {success: true, filePath: updateFilePath};
|
||||
return {success: true, asarPath: downloadedAsarPath, jarPath: downloadedJarPath};
|
||||
} catch (error: unknown) {
|
||||
isDownloading = false;
|
||||
downloadedFilePath = null;
|
||||
|
||||
const tempPath = join(app.getPath('temp'), 'app.asar.new');
|
||||
if (existsSync(tempPath)) {
|
||||
fs.unlink(tempPath).catch(() => {
|
||||
});
|
||||
}
|
||||
|
||||
downloadedAsarPath = null;
|
||||
downloadedJarPath = null;
|
||||
return {success: false, error: error instanceof Error ? error.message : '下载失败'};
|
||||
}
|
||||
});
|
||||
@@ -426,26 +415,37 @@ ipcMain.handle('get-download-progress', () => {
|
||||
|
||||
ipcMain.handle('install-update', async () => {
|
||||
try {
|
||||
const updateFilePath = join(process.resourcesPath, 'app.asar.update');
|
||||
const hasUpdateFile = existsSync(updateFilePath);
|
||||
const asarUpdatePath = join(process.resourcesPath, 'app.asar.update');
|
||||
const hasAsarUpdate = existsSync(asarUpdatePath);
|
||||
|
||||
// 查找jar更新文件
|
||||
let jarUpdatePath = '';
|
||||
if (process.resourcesPath && existsSync(process.resourcesPath)) {
|
||||
const files = readdirSync(process.resourcesPath);
|
||||
const jarUpdateFile = files.find(f => f.startsWith('erp_client_sb-') && f.endsWith('.jar.update'));
|
||||
if (jarUpdateFile) {
|
||||
jarUpdatePath = join(process.resourcesPath, jarUpdateFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (!downloadedFilePath || (downloadedFilePath !== 'completed' && !hasUpdateFile)) {
|
||||
if (!hasAsarUpdate && !jarUpdatePath) {
|
||||
return {success: false, error: '更新文件不存在'};
|
||||
}
|
||||
|
||||
const appDir = dirname(process.execPath);
|
||||
const helperPath = join(appDir, 'update-helper.bat');
|
||||
const appAsarPath = join(process.resourcesPath, 'app.asar');
|
||||
|
||||
if (!existsSync(helperPath)) {
|
||||
return {success: false, error: '更新助手不存在'};
|
||||
}
|
||||
|
||||
const appAsarPath = join(process.resourcesPath, 'app.asar');
|
||||
|
||||
const vbsPath = join(app.getPath('temp'), 'update-install.vbs');
|
||||
const vbsContent = `Set WshShell = CreateObject("WScript.Shell")
|
||||
WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${appAsarPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${updateFilePath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${process.execPath.replace(/\\/g, '\\\\')}" & Chr(34), 0, False`;
|
||||
WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${appAsarPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${asarUpdatePath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${jarUpdatePath.replace(/\\/g, '\\\\')}" & Chr(34) & " " & Chr(34) & "${process.execPath.replace(/\\/g, '\\\\')}" & Chr(34), 0, False`;
|
||||
|
||||
require('fs').writeFileSync(vbsPath, vbsContent);
|
||||
writeFileSync(vbsPath, vbsContent);
|
||||
|
||||
spawn('wscript.exe', [vbsPath], {
|
||||
detached: true,
|
||||
@@ -455,6 +455,8 @@ WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " &
|
||||
|
||||
setTimeout(() => {
|
||||
downloadedFilePath = null;
|
||||
downloadedAsarPath = null;
|
||||
downloadedJarPath = null;
|
||||
app.quit();
|
||||
}, 500);
|
||||
|
||||
@@ -466,8 +468,10 @@ WshShell.Run Chr(34) & "${helperPath.replace(/\\/g, '\\\\')}" & Chr(34) & " " &
|
||||
|
||||
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'};
|
||||
downloadedFilePath = null;
|
||||
downloadedAsarPath = null;
|
||||
downloadedJarPath = null;
|
||||
return {success: true};
|
||||
});
|
||||
|
||||
@@ -492,7 +496,17 @@ ipcMain.handle('write-file', async (event, filePath: string, data: Uint8Array) =
|
||||
});
|
||||
|
||||
|
||||
async function downloadFile(url: string, filePath: string, onProgress: (progress: any) => void): Promise<void> {
|
||||
async function getFileSize(url: string): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const protocol = url.startsWith('https') ? https : http;
|
||||
protocol.get(url, {method: 'HEAD'}, (response) => {
|
||||
const size = parseInt(response.headers['content-length'] || '0', 10);
|
||||
resolve(size);
|
||||
}).on('error', () => resolve(0));
|
||||
});
|
||||
}
|
||||
|
||||
async function downloadFile(url: string, filePath: string, onProgress: (progress: {downloaded: number}) => void): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const protocol = url.startsWith('https') ? https : http;
|
||||
|
||||
@@ -502,22 +516,12 @@ async function downloadFile(url: string, filePath: string, onProgress: (progress
|
||||
return;
|
||||
}
|
||||
|
||||
const totalBytes = parseInt(response.headers['content-length'] || '0', 10);
|
||||
let downloadedBytes = 0;
|
||||
const startTime = Date.now();
|
||||
|
||||
const fileStream = createWriteStream(filePath);
|
||||
|
||||
response.on('data', (chunk) => {
|
||||
downloadedBytes += chunk.length;
|
||||
|
||||
const percentage = totalBytes > 0 ? Math.round((downloadedBytes / totalBytes) * 100) : 0;
|
||||
const current = `${(downloadedBytes / 1024 / 1024).toFixed(1)} MB`;
|
||||
const total = `${(totalBytes / 1024 / 1024).toFixed(1)} MB`;
|
||||
const elapsed = (Date.now() - startTime) / 1000;
|
||||
const speed = elapsed > 0 ? `${((downloadedBytes / elapsed) / 1024 / 1024).toFixed(1)} MB/s` : '';
|
||||
|
||||
onProgress({percentage, current, total, speed});
|
||||
onProgress({downloaded: downloadedBytes});
|
||||
});
|
||||
|
||||
response.pipe(fileStream);
|
||||
@@ -528,8 +532,7 @@ async function downloadFile(url: string, filePath: string, onProgress: (progress
|
||||
});
|
||||
|
||||
fileStream.on('error', (error) => {
|
||||
fs.unlink(filePath).catch(() => {
|
||||
});
|
||||
fs.unlink(filePath).catch(() => {});
|
||||
reject(error);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user