1
hejianhao
2024-12-31 4d151a9bd66df24c33d7cf35ba5c6b593f1bdf79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"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);
// });