当前位置: 首页 > article >正文

超详细!!!electron-vite-vue开发桌面应用之应用更新版本提示(十三)

云风网
云风笔记
云风知识库

当项目应用包更新后应该主动提示用户更新,这是采用electron-updater进行更新提示

一、安装依赖

npm i electron-updater  

二、配置安装包更新地址

electron-builder.json5添加配置

// 配置安装包更新地址
  publish: [
    {
      provider: "generic",
      url: "http://www.niech.cn/everyDayNote",
    },
  ]

三、新建封装版本管理工具electron/version.ts

import { autoUpdater } from 'electron-updater';
import { dialog, BrowserWindow } from 'electron';

const updateUrl = 'http://www.niech.cn/everyDayNote';

/**检测更新 */
export const checkUpdate = (win: BrowserWindow) => {
  console.log('开始检测');

  // 设置更新检测的资源路径,会检测对应路径下的 last.yaml文件中的版本信息 上线后确保该文件能正常访问
  if (process.platform == 'darwin') {
    autoUpdater.setFeedURL(`${updateUrl}/mac`);
    return;
  } else {
    autoUpdater.setFeedURL(`${updateUrl}/win`);
  }

  //检测更新
  autoUpdater.checkForUpdates();

  //监听'error'事件
  autoUpdater.on('error', err => {
    console.log('出错拉' + err);
    dialog.showErrorBox('更新出错拉!', err.message);
  });

  //监听'update-available'事件,发现有新版本时触发
  autoUpdater.on('update-available', () => {
    console.log('found new version');
    dialog.showMessageBox({
      message: '发现新版本,正在下载安装包'
    });
  });

  // 更新包下载百分比回调
  autoUpdater.on('download-progress', function (progressObj) {
    if (win) {
      win.webContents.send('download-progress', progressObj);
    }
  });

  //默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false
  // autoUpdater.autoDownload = false;

  //监听'update-downloaded'事件,新版本下载完成时触发
  autoUpdater.on('update-downloaded', () => {
    dialog
      .showMessageBox({
        type: 'info',
        title: '应用更新',
        message: '需要退出程序才能安装新版本,是否安装?',
        buttons: ['是', '否']
      })
      .then(buttonIndex => {
        if (buttonIndex.response == 0) {
          //选择是,则退出程序,安装新版本
          autoUpdater.quitAndInstall();

        }
      });
  });
};

四、主进程main.ts调用更新校检

/**
* 版本更新检测
*/
ipcMain.handle("check-update",(e:any)=>{
  // 获取发送通知的渲染进程窗口
  const currentWin = getWindowByEvent(e);
  // 升级校验
  checkUpdate(currentWin);
});


/**
 * 通过窗口事件获取发送者的窗口
 * @param event ipc发送窗口事件
 */
function getWindowByEvent(event: IpcMainEvent): BrowserWindow {
  const webContentsId = event.sender.id;
  for (const currentWin of BrowserWindow.getAllWindows()) {
    if (currentWin.webContents.id === webContentsId) {
      return currentWin;
    }
  }
  return null;
}

utils/electron.ts添加工具方法定义

/**
 * 新建窗口
 * @param path 路由地址
 */
export function openWindow(path: string) {
  window.ipcRenderer.invoke("open-win", path);
}
/**
 * 检查版本更新
 */
export function checkUpdate(){
  window.ipcRenderer.invoke("check-update");
}
export default {
  openWindow,
  checkUpdate
};

http://www.kler.cn/news/293994.html

相关文章:

  • 数据集火焰检测 >> DataBall
  • 搭贝低代码平台在零售管理中的应用:推动企业快速数据化转型
  • Node.js应用的高效部署与运维:从流程自动化到精细化监控
  • Excel中.xls和.xlsx文件格式的区别,及C++操作Excel文件
  • 2024年全国大学生数学建模C题完整论文
  • SQL治理经验谈:索引覆盖
  • 数据结构(1)
  • LIN协议栈 AUTOSAR架构下 状态管理
  • Matplotlib通过axis()配置坐标轴数据详解
  • JavaEE(3)
  • 【debug】dpkg: error processing archive...Invalid cross-device link
  • pgrx在docker中问题无法解决
  • gitlab 启动/关闭/启用开机启动/禁用开机启动
  • 关于HTTP SESSION
  • 算法复盘——Leetcode hot100: 双指针算法
  • 软件测试基础总结+面试八股文
  • Vue2电商项目(二) Home模块的开发;(还需要补充js节流和防抖的回顾链接)
  • 数据结构(单向链表)
  • 软文发稿相比其他广告形式有哪些持续性优势?
  • 如何从硬盘恢复已删除/丢失的文件?硬盘恢复已删除的文件技巧
  • 如何录制黑神话悟空的游戏BGM导入iPhone手机制作铃声?
  • notepad下载安装使用以及高级使用技巧
  • Vue 中 nextTick 的最主要作用是什么,为什么要有这个 API
  • spring项目使用邮箱验证码校验
  • Vue3状态管理Pinia
  • APS开源源码解读: 排程工具 optaplanner
  • PHP批量修改MySQL数据表字符集为utf8mb4/utf8mb4_unicode_ci
  • 全网首发!!!opencv三通道Mat点云转halcon点云—HTuple类型
  • linux编译出现报错
  • ★ 算法OJ题 ★ 力扣3 - 无重复字符的最长子串