background.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict'
  2. import { app, protocol, BrowserWindow, Menu, globalShortcut, screen } from 'electron'
  3. import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
  4. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
  5. import { updateHandle } from './update'
  6. const isDevelopment = process.env.NODE_ENV !== 'production'
  7. if (!isDevelopment) {
  8. global.__static = require("path")
  9. .join(__dirname, "/static")
  10. .replace(/\\/g, "\\\\");
  11. }
  12. let mainWindow, loadingWindow;
  13. const loadingURL =
  14. isDevelopment //加载loading.html页面地址
  15. ? require("path").join(__static, "loading.html")
  16. : `file://${__static}/loading.html`;
  17. const showLoading = (cb) => {
  18. loadingWindow = new BrowserWindow({
  19. show: false,
  20. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  21. width: 400,
  22. height: 300,
  23. resizable: false,
  24. transparent: true, // 窗口是否支持透明,如果想做高级效果最好为true
  25. });
  26. loadingWindow.once("show", cb);
  27. loadingWindow.loadURL(loadingURL);
  28. loadingWindow.show();
  29. };
  30. // Scheme must be registered before the app is ready
  31. protocol.registerSchemesAsPrivileged([
  32. { scheme: 'app', privileges: { secure: true, standard: true } }
  33. ])
  34. async function createWindow() {
  35. const {width,height} = screen.getPrimaryDisplay().workAreaSize;//获取到屏幕的宽度和高度
  36. console.log(width,height);
  37. // Create the browser window.
  38. const win = new BrowserWindow({
  39. width: width,
  40. height: height,
  41. darkTheme: true,
  42. minWidth: 710,
  43. minHeight: 500,
  44. show: true,
  45. backgroundColor: '#211f1d',
  46. fullscreen: false,
  47. webPreferences: {
  48. webSecurity: false,
  49. enableRemoteModule: true,
  50. // Use pluginOptions.nodeIntegration, leave this alone
  51. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  52. nodeIntegration: true,
  53. contextIsolation: false,
  54. icon: `${__static}/app.ico`
  55. }
  56. })
  57. mainWindow = win
  58. if (process.env.WEBPACK_DEV_SERVER_URL) {
  59. // Load the url of the dev server if in development mode
  60. // await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
  61. await win.loadURL('http://localhost:8000/')
  62. if (!process.env.IS_TEST) win.webContents.openDevTools()
  63. } else {
  64. createProtocol('app')
  65. win.loadURL(process.env.VUE_APP_API_BASE_URL)
  66. win.on('ready-to-show', (event) => {
  67. setTimeout(() => {
  68. // loadingWindow.hide();
  69. // loadingWindow.close();
  70. win.show()
  71. }, 2000);
  72. })
  73. }
  74. // 在开发环境和生产环境均可通过快捷键打开devTools
  75. globalShortcut.register('CommandOrControl+Shift+i', function () {
  76. win.webContents.openDevTools()
  77. })
  78. // 设置菜单栏
  79. Menu.setApplicationMenu(null)
  80. // 更新,第二个参数就是public里的url
  81. updateHandle(win, process.env.VUE_APP_API_BASE_URL+'electron')
  82. }
  83. const gotTheLock = app.requestSingleInstanceLock()
  84. if (!gotTheLock) {
  85. app.quit()
  86. } else {
  87. /**
  88. * 兼容https非可信域
  89. */
  90. app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
  91. //允许私有证书
  92. event.preventDefault()
  93. callback(true)
  94. });
  95. app.on('second-instance', (event) => {
  96. if (mainWindow) {
  97. if (mainWindow.isMinimized()) mainWindow.restore()
  98. mainWindow.focus()
  99. }
  100. })
  101. // Quit when all windows are closed.
  102. app.on('window-all-closed', () => {
  103. // On macOS it is common for applications and their menu bar
  104. // to stay active until the user quits explicitly with Cmd + Q
  105. if (process.platform !== 'darwin') {
  106. app.quit()
  107. }
  108. })
  109. app.on('activate', () => {
  110. // On macOS it's common to re-create a window in the app when the
  111. // dock icon is clicked and there are no other windows open.
  112. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  113. })
  114. // 禁用缓存
  115. app.commandLine.appendSwitch("--disable-http-cache")
  116. // This method will be called when Electron has finished
  117. // initialization and is ready to create browser windows.
  118. // Some APIs can only be used after this event occurs.
  119. app.on('ready', async () => {
  120. if (isDevelopment && !process.env.IS_TEST) {
  121. // Install Vue Devtools
  122. try {
  123. await installExtension(VUEJS_DEVTOOLS)
  124. } catch (e) {
  125. console.error('Vue Devtools failed to install:', e.toString())
  126. }
  127. }
  128. // showLoading(createWindow);
  129. createWindow()
  130. })
  131. }
  132. // Exit cleanly on request from parent process in development mode.
  133. if (isDevelopment) {
  134. if (process.platform === 'win32') {
  135. process.on('message', (data) => {
  136. if (data === 'graceful-exit') {
  137. app.quit()
  138. }
  139. })
  140. } else {
  141. process.on('SIGTERM', () => {
  142. app.quit()
  143. })
  144. }
  145. }