104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
import path from "path";
|
||
import { app, BrowserWindow, ipcMain } from "electron";
|
||
|
||
let win;
|
||
app.whenReady().then(() => {
|
||
win = new BrowserWindow({
|
||
title: "银收客",
|
||
width: 1024,
|
||
height: 768,
|
||
fullscreenable: true,
|
||
fullscreen: process.env.VITE_DEV_SERVER_URL ? false : true,
|
||
simpleFullscreen: true,
|
||
frame: process.env.VITE_DEV_SERVER_URL ? true : false,
|
||
webPreferences: {
|
||
// 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法
|
||
nodeIntegration: true,
|
||
contextIsolation: false,
|
||
},
|
||
});
|
||
|
||
// You can use `process.env.VITE_DEV_SERVER_URL` when the vite command is called `serve`
|
||
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")); // 打包后使用文件路径访问应用
|
||
}
|
||
|
||
app.on("activate", () => {
|
||
// 在 macOS 系统内, 如果没有已开启的应用窗口
|
||
// 点击托盘图标时通常会重新创建一个新窗口
|
||
if (BrowserWindow.getAllWindows().length === 0) {
|
||
createWindow();
|
||
}
|
||
});
|
||
|
||
ipcMain.on("quitHandler", (_, msg) => {
|
||
app.quit();
|
||
});
|
||
|
||
// 给渲染进程返回打印机列表
|
||
ipcMain.on('getPrintList', () => {
|
||
win.webContents.getPrintersAsync().then(res => {
|
||
win.webContents.send('printList', res)
|
||
})
|
||
})
|
||
|
||
// 创建打印小票子窗口
|
||
const printWin = new BrowserWindow({
|
||
show: true,
|
||
width: 464,
|
||
height: 2206,
|
||
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")); // 打包后使用文件路径访问应用
|
||
}
|
||
|
||
// 接收订单页面发过来的参数发送给打印页
|
||
ipcMain.on('printerInfoSync', (event, arg) => {
|
||
printWin.webContents.send('getParams', arg)
|
||
})
|
||
|
||
// 执行打印操作
|
||
printWin.on('printStart', (event, arg) => {
|
||
const _parmas = JSON.parse(arg)
|
||
let name = _parmas.deviceName
|
||
printWin.webContents.print({
|
||
silent: true,
|
||
deviceName: name,
|
||
pageSize: {
|
||
width: 58000,
|
||
height: 276000
|
||
},
|
||
scaleFactor: 80,
|
||
landscape: false,
|
||
margins: {
|
||
marginType: "none",
|
||
top: 0,
|
||
bottom: 0,
|
||
left: 0,
|
||
right: 0
|
||
},
|
||
dpi: {
|
||
horizontal: 203,
|
||
vertical: 203
|
||
}
|
||
})
|
||
})
|
||
});
|
||
|
||
app.on("window-all-closed", () => {
|
||
if (process.platform !== "darwin") app.quit();
|
||
});
|