|
1 | 1 | /* eslint-disable no-await-in-loop */ |
2 | | -import path from 'path'; |
3 | 2 | import EscPosEncoder from '@freedom_sky/esc-pos-encoder'; |
4 | | -import fs from 'fs-extra'; |
5 | 3 | import superagent from 'superagent'; |
6 | 4 | import { config } from '../config'; |
7 | 5 | import { |
@@ -33,65 +31,37 @@ const i18n = { |
33 | 31 | }, |
34 | 32 | }; |
35 | 33 |
|
36 | | -let template = [ |
37 | | - '#align center', |
38 | | - '', |
39 | | - '#bold true', |
40 | | - '#size 2', |
41 | | - '#receipt', |
42 | | - '', |
43 | | - '#id', |
44 | | - '', |
45 | | - '#bold false', |
46 | | - '#size 1', |
47 | | - '===============================', |
48 | | - '', |
49 | | - '#location', |
50 | | - '#problem', |
51 | | - '#color', |
52 | | - '#comment', |
53 | | - '', |
54 | | - '#align center', |
55 | | - '#bold true', |
56 | | - '===============================', |
57 | | - '', |
58 | | - '#size 0', |
59 | | - '#team', |
60 | | - '#status', |
61 | | - '#time', |
62 | | -]; |
63 | | -try { |
64 | | - template = fs.readFileSync(path.resolve(process.cwd(), 'balloon.template'), 'utf8').split('\n').map((i) => i.trim()); |
65 | | -} catch (e) { |
66 | | - logger.info('Using builtin balloon template'); |
67 | | -} |
68 | | - |
69 | 34 | export const receiptBalloonText = ( |
70 | 35 | id: number, location: string, problem: string, color: string, comment: string, teamname: string, status: string, lang: 'zh' | 'en' = 'zh', |
71 | 36 | ) => { |
72 | 37 | let enc = encoder.initialize().codepage('cp936').setPinterType(config.balloonType ?? 80); |
73 | | - const commands = { |
74 | | - align: (align: 'center' | 'left' | 'right') => enc.align(align), |
75 | | - emptyLine: (lines: number) => enc.emptyLine(lines), |
76 | | - bold: (bold: boolean) => enc.bold(bold), |
77 | | - size: (size: number) => enc.size(size), |
78 | | - line: (line: string) => enc.line(line), |
79 | | - oneLine: (left: string, right: string) => enc.oneLine(left, right), |
80 | | - cut: () => enc.cut(), |
81 | | - id: () => enc.line(`ID: ${String(id).substring(0, 8)}`), |
82 | | - location: () => enc.oneLine(i18n[lang].location, location), |
83 | | - problem: () => enc.oneLine(i18n[lang].problem, problem), |
84 | | - color: () => enc.oneLine(i18n[lang].color, color), |
85 | | - comment: () => enc.oneLine(i18n[lang].comment, comment), |
86 | | - team: () => enc.line(`${i18n[lang].team}: ${teamname}`), |
87 | | - status: () => enc.line(`${i18n[lang].status}:\n${status}`), |
88 | | - time: () => enc.line(`Time: ${new Date().toLocaleString()}`), |
89 | | - }; |
90 | | - for (const line of template) { |
91 | | - if (!line.startsWith('#')) enc = enc.line(line); |
| 38 | + const whitelist = [ |
| 39 | + 'align', 'barcode', 'bold', 'cut', 'curPartial', |
| 40 | + 'emptyLine', 'image', 'italic', 'line', 'newLine', |
| 41 | + 'oneLine', 'qrcode', 'size', 'text', 'underline', |
| 42 | + ]; |
| 43 | + const replace = (input: string) => input |
| 44 | + .replace(/\{id\}/g, String(id).substring(0, 8)) |
| 45 | + .replace(/\{location\}/g, location) |
| 46 | + .replace(/\{problem\}/g, problem) |
| 47 | + .replace(/\{color\}/g, color) |
| 48 | + .replace(/\{comment\}/g, comment) |
| 49 | + .replace(/\{team\}/g, teamname) |
| 50 | + .replace(/\{status\}/g, status) |
| 51 | + .replace(/\{time\}/g, new Date().toLocaleString()) |
| 52 | + .replace(/%LOCATION/g, i18n[lang].location) |
| 53 | + .replace(/%PROBLEM/g, i18n[lang].problem) |
| 54 | + .replace(/%COLOR/g, i18n[lang].color) |
| 55 | + .replace(/%COMMENT/g, i18n[lang].comment) |
| 56 | + .replace(/%TEAM/g, i18n[lang].team) |
| 57 | + .replace(/%STATUS/g, i18n[lang].status) |
| 58 | + .replace(/%RECEIPT/g, i18n[lang].receipt); |
| 59 | + for (const line of config.balloonTemplate.split('\n')) { |
| 60 | + if (!line.startsWith('#')) enc = enc.line(replace(line)); |
92 | 61 | const [command, ...rawArgs] = line.slice(1).split(' '); |
93 | | - const args = rawArgs.map((arg) => (arg === 'true' ? true : arg === 'false' ? false : Number.isSafeInteger(arg) ? +arg : arg)); |
94 | | - if (commands[command]) enc = commands[command](...args); |
| 62 | + const args = rawArgs.map((arg) => (arg === 'true' ? true : arg === 'false' ? false : Number.isSafeInteger(arg) ? +arg : replace(arg))); |
| 63 | + if (whitelist.includes(command)) enc = enc[command](...args); |
| 64 | + else logger.warn(`Unsupported printer command: ${command}`); |
95 | 65 | } |
96 | 66 | return enc |
97 | 67 | .line('Powered by hydro-dev/xcpc-tools') |
|
0 commit comments