From d00612da5abbd9693ea463498fc533db188ce75c Mon Sep 17 00:00:00 2001 From: gyq <875626088@qq.com> Date: Tue, 5 Mar 2024 09:52:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 6 ++ .env.production | 5 + dist-electron/main.js | 56 +++-------- electron/main.js | 11 +-- electron/printUtils.js | 37 +++---- public/print.html | 102 +++++++++++++++++--- src/assets/xp.png | Bin 0 -> 59685 bytes src/utils/request.js | 2 +- src/views/home/components/settleAccount.vue | 15 +-- src/views/home/index.vue | 2 +- 10 files changed, 143 insertions(+), 93 deletions(-) create mode 100644 .env.development create mode 100644 .env.production create mode 100644 src/assets/xp.png diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..f2a84a7 --- /dev/null +++ b/.env.development @@ -0,0 +1,6 @@ +# 本地环境 +ENV = development + +# 本地环境接口地址 +# VITE_API_URL = 'http://192.168.2.87:10587/cashier-client' +VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client' \ No newline at end of file diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..c9256cd --- /dev/null +++ b/.env.production @@ -0,0 +1,5 @@ +# 线上环境 +ENV = production + +# 线上环境接口地址 +VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client/' \ No newline at end of file diff --git a/dist-electron/main.js b/dist-electron/main.js index bc9ec83..3d00bfd 100644 --- a/dist-electron/main.js +++ b/dist-electron/main.js @@ -1,10 +1,10 @@ "use strict"; const electron = require("electron"); const path = require("path"); -function printUtils(data) { +function printUtils(params) { return new Promise(async (resolvePrint, rejectPrint) => { let subMainWindow = new electron.BrowserWindow({ - show: false, + // show: false, webPreferences: { nodeIntegration: true, // For electron >= 4.0.0 @@ -13,38 +13,18 @@ function printUtils(data) { enableRemoteModule: true } }); - function renderPrintDocument(window, data2) { - return new Promise(async (resolve, reject) => { - electron.ipcMain.on("load-ok", (event, res) => { - setTimeout(() => { - resolve({ message: "page-rendered", ...res }); - }, 500); - }); - }); - } - subMainWindow.on("closed", () => { - subMainWindow = null; - }); - subMainWindow.loadFile(path.resolve(__dirname, "./public/print.html")); + subMainWindow.loadFile(path.join(__dirname, "../public/print.html")); subMainWindow.webContents.on("did-finish-load", async (res) => { - return renderPrintDocument().then(async (result) => { - let height = Math.ceil((result.height + 60) * 264.5833); - console.info("height", result, height); - subMainWindow.webContents.print({ - silent: true, - margins: { - marginType: "none" - }, - printBackground: false, - deviceName: options.printerName, - copies: 1 - }, (success) => { - if (success) { - resolvePrint({ type: "success" }); - } - subMainWindow.close(); - }); - }).catch((err) => console.warn(33, err)); + subMainWindow.webContents.openDevTools(); + console.log("网页加载完成", res); + subMainWindow.webContents.send("getParams", params); + return; + }); + electron.ipcMain.on("printStart", () => { + console.log("开始打印"); + subMainWindow.webContents.print({ + silent: true + }); }); }); } @@ -54,9 +34,9 @@ electron.app.whenReady().then(() => { width: 1200, height: 800, fullscreenable: true, - fullscreen: false, + fullscreen: true, simpleFullscreen: true, - frame: true, + frame: false, webPreferences: { // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 nodeIntegration: true, @@ -68,7 +48,6 @@ electron.app.whenReady().then(() => { } else { win.loadFile("dist/index.html"); } - win.webContents.openDevTools(); electron.app.on("activate", () => { if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); @@ -78,10 +57,7 @@ electron.app.whenReady().then(() => { electron.app.quit(); }); electron.ipcMain.on("printerInfoSync", async (event, params) => { - console.log("接收到打印消息", params); - const res = await printUtils(); - event.returnValue = res; - console.log("已打印", res); + await printUtils(params); }); }); electron.app.on("window-all-closed", () => { diff --git a/electron/main.js b/electron/main.js index 6f6ddce..f5f0c65 100644 --- a/electron/main.js +++ b/electron/main.js @@ -7,9 +7,9 @@ app.whenReady().then(() => { width: 1200, height: 800, fullscreenable: true, - fullscreen: false, + fullscreen: true, simpleFullscreen: true, - frame: true, + frame: false, webPreferences: { // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 nodeIntegration: true, @@ -24,7 +24,7 @@ app.whenReady().then(() => { // Load your file win.loadFile("dist/index.html"); } - win.webContents.openDevTools(); + // win.webContents.openDevTools(); app.on("activate", () => { // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 @@ -38,10 +38,7 @@ app.whenReady().then(() => { }); ipcMain.on('printerInfoSync', async (event, params) => { - console.log('接收到打印消息', params) - const res = await printUtils(params) - event.returnValue = res - console.log('已打印', res) + await printUtils(params) }) }); diff --git a/electron/printUtils.js b/electron/printUtils.js index 7b0f638..fecd784 100644 --- a/electron/printUtils.js +++ b/electron/printUtils.js @@ -1,12 +1,10 @@ import { BrowserWindow, ipcMain } from 'electron'; import path from "path"; - -export function printUtils(data) { +export function printUtils(params) { return new Promise(async (resolvePrint, rejectPrint) => { - let subMainWindow = new BrowserWindow({ - show: false, + // show: false, webPreferences: { nodeIntegration: true, // For electron >= 4.0.0 contextIsolation: false, @@ -15,26 +13,14 @@ export function printUtils(data) { } }); - function renderPrintDocument(window, data) { - return new Promise(async (resolve, reject) => { - ipcMain.on('load-ok', (event, res) => { - //在这里可以添加打印的判断条件等...... - setTimeout(() => { - resolve({ message: 'page-rendered', ...res }); - }, 500) - }) - }) - } - - - // If the subMainWindow is closed, reset the `subMainWindow` var to null - subMainWindow.on('closed', () => { - subMainWindow = null; - }); // 加载打印的html文件 - subMainWindow.loadFile(path.resolve(__dirname, "./public/print.html")); + subMainWindow.loadFile(path.join(__dirname, "../public/print.html")); subMainWindow.webContents.on('did-finish-load', async (res) => { + subMainWindow.webContents.openDevTools(); + console.log('网页加载完成', res) + subMainWindow.webContents.send('getParams', params) + return let data = [] return renderPrintDocument(subMainWindow, data) .then(async (result) => { @@ -47,10 +33,9 @@ export function printUtils(data) { marginType: 'none' }, printBackground: false, - deviceName: options.printerName, + deviceName: '', copies: 1, }, (success) => { - if (success) { resolvePrint({ type: 'success' }) } @@ -59,5 +44,11 @@ export function printUtils(data) { }) .catch(err => console.warn(33, err)) }) + ipcMain.on('printStart', () => { + console.log('开始打印') + subMainWindow.webContents.print({ + silent: true + }) + }) }) } \ No newline at end of file diff --git a/public/print.html b/public/print.html index 183d557..e2b37a1 100644 --- a/public/print.html +++ b/public/print.html @@ -10,25 +10,99 @@ @page { size: auto; } + + html { + padding-bottom: 100px; + } + + .title_wrap { + font-weight: bold; + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + padding: 50px 0; + } + + .title { + font-size: 24px; + } + + .min_title { + font-size: 18px; + padding-top: 10px; + } + + .item { + padding: 20px 0; + } + + .item.b { + font-weight: bold; + } + + .img { + width: 100%; + display: block; + }
-