"use strict"; const { app, protocol, BrowserWindow, ipcMain, } = require("electron");// 控制应用生命周期的模块。 const { createProtocol } = require("vue-cli-plugin-electron-builder/lib"); const isDevelopment = process.env.NODE_ENV !== "production"; const rcInit = require('@rongcloud/electron') const path = require('path') let rcService protocol.registerSchemesAsPrivileged([ { scheme: "app", privileges: { secure: true, standard: true } }, ]); let win; let tray; //禁止多开 点击打开原来应用 const gotTheLock = app.requestSingleInstanceLock() if (!gotTheLock) { app.quit() } else { app.on('second-instance', () => { // 有人试图运行第二个实例,我们应该关注我们的窗口 if (win) { if (win.isMinimized()) win.restore() if (!win.isVisible()) win.show() win.focus() } }) } async function createWindow() { win = new BrowserWindow({ width: 1080, height: 1920, resizable: false, autoHideMenuBar: true, show: false, fullscreen: true, icon: "./src/assets/logo.png", // frame: false, webPreferences: { inputMethods: false, webSecurity: false, nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION, contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION, // contextIsolation: false, // nodeIntegration: true, preload: path.resolve(__dirname, './preload.js') }, }); if (process.env.WEBPACK_DEV_SERVER_URL) { await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL); if (!process.env.IS_TEST) win.webContents.openDevTools(); } else { createProtocol("app"); win.loadURL("app://./index.html"); } win.webContents.once("did-fail-load", function () { setTimeout(() => { win.reload(); }, 1000); }); } // 当所有窗口被关闭了,退出。 app.on("window-all-closed", () => { // 在 OS X 上,通常用户在明确地按下 Cmd + Q 之前 // 应用会保持活动状态 if (process.platform !== "darwin") { app.quit(); } }); app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); }); // 创建系统托盘 app.whenReady().then(() => { // tray = new Tray(nativeImage.createFromPath("./src/assets/logo.png")); // const contextMenu = Menu.buildFromTemplate([ // { label: "退出程序", role: "quit" }, // ]); // tray.on("click", () => { // win.show(); // }); // tray.setContextMenu(contextMenu); }); // 当 Electron 完成了初始化并且准备创建浏览器窗口的时候 // 这个方法就被调用 app.on("ready", async () => { rcService = rcInit({ appkey: '6tnym1br6ka67', dbpath: app.getPath('userData'), logLevel: 4, }) if (isDevelopment && !process.env.IS_TEST) { try { } catch (e) { console.error("Vue Devtools failed to install:", e.toString()); } } createWindow(); // 打开开发工具 // mainWindow.openDevTools(); }); if (isDevelopment) { if (process.platform === "win32") { process.on("message", (data) => { if (data === "graceful-exit") { app.quit(); } }); } else { process.on("SIGTERM", () => { app.quit(); }); } } // 展示 ipcMain.handle("winShow", async (value) => { win.show(); }); // 闪烁 ipcMain.handle("shanshuo", async (value) => { if (win.isFocused()) { win.showInactive(); win.flashFrame(true); } }); ipcMain.on('print', async (e, url) => { // 创建一个新的隐藏窗口,用于打印 let printWindow = new BrowserWindow({ show: false, width: 1920, height: 1080, frame: false, // 禁用窗口框架,移除菜单和标题栏 contextIsolation: false, enableRemoteModule: true, nodeIntegration: true, webSecurity: false }) // 指定窗口加载的页面 // printWindow.loadFile(url); printWindow.loadURL(url); // 导航完成时触发,即选项卡的旋转器将停止旋转,并指派onload事件后。 printWindow.webContents.on('did-finish-load', async () => { try { // 进行打印 printWindow.webContents.print({ silent: true }, (success, failureReason) => { // 关闭窗口 printWindow.close(); }); } catch (error) { // 关闭窗口 printWindow.close(); } }) }) //设置宽高 // ipcMain.handle("setSize", async (value) => { // win.setContentSize(960, 700); // });