新增在线更新功能

This commit is contained in:
gyq
2024-09-04 09:08:32 +08:00
parent 86c8ca6472
commit d57cecd91d
7 changed files with 16057 additions and 9 deletions

View File

@@ -1,6 +1,10 @@
import path from "path";
import { app, BrowserWindow, ipcMain } from "electron";
import axios from "axios";
import os from "os";
import fs from "fs";
import { exec } from "child_process";
let win;
app.whenReady().then(() => {
@@ -28,6 +32,56 @@ app.whenReady().then(() => {
win.loadFile(path.resolve(__dirname, "../dist/index.html")); // 打包后使用文件路径访问应用
}
const installExe = async (exePath) => {
return new Promise((resolve, reject) => {
exec(`start ${exePath}`, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
});
});
};
ipcMain.on("downloadFile", async (event, arg) => {
let _parmas = JSON.parse(arg);
axios({
url: _parmas.url,
method: "get",
responseType: "arraybuffer",
onDownloadProgress: (propessEvent) => {
// 更新进度条
const propress = Math.round(
(propessEvent.loaded / propessEvent.total) * 100
);
win.webContents.send("updateProgress", propress);
},
})
.then(async (response) => {
try {
const tempFilePath = path.join(
app.getPath("temp"),
"temp-exe-file.exe"
);
fs.writeFileSync(tempFilePath, response.data);
setTimeout(() => {
win = null;
app.exit();
}, 1000);
const installResult = await installExe(tempFilePath);
console.log(`安装结果:${installResult}`);
} catch (error) {
console.log("error", error);
}
})
.catch((err) => {
console.log("下载失败", JSON.stringify(err));
});
});
app.on("activate", () => {
// 在 macOS 系统内, 如果没有已开启的应用窗口
// 点击托盘图标时通常会重新创建一个新窗口