欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > electron-vite_10electron-updater软件更新

electron-vite_10electron-updater软件更新

2024/10/24 2:03:02 来源:https://blog.csdn.net/u014708123/article/details/142849299  浏览:    关键词:electron-vite_10electron-updater软件更新

网很多electron-updater更新文章,这里只简单写一下演示代码;

为什么选择

electron-updater插件可以自动更新应用程序,同时支持多个平台;比官方要强;
官方的autoUpdater仅支持macOS 和 Windows 自动更新; 注意是自动,直接更新那种;

脚手架中是含有的electron-updater的
// 安装依赖如果你没有的话
npm i electron-updater
// 引入项目
const { autoUpdater } = require('electron-updater');
// 配置项
autoUpdater.forceDevUpdateConfig = true //开发环境下强制更新
autoUpdater.autoDownload = false; // 自动下载
autoUpdater.autoInstallOnAppQuit = true; // 应用退出后自动安装
封装一个简单方法
/****************  检查版本更新 start ***************** autoUpdater.checkForUpdates(); 检测是否最新版* autoUpdater.downloadUpdate(); 下载最新版
*/
// 检查是否更新
// 是否开启了检测
let isStartCheckForUpdates = false;
let customDialog: any = null;
function checkForUpdates(mainWindow, url?: string) {if (isStartCheckForUpdates) {return;}isStartCheckForUpdates = true;if (typeof url !== 'string') {url = 'https://xxx.cn/yyhDesktopUpdate/'}// 指定更新地址autoUpdater.setFeedURL(url);autoUpdater.autoDownload = false;autoUpdater.checkForUpdates();// 开始检查更新事件; 提示语: '正在检查更新';autoUpdater.on('checking-for-update', function () {});// 发现可更新版本; 提示语: 检测到新版本,准备下载autoUpdater.on('update-available', function () {if (customDialog) {customDialog.close();customDialog = null;}// 系统原生弹窗让用户选择是否更新customDialog = dialog.showMessageBox(mainWindow, {type: 'info',title: '检测到有新版本',message: '检测到新版本,是否立即更新?',buttons: ['确定', '取消'],cancelId: 1, // 指定“取消”按钮的索引}).then((result) => {customDialog = null;if (result.response === 0) {// 下载最新版autoUpdater.downloadUpdate();} else { // 用户点击了“取消”}isStartCheckForUpdates = false;}).catch(() => {customDialog = null;isStartCheckForUpdates = false;});});// 没有可更新版本事件; 提示语: '已经是最新版本';autoUpdater.on('update-not-available', function () {isStartCheckForUpdates = false;});// 更新发生错误时事件; 提示语: '软件更新异常,请重试';autoUpdater.on('error', function () {if (customDialog) {customDialog.close();customDialog = null;}// 提示用户更新失败customDialog = dialog.showMessageBox({type: 'info',title: '更新检查',message: '软件更新异常,请重试!',buttons: ['好的'],}).then(() => {customDialog = null;});});// 更新下载完毕后事件; 提示语: '下载完成,准备安装';autoUpdater.on('update-downloaded', function () {if (customDialog) {customDialog.close();customDialog = null;}// 下载后安装更新customDialog = dialog.showMessageBox({type: 'info',title: '更新完成',message: '更新已下载完成,是否立即安装应用?',buttons: ['立即安装'],}).then((result) => {customDialog = null;if (result.response === 0) {autoUpdater.quitAndInstall();}}).catch(() => {customDialog = null;});});// 更新下载进度事件; 提示语: '软件下载中,请耐心等待';autoUpdater.on('download-progress', function (progressObj) {/** progressObj {total: 1000000, // 总字节数,例如 1MBdelta: 1024, // 自上次事件以来下载的字节数,例如 1KBtransferred: 512000, // 已下载的总字节数,例如 512KBpercent: 0.5, // 下载进度的百分比,例如 50%bytesPerSecond: 2048 // 当前下载速度,例如 2KB/s}*/// 使用原生进度条if (progressObj.percent !== undefined) {mainWindow.setProgressBar(progressObj.percent);} else {mainWindow.setProgressBar(-1);}});
}
/****************  检查版本更新 end *****************/
调用
mainWindow.on('ready-to-show', () => {mainWindow.webContents.closeDevTools();mainWindow.show()// 调用检测方法checkForUpdates(mainWindow);})
软件更新win下载到哪个文件夹了
C:\Users\Administrator\AppData\Local
判断当前是那个系统_如果做系统差异化说不定用得到
/* 判断当前是那个系统* 如果 process.platform 返回的是 'darwin',则表示当前操作系统是 macOS。* 如果 process.platform 返回的是 'win32',则表示当前操作系统是 Windows。- 如果 process.arch 返回的是 'ia32',则表示当前系统是 32 位。- 如果 process.arch 返回的是 'x64',则表示当前系统是 64 位。* 如果 process.platform 返回的是 'linux',则表示当前操作系统是 Linux。
*/

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com