-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmain.js
387 lines (350 loc) · 9.64 KB
/
main.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* @Date: 2024-01-25 15:52:14
* @LastEditors: [email protected]
* @LastEditTime: 2024-12-23 15:23:56
* @FilePath: \electron-hiprint\main.js
*/
const {
app,
BrowserWindow,
BrowserView,
ipcMain,
Notification,
Tray,
Menu,
shell,
} = require("electron");
const path = require("path");
const server = require("http").createServer();
const helper = require("./src/helper");
const printSetup = require("./src/print");
const renderSetup = require("./src/render");
const setSetup = require("./src/set");
const printLogSetup = require("./src/printLog");
const log = require("./tools/log");
const {
store,
address,
initServeEvent,
initClientEvent,
} = require("./tools/utils");
const { machineIdSync } = require("node-machine-id");
const TaskRunner = require("concurrent-tasks");
// 主进程
global.MAIN_WINDOW = null;
// 托盘
global.APP_TRAY = null;
// 打印窗口
global.PRINT_WINDOW = null;
// 设置窗口
global.SET_WINDOW = null;
// 渲染窗口
global.RENDER_WINDOW = null;
// 打印日志窗口
global.PRINT_LOG_WINDOW = null;
// socket.io 服务端
global.SOCKET_SERVER = null;
// socket.io-client 客户端
global.SOCKET_CLIENT = null;
// 打印队列,解决打印并发崩溃问题
global.PRINT_RUNNER = new TaskRunner({ concurrency: 1 });
// 打印队列 done 集合
global.PRINT_RUNNER_DONE = {};
// 分批打印任务的打印任务信息
global.PRINT_FRAGMENTS_MAPPING = {
// [id: string]: { // 当前打印任务id,当此任务完成或超过指定时间会删除该对象
// {
// total: number, // html片段总数
// count: number, // 已经保存完成的片段数量,当count与total相同时,所有片段传输完成
// fragments: Array<string | undefined>, // 按照顺序摆放的html文本片段
// updateTime: number, // 最后更新此任务信息的时间戳,用于超时时移除此对象
// }
// }
};
global.RENDER_RUNNER = new TaskRunner({ concurrency: 1 });
global.RENDER_RUNNER_DONE = {};
// socket.io 服务端,用于创建本地服务
const ioServer = (global.SOCKET_SERVER = new require("socket.io")(server, {
pingInterval: 10000,
pingTimeout: 5000,
maxHttpBufferSize: 10000000000,
allowEIO3: true, // 兼容 Socket.IO 2.x
// 跨域问题(Socket.IO 3.x 使用这种方式)
cors: {
// origin: "*",
// 兼容 Socket.IO 2.x
origin: (requestOrigin, callback) => {
// 允许所有域名连接
callback(null, requestOrigin);
},
methods: "GET, POST, PUT, DELETE, OPTIONS",
allowedHeaders: "*",
// 详情参数见 https://www.npmjs.com/package/cors
credentials: false,
},
}));
// socket.io 客户端,用于连接中转服务
const ioClient = require("socket.io-client").io;
/**
* @description: 初始化
*/
async function initialize() {
// 限制一个窗口
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
// 销毁所有窗口、托盘、退出应用
helper.appQuit();
}
app.setAppLogsPath(store.get("logPath"));
// 当运行第二个实例时,聚焦到 MAIN_WINDOW 这个窗口
app.on("second-instance", () => {
if (MAIN_WINDOW) {
if (MAIN_WINDOW.isMinimized()) {
// 将窗口从最小化状态恢复到以前的状态
MAIN_WINDOW.restore();
}
MAIN_WINDOW.focus();
}
});
// 允许渲染进程创建通知
ipcMain.on("notification", (event, data) => {
const notification = new Notification(data);
// 显示通知
notification.show();
});
// 打开设置窗口
ipcMain.on("openSetting", openSetWindow);
// 获取设备唯一id
ipcMain.on("getMachineId", (event) => {
try {
const machineId = machineIdSync({ original: true });
event.sender.send("machineId", machineId);
} catch (error) {
event.sender.send("machineId", "");
}
});
// 获取设备ip、mac等信息
ipcMain.on("getAddress", (event) => {
address.all().then((obj) => {
event.sender.send("address", {
...obj,
port: store.get("port"),
});
});
});
// 当electron完成初始化
app.whenReady().then(() => {
// 创建浏览器窗口
createWindow();
app.on("activate", function() {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
log("==> Electron-hiprint 启动 <==");
});
}
/**
* @description: 创建渲染进程 主窗口
* @return {BrowserWindow} MAIN_WINDOW 主窗口
*/
async function createWindow() {
const windowOptions = {
width: 500, // 窗口宽度
height: 300, // 窗口高度
title: store.get("mainTitle") || "Electron-hiprint",
useContentSize: true, // 窗口大小不包含边框
center: true, // 居中
resizable: false, // 不可缩放
show: store.get("openAsHidden") ? false : true, // 显示
webPreferences: {
// 设置此项为false后,才可在渲染进程中使用 electron api
contextIsolation: false,
nodeIntegration: true,
},
};
// 窗口左上角图标
if (!app.isPackaged) {
windowOptions.icon = path.join(__dirname, "build/icons/256x256.png");
} else {
app.setLoginItemSettings({
openAtLogin: store.get("openAtLogin"),
openAsHidden: store.get("openAsHidden"),
});
}
// 创建主窗口
MAIN_WINDOW = new BrowserWindow(windowOptions);
// 添加加载页面 解决白屏的问题
loadingView(windowOptions);
// 初始化系统设置
systemSetup();
// 加载主页面
const indexHtml = path.join("file://", app.getAppPath(), "assets/index.html");
MAIN_WINDOW.webContents.loadURL(indexHtml);
// 退出
MAIN_WINDOW.on("closed", () => {
MAIN_WINDOW = null;
server.close();
});
// 点击关闭,最小化到托盘
MAIN_WINDOW.on("close", (event) => {
if (store.get("closeType") === "tray") {
// 最小化到托盘
MAIN_WINDOW.hide();
// 隐藏任务栏
MAIN_WINDOW.setSkipTaskbar(true);
// 阻止窗口关闭
event.preventDefault();
} else {
// 销毁所有窗口、托盘、退出应用
helper.appQuit();
}
});
// 主窗口 Dom 加载完毕
MAIN_WINDOW.webContents.on("dom-ready", async () => {
try {
// 未打包时打开开发者工具
if (!app.isPackaged) {
MAIN_WINDOW.webContents.openDevTools();
}
// 本地服务开启端口监听
server.listen(store.get("port") || 17521);
// 初始化本地 服务端事件
initServeEvent(ioServer);
// 有配置中转服务时连接中转服务
if (
store.get("connectTransit") &&
store.get("transitUrl") &&
store.get("transitToken")
) {
global.SOCKET_CLIENT = ioClient(store.get("transitUrl"), {
transports: ["websocket"],
query: {
client: "electron-hiprint",
},
auth: {
token: store.get("transitToken"),
},
});
// 初始化中转 客户端事件
initClientEvent();
}
} catch (error) {
console.error(error);
}
});
// 初始化托盘
initTray();
// 打印窗口初始化
await printSetup();
// 渲染窗口初始化
await renderSetup();
return MAIN_WINDOW;
}
/**
* @description: 加载等待页面,解决主窗口白屏问题
* @param {Object} windowOptions 主窗口配置
* @return {Void}
*/
function loadingView(windowOptions) {
const loadingBrowserView = new BrowserView();
MAIN_WINDOW.setBrowserView(loadingBrowserView);
loadingBrowserView.setBounds({
x: 0,
y: 0,
width: windowOptions.width,
height: windowOptions.height,
});
const loadingHtml = path.join(
"file://",
app.getAppPath(),
"assets/loading.html",
);
loadingBrowserView.webContents.loadURL(loadingHtml);
// 主窗口 dom 加载完毕,移除 loadingBrowserView
MAIN_WINDOW.webContents.on("dom-ready", async (event) => {
MAIN_WINDOW.removeBrowserView(loadingBrowserView);
});
}
/**
* @description: 初始化系统设置
* @return {Void}
*/
function systemSetup() {
// 隐藏菜单栏
Menu.setApplicationMenu(null);
}
/**
* @description: 初始化托盘
* @return {Tray} APP_TRAY 托盘实例
*/
function initTray() {
let trayPath = path.join(app.getAppPath(), "assets/icons/tray.png");
APP_TRAY = new Tray(trayPath);
// 托盘提示标题
APP_TRAY.setToolTip("hiprint");
// 托盘菜单
const trayMenuTemplate = [
{
label: "设置",
click: () => {
openSetWindow();
},
},
{
label: "软件日志",
click: () => {
shell.openPath(app.getPath("logs"));
},
},
{
label: "打印记录",
click: () => {
if (!PRINT_LOG_WINDOW) {
printLogSetup();
} else {
PRINT_LOG_WINDOW.show();
}
},
},
{
label: "退出",
click: () => {
helper.appQuit();
},
},
];
APP_TRAY.setContextMenu(Menu.buildFromTemplate(trayMenuTemplate));
// 监听点击事件
APP_TRAY.on("click", function() {
if (MAIN_WINDOW.isMinimized()) {
// 将窗口从最小化状态恢复到以前的状态
MAIN_WINDOW.restore();
MAIN_WINDOW.setSkipTaskbar(false);
}
if (!MAIN_WINDOW.isVisible()) {
// 主窗口关闭不会被销毁,只是隐藏,重新显示即可
MAIN_WINDOW.show();
MAIN_WINDOW.setSkipTaskbar(false);
}
if (!MAIN_WINDOW.isFocused()) {
// 主窗口未聚焦,使其聚焦
MAIN_WINDOW.focus();
}
});
return APP_TRAY;
}
/**
* @description: 打开设置窗口
* @return {BrowserWindow} SET_WINDOW 设置窗口
*/
async function openSetWindow() {
if (!SET_WINDOW) {
await setSetup();
} else {
SET_WINDOW.show();
}
return SET_WINDOW;
}
// 初始化主窗口
initialize();