From f9012419882fd44a6e95945f42b68f1a35cfd48e Mon Sep 17 00:00:00 2001 From: gaoshuaixing <530353222@qq.com> Date: Sat, 28 Dec 2024 16:42:07 +0800 Subject: [PATCH] chore: autoupdater --- electron/config/config.default.js | 10 ++ electron/service/os/auto_updater.js | 166 ++++++++++++++++++ frontend/src/router/routerMap.js | 5 - frontend/src/router/subMenu.js | 8 +- .../src/views/framework/updater/Index.vue | 86 --------- 5 files changed, 177 insertions(+), 98 deletions(-) create mode 100644 electron/service/os/auto_updater.js delete mode 100644 frontend/src/views/framework/updater/Index.vue diff --git a/electron/config/config.default.js b/electron/config/config.default.js index 6667596..ac87532 100644 --- a/electron/config/config.default.js +++ b/electron/config/config.default.js @@ -64,5 +64,15 @@ module.exports = () => { mainServer: { indexPath: '/public/dist/index.html', }, + customize: { + tray: { + title: 'EE程序', + icon: '/public/images/tray.png' + }, + awaken: { + protocol: 'ee', + args: [] + } + }, } } diff --git a/electron/service/os/auto_updater.js b/electron/service/os/auto_updater.js new file mode 100644 index 0000000..84135f1 --- /dev/null +++ b/electron/service/os/auto_updater.js @@ -0,0 +1,166 @@ +const { app: electronApp } = require('electron'); +const { autoUpdater } = require("electron-updater"); +const { is } = require('ee-core/utils'); +const { logger } = require('ee-core/log'); +const { getConfig } = require('ee-core/config'); +const { getMainWindow, setCloseAndQuit } = require('ee-core/electron/window'); + +/** + * 自动升级 + * @class + */ +class AutoUpdater { + + constructor() { + } + + /** + * 创建 + */ + create () { + logger.info('[addon:autoUpdater] load'); + const cfg = getConfig().customize.autoUpdater; + if ((is.windows() && cfg.windows) + || (is.macOS() && cfg.macOS) + || (is.linux() && cfg.linux)) + { + // continue + } else { + return + } + + // 是否检查更新 + if (cfg.force) { + this.checkUpdate(); + } + + const status = { + error: -1, + available: 1, + noAvailable: 2, + downloading: 3, + downloaded: 4, + } + + const version = electronApp.getVersion(); + logger.info('[addon:autoUpdater] current version: ', version); + + // 设置下载服务器地址 + let server = cfg.options.url; + let lastChar = server.substring(server.length - 1); + server = lastChar === '/' ? server : server + "/"; + cfg.options.url = server; + + // 是否后台自动下载 + autoUpdater.autoDownload = cfg.force ? true : false; + + try { + autoUpdater.setFeedURL(cfg.options); + } catch (error) { + logger.error('[addon:autoUpdater] setFeedURL error : ', error); + } + + autoUpdater.on('checking-for-update', () => { + //sendStatusToWindow('正在检查更新...'); + }) + autoUpdater.on('update-available', (info) => { + info.status = status.available; + info.desc = '有可用更新'; + this.sendStatusToWindow(info); + }) + autoUpdater.on('update-not-available', (info) => { + info.status = status.noAvailable; + info.desc = '没有可用更新'; + this.sendStatusToWindow(info); + }) + autoUpdater.on('error', (err) => { + const info = { + status: status.error, + desc: err + } + this.sendStatusToWindow(info); + }) + autoUpdater.on('download-progress', (progressObj) => { + let percentNumber = parseInt(progressObj.percent); + let totalSize = this.bytesChange(progressObj.total); + let transferredSize = this.bytesChange(progressObj.transferred); + let text = '已下载 ' + percentNumber + '%'; + text = text + ' (' + transferredSize + "/" + totalSize + ')'; + + let info = { + status: status.downloading, + desc: text, + percentNumber: percentNumber, + totalSize: totalSize, + transferredSize: transferredSize + } + logger.info('[addon:autoUpdater] progress: ', text); + this.sendStatusToWindow(info); + }) + autoUpdater.on('update-downloaded', (info) => { + info.status = status.downloaded; + info.desc = '下载完成'; + this.sendStatusToWindow(info); + + // 托盘插件里面设置了阻止窗口关闭,这里设置允许关闭窗口 + setCloseAndQuit(true); + + // Install updates and exit the application + autoUpdater.quitAndInstall(); + }); + } + + /** + * 检查更新 + */ + checkUpdate () { + autoUpdater.checkForUpdates(); + } + + /** + * 下载更新 + */ + download () { + autoUpdater.downloadUpdate(); + } + + /** + * 向前端发消息 + */ + sendStatusToWindow(content = {}) { + const textJson = JSON.stringify(content); + const channel = 'custom.app.updater'; + const win = getMainWindow(); + win.webContents.send(channel, textJson); + } + + /** + * 单位转换 + */ + bytesChange (limit) { + let size = ""; + if(limit < 0.1 * 1024){ + size = limit.toFixed(2) + "B"; + }else if(limit < 0.1 * 1024 * 1024){ + size = (limit/1024).toFixed(2) + "KB"; + }else if(limit < 0.1 * 1024 * 1024 * 1024){ + size = (limit/(1024 * 1024)).toFixed(2) + "MB"; + }else{ + size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB"; + } + + let sizeStr = size + ""; + let index = sizeStr.indexOf("."); + let dou = sizeStr.substring(index + 1 , index + 3); + if(dou == "00"){ + return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5); + } + + return size; + } +} + +AutoUpdater.toString = () => '[class AutoUpdater]'; +module.exports = { + autoUpdater: new AutoUpdater() +}; \ No newline at end of file diff --git a/frontend/src/router/routerMap.js b/frontend/src/router/routerMap.js index 8aebe1e..8113d83 100644 --- a/frontend/src/router/routerMap.js +++ b/frontend/src/router/routerMap.js @@ -41,11 +41,6 @@ const constantRouterMap = [ name: 'FrameworkJobsIndex', component: () => import('@/views/framework/jobs/Index.vue') }, - { - path: '/framework/updater/index', - name: 'FrameworkUpdaterIndex', - component: () => import('@/views/framework/updater/Index.vue') - }, { path: '/framework/software/index', name: 'FrameworkSoftwareIndex', diff --git a/frontend/src/router/subMenu.js b/frontend/src/router/subMenu.js index ebf695f..3104a03 100644 --- a/frontend/src/router/subMenu.js +++ b/frontend/src/router/subMenu.js @@ -30,13 +30,7 @@ export default { title: '任务', pageName: 'FrameworkJobsIndex', params: {} - }, - 'menu_106' : { - icon: 'profile', - title: '自动更新', - pageName: 'FrameworkUpdaterIndex', - params: {} - }, + }, 'menu_107' : { icon: 'profile', title: '软件调用', diff --git a/frontend/src/views/framework/updater/Index.vue b/frontend/src/views/framework/updater/Index.vue deleted file mode 100644 index e37bfe3..0000000 --- a/frontend/src/views/framework/updater/Index.vue +++ /dev/null @@ -1,86 +0,0 @@ - - -