Skip to content

Commit

Permalink
add 升级 electron 依赖,添加 log 日志功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier9896 committed Mar 9, 2024
1 parent fe028b7 commit 2f88c41
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 20 deletions.
15 changes: 15 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* @Date: 2024-01-25 15:52:14
* @LastEditors: [email protected]
* @LastEditTime: 2024-03-09 12:51:06
* @FilePath: \electron-hiprint\main.js
*/
const {
app,
BrowserWindow,
Expand All @@ -6,12 +12,14 @@ const {
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 setSetup = require("./src/set");
const log = require("./tools/log");
const {
store,
address,
Expand Down Expand Up @@ -118,6 +126,7 @@ async function initialize() {
createWindow();
}
});
log("==> Electron-hiprint 启动 <==")
});
}

Expand Down Expand Up @@ -282,6 +291,12 @@ function initTray() {
}
},
},
{
label: "查看日志",
click: () => {
shell.openPath(app.getPath("logs"));
}
},
{
label: "退出",
click: () => {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"homepage": "https://gitee.com/CcSimple",
"scripts": {
"start": "electron .",
"start": "chcp 65001 && electron .",
"build-w": "electron-builder -w nsis:ia32 && node ./tools/rename --tag win_x32",
"build-w-64": "electron-builder -w nsis:x64 && node ./tools/rename --tag win_x64",
"build-m": "electron-builder -m --x64 && node ./tools/rename --tag mac_x64",
Expand Down Expand Up @@ -91,6 +91,7 @@
"dependencies": {
"address": "^1.2.0",
"concurrent-tasks": "^1.0.7",
"dayjs": "^1.11.10",
"electron-store": "^8.1.0",
"ipp": "^2.0.1",
"jquery": "^3.6.0",
Expand All @@ -101,7 +102,7 @@
"unix-print": "^1.2.0"
},
"devDependencies": {
"electron": "^13.6.9",
"electron": "^15.0.0",
"electron-builder": "23.0.6",
"fs-extra": "^9.1.0",
"prettier": "^1.16.4",
Expand Down
2 changes: 2 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"use strict";
const { app } = require("electron");
const log = require("../tools/log");

/**
* 退出应用
*
* @return {undefined}
*/
exports.appQuit = function() {
log("==> Electron-hiprint 关闭 <==");
SET_WINDOW && SET_WINDOW.destroy();
PRINT_WINDOW && PRINT_WINDOW.destroy();
MAIN_WINDOW && MAIN_WINDOW.destroy();
Expand Down
45 changes: 29 additions & 16 deletions src/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require("path");
const os = require("os");
const fs = require("fs");
const printPdf = require("./pdf-print");
const log = require("../tools/log");

/**
* @description: 创建打印窗口
Expand Down Expand Up @@ -78,19 +79,19 @@ function initPrintEvent() {
}
});
if (printerError) {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印失败,打印机异常,打印机:${data.printer}`
);
socket &&
socket.emit("error", {
msg: data.printer + "打印机异常",
templateId: data.templateId,
replyId: data.replyId
replyId: data.replyId,
});
// 通过 taskMap 调用 task done 回调
PRINT_RUNNER_DONE[data.taskId]();
delete PRINT_RUNNER_DONE[data.taskId];
MAIN_WINDOW.webContents.send(
"printTask",
PRINT_RUNNER.isBusy()
);
MAIN_WINDOW.webContents.send("printTask", PRINT_RUNNER.isBusy());
return;
}
let deviceName = havePrinter ? data.printer : defaultPrinter;
Expand Down Expand Up @@ -119,33 +120,36 @@ function initPrintEvent() {
fs.writeFileSync(pdfPath, pdfData);
printPdf(pdfPath, deviceName, data)
.then(() => {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印成功,打印类型:PDF,打印机:${deviceName},页数:${data.pageNum}`
);
if (socket) {
const result = {
msg: "打印成功",
templateId: data.templateId,
replyId: data.replyId
replyId: data.replyId,
};
socket.emit("successs", result); // 兼容 vue-plugin-hiprint 0.0.56 之前包
socket.emit("success", result);
}
})
.catch((err) => {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印失败,打印类型:PDF,打印机:${deviceName},原因:${err.message}`
);
socket &&
socket.emit("error", {
msg: "打印失败: " + err.message,
templateId: data.templateId,
replyId: data.replyId
replyId: data.replyId,
});
})
.finally(() => {
// 通过taskMap 调用 task done 回调
PRINT_RUNNER_DONE[data.taskId]();
// 删除 task
delete PRINT_RUNNER_DONE[data.taskId];
MAIN_WINDOW.webContents.send(
"printTask",
PRINT_RUNNER.isBusy()
);
MAIN_WINDOW.webContents.send("printTask", PRINT_RUNNER.isBusy());
});
});
return;
Expand All @@ -155,6 +159,9 @@ function initPrintEvent() {
if (isUrlPdf) {
printPdf(data.pdf_path, deviceName, data)
.then(() => {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印成功,打印类型:URL_PDF,打印机:${deviceName},页数:${data.pageNum}`
);
if (socket) {
const result = {
msg: "打印成功",
Expand All @@ -166,22 +173,22 @@ function initPrintEvent() {
}
})
.catch((err) => {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印失败,打印类型:URL_PDF,打印机:${deviceName},原因:${err.message}`
);
socket &&
socket.emit("error", {
msg: "打印失败: " + err.message,
templateId: data.templateId,
replyId: data.replyId
replyId: data.replyId,
});
})
.finally(() => {
// 通过 taskMap 调用 task done 回调
PRINT_RUNNER_DONE[data.taskId]();
// 删除 task
delete PRINT_RUNNER_DONE[data.taskId];
MAIN_WINDOW.webContents.send(
"printTask",
PRINT_RUNNER.isBusy()
);
MAIN_WINDOW.webContents.send("printTask", PRINT_RUNNER.isBusy());
});
return;
}
Expand Down Expand Up @@ -211,6 +218,9 @@ function initPrintEvent() {
(success, failureReason) => {
if (socket) {
if (success) {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印成功,打印类型 HTML,打印机:${deviceName},页数:${data.pageNum}`
);
const result = {
msg: "打印成功",
templateId: data.templateId,
Expand All @@ -219,6 +229,9 @@ function initPrintEvent() {
socket.emit("successs", result); // 兼容 vue-plugin-hiprint 0.0.56 之前包
socket.emit("success", result);
} else {
log(
`${data.replyId?'中转服务':'插件端'} ${socket.id} 模板 【${data.templateId}】 打印失败,打印类型 HTML,打印机:${deviceName},原因:${failureReason}`
);
socket.emit("error", {
msg: failureReason,
templateId: data.templateId,
Expand Down
4 changes: 3 additions & 1 deletion src/set.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* @Date: 2023-09-05 17:34:28
* @LastEditors: [email protected]
* @LastEditTime: 2024-02-20 13:09:38
* @LastEditTime: 2024-03-09 12:00:46
* @FilePath: \electron-hiprint\src\set.js
*/
"use strict";

const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const path = require("path");
const { store } = require("../tools/utils");
const log = require("../tools/log");

/**
* @description: 创建设置窗口
Expand Down Expand Up @@ -65,6 +66,7 @@ function getConfig(event) {
* @return {Void}
*/
function setConfig(event, data) {
log("==> 设置窗口:保存配置 <==")
// 保存配置前,弹出 dialog 确认
dialog
.showMessageBox(SET_WINDOW, {
Expand Down
63 changes: 63 additions & 0 deletions tools/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* @Date: 2024-03-08 13:05:10
* @LastEditors: [email protected]
* @LastEditTime: 2024-03-09 12:53:03
* @FilePath: \electron-hiprint\tools\log.js
*/
const { app } = require("electron");
const { access, appendFile, constants, writeFile } = require("node:fs");
const dayjs = require("dayjs");

const logs = app.getPath('logs')

/**
* This function checks if a log file exists. If it does not exist, a new log file will be created.
* @returns {Promise} A Promise object that resolves if the file exists, or rejects if creating the file fails.
*/
function checkLogFile() {
const filePath = `${logs}/${dayjs().format("YYYY-MM-DD")}.log`;
return new Promise((resolve, reject) => {
access(filePath, constants.F_OK, (err) => {
if (err) {
writeFile(filePath, "", (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
} else {
resolve();
}
});
});
}

/**
* Writes log message to log file.
* @param {string} message - The log message to be written.
* @returns {Promise} - A Promise object that resolves when writing is successful, or rejects when writing fails.
*/
function log(message) {
const filePath = `${logs}/${dayjs().format("YYYY-MM-DD")}.log`;
return new Promise((resolve, reject) => {
checkLogFile()
.then(() => {
const logMessage = `${dayjs().format(
"YYYY/MM/DD HH:mm:ss"
)}: ${message}\n`;
appendFile(filePath, logMessage, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
})
.catch((err) => {
reject(err);
});
});
}

module.exports = log;
Loading

0 comments on commit 2f88c41

Please sign in to comment.