Skip to content

Commit 50b1f5c

Browse files
committed
try support win receipt
1 parent a113dbf commit 50b1f5c

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

packages/server/client/balloon.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-await-in-loop */
2+
import { exec } from 'child_process';
23
import path from 'path';
34
import EscPosEncoder from '@freedom_sky/esc-pos-encoder';
45
import superagent from 'superagent';
@@ -73,6 +74,7 @@ let timer = null;
7374
let printer = null;
7475

7576
async function getReceiptStatus(receipt) {
77+
if (process.platform === 'win32') printer = { printer: receipt };
7678
const lp = receipt.split('/').pop();
7779
const oldPrinter = printer;
7880
printer = {
@@ -109,7 +111,14 @@ async function printBalloon(doc, lang) {
109111
);
110112
if (printer) {
111113
await getReceiptStatus(printer.printer);
112-
fs.writeFileSync(path.resolve(printer.printer), bReceipt);
114+
if (process.platform === 'win32') {
115+
fs.writeFileSync(path.resolve(process.cwd(), 'data', 'balloon.txt'), bReceipt);
116+
exec(`COPY /B "${path.resolve(process.cwd(), 'data', 'balloon.txt')}" "${printer.printer}"`, (err, stdout, stderr) => {
117+
if (err) logger.error(err);
118+
if (stdout) logger.info(stdout);
119+
if (stderr) logger.error(stderr);
120+
});
121+
} else fs.writeFileSync(path.resolve(printer.printer), bReceipt);
113122
}
114123
}
115124

packages/server/config.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import path from 'path';
22
import Schema from 'schemastery';
33
import { version } from './package.json';
44
import {
5-
fs, getPrinters, Logger, yaml,
5+
fs, getPrinters, getWinReceiptPrinter,
6+
Logger, yaml,
67
} from './utils';
78

89
const logger = new Logger('init');
@@ -42,7 +43,15 @@ password:
4243
logger.info(`If you want to use this printer for balloon print, please set balloon: /dev/usb/${f} in config.yaml.`);
4344
}
4445
}
45-
} else logger.info('If you want to use balloon client, please run this on linux.');
46+
if (!usbDevices.length) logger.info('If you want to use balloon client, please connect your receipt printer first.');
47+
} else if (process.platform === 'win32') {
48+
const printerList = await getWinReceiptPrinter();
49+
for (const printer of printerList) {
50+
logger.info(`Receipt Printer ${printer.printer}(${printer.device})) found: ${printer.description}`);
51+
logger.info(`If you want to use this printer for balloon print, please set balloon: ${printer.printer} in config.yaml.`);
52+
}
53+
if (!printers.length) logger.info('If you want to use balloon client, please share your receipt printer on settings first.');
54+
} else logger.info('If you want to use balloon client, please run this on Linux/Windows.');
4655
}
4756
const clientConfigDefault = yaml.dump({
4857
server: '',

packages/server/utils/printers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ export async function print(file: string, printer: string, startPage?: number, e
6363
}
6464
return unixPrint(file, printer, startPage && endPage ? ['-P', `${startPage}-${endPage}`] : []);
6565
}
66+
67+
export async function getWinReceiptPrinter() {
68+
const winprinters = await wingetPrinters();
69+
return winprinters.filter((p: any) => p.DeviceID).filter((p: any) => p.ShareName).map((p: any) => ({
70+
printer: `\\\\${p.SystemName}\\${p.ShareName}`,
71+
device: p.DeviceID,
72+
description: p.Caption,
73+
status: windowsPrinterStatus[p.PrinterStatus] ? windowsPrinterStatus[p.PrinterStatus] : 'unknown',
74+
}));
75+
}

0 commit comments

Comments
 (0)