diff --git a/.env.production b/.env.production index a2c524a..c9256cd 100644 --- a/.env.production +++ b/.env.production @@ -2,4 +2,4 @@ ENV = production # 线上环境接口地址 -VITE_API_URL = 'https://cashierclient.sxczgkj.cn/cashier-client/api/' \ No newline at end of file +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 49faa13..3085f13 100644 --- a/dist-electron/main.js +++ b/dist-electron/main.js @@ -1,38 +1,11 @@ "use strict"; const electron = require("electron"); const path = require("path"); -function printUtils(params) { - 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 - } - }); - 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; - }); - electron.ipcMain.on("printStart", () => { - console.log("开始打印"); - subMainWindow.webContents.print({ - silent: true - }); - }); - }); -} electron.app.whenReady().then(() => { const win = new electron.BrowserWindow({ title: "银收客", - width: 1200, - height: 800, + width: 1024, + height: 768, fullscreenable: true, fullscreen: false, simpleFullscreen: true, @@ -48,7 +21,6 @@ electron.app.whenReady().then(() => { } else { win.loadFile(path.resolve(__dirname, "../dist/index.html")); } - win.webContents.openDevTools(); electron.app.on("activate", () => { if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); @@ -57,8 +29,29 @@ electron.app.whenReady().then(() => { electron.ipcMain.on("quitHandler", (_, msg) => { electron.app.quit(); }); - electron.ipcMain.on("printerInfoSync", async (event, params) => { - await printUtils(params); + const printWin = new electron.BrowserWindow({ + show: false, + webPreferences: { + // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 + nodeIntegration: true, + contextIsolation: false + } + }); + if (process.env.VITE_DEV_SERVER_URL) { + printWin.loadFile(path.join(__dirname, "../public/print.html")); + } else { + printWin.loadFile(path.resolve(__dirname, "../dist/print.html")); + } + printWin.webContents.openDevTools(); + electron.ipcMain.on("printerInfoSync", (event, params) => { + console.log(JSON.parse(params)); + printWin.webContents.send("getParams", params); + }); + electron.ipcMain.on("printStart", () => { + console.log("开始打印"); + printWin.webContents.print({ + silent: true + }); }); }); electron.app.on("window-all-closed", () => { diff --git a/electron/main.js b/electron/main.js index 50313f8..e911ac2 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,12 +1,11 @@ import { app, BrowserWindow, ipcMain } from "electron"; import path from 'path' -import { printUtils } from './printUtils' app.whenReady().then(() => { const win = new BrowserWindow({ title: "银收客", - width: 1200, - height: 800, + width: 1024, + height: 768, fullscreenable: true, fullscreen: false, simpleFullscreen: true, @@ -22,10 +21,10 @@ app.whenReady().then(() => { if (process.env.VITE_DEV_SERVER_URL) { win.loadURL(process.env.VITE_DEV_SERVER_URL); // 使用vite开发服务的url路径访问应用 + // win.webContents.openDevTools(); } else { win.loadFile(path.resolve(__dirname, '../dist/index.html')); // 打包后使用文件路径访问应用 } - win.webContents.openDevTools(); app.on("activate", () => { // 在 macOS 系统内, 如果没有已开启的应用窗口 @@ -39,8 +38,36 @@ app.whenReady().then(() => { app.quit(); }); - ipcMain.on('printerInfoSync', async (event, params) => { - await printUtils(params) + // 创建打印小票子窗口 + const printWin = new BrowserWindow({ + show: false, + webPreferences: { + // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 + nodeIntegration: true, + contextIsolation: false, + } + }) + + if (process.env.VITE_DEV_SERVER_URL) { + // 加载打印的html文件 + printWin.loadFile(path.join(__dirname, "../public/print.html")); + } else { + printWin.loadFile(path.resolve(__dirname, '../dist/print.html')); // 打包后使用文件路径访问应用 + } + + printWin.webContents.openDevTools() + + ipcMain.on('printerInfoSync', (event, params) => { + console.log(JSON.parse(params)) + printWin.webContents.send('getParams', params) + }) + + // 执行打印操作 + ipcMain.on('printStart', () => { + console.log('开始打印') + printWin.webContents.print({ + silent: true + }) }) }); diff --git a/electron/printUtils.js b/electron/printUtils.js deleted file mode 100644 index fecd784..0000000 --- a/electron/printUtils.js +++ /dev/null @@ -1,54 +0,0 @@ -import { BrowserWindow, ipcMain } from 'electron'; -import path from "path"; - -export function printUtils(params) { - 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 - } - }); - - // 加载打印的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) => { - // 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: '', - copies: 1, - }, (success) => { - if (success) { - resolvePrint({ type: 'success' }) - } - subMainWindow.close(); - }) - }) - .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 847b5d3..7e4976b 100644 --- a/public/print.html +++ b/public/print.html @@ -7,69 +7,40 @@