update.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. autoUpdater
  3. } from 'electron-updater'
  4. import {
  5. ipcMain, dialog
  6. } from 'electron'
  7. import http from 'http'
  8. let mainWindow = null;
  9. export function updateHandle(window, feedUrl) {
  10. mainWindow = window;
  11. let message = {
  12. error: '检查更新出错',
  13. checking: '正在检查更新……',
  14. updateAva: '检测到新版本,正在下载……',
  15. updateNotAva: '现在使用的就是最新版本,不用更新',
  16. };
  17. //设置更新包的地址
  18. autoUpdater.setFeedURL(feedUrl);
  19. //监听升级失败事件
  20. autoUpdater.on('error', function (error) {
  21. sendUpdateMessage({
  22. cmd: 'error',
  23. message: error
  24. })
  25. });
  26. //监听开始检测更新事件
  27. autoUpdater.on('checking-for-update', function (message) {
  28. sendUpdateMessage({
  29. cmd: 'checking-for-update',
  30. message: message
  31. })
  32. });
  33. //监听发现可用更新事件
  34. autoUpdater.on('update-available', function (message) {
  35. sendUpdateMessage({
  36. cmd: 'update-available',
  37. message: message
  38. })
  39. });
  40. //监听没有可用更新事件
  41. autoUpdater.on('update-not-available', function (message) {
  42. sendUpdateMessage({
  43. cmd: 'update-not-available',
  44. message: message
  45. })
  46. });
  47. // 更新下载进度事件
  48. autoUpdater.on('download-progress', function (progressObj) {
  49. sendUpdateMessage({
  50. cmd: 'download-progress',
  51. message: progressObj
  52. })
  53. });
  54. //监听下载完成事件
  55. autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl) {
  56. sendUpdateMessage({
  57. cmd: 'update-downloaded',
  58. message: {
  59. releaseNotes,
  60. releaseName,
  61. releaseDate,
  62. updateUrl
  63. }
  64. })
  65. //退出并安装更新包
  66. autoUpdater.quitAndInstall();
  67. });
  68. //接收渲染进程消息,开始检查更新
  69. ipcMain.on("checkForUpdate", (e, arg) => {
  70. //执行自动更新检查
  71. // sendUpdateMessage({cmd:'checkForUpdate',message:arg})
  72. autoUpdater.checkForUpdates();
  73. })
  74. }
  75. //给渲染进程发送消息
  76. function sendUpdateMessage(text) {
  77. mainWindow.webContents.send('message', text)
  78. }