// 注意这个autoUpdater不是electron中的autoUpdater
|
import { autoUpdater } from "electron-updater"
|
import { dialog } from 'electron'
|
// import log from 'electron-log';
|
|
function handleUpdate(sender) {
|
const returnData = {
|
error: { status: -1, msg: '检测更新查询异常' },
|
checking: { status: 0, msg: '正在检查应用程序更新' },
|
updateAva: { status: 1, msg: '检测到新版本,正在下载,请稍后' },
|
updateNotAva: { status: 2, msg: '您现在使用的版本为最新版本,无需更新!' },
|
};
|
|
// 清空事件绑定 避免重复触发
|
autoUpdater._events = {};
|
|
// 设置是否自动下载,默认是true,当点击检测到新版本时,会自动下载安装包,所以设置为false,默认为true,点击立即更新以后直接检测是否有新版本,然后进行安装
|
autoUpdater.autoDownload = false
|
|
/*
|
重点说明服务器的地址的内容,内容是打包以后生成的latest.yml,把这个文件放到服务器上,同时和你打包以后的安装包同级目录,这样latest.yml中的地址才能读取到
|
*/
|
let updataPath = 'http://118.89.139.230:9098/res-update'
|
autoUpdater.setFeedURL({
|
provider: 'generic',
|
url: updataPath
|
});
|
// autoUpdater.setFeedURL(updataPath);
|
|
|
//更新错误
|
autoUpdater.on('error', function (error) {
|
sendUpdateMessage(sender, returnData.error)
|
});
|
|
//检查中
|
autoUpdater.on('checking-for-update', function () {
|
sendUpdateMessage(sender, returnData.checking);
|
});
|
|
//发现新版本
|
autoUpdater.on('update-available', function (info) {
|
const options = {
|
type: 'info',
|
buttons: ['确定', '取消'],
|
title: '更新提示',
|
// ${info.version} Cannot read property 'version' of undefined
|
message: `发现有新版本'${info.version}',是否下载?`,
|
cancelId: 1
|
}
|
dialog.showMessageBox(options).then(res => {
|
// log.info(JSON.stringify(res));
|
if (res.response === 0) {
|
autoUpdater.downloadUpdate()
|
} else {
|
return;
|
}
|
})
|
});
|
|
//当前版本为最新版本
|
autoUpdater.on('update-not-available', function (info) {
|
setTimeout(function () {
|
sendUpdateMessage(sender, returnData.updateNotAva)
|
}, 1000);
|
});
|
|
// 更新下载进度事件
|
autoUpdater.on('download-progress', function (progressObj) {
|
// log.info(progressObj.percent, 'p?')
|
sender.send('autodownloadprogress', progressObj)
|
});
|
|
//下载完成后重新安装
|
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
|
sender.send('isUpdateNow', { event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate })
|
// autoUpdater.quitAndInstall();
|
});
|
//执行自动更新检查
|
autoUpdater.checkForUpdates();
|
// autoUpdater.checkForUpdatesAndNotify()
|
}
|
// 通过main进程发送事件给renderer进程,提示更新信息
|
function sendUpdateMessage(sender, text) {
|
sender.send('messageupdate', text)
|
}
|
|
export default handleUpdate
|