software-update.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict'
  2. /**
  3. *
  4. *
  5. * @author EllisRan.
  6. * @date 2018/6/4
  7. * @version
  8. */
  9. const electron = require('electron')
  10. const ipcMain = electron.ipcMain
  11. const autoUpdater = require('electron-updater').autoUpdater
  12. const uploadUrl = 'http://d2.smartcost.com.cn/startup/'
  13. exports.initialize = function (win) {
  14. // let message = {
  15. // error: '检查更新出错 1',
  16. // checking: '正在检查更新…… 2',
  17. // updateAva: '检测到新版本,正在下载…… 3',
  18. // updateNotAva: '现在使用的就是最新版本,不用更新 4'
  19. // }
  20. autoUpdater.setFeedURL(uploadUrl)
  21. autoUpdater.autoDownload = false
  22. autoUpdater.on('error', function () {
  23. // sendUpdateMessage(win, message.error)
  24. })
  25. autoUpdater.on('checking-for-update', function () {
  26. // sendUpdateMessage(win, message.checking)
  27. })
  28. autoUpdater.on('update-available', function (info) {
  29. sendUpdateMessage(win, 2)
  30. })
  31. autoUpdater.on('update-not-available', function (info) {
  32. // sendUpdateMessage(win, message.updateNotAva)
  33. })
  34. // 更新下载进度事件
  35. autoUpdater.on('download-progress', function (progressObj) {
  36. win.webContents.send('downloadProgress', progressObj)
  37. win.setProgressBar(progressObj.percent / 100)
  38. // console.log(progressObj)
  39. })
  40. autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
  41. ipcMain.on('isUpdateNow', (e, arg) => {
  42. // some code here to handle event
  43. autoUpdater.quitAndInstall()
  44. })
  45. win.webContents.send('isUpdateNow')
  46. })
  47. ipcMain.on('checkForUpdate', () => {
  48. // 执行自动更新检查
  49. autoUpdater.checkForUpdates()
  50. })
  51. }
  52. ipcMain.on('downloadUpdate', () => {
  53. autoUpdater.downloadUpdate()
  54. })
  55. // 通过main进程发送事件给renderer进程,提示更新信息
  56. function sendUpdateMessage (win, status = 0) {
  57. win.webContents.send('msgBox', status)
  58. }