• vue+electron 自动更新


    1. 配置 package.json或vue.config.js文件中的应用更新服务器地址
    1. module.exports = {
    2. pluginOptions: {
    3. electronBuilder: {
    4. ...
    5. publish: [
    6. {
    7. provider: 'generic',
    8. url: 'http://qc***/***' // 服务器地址
    9. }
    10. ]
    11. ...
    12. }
    13. }
    14. }
    15. }
    2. 在主进程写更新应用逻辑

    在 checkupdate.js 文件或 main.js 中写入

    1. import { autoUpdater } from 'electron-updater'
    2. const { build } = require("../../../package.json")
    3. const { app, dialog } = require('electron');
    4. const path = require("path");
    5. import { ipcRenderer } from "electron";
    6. const websocket = require('ws')
    7. //跳过打包检查 开发环境使用 打包的时候注释掉
    8. // Object.defineProperty(app, 'isPackaged', {
    9. // get() {
    10. // return true;
    11. // }
    12. // });
    13. /**
    14. * -1 检查更新失败 0 正在检查更新 1 检测到新版本,准备下载 2 未检测到新版本 3 下载中 4 下载完成
    15. **/
    16. class Update {
    17. mainWindow
    18. constructor() {
    19. autoUpdater.setFeedURL(build.publish[0].url)
    20. // 当更新发生错误的时候触发。
    21. autoUpdater.on('error', (err) => {
    22. if (err.message.includes('sha512 checksum mismatch')) {
    23. this.Message(this.mainWindow, -1, 'sha512校验失败')
    24. } else {
    25. this.Message(this.mainWindow, -1, '错误信息请看主进程控制台')
    26. }
    27. })
    28. // 当开始检查更新的时候触发
    29. autoUpdater.on('checking-for-update', (event, arg) => {
    30. this.timer(this.mainWindow, 0, "正在检查更新...", 5000)
    31. })
    32. // 发现可更新数据时
    33. autoUpdater.on('update-available', (event, arg) => {
    34. this.timer(this.mainWindow, 1, "发现新版本", 10000)
    35. this.timer(this.mainWindow, 1, "检测到新版本,正在下载安装包...", 15000)
    36. })
    37. // 没有可更新数据时
    38. autoUpdater.on('update-not-available', (event, arg) => {
    39. this.timer(this.mainWindow, 2, "暂未检测到新版本,当前版本为最新版本,无需更新", 15000)
    40. })
    41. // 下载监听
    42. autoUpdater.on('download-progress', (progressObj) => {
    43. // {
    44. // total: 1145589126,
    45. // delta: 71139212,
    46. // transferred: 71139212,
    47. // percent: 6.209836527376395,
    48. // bytesPerSecond: 69881348
    49. // }
    50. let total = this.renderSize(progressObj.total); //总大小
    51. let percent = parseFloat(progressObj.percent.toFixed(2)); //下载进度
    52. let bytesPerSecond = this.renderSize(progressObj.bytesPerSecond); //下载速度
    53. // let message = `总大小:${total} 下载进度:${percent}% 下载速度${bytesPerSecond}/S`
    54. let message = `总大小:${total} 下载进度:${percent}%`
    55. // this.Message(this.mainWindow, 3, message)
    56. })
    57. // 下载完成
    58. autoUpdater.on('update-downloaded', () => {
    59. // console.log('done')
    60. this.Message(this.mainWindow, 4, "新版本已下载完成,五秒后自动安装并重启...")
    61. setTimeout(() => {
    62. this.quitInstall();
    63. }, 5000)
    64. })
    65. }
    66. // 负责向渲染进程发送信息
    67. Message(mainWindow, type, data) {
    68. const senddata = {
    69. state: type,
    70. msg: data || ''
    71. }
    72. let journalSocket = new websocket("ws://localhost:16478");
    73. // mainWindow.webContents.send('update-msg', senddata)
    74. journalSocket.on("open", function () {
    75. journalSocket.send(data);
    76. });
    77. }
    78. // 执行自动更新检查
    79. checkUpdate(mainWindow) {
    80. this.mainWindow = mainWindow
    81. autoUpdater.updateConfigPath = path.resolve(__static, 'default-app-update.yml')
    82. // this.Message(this.mainWindow, 4, path.resolve(__static, 'default-app-update.yml'))
    83. // autoUpdater.currentVersion = "0.0.1";//调试使用 正式上线后注释掉
    84. autoUpdater.checkForUpdates().catch(err => {
    85. console.log('网络连接问题', err)
    86. })
    87. }
    88. // 退出并安装
    89. quitInstall() {
    90. autoUpdater.quitAndInstall()
    91. }
    92. // 处理文件大小格式化
    93. renderSize(value) {
    94. if (null == value || value == '') {
    95. return "0 Bytes";
    96. }
    97. var unitArr = new Array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
    98. var index = 0;
    99. var srcsize = parseFloat(value);
    100. index = Math.floor(Math.log(srcsize) / Math.log(1024));
    101. var size = srcsize / Math.pow(1024, index);
    102. size = size.toFixed(2);//保留的小数位数
    103. return size + unitArr[index];
    104. }
    105. // 定时器
    106. timer(mainWindow, number, message, time) {
    107. setTimeout(() => {
    108. this.Message(mainWindow, number, message)
    109. }, time)
    110. }
    111. }
    112. export default Update

    3. 打包升级版的应用(v1.1.0)

    注意:

    每次打包记得去package.json文件中修改version版本号,这样系统才会检测到最新版本

    打包后 dis有如下两个文件:

    新版本安装包.exe
    latest.yml

    将上面两个文件复制到 ‘更新服务器’ (http://qc***/***) 目录下;

    以后每次有更新就复制这两个文件至 ‘更新服务器’,旧版本的应用的执行文件(.exe)可以删除。

  • 相关阅读:
    基于CUBEMX的STM32F4 Hal库,配置LVGL(无操作系统版)
    Python 正则表达式:强大的文本处理工具
    一文解达梦
    4 轮拿下字节 Offer,面试题复盘
    多线程---synchronized特性+原理
    mysql表的导出和导入
    【C++基础】左值引用、右值引用、move、forward
    基于RV1126 Video分析-----图像处理模块所代表的V4L2设备注册
    JavaScript Promise 的真正工作原理
    y121.第七章 服务网格与治理-Istio从入门到精通 -- Istio流量治理快速入门和流量治理进阶(七)
  • 原文地址:https://blog.csdn.net/A_man_of_ideas/article/details/136235852