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/.gitignore b/.gitignore index 62d0178..cce5b04 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ pnpm-debug.log* lerna-debug.log* node_modules +release +dist-electron dist dist-ssr diff --git a/dist-electron/main.js b/dist-electron/main.js index bc9ec83..fb3d5a9 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) { +const path$1 = require("path"); +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,50 +13,31 @@ 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$1.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 + }); }); }); } +const NODE_ENV = process.env.NODE_ENV; electron.app.whenReady().then(() => { const win = new electron.BrowserWindow({ - title: "Main window", + title: "银收客", width: 1200, height: 800, fullscreenable: true, - fullscreen: false, + fullscreen: true, simpleFullscreen: true, - frame: true, + frame: false, webPreferences: { // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 nodeIntegration: true, @@ -66,9 +47,11 @@ electron.app.whenReady().then(() => { if (process.env.VITE_DEV_SERVER_URL) { win.loadURL(process.env.VITE_DEV_SERVER_URL); } else { - win.loadFile("dist/index.html"); + win.loadFile(path.join(__dirname, "../dist/index.html")); + } + if (NODE_ENV == "development") { + win.webContents.openDevTools(); } - win.webContents.openDevTools(); electron.app.on("activate", () => { if (electron.BrowserWindow.getAllWindows().length === 0) { createWindow(); @@ -78,10 +61,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..bb05ed3 100644 --- a/electron/main.js +++ b/electron/main.js @@ -1,15 +1,17 @@ import { app, BrowserWindow, ipcMain } from "electron"; import { printUtils } from './printUtils' +const NODE_ENV = process.env.NODE_ENV + app.whenReady().then(() => { const win = new BrowserWindow({ - title: "Main window", + title: "银收客", width: 1200, height: 800, fullscreenable: true, - fullscreen: false, + fullscreen: true, simpleFullscreen: true, - frame: true, + frame: false, webPreferences: { // 集成网页和 Node.js,也就是在渲染进程中,可以调用 Node.js 方法 nodeIntegration: true, @@ -19,12 +21,15 @@ app.whenReady().then(() => { // 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); + win.loadURL(process.env.VITE_DEV_SERVER_URL); // 使用vite开发服务的url路径访问应用 } else { - // Load your file - win.loadFile("dist/index.html"); + win.loadFile(path.join(__dirname, '../dist/index.html')); // 打包后使用文件路径访问应用 } - win.webContents.openDevTools(); + + if (NODE_ENV == 'development') { + win.webContents.openDevTools(); + } + app.on("activate", () => { // 在 macOS 系统内, 如果没有已开启的应用窗口 // 点击托盘图标时通常会重新创建一个新窗口 @@ -38,10 +43,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/package.json b/package.json index 902a133..29ef93b 100644 --- a/package.json +++ b/package.json @@ -5,14 +5,13 @@ "main": "dist-electron/main.js", "scripts": { "dev": "chcp 65001 && vite", - "build": "vite build", + "build": "vite build && electron-builder", "preview": "vite preview" }, "dependencies": { "@element-plus/icons-vue": "^2.3.1", "axios": "^1.6.2", "dayjs": "^1.11.10", - "electron": "^29.0.1", "element-plus": "^2.4.3", "lodash": "^4.17.21", "pinia": "^2.1.7", @@ -22,6 +21,7 @@ "devDependencies": { "@vitejs/plugin-vue": "^4.5.0", "electron": "^28.2.3", + "electron-builder": "^24.13.3", "path": "^0.12.7", "sass": "^1.69.5", "sass-loader": "^13.3.2", @@ -29,5 +29,29 @@ "vite": "^5.0.0", "vite-plugin-electron": "^0.15.4", "vite-plugin-electron-renderer": "^0.14.5" + }, + "build": { + "appId": "com.example.app", + "productName": "cashier_desktop", + "asar": true, + "directories": { + "buildResources": "build", + "output": "release" + }, + "win": { + "icon": "./src/assets/logo.ico", + "target": "nsis" + }, + "nsis": { + "oneClick": false, + "allowElevation": true, + "allowToChangeInstallationDirectory": true, + "installerIcon": "./src/assets/logo.ico", + "uninstallerIcon": "./src/assets/logo.ico", + "installerHeaderIcon": "./src/assets/logo.ico", + "createDesktopShortcut": true, + "createStartMenuShortcut": true, + "license": "LICENSE.txt" + } } } 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; + }
-