main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const fs = require('fs')
  2. const path = require('path')
  3. const yaml = require('js-yaml')
  4. const { app, BrowserWindow, Menu, dialog } = require('electron')
  5. const { autoUpdater } = require('electron-updater')
  6. const updateHandle = (win) => {
  7. if (app.isPackaged) {
  8. const fileContents = fs.readFileSync('resources/app-update.yml', 'utf-8')
  9. const data = yaml.load(fileContents)
  10. if (data.url) {
  11. autoUpdater.autoDownload = true // 自动下载更新
  12. autoUpdater.autoInstallOnAppQuit = false
  13. autoUpdater.setFeedURL({
  14. provider: data.provider,
  15. url: data.url
  16. })
  17. // 检查更新出错
  18. // autoUpdater.on('error', (err) => {
  19. // dialog.showMessageBox(win, {
  20. // type: 'error',
  21. // title: '错误',
  22. // noLink: true,
  23. // message: '更新发生错误',
  24. // detail: err.message
  25. // })
  26. // })
  27. // 检测到有版本更新
  28. // autoUpdater.on('update-available', (e) => {
  29. // dialog.showMessageBox(win, {
  30. // type: 'info',
  31. // title: '提示',
  32. // noLink: true,
  33. // message: e.version,
  34. // detail: '发现新版本,是否现在下载?',
  35. // buttons: ['下载', '取消']
  36. // }).then((res) => {
  37. // if (res.response === 1) {
  38. // autoUpdater.downloadUpdate()
  39. // }
  40. // })
  41. // })
  42. // 更新下载进度事件
  43. autoUpdater.on('download-progress', (progress) => {
  44. // https://www.electronjs.org/zh/docs/latest/tutorial/progress-bar
  45. win.setProgressBar(progress.percent / 100)
  46. })
  47. // 下载完成,询问用户是否更新
  48. autoUpdater.on('update-downloaded', (e) => {
  49. dialog.showMessageBox(win, {
  50. type: 'info',
  51. title: '提示',
  52. noLink: true,
  53. message: e.version,
  54. detail: '已下载新版本,是否立即安装?',
  55. buttons: ['安装', '退出']
  56. }).then((res) => {
  57. win.setProgressBar(-1)
  58. if (res.response === 0) {
  59. autoUpdater.quitAndInstall()
  60. } else {
  61. app.quit()
  62. }
  63. })
  64. })
  65. autoUpdater.checkForUpdates()
  66. }
  67. }
  68. }
  69. const createWindow = () => {
  70. Menu.setApplicationMenu(null)
  71. const win = new BrowserWindow({
  72. show: false,
  73. center: true,
  74. width: 1280,
  75. height: 800,
  76. minWidth: 480,
  77. minHeight: 300,
  78. icon: path.resolve(__dirname, 'dist/favicon.ico')
  79. })
  80. win.loadFile(path.resolve(__dirname, 'dist/index.html'))
  81. win.maximize()
  82. win.once('ready-to-show', () => {
  83. win.show()
  84. updateHandle(win)
  85. })
  86. }
  87. app.whenReady().then(() => {
  88. createWindow()
  89. })
  90. // 阻止应用多开
  91. // const gotTheLock = app.requestSingleInstanceLock()
  92. // if (!gotTheLock) {
  93. // app.quit()
  94. // } else {
  95. // app.whenReady().then(() => {
  96. // createWindow()
  97. // })
  98. // }