From 27721ca0960f8368ff038fd98401491fbba6c0d2 Mon Sep 17 00:00:00 2001 From: gyq <875626088@qq.com> Date: Mon, 4 Mar 2024 15:48:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E5=AE=8C=E6=AF=95=E4=B8=8B?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist-electron/main.js | 58 +++- electron/main.js | 13 +- electron/printUtils.js | 63 ++++ index.html | 4 +- public/print.html | 34 ++ src/App.vue | 4 +- src/api/pay.js | 79 +++++ src/api/product.js | 13 + src/assets/icon_scan.png | Bin 0 -> 2418 bytes src/components/payCard.vue | 199 ----------- src/components/payCard/payCard.vue | 311 ++++++++++++++++++ src/components/payCard/scanModal.vue | 272 +++++++++++++++ src/components/remarkModal.vue | 9 +- src/router/index.js | 1 + src/utils/index.js | 26 ++ src/utils/request.js | 2 +- src/views/home/components/goods.vue | 18 +- .../home/components/pendingCartModal.vue | 3 + src/views/home/components/settleAccount.vue | 50 ++- src/views/home/index.vue | 51 ++- src/views/home/test.vue | 37 +++ 21 files changed, 1010 insertions(+), 237 deletions(-) create mode 100644 electron/printUtils.js create mode 100644 public/print.html create mode 100644 src/api/pay.js create mode 100644 src/assets/icon_scan.png delete mode 100644 src/components/payCard.vue create mode 100644 src/components/payCard/payCard.vue create mode 100644 src/components/payCard/scanModal.vue create mode 100644 src/views/home/test.vue diff --git a/dist-electron/main.js b/dist-electron/main.js index 04c71b9..bc9ec83 100644 --- a/dist-electron/main.js +++ b/dist-electron/main.js @@ -1,5 +1,53 @@ "use strict"; const electron = require("electron"); +const path = require("path"); +function printUtils(data) { + return new Promise(async (resolvePrint, rejectPrint) => { + let subMainWindow = new electron.BrowserWindow({ + show: false, + webPreferences: { + nodeIntegration: true, + // For electron >= 4.0.0 + contextIsolation: false, + webSecurity: false, + 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.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)); + }); + }); +} electron.app.whenReady().then(() => { const win = new electron.BrowserWindow({ title: "Main window", @@ -22,13 +70,19 @@ electron.app.whenReady().then(() => { } win.webContents.openDevTools(); electron.app.on("activate", () => { - if (electron.BrowserWindow.getAllWindows().length === 0) + if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); + } }); electron.ipcMain.on("quitHandler", (_, msg) => { - console.log(msg); electron.app.quit(); }); + electron.ipcMain.on("printerInfoSync", async (event, params) => { + console.log("接收到打印消息", params); + const res = await printUtils(); + event.returnValue = res; + console.log("已打印", res); + }); }); electron.app.on("window-all-closed", () => { if (process.platform !== "darwin") diff --git a/electron/main.js b/electron/main.js index daeebae..6f6ddce 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,4 +1,5 @@ import { app, BrowserWindow, ipcMain } from "electron"; +import { printUtils } from './printUtils' app.whenReady().then(() => { const win = new BrowserWindow({ @@ -27,13 +28,21 @@ app.whenReady().then(() => { app.on("activate", () => { // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 - if (BrowserWindow.getAllWindows().length === 0) createWindow(); + if (BrowserWindow.getAllWindows().length === 0) { + createWindow() + }; }); ipcMain.on("quitHandler", (_, msg) => { - console.log(msg); app.quit(); }); + + ipcMain.on('printerInfoSync', async (event, params) => { + console.log('接收到打印消息', params) + const res = await printUtils(params) + event.returnValue = res + console.log('已打印', res) + }) }); app.on("window-all-closed", () => { diff --git a/electron/printUtils.js b/electron/printUtils.js new file mode 100644 index 0000000..7b0f638 --- /dev/null +++ b/electron/printUtils.js @@ -0,0 +1,63 @@ +import { BrowserWindow, ipcMain } from 'electron'; +import path from "path"; + + +export function printUtils(data) { + return new Promise(async (resolvePrint, rejectPrint) => { + + let subMainWindow = new BrowserWindow({ + show: false, + webPreferences: { + nodeIntegration: true, // For electron >= 4.0.0 + contextIsolation: false, + webSecurity: false, + enableRemoteModule: true + } + }); + + 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.webContents.on('did-finish-load', async (res) => { + let data = [] + return renderPrintDocument(subMainWindow, data) + .then(async (result) => { + // let width = Math.ceil((result.width) * 264.5833); + 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)) + }) + }) +} \ No newline at end of file diff --git a/index.html b/index.html index 433866d..2859b32 100644 --- a/index.html +++ b/index.html @@ -1,10 +1,10 @@ - +
- +