background.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict'
  2. import { app, protocol, BrowserWindow, Menu, globalShortcut } from 'electron'
  3. import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
  4. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
  5. const isDevelopment = process.env.NODE_ENV !== 'production'
  6. // Scheme must be registered before the app is ready
  7. protocol.registerSchemesAsPrivileged([
  8. { scheme: 'app', privileges: { secure: true, standard: true } }
  9. ])
  10. async function createWindow() {
  11. // Create the browser window.
  12. const win = new BrowserWindow({
  13. width: 800,
  14. height: 600,
  15. darkTheme: true,
  16. webPreferences: {
  17. webSecurity: false,
  18. enableRemoteModule: true,
  19. // Use pluginOptions.nodeIntegration, leave this alone
  20. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  21. nodeIntegration: true,
  22. contextIsolation: false,
  23. icon: `${__static}/app.ico`
  24. }
  25. })
  26. if (process.env.WEBPACK_DEV_SERVER_URL) {
  27. // Load the url of the dev server if in development mode
  28. // await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
  29. await win.loadURL('http://localhost:8000/')
  30. if (!process.env.IS_TEST) win.webContents.openDevTools()
  31. } else {
  32. createProtocol('app')
  33. // Load the index.html when not in development
  34. // win.loadURL('app://./index.html')
  35. // 预发布地址
  36. // win.loadURL('http://p.ocs.360arrow.com/')
  37. win.loadURL('http://p.iscm.360arrow.com/')
  38. // 生产地址
  39. // win.loadURL('https://ocs.360arrow.com/')
  40. // win.loadURL('https://iscm.360arrow.com/')
  41. // 开发地址
  42. // win.loadURL('http://localhost:8000/')
  43. }
  44. // 在开发环境和生产环境均可通过快捷键打开devTools
  45. globalShortcut.register('CommandOrControl+Shift+i', function () {
  46. win.webContents.openDevTools()
  47. })
  48. // 设置菜单栏
  49. Menu.setApplicationMenu(null)
  50. }
  51. // Quit when all windows are closed.
  52. app.on('window-all-closed', () => {
  53. // On macOS it is common for applications and their menu bar
  54. // to stay active until the user quits explicitly with Cmd + Q
  55. if (process.platform !== 'darwin') {
  56. app.quit()
  57. }
  58. })
  59. app.on('activate', () => {
  60. // On macOS it's common to re-create a window in the app when the
  61. // dock icon is clicked and there are no other windows open.
  62. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  63. })
  64. // This method will be called when Electron has finished
  65. // initialization and is ready to create browser windows.
  66. // Some APIs can only be used after this event occurs.
  67. app.on('ready', async () => {
  68. if (isDevelopment && !process.env.IS_TEST) {
  69. // Install Vue Devtools
  70. try {
  71. await installExtension(VUEJS_DEVTOOLS)
  72. } catch (e) {
  73. console.error('Vue Devtools failed to install:', e.toString())
  74. }
  75. }
  76. createWindow()
  77. })
  78. // Exit cleanly on request from parent process in development mode.
  79. if (isDevelopment) {
  80. if (process.platform === 'win32') {
  81. process.on('message', (data) => {
  82. if (data === 'graceful-exit') {
  83. app.quit()
  84. }
  85. })
  86. } else {
  87. process.on('SIGTERM', () => {
  88. app.quit()
  89. })
  90. }
  91. }