background.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. import { app, protocol, BrowserWindow, ipcMain, dialog } 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. // const Store = require('electron-store');
  7. // Scheme must be registered before the app is ready
  8. protocol.registerSchemesAsPrivileged([
  9. { scheme: "app", privileges: { secure: true, standard: true } },
  10. ]);
  11. async function createWindow() {
  12. // Create the browser window.
  13. const win = new BrowserWindow({
  14. width: 800,
  15. height: 600,
  16. webPreferences: {
  17. // Use pluginOptions.nodeIntegration, leave this alone
  18. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  19. contextIsolation: false, //上下文隔离
  20. enableRemoteModule: true, //启用远程模块
  21. nodeIntegration: true, //开启自带node环境
  22. webviewTag: true, //开启webview
  23. webSecurity: false,
  24. allowDisplayingInsecureContent: true,
  25. allowRunningInsecureContent: true,
  26. },
  27. });
  28. win.maximize();
  29. // win.show();
  30. // 打开控制台
  31. win.webContents.openDevTools();
  32. ipcMain.on("getPrinterList", (event) => {
  33. //主线程获取打印机列表
  34. win.webContents.getPrintersAsync().then((data) => {
  35. win.webContents.send("getPrinterList", data);
  36. });
  37. //通过webContents发送事件到渲染线程,同时将打印机列表也传过去
  38. });
  39. // 处理选择文件夹的 IPC 调用
  40. ipcMain.handle("select-folder", async () => {
  41. const result = await dialog.showOpenDialog(win, {
  42. properties: ["openFile", "openDirectory"],
  43. });
  44. return result.canceled ? null : result.filePaths[0];
  45. });
  46. ipcMain.handle("select-file", async () => {
  47. const result = await dialog.showOpenDialog(win, {
  48. properties: ["openFile"],
  49. });
  50. return result.canceled ? null : result.filePaths[0];
  51. });
  52. if (process.env.WEBPACK_DEV_SERVER_URL) {
  53. // Load the url of the dev server if in development mode
  54. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
  55. // if (!process.env.IS_TEST) win.webContents.openDevTools();
  56. } else {
  57. createProtocol("app");
  58. // Load the index.html when not in development
  59. win.loadURL("app://./index.html");
  60. }
  61. }
  62. // Quit when all windows are closed.
  63. app.on("window-all-closed", () => {
  64. // On macOS it is common for applications and their menu bar
  65. // to stay active until the user quits explicitly with Cmd + Q
  66. if (process.platform !== "darwin") {
  67. app.quit();
  68. }
  69. });
  70. app.on("activate", () => {
  71. // On macOS it's common to re-create a window in the app when the
  72. // dock icon is clicked and there are no other windows open.
  73. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  74. });
  75. // This method will be called when Electron has finished
  76. // initialization and is ready to create browser windows.
  77. // Some APIs can only be used after this event occurs.
  78. app.on("ready", async () => {
  79. // Store.initRenderer();
  80. if (isDevelopment && !process.env.IS_TEST) {
  81. // Install Vue Devtools
  82. try {
  83. // await installExtension(VUEJS_DEVTOOLS)
  84. } catch (e) {
  85. console.error("Vue Devtools failed to install:", e.toString());
  86. }
  87. }
  88. createWindow();
  89. });
  90. // Exit cleanly on request from parent process in development mode.
  91. if (isDevelopment) {
  92. if (process.platform === "win32") {
  93. process.on("message", (data) => {
  94. if (data === "graceful-exit") {
  95. app.quit();
  96. }
  97. });
  98. } else {
  99. process.on("SIGTERM", () => {
  100. app.quit();
  101. });
  102. }
  103. }