taro-bluetooth-print 2.2.1 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -165
- package/README.md +142 -285
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +1 -81644
- package/dist/index.umd.js +1 -1
- package/dist/types/adapters/AdapterFactory.d.ts +0 -1
- package/dist/types/adapters/AlipayAdapter.d.ts +0 -1
- package/dist/types/adapters/BaiduAdapter.d.ts +0 -1
- package/dist/types/adapters/BaseAdapter.d.ts +0 -1
- package/dist/types/adapters/ByteDanceAdapter.d.ts +0 -1
- package/dist/types/adapters/TaroAdapter.d.ts +0 -1
- package/dist/types/adapters/WebBluetoothAdapter.d.ts +0 -1
- package/dist/types/config/PrinterConfig.d.ts +0 -1
- package/dist/types/core/BluetoothPrinter.d.ts +1 -2
- package/dist/types/core/EventEmitter.d.ts +6 -26
- package/dist/types/core/index.d.ts +6 -0
- package/dist/types/drivers/CpclDriver.d.ts +304 -0
- package/dist/types/drivers/EscPos.d.ts +0 -1
- package/dist/types/drivers/GPrinterDriver.d.ts +63 -0
- package/dist/types/drivers/TsplDriver.d.ts +251 -0
- package/dist/types/drivers/ZplDriver.d.ts +325 -0
- package/dist/types/drivers/index.d.ts +9 -0
- package/dist/types/encoding/gbk-lite.d.ts +8 -0
- package/dist/types/encoding/gbk-table.d.ts +8 -30
- package/dist/types/index.d.ts +10 -5
- package/dist/types/plugins/PluginManager.d.ts +87 -0
- package/dist/types/plugins/builtin/LoggingPlugin.d.ts +14 -0
- package/dist/types/plugins/builtin/RetryPlugin.d.ts +18 -0
- package/dist/types/plugins/index.d.ts +7 -0
- package/dist/types/plugins/types.d.ts +97 -0
- package/dist/types/services/CommandBuilder.d.ts +0 -1
- package/dist/types/services/ConnectionManager.d.ts +1 -2
- package/dist/types/services/PrintJobManager.d.ts +0 -1
- package/dist/types/services/index.d.ts +8 -0
- package/dist/types/services/interfaces/index.d.ts +0 -1
- package/dist/types/template/TemplateEngine.d.ts +0 -1
- package/package.json +36 -20
- package/src/adapters/BaseAdapter.ts +6 -8
- package/src/core/BluetoothPrinter.ts +15 -15
- package/src/core/EventEmitter.ts +15 -15
- package/src/core/index.ts +7 -0
- package/src/drivers/CpclDriver.ts +549 -0
- package/src/drivers/GPrinterDriver.ts +115 -0
- package/src/drivers/TsplDriver.ts +405 -0
- package/src/drivers/ZplDriver.ts +543 -0
- package/src/drivers/index.ts +37 -0
- package/src/encoding/gbk-lite.ts +108 -0
- package/src/encoding/gbk-table.ts +80 -58
- package/src/index.ts +27 -23
- package/src/plugins/PluginManager.ts +195 -0
- package/src/plugins/builtin/LoggingPlugin.ts +99 -0
- package/src/plugins/builtin/RetryPlugin.ts +103 -0
- package/src/plugins/index.ts +10 -0
- package/src/plugins/types.ts +119 -0
- package/src/services/ConnectionManager.ts +22 -22
- package/src/services/index.ts +16 -0
package/README.md
CHANGED
|
@@ -4,12 +4,14 @@
|
|
|
4
4
|
<img src="https://img.shields.io/npm/v/taro-bluetooth-print?style=flat-square&color=00d8ff" alt="npm version">
|
|
5
5
|
<img src="https://img.shields.io/npm/dm/taro-bluetooth-print?style=flat-square&color=00d8ff" alt="downloads">
|
|
6
6
|
<img src="https://img.shields.io/npm/l/taro-bluetooth-print?style=flat-square&color=00d8ff" alt="license">
|
|
7
|
-
<img src="https://img.shields.io/
|
|
7
|
+
<img src="https://img.shields.io/github/stars/agions/taro-bluetooth-print?style=flat-square" alt="stars">
|
|
8
|
+
<img src="https://img.shields.io/github/forks/agions/taro-bluetooth-print?style=flat-square" alt="forks">
|
|
9
|
+
<img src="https://img.shields.io/bundlephobia/minzip/taro-bluetooth-print?style=flat-square" alt="bundle size">
|
|
8
10
|
</p>
|
|
9
11
|
|
|
10
12
|
<p align="center">
|
|
11
13
|
<strong>轻量级、高性能的 Taro 蓝牙打印库</strong><br>
|
|
12
|
-
|
|
14
|
+
支持热敏打印机、标签打印机,多平台适配
|
|
13
15
|
</p>
|
|
14
16
|
|
|
15
17
|
---
|
|
@@ -17,342 +19,195 @@
|
|
|
17
19
|
## ✨ 特性
|
|
18
20
|
|
|
19
21
|
- 🚀 **高性能** - 直接字节缓冲区操作,服务缓存优化
|
|
20
|
-
- 📱
|
|
21
|
-
- 🎨
|
|
22
|
-
- 🖼️ **图片打印** - 内置 Floyd-Steinberg
|
|
23
|
-
- 📲
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
- 📶 **弱网适配** - 智能分片与重试机制,自适应传输参数
|
|
22
|
+
- 📱 **多平台** - 微信小程序、H5、鸿蒙、支付宝、百度、字节跳动
|
|
23
|
+
- 🎨 **多驱动** - ESC/POS (热敏)、TSPL/ZPL/CPCL (标签)
|
|
24
|
+
- 🖼️ **图片打印** - 内置 Floyd-Steinberg 抖动算法
|
|
25
|
+
- 📲 **二维码/条码** - 原生指令支持,多种格式
|
|
26
|
+
- 🔄 **断点续传** - 暂停/恢复/取消打印任务
|
|
27
|
+
- 📶 **弱网适配** - 智能分片与重试机制
|
|
27
28
|
- 📊 **进度追踪** - 实时打印进度事件
|
|
28
|
-
- 💾 **离线缓存** -
|
|
29
|
+
- 💾 **离线缓存** - 断网自动缓存,联网自动同步
|
|
29
30
|
- 📋 **打印队列** - 优先级排序,失败自动重试
|
|
30
31
|
- 📝 **模板引擎** - 内置收据和标签模板
|
|
31
|
-
- 🔍 **打印预览** - ESC/POS
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
- 🛠️ **TypeScript** - 完整的类型定义和 JSDoc 文档
|
|
35
|
-
- 🧪 **高测试覆盖** - 76%+ 代码覆盖率
|
|
32
|
+
- 🔍 **打印预览** - ESC/POS 命令渲染为图像
|
|
33
|
+
- 🔌 **插件系统** - 可扩展架构,支持自定义钩子
|
|
34
|
+
- 🛠️ **TypeScript** - 完整的类型定义和 JSDoc
|
|
36
35
|
|
|
37
36
|
## 📦 安装
|
|
38
37
|
|
|
39
38
|
```bash
|
|
39
|
+
# npm
|
|
40
40
|
npm install taro-bluetooth-print
|
|
41
|
-
|
|
41
|
+
|
|
42
|
+
# yarn
|
|
42
43
|
yarn add taro-bluetooth-print
|
|
43
|
-
|
|
44
|
+
|
|
45
|
+
# pnpm
|
|
44
46
|
pnpm add taro-bluetooth-print
|
|
45
47
|
```
|
|
46
48
|
|
|
47
|
-
##
|
|
49
|
+
## 📊 性能
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
| 指标 | 值 |
|
|
52
|
+
|------|-----|
|
|
53
|
+
| Bundle 大小 | **26 KB** (gzip) |
|
|
54
|
+
| Tree-shaking | ✅ 支持 |
|
|
55
|
+
| 按需加载 | ✅ 编码表懒加载 |
|
|
53
56
|
|
|
54
|
-
|
|
57
|
+
## 🚀 快速开始
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.text('------------------------')
|
|
69
|
-
.text('合计: ¥30.00')
|
|
70
|
-
.feed(2)
|
|
71
|
-
.qr('https://example.com', { size: 8 })
|
|
72
|
-
.feed(2)
|
|
73
|
-
.cut()
|
|
74
|
-
.print(); // 发送到打印机
|
|
75
|
-
|
|
76
|
-
console.log('打印成功!');
|
|
77
|
-
} catch (error) {
|
|
78
|
-
console.error('打印失败:', error);
|
|
79
|
-
} finally {
|
|
80
|
-
await printer.disconnect();
|
|
59
|
+
```typescript
|
|
60
|
+
import { BluetoothPrinter, DeviceManager } from 'taro-bluetooth-print';
|
|
61
|
+
|
|
62
|
+
async function print() {
|
|
63
|
+
// 1. 扫描设备
|
|
64
|
+
const manager = new DeviceManager();
|
|
65
|
+
await manager.startScan({ timeout: 10000 });
|
|
66
|
+
const devices = manager.getDiscoveredDevices();
|
|
67
|
+
|
|
68
|
+
if (devices.length === 0) {
|
|
69
|
+
console.log('未发现设备');
|
|
70
|
+
return;
|
|
81
71
|
}
|
|
72
|
+
|
|
73
|
+
// 2. 连接打印机
|
|
74
|
+
const printer = new BluetoothPrinter();
|
|
75
|
+
await printer.connect(devices[0].deviceId);
|
|
76
|
+
|
|
77
|
+
// 3. 打印
|
|
78
|
+
await printer
|
|
79
|
+
.text('=== 欢迎光临 ===', 'GBK')
|
|
80
|
+
.feed()
|
|
81
|
+
.text('商品A x1 ¥10.00', 'GBK')
|
|
82
|
+
.text('商品B x2 ¥20.00', 'GBK')
|
|
83
|
+
.feed()
|
|
84
|
+
.text('------------------------')
|
|
85
|
+
.text('合计: ¥30.00', 'GBK')
|
|
86
|
+
.feed(2)
|
|
87
|
+
.qr('https://example.com')
|
|
88
|
+
.feed(2)
|
|
89
|
+
.cut()
|
|
90
|
+
.print();
|
|
91
|
+
|
|
92
|
+
// 4. 断开
|
|
93
|
+
await printer.disconnect();
|
|
94
|
+
|
|
95
|
+
console.log('打印完成!');
|
|
82
96
|
}
|
|
83
97
|
```
|
|
84
98
|
|
|
85
|
-
|
|
99
|
+
## 🖥️ 支持的平台
|
|
86
100
|
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
| 平台 | 适配器 | 状态 |
|
|
102
|
+
|------|--------|------|
|
|
103
|
+
| 微信小程序 | `TaroAdapter` | ✅ |
|
|
104
|
+
| H5 (Web Bluetooth) | `WebBluetoothAdapter` | ✅ |
|
|
105
|
+
| 支付宝小程序 | `AlipayAdapter` | ✅ |
|
|
106
|
+
| 百度小程序 | `BaiduAdapter` | ✅ |
|
|
107
|
+
| 字节跳动小程序 | `ByteDanceAdapter` | ✅ |
|
|
108
|
+
| 鸿蒙 HarmonyOS | `HarmonyOSAdapter` | ✅ |
|
|
109
|
+
| React Native | `TaroAdapter` | ✅ |
|
|
89
110
|
|
|
90
|
-
|
|
91
|
-
Logger.setLevel(LogLevel.DEBUG);
|
|
111
|
+
## 🖨️ 支持的驱动
|
|
92
112
|
|
|
93
|
-
|
|
113
|
+
| 驱动 | 协议 | 适用打印机 |
|
|
114
|
+
|------|------|-----------|
|
|
115
|
+
| `EscPos` | ESC/POS | 热敏票据打印机 (58/80mm) |
|
|
116
|
+
| `TsplDriver` | TSPL | TSC 标签打印机 |
|
|
117
|
+
| `ZplDriver` | ZPL | Zebra 斑马标签打印机 |
|
|
118
|
+
| `CpclDriver` | CPCL | HP/霍尼韦尔移动打印机 |
|
|
94
119
|
|
|
95
|
-
|
|
96
|
-
printer.on('progress', ({ sent, total }) => {
|
|
97
|
-
const percent = ((sent / total) * 100).toFixed(1);
|
|
98
|
-
console.log(`打印进度: ${percent}%`);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// 监听错误
|
|
102
|
-
printer.on('error', error => {
|
|
103
|
-
console.error('打印错误:', error.code, error.message);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
// 监听完成
|
|
107
|
-
printer.on('print-complete', () => {
|
|
108
|
-
console.log('打印完成!');
|
|
109
|
-
});
|
|
120
|
+
### 标签打印示例 (TSPL)
|
|
110
121
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
```
|
|
122
|
+
```typescript
|
|
123
|
+
import { BluetoothPrinter, TsplDriver } from 'taro-bluetooth-print';
|
|
114
124
|
|
|
115
|
-
|
|
125
|
+
const driver = new TsplDriver();
|
|
126
|
+
const printer = new BluetoothPrinter(undefined, driver);
|
|
116
127
|
|
|
117
|
-
|
|
118
|
-
|
|
128
|
+
driver
|
|
129
|
+
.size(60, 40) // 60x40mm 标签
|
|
130
|
+
.gap(3) // 间隙 3mm
|
|
131
|
+
.clear()
|
|
132
|
+
.text('商品名称', { x: 20, y: 20, font: 3 })
|
|
133
|
+
.text('¥99.00', { x: 20, y: 60, font: 4 })
|
|
134
|
+
.barcode('6901234567890', { x: 20, y: 100, type: 'EAN13' })
|
|
135
|
+
.qrcode('https://example.com', { x: 250, y: 20 })
|
|
136
|
+
.print(1);
|
|
119
137
|
|
|
120
138
|
await printer.connect(deviceId);
|
|
139
|
+
await printer.print();
|
|
140
|
+
```
|
|
121
141
|
|
|
122
|
-
|
|
123
|
-
printer.text('第1页内容...').feed(10).text('第2页内容...').feed(10).text('第3页内容...');
|
|
142
|
+
## 📚 示例项目
|
|
124
143
|
|
|
125
|
-
|
|
126
|
-
const printPromise = printer.print();
|
|
144
|
+
完整的示例项目,帮助快速上手:
|
|
127
145
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}, 5000);
|
|
146
|
+
- [微信小程序示例](examples/weapp/) - 完整的打印页面
|
|
147
|
+
- [H5 示例](examples/h5/) - Web Bluetooth 网页打印
|
|
148
|
+
- [鸿蒙示例](examples/harmonyos/) - HarmonyOS 原生打印服务
|
|
149
|
+
- [React Native 示例](examples/react-native/) - RN 打印组件
|
|
133
150
|
|
|
134
|
-
|
|
135
|
-
setTimeout(async () => {
|
|
136
|
-
await printer.resume();
|
|
137
|
-
console.log('已恢复打印');
|
|
138
|
-
}, 10000);
|
|
151
|
+
## 📖 文档
|
|
139
152
|
|
|
140
|
-
|
|
141
|
-
|
|
153
|
+
- [快速开始](https://agions.github.io/taro-bluetooth-print/guide/getting-started) - 5 分钟入门
|
|
154
|
+
- [功能特性](https://agions.github.io/taro-bluetooth-print/guide/features) - 全部功能介绍
|
|
155
|
+
- [驱动支持](https://agions.github.io/taro-bluetooth-print/guide/drivers) - ESC/POS, TSPL, ZPL, CPCL
|
|
156
|
+
- [核心概念](https://agions.github.io/taro-bluetooth-print/guide/core-concepts) - 架构设计与原理
|
|
157
|
+
- [API 参考](https://agions.github.io/taro-bluetooth-print/api) - 完整的 API 文档
|
|
158
|
+
- [故障排除](https://agions.github.io/taro-bluetooth-print/guide/troubleshooting) - 常见问题解决
|
|
142
159
|
|
|
143
|
-
|
|
160
|
+
## 🔧 配置
|
|
144
161
|
|
|
145
162
|
```typescript
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
// 从 Canvas 获取图片数据
|
|
149
|
-
const canvas = Taro.createCanvasContext('myCanvas');
|
|
150
|
-
// ... 绘制内容 ...
|
|
151
|
-
|
|
152
|
-
Taro.canvasGetImageData({
|
|
153
|
-
canvasId: 'myCanvas',
|
|
154
|
-
x: 0,
|
|
155
|
-
y: 0,
|
|
156
|
-
width: 200,
|
|
157
|
-
height: 100,
|
|
158
|
-
success: res => {
|
|
159
|
-
const imageData = new Uint8Array(res.data);
|
|
160
|
-
|
|
161
|
-
printer.image(imageData, res.width, res.height).feed(2).print();
|
|
162
|
-
},
|
|
163
|
-
});
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### 弱网适配
|
|
163
|
+
const printer = new BluetoothPrinter();
|
|
167
164
|
|
|
168
|
-
|
|
169
|
-
// 配置重试和分片参数
|
|
165
|
+
// 适配器参数
|
|
170
166
|
printer.setOptions({
|
|
171
|
-
chunkSize: 20,
|
|
172
|
-
delay:
|
|
173
|
-
retries:
|
|
167
|
+
chunkSize: 20, // 分片大小 (默认 20)
|
|
168
|
+
delay: 20, // 分片间隔 ms (默认 20)
|
|
169
|
+
retries: 3, // 重试次数 (默认 3)
|
|
174
170
|
});
|
|
175
171
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
### 文本格式化 (v2.2+)
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
await printer
|
|
183
|
-
.align('center')
|
|
184
|
-
.setSize(2, 2)
|
|
185
|
-
.setBold(true)
|
|
186
|
-
.text('大标题')
|
|
187
|
-
.resetStyle()
|
|
188
|
-
.feed()
|
|
189
|
-
.align('left')
|
|
190
|
-
.text('正常文本')
|
|
191
|
-
.print();
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### 条码打印 (v2.2+)
|
|
195
|
-
|
|
196
|
-
```typescript
|
|
197
|
-
await printer.text('商品条码:').barcode('6901234567890', 'EAN13', { height: 80 }).feed(2).print();
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### 设备管理 (v2.2+)
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
import { DeviceManager } from 'taro-bluetooth-print';
|
|
204
|
-
|
|
205
|
-
const deviceManager = new DeviceManager();
|
|
206
|
-
|
|
207
|
-
// 监听设备发现
|
|
208
|
-
deviceManager.on('device-found', device => {
|
|
209
|
-
console.log('发现设备:', device.name, device.deviceId);
|
|
172
|
+
// 事件监听
|
|
173
|
+
printer.on('progress', ({ sent, total }) => {
|
|
174
|
+
console.log(`进度: ${(sent / total * 100).toFixed(1)}%`);
|
|
210
175
|
});
|
|
211
176
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// 获取已发现的设备
|
|
216
|
-
const devices = deviceManager.getDiscoveredDevices();
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
### 打印队列 (v2.2+)
|
|
220
|
-
|
|
221
|
-
```typescript
|
|
222
|
-
import { PrintQueue } from 'taro-bluetooth-print';
|
|
223
|
-
|
|
224
|
-
const queue = new PrintQueue({ maxSize: 100 });
|
|
225
|
-
|
|
226
|
-
// 添加高优先级任务
|
|
227
|
-
queue.add(printData, { priority: 'HIGH' });
|
|
228
|
-
|
|
229
|
-
// 监听完成事件
|
|
230
|
-
queue.on('job-completed', job => {
|
|
231
|
-
console.log('任务完成:', job.id);
|
|
177
|
+
printer.on('error', (error) => {
|
|
178
|
+
console.error('错误:', error.code, error.message);
|
|
232
179
|
});
|
|
233
|
-
```
|
|
234
180
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
import { OfflineCache } from 'taro-bluetooth-print';
|
|
239
|
-
|
|
240
|
-
const cache = new OfflineCache();
|
|
241
|
-
|
|
242
|
-
// 断网时自动缓存
|
|
243
|
-
await cache.save({ id: 'job-1', data: printData });
|
|
244
|
-
|
|
245
|
-
// 重连后同步
|
|
246
|
-
await cache.sync();
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
## 📚 文档
|
|
250
|
-
|
|
251
|
-
完整文档请访问:**[https://agions.github.io/taro-bluetooth-print/](https://agions.github.io/taro-bluetooth-print/)**
|
|
252
|
-
|
|
253
|
-
- [安装指南](https://agions.github.io/taro-bluetooth-print/guide/getting-started)
|
|
254
|
-
- [功能特性](https://agions.github.io/taro-bluetooth-print/guide/features)
|
|
255
|
-
- [核心概念](https://agions.github.io/taro-bluetooth-print/guide/core-concepts)
|
|
256
|
-
- [高级用法](https://agions.github.io/taro-bluetooth-print/guide/advanced)
|
|
257
|
-
- [API 参考](https://agions.github.io/taro-bluetooth-print/api)
|
|
258
|
-
- [故障排除](https://agions.github.io/taro-bluetooth-print/guide/troubleshooting)
|
|
259
|
-
|
|
260
|
-
## 🎯 核心 API
|
|
261
|
-
|
|
262
|
-
### BluetoothPrinter
|
|
263
|
-
|
|
264
|
-
| 方法 | 说明 | 返回值 |
|
|
265
|
-
| ------------------------------------ | -------------------- | --------------- |
|
|
266
|
-
| `connect(deviceId)` | 连接蓝牙设备 | `Promise<this>` |
|
|
267
|
-
| `disconnect()` | 断开连接 | `Promise<void>` |
|
|
268
|
-
| `text(content, encoding?)` | 添加文本 | `this` |
|
|
269
|
-
| `feed(lines?)` | 换行 | `this` |
|
|
270
|
-
| `image(data, width, height)` | 打印图片 | `this` |
|
|
271
|
-
| `qr(content, options?)` | 打印二维码 | `this` |
|
|
272
|
-
| `barcode(content, format, options?)` | 打印条码 (v2.2+) | `this` |
|
|
273
|
-
| `align(alignment)` | 设置对齐 (v2.2+) | `this` |
|
|
274
|
-
| `setSize(width, height)` | 设置字体大小 (v2.2+) | `this` |
|
|
275
|
-
| `setBold(enabled)` | 设置粗体 (v2.2+) | `this` |
|
|
276
|
-
| `setUnderline(enabled)` | 设置下划线 (v2.2+) | `this` |
|
|
277
|
-
| `resetStyle()` | 重置样式 (v2.2+) | `this` |
|
|
278
|
-
| `cut()` | 切纸 | `this` |
|
|
279
|
-
| `setOptions(options)` | 设置适配器参数 | `this` |
|
|
280
|
-
| `print()` | 发送打印 | `Promise<void>` |
|
|
281
|
-
| `pause()` | 暂停打印 | `void` |
|
|
282
|
-
| `resume()` | 恢复打印 | `Promise<void>` |
|
|
283
|
-
| `cancel()` | 取消打印 | `void` |
|
|
284
|
-
| `remaining()` | 获取剩余字节数 | `number` |
|
|
285
|
-
|
|
286
|
-
### 事件
|
|
287
|
-
|
|
288
|
-
| 事件名 | 数据类型 | 说明 |
|
|
289
|
-
| ---------------- | --------------------- | ------------ |
|
|
290
|
-
| `state-change` | `PrinterState` | 连接状态变化 |
|
|
291
|
-
| `progress` | `{ sent, total }` | 打印进度 |
|
|
292
|
-
| `error` | `BluetoothPrintError` | 错误事件 |
|
|
293
|
-
| `connected` | `string` (deviceId) | 已连接 |
|
|
294
|
-
| `disconnected` | `string` (deviceId) | 已断开 |
|
|
295
|
-
| `print-complete` | `void` | 打印完成 |
|
|
296
|
-
|
|
297
|
-
## 🔧 配置选项
|
|
298
|
-
|
|
299
|
-
```typescript
|
|
300
|
-
interface IAdapterOptions {
|
|
301
|
-
chunkSize?: number; // 分片大小(默认: 20字节)
|
|
302
|
-
delay?: number; // 分片延迟(默认: 20ms)
|
|
303
|
-
retries?: number; // 重试次数(默认: 3)
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
interface IQrOptions {
|
|
307
|
-
model?: 1 | 2; // 二维码模型(默认: 2)
|
|
308
|
-
size?: number; // 模块大小 1-16(默认: 6)
|
|
309
|
-
errorCorrection?: 'L' | 'M' | 'Q' | 'H'; // 纠错级别(默认: 'M')
|
|
310
|
-
}
|
|
181
|
+
printer.on('print-complete', () => {
|
|
182
|
+
console.log('打印完成');
|
|
183
|
+
});
|
|
311
184
|
```
|
|
312
185
|
|
|
313
|
-
##
|
|
314
|
-
|
|
315
|
-
| 平台 | 支持情况 | 说明 |
|
|
316
|
-
| -------------- | -------- | ---------------------------- |
|
|
317
|
-
| 微信小程序 | ✅ | 完全支持 |
|
|
318
|
-
| H5 | ✅ | 需要浏览器支持 Web Bluetooth |
|
|
319
|
-
| React Native | ✅ | 通过 Taro RN |
|
|
320
|
-
| 支付宝小程序 | ✅ | 完全支持 |
|
|
321
|
-
| 百度小程序 | ✅ | 完全支持 |
|
|
322
|
-
| 字节跳动小程序 | ✅ | 完全支持(抖音、头条等) |
|
|
323
|
-
|
|
324
|
-
## 🏗️ 架构设计
|
|
186
|
+
## 🏗️ 架构
|
|
325
187
|
|
|
326
188
|
```
|
|
327
|
-
|
|
328
|
-
│
|
|
329
|
-
│ - 连接管理
|
|
330
|
-
|
|
331
|
-
│
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
└──────────┘ └───────────┘
|
|
189
|
+
┌─────────────────────────────────────────────────┐
|
|
190
|
+
│ BluetoothPrinter (Core) │
|
|
191
|
+
│ - 连接管理 - 打印队列 - 事件系统 - 断点续传 │
|
|
192
|
+
└──────────┬──────────────────┬───────────────────┘
|
|
193
|
+
│ │
|
|
194
|
+
┌─────▼──────┐ ┌────▼──────┐
|
|
195
|
+
│ Adapter │ │ Driver │
|
|
196
|
+
│ 层 │ │ 层 │
|
|
197
|
+
└────────────┘ └───────────┘
|
|
198
|
+
│ │
|
|
199
|
+
┌─────▼──────┐ ┌────▼──────┐ ┌────────────┐
|
|
200
|
+
│ Taro │ │ ESC/POS │ │ Plugin │
|
|
201
|
+
│ Web BT │ │ TSPL │ │ System │
|
|
202
|
+
│ HarmonyOS │ │ ZPL │ │ (v2.3+) │
|
|
203
|
+
└────────────┘ │ CPCL │ └────────────┘
|
|
204
|
+
└───────────┘
|
|
344
205
|
```
|
|
345
206
|
|
|
346
|
-
- **Core 层**: 核心业务逻辑
|
|
347
|
-
- **Adapter 层**: 平台适配(Taro、Web Bluetooth 等)
|
|
348
|
-
- **Driver 层**: 打印机协议(ESC/POS、TSPL 等)
|
|
349
|
-
|
|
350
207
|
## 🤝 贡献
|
|
351
208
|
|
|
352
209
|
欢迎贡献!请查看 [贡献指南](./CONTRIBUTING.md)。
|
|
353
210
|
|
|
354
|
-
### 开发设置
|
|
355
|
-
|
|
356
211
|
```bash
|
|
357
212
|
# 克隆仓库
|
|
358
213
|
git clone https://github.com/agions/taro-bluetooth-print.git
|
|
@@ -377,7 +232,9 @@ npm run docs:dev
|
|
|
377
232
|
|
|
378
233
|
## 🙏 致谢
|
|
379
234
|
|
|
380
|
-
|
|
235
|
+
- [Taro](https://taro.jd.com/) - 跨平台开发框架
|
|
236
|
+
- [ESC/POS](https://www.epson-biz.com/) - 打印机指令集
|
|
237
|
+
- 所有贡献者和测试用户
|
|
381
238
|
|
|
382
239
|
---
|
|
383
240
|
|