ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

electron自动更新

2022-07-04 20:03:16  阅读:201  来源: 互联网

标签:cmd update 更新 autoUpdater electron 自动更新 arg message


1.主要是删除package.json中dependencies节点的"electron-updater": "^4.6.5",这个版本的包,node写文件有问题
2.devDependencies中的electron-updater降低版本,由4.6.5降到3.0.0
3.然后就是利用electron的ipc通信,实现更新进度和渲染进程的交互
4.具体更新代码如下:

/* eslint-disable */
// 注意这个autoUpdater不是electron中的autoUpdater
import { ipcMain } from 'electron'
import { autoUpdater } from 'electron-updater'
import config from '../../../package.json'
let feedUrl = config.build.publish[0].url // 获取在package.json里面设定的服务器安装包存放地址publish

// 检测更新,在你想要检查更新的时候执行,renderer事件触发后的操作自行编写
export function updateHandle(mainWindow) {

    let message = {
        error: '检查更新出错',
        checking: '正在检查更新……',
        updateAva: '检测到新版本,正在下载……',
        updateNotAva: '现在使用的就是最新版本,不用更新'
    }
    
    autoUpdater.setFeedURL(feedUrl)

    // 错误
    autoUpdater.on('error', function (error) {
        sendUpdateMessage(mainWindow, {cmd: "error", message: error})
    })

    // 检查是否可以更新
    autoUpdater.on('checking-for-update', function (info) {
        sendUpdateMessage(mainWindow, {cmd: "checking-for-update", message: info})
    })

    // 有更新
    autoUpdater.on('update-available', function (info) {
        sendUpdateMessage(mainWindow, {cmd: "update-available", message: info})
    })

    // 无更新
    autoUpdater.on('update-not-available', function (info) {
        sendUpdateMessage(mainWindow, {cmd: "update-not-available", message: info})
    })

    // 更新下载进度事件
    autoUpdater.on('download-progress', function (progressObj) {
        sendUpdateMessage(mainWindow, {cmd: "download-progress", message: progressObj})
    })
    // 完成下载
    autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl) {
        sendUpdateMessage(mainWindow, {cmd: "update-downloaded", message: {
            event,
            releaseNotes,
            releaseName,
            releaseDate,
            updateUrl
        }})

        // 接收渲染进程消息
        ipcMain.on('confirmDownloadUpdate', (e, arg) => {
            autoUpdater.quitAndInstall()
        })
    })

    // 接收渲染进程消息,开始检查更新
    ipcMain.on('checkForUpdate', (e, arg) => {
        autoUpdater.checkForUpdates()
    })
}

// 发送消息给渲染进程
function sendUpdateMessage(mainWindow, cmd) {
    mainWindow.webContents.send('message', cmd)
}

 

    // todo: 检查更新 点击事件
    async clickUpdate () {
        var feedUrl = config.build.publish[0].url // 获取在package.json里面设定的服务器安装包存放地址publish
        // 需要在IIS上允许所有请求头:https://jingyan.baidu.com/article/6dad5075fd697ce023e36ed9.html
        var res = await axios({
            method:"get",
            url:feedUrl + "/latest.yml"
        })

        // 本地版本号
        let localVersion = config.version
        // 服务器版本号
        var remoteVersionStr = res.data.split('\n')[0];
        var remoteVersion = remoteVersionStr.replace("version:", "").replace(" ", "");

        console.log("localVersion", localVersion);
        console.log("remoteVersion", remoteVersion);

        var lv = parseInt(localVersion.replace(/\./ig, ""));
        var rv = parseInt(remoteVersion.replace(/\./ig, ""));

        console.log("lv", lv);
        console.log("rv", rv);
        
        // 判断是否可以更新
        if (rv > lv) {
            this.$confirm('检查到有新的版本[' + remoteVersion + '],是否立即更新?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
            .then(() => {
                console.log('click update button')
                ipc.send('checkForUpdate')
            })
            .catch(err => {
                console.log(err)
            })
        } else {
            this.$message({
            duration: 1500,
            type: "warning",
            message: '没有需要更新的版本!',
            offset: 200,
        });
        }
    },

 

  mounted () {
    this.version = config.version
    // 接收主进程版本更新消息
    ipc.on('message', (event, arg) => {
        console.log("arg----------------", arg);
        if (arg.cmd === 'update-available') {
            // 监听发现可用更新事件
        } else if (arg.cmd === 'update-not-available') {
            // 监听不需要更新 事件
            this.notAvailable()
        } else if (arg.cmd === 'download-progress') {
            // 更新下载进度事件
            this.downloadProgress(arg)
        } else if (arg.cmd === 'error') {
            // 监听升级失败事件
            this.error(arg)
        } else if (arg.cmd === 'update-downloaded') {
            // 监听下载完成事件
            this.updateDownloaded()
        }
    })
  },

 

    // 更新下载进度事件
    downloadProgress (arg) {
      // 更新升级进度
      let percent = Math.round(parseFloat(arg.message.percent))
      this.percentage = percent.toString() + "%";
    },
    // 监听升级失败事件
    error (arg) {
      this.dialogVisible = false // 关闭弹窗
      console.log('更新失败')
    },
    notAvailable () {
        this.$message({
            duration: 1500,
            type: "warning",
            message: '没有需要更新的版本!',
            offset: 200,
        });
    },
    // 监听下载完成事件
    updateDownloaded () {
        this.$confirm('下载完成,是否立即更新?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
        .then(() => {
            ipc.send('confirmDownloadUpdate') // 手动下载更新文件
        })
        .catch(() => {
            this.dialogVisible = false // 关闭弹窗
        })
    },

 

标签:cmd,update,更新,autoUpdater,electron,自动更新,arg,message
来源: https://www.cnblogs.com/subendong/p/16444239.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有