chore: autoupdater

This commit is contained in:
gaoshuaixing
2024-12-28 16:42:07 +08:00
parent c39067953b
commit f901241988
5 changed files with 177 additions and 98 deletions

View File

@@ -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: []
}
},
}
}

View File

@@ -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()
};

View File

@@ -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',

View File

@@ -30,13 +30,7 @@ export default {
title: '任务',
pageName: 'FrameworkJobsIndex',
params: {}
},
'menu_106' : {
icon: 'profile',
title: '自动更新',
pageName: 'FrameworkUpdaterIndex',
params: {}
},
},
'menu_107' : {
icon: 'profile',
title: '软件调用',

View File

@@ -1,86 +0,0 @@
<template>
<div id="app-demo-window">
<div class="one-block-1">
<span>
1. 自动更新
</span>
</div>
<div class="one-block-2">
<a-space>
<a-button @click="checkForUpdater()">检查更新</a-button>
<a-button @click="download()">下载并安装</a-button>
</a-space>
</div>
<div class="one-block-1">
<span>
2. 下载进度
</span>
</div>
<div class="one-block-2">
<a-progress :percent="percentNumber" status="active" />
<a-space>
{{ progress }}
</a-space>
</div>
</div>
</template>
<script>
import { ipcApiRoute, specialIpcRoute } from '@/api';
import { ipc } from '@/utils/ipcRenderer';
export default {
data() {
return {
status: 0, // -1:异常1有可用更新2没有可用更新3下载中, 4下载完成
progress: '',
percentNumber: 0
};
},
mounted () {
this.init();
},
methods: {
init () {
ipc.removeAllListeners(specialIpcRoute.appUpdater);
ipc.on(specialIpcRoute.appUpdater, (event, result) => {
result = JSON.parse(result);
this.status = result.status;
if (result.status == 3) {
this.progress = result.desc;
this.percentNumber = result.percentNumber;
} else {
this.$message.info(result.desc);
}
})
},
checkForUpdater () {
ipc.invoke(ipcApiRoute.framework.checkForUpdater).then(r => {
console.log(r);
})
},
download () {
if (this.status !== 1) {
this.$message.info('没有可用版本');
return
}
ipc.invoke(ipcApiRoute.framework.downloadApp).then(r => {
console.log(r);
})
},
}
};
</script>
<style lang="less" scoped>
#app-demo-window {
padding: 0px 10px;
text-align: left;
width: 100%;
.one-block-1 {
font-size: 16px;
padding-top: 10px;
}
.one-block-2 {
padding-top: 10px;
}
}
</style>