对接完毕下单
This commit is contained in:
@@ -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", () => {
|
||||
|
||||
63
electron/printUtils.js
Normal file
63
electron/printUtils.js
Normal file
@@ -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))
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user