• electron自动更新


    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 // 关闭弹窗
            })
        },
  • 相关阅读:
    MySQL索引方法
    【转】CentOS Linux解决Device eth0 does not seem to be present(linux)
    charles4.2下载与破解方法以及配置https
    laravel 安装碰到的问题:全局安装 Laravel Installer,然后用下面的指令创建新项目: laravel new blog报连接超时解决方案
    Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[2]
    Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[1]
    跨集群拷贝hdfs
    kylin
    Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerException
    笔记
  • 原文地址:https://www.cnblogs.com/subendong/p/16444239.html
Copyright © 2020-2023  润新知